| 1 23 24 package tools.com.sun.enterprise.util; 25 26 import org.xml.sax.SAXParseException ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.helpers.DefaultHandler ; 29 import org.xml.sax.InputSource ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.IOException ; 33 34 import com.sun.enterprise.deployment.xml.DTDRegistry; 35 import com.sun.enterprise.logging.LogDomains; 36 import java.util.Hashtable ; 37 import java.util.logging.Logger ; 38 import java.util.logging.Level ; 39 40 45 public class XMLValidationHandler extends DefaultHandler { 46 47 protected static Hashtable <String , String > mapping = null; 48 private Logger logger = LogDomains.getLogger(LogDomains.AVK_VERIFIER_LOGGER); 49 private boolean throwsException; 50 51 52 public XMLValidationHandler(boolean thowsException) { 53 this.throwsException = thowsException; 54 Init(); 55 } 56 57 public XMLValidationHandler() { 58 this.throwsException=true; 59 Init(); 60 } 61 62 63 private static void Init() { 64 if (mapping==null) { 65 mapping = new Hashtable <String , String >(); 66 mapping.put(com.sun.enterprise.deployment.node.ApplicationNode.PUBLIC_DTD_ID , "application_1_3.dtd" ); 67 mapping.put(com.sun.enterprise.deployment.node.ApplicationNode.PUBLIC_DTD_ID_12 , "application_1_2.dtd"); 68 mapping.put(com.sun.enterprise.deployment.node.ejb.EjbBundleNode.PUBLIC_DTD_ID, "ejb-jar_2_0.dtd" ) ; 69 mapping.put(com.sun.enterprise.deployment.node.ejb.EjbBundleNode.PUBLIC_DTD_ID_12, "ejb-jar_1_1.dtd" ); 70 mapping.put(com.sun.enterprise.deployment.node.appclient.AppClientNode.PUBLIC_DTD_ID, "application-client_1_3.dtd" ); 71 mapping.put(com.sun.enterprise.deployment.node.appclient.AppClientNode.PUBLIC_DTD_ID_12, "application-client_1_2.dtd" ); 72 73 mapping.put(com.sun.enterprise.deployment.node.web.WebBundleNode.PUBLIC_DTD_ID, "web-app_2_3.dtd" ); 74 mapping.put(com.sun.enterprise.deployment.node.web.WebBundleNode.PUBLIC_DTD_ID_12, "web-app_2_2.dtd"); 75 76 mapping.put(com.sun.enterprise.deployment.node.connector.ConnectorNode.PUBLIC_DTD_ID, "connector_1_5.dtd" ); 78 mapping.put(com.sun.enterprise.deployment.node.connector.ConnectorNode.PUBLIC_DTD_ID_10, "connector_1_0.dtd"); 79 80 mapping.put(DTDRegistry.SUN_APPLICATION_130_DTD_PUBLIC_ID,"sun-application_1_3-0.dtd"); 82 mapping.put(DTDRegistry.SUN_APPLICATION_140_DTD_PUBLIC_ID,"sun-application_1_4-0.dtd"); 83 mapping.put(DTDRegistry.SUN_APPLICATION_140beta_DTD_PUBLIC_ID,"sun-application_1_4-0.dtd"); 84 mapping.put(DTDRegistry.SUN_APPLICATION_500_DTD_PUBLIC_ID,"sun-application_5_0-0.dtd"); 85 86 mapping.put(DTDRegistry.SUN_APPCLIENT_130_DTD_PUBLIC_ID,"sun-application-client_1_3-0.dtd"); 87 mapping.put(DTDRegistry.SUN_APPCLIENT_140_DTD_PUBLIC_ID,"sun-application-client_1_4-0.dtd"); 88 mapping.put(DTDRegistry.SUN_APPCLIENT_140beta_DTD_PUBLIC_ID,"sun-application-client_1_4-0.dtd"); 89 mapping.put(DTDRegistry.SUN_APPCLIENT_141_DTD_PUBLIC_ID,"sun-application-client_1_4-1.dtd"); 90 mapping.put(DTDRegistry.SUN_APPCLIENT_500_DTD_PUBLIC_ID,"sun-application-client_5_0-0.dtd"); 91 92 mapping.put(DTDRegistry.SUN_WEBAPP_230_DTD_PUBLIC_ID,"sun-web-app_2_3-0.dtd"); 93 mapping.put(DTDRegistry.SUN_WEBAPP_231_DTD_PUBLIC_ID,"sun-web-app_2_3-1.dtd"); 94 mapping.put(DTDRegistry.SUN_WEBAPP_240_DTD_PUBLIC_ID,"sun-web-app_2_4-0.dtd"); 95 mapping.put(DTDRegistry.SUN_WEBAPP_240beta_DTD_PUBLIC_ID,"sun-web-app_2_4-0.dtd"); 96 mapping.put(DTDRegistry.SUN_WEBAPP_241_DTD_PUBLIC_ID,"sun-web-app_2_4-1.dtd"); 97 mapping.put(DTDRegistry.SUN_WEBAPP_250_DTD_PUBLIC_ID,"sun-web-app_2_5-0.dtd"); 98 99 mapping.put(DTDRegistry.SUN_EJBJAR_200_DTD_PUBLIC_ID,"sun-ejb-jar_2_0-0.dtd"); 100 mapping.put(DTDRegistry.SUN_EJBJAR_201_DTD_PUBLIC_ID,"sun-ejb-jar_2_0-1.dtd"); 101 mapping.put(DTDRegistry.SUN_EJBJAR_210_DTD_PUBLIC_ID,"sun-ejb-jar_2_1-0.dtd"); 102 mapping.put(DTDRegistry.SUN_EJBJAR_210beta_DTD_PUBLIC_ID,"sun-ejb-jar_2_1-0.dtd"); 103 mapping.put(DTDRegistry.SUN_EJBJAR_211_DTD_PUBLIC_ID,"sun-ejb-jar_2_1-1.dtd"); 104 mapping.put(DTDRegistry.SUN_EJBJAR_300_DTD_PUBLIC_ID,"sun-ejb-jar_3_0-0.dtd"); 105 106 mapping.put(DTDRegistry.SUN_CONNECTOR_100_DTD_PUBLIC_ID,"sun-connector_1_0-0.dtd"); 107 mapping.put(DTDRegistry.SUN_CLIENTCONTAINER_700_DTD_PUBLIC_ID,"sun-application-client-container_1_0.dtd"); 108 109 mapping.put(DTDRegistry.SUN_CMP_MAPPING_700_DTD_PUBLIC_ID,"sun-cmp-mapping_1_0.dtd"); 110 mapping.put(DTDRegistry.SUN_CMP_MAPPING_800_DTD_PUBLIC_ID,"sun-cmp-mapping_1_1.dtd"); 111 mapping.put(DTDRegistry.SUN_CMP_MAPPING_810_DTD_PUBLIC_ID,"sun-cmp-mapping_1_2.dtd"); 112 113 mapping.put(DTDRegistry.TAGLIB_12_DTD_PUBLIC_ID,"web-jsptaglibrary_1_2.dtd"); 114 mapping.put(DTDRegistry.TAGLIB_11_DTD_PUBLIC_ID,"web-jsptaglibrary_1_1.dtd"); 115 } 116 } 117 118 public InputSource resolveEntity(String publicID, String systemID) throws SAXException { 119 try{ 120 if (publicID==null) { 121 if (systemID==null || systemID.lastIndexOf('/')==systemID.length()) { 123 return null; 124 } 125 126 String fileName = getSchemaURLFor(systemID.substring(systemID.lastIndexOf('/')+1)); 127 if (fileName==null) { 130 fileName = systemID; 131 } 132 return new InputSource (fileName); 133 } 134 135 if (mapping.containsKey(publicID)) { 136 return new InputSource (new FileInputStream (new File (getAbsoluteFilenameForDTD((String ) mapping.get(publicID))))); 137 } 138 } catch(java.io.IOException ioe) { 139 throw new SAXException (ioe); 140 } 141 return null; 142 } 143 144 public void error(SAXParseException spe) throws SAXParseException { 145 logger.log(Level.FINE,"XML Error line : " + spe.getLineNumber() + " " + spe.getLocalizedMessage()); 146 if (throwsException) 147 throw spe; 148 } 149 150 public void fatalError(SAXParseException spe) throws SAXParseException { 151 logger.log(Level.FINE,"XML Error line : " + spe.getLineNumber() + " " + spe.getLocalizedMessage()); 152 throw spe; 153 } 154 155 protected String getAbsoluteFilenameForDTD(String dtdFilename) 156 { 157 String j2ee13 = DTDRegistry.DTD_LOCATION; 159 File f = new File (j2ee13 +File.separator+ dtdFilename); 160 return f.getAbsolutePath(); 161 } 162 163 protected String getSchemaURLFor(String schemaSystemID) throws IOException { 164 File f = getSchemaFileFor(schemaSystemID); 165 if (f!=null) { 166 return f.toURL().toString(); 167 } else { 168 return null; 169 } 170 } 171 172 protected File getSchemaFileFor(String schemaSystemID) throws IOException { 173 174 String schemaLoc = DTDRegistry.SCHEMA_LOCATION.replace('/', File.separatorChar); 175 File f = new File (schemaLoc +File.separatorChar+ schemaSystemID); 176 if (!f.exists()) { 177 return null; 178 } 179 return f; 180 } 181 } 182 | Popular Tags |