1 23 24 package com.sun.enterprise.admin.servermgmt; 25 26 import javax.xml.parsers.*; 27 import org.xml.sax.*; 28 import java.io.ByteArrayInputStream ; 29 30 import javax.management.ObjectName ; 31 32 import com.sun.enterprise.util.i18n.StringManager; 33 34 40 public class RepositoryNameValidator extends StringValidator 41 { 42 private static final String VALID_CHAR = 43 "[^\\,\\/ \\&\\;\\`\\'\\\\\"\\|\\*\\!\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\:\\%]*"; 44 45 private static final String IAS_NAME = "com.sun.appserv:name="; 46 47 private static final String XML_1 = 48 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <xml>"; 49 50 private static final String XML_2 = "</xml>"; 51 52 55 private static final StringManager strMgr = 56 StringManager.getManager(RepositoryNameValidator.class); 57 58 62 public RepositoryNameValidator(String name) 63 { 64 super(name); 65 } 66 67 72 public void validate(Object str) throws InvalidConfigException 73 { 74 super.validate(str); 75 checkValidName((String )str); 76 checkValidXmlToken((String )str); 77 checkValidObjectNameToken((String )str); 78 } 79 80 public void checkValidName(String name) throws InvalidConfigException 81 { 82 if (!name.matches(VALID_CHAR)) 83 { 84 throw new InvalidConfigException( 85 strMgr.getString("validator.invalid_value", getName(), name)); 86 } 87 } 88 89 93 public void checkValidXmlToken(String name) throws InvalidConfigException 94 { 95 try 96 { 97 String xml = XML_1 + name + XML_2; 99 ByteArrayInputStream bais = new ByteArrayInputStream (xml.getBytes()); 100 InputSource is = new InputSource(bais); 101 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 102 dbf.setValidating(false); 103 DocumentBuilder db = dbf.newDocumentBuilder(); 104 db.parse(is); 105 } 106 catch (Exception e) 107 { 108 throw new InvalidConfigException( 109 strMgr.getString("validator.invalid_value", getName(), name)); 110 } 111 } 112 113 public void checkValidObjectNameToken(String name) 114 throws InvalidConfigException 115 { 116 try 117 { 118 new ObjectName (IAS_NAME + name); 119 } 120 catch (Exception e) 121 { 122 throw new InvalidConfigException( 123 strMgr.getString("validator.invalid_value", getName(), name)); 124 } 125 } 126 } 127 | Popular Tags |