KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tools > com > sun > enterprise > util > XMLValidationHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package tools.com.sun.enterprise.util;
25
26 import org.xml.sax.SAXParseException JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import com.sun.enterprise.deployment.xml.DTDRegistry;
35 import com.sun.enterprise.logging.LogDomains;
36 import java.util.Hashtable JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39
40 /**
41  *
42  * @author dochez
43  * @version
44  */

45 public class XMLValidationHandler extends DefaultHandler JavaDoc {
46
47     protected static Hashtable JavaDoc<String JavaDoc, String JavaDoc> mapping = null;
48     private Logger JavaDoc logger = LogDomains.getLogger(LogDomains.AVK_VERIFIER_LOGGER);
49     private boolean throwsException;
50
51     /** Creates new DTDEntityResolver */
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 JavaDoc<String JavaDoc, String JavaDoc>();
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             //connector1.5
77
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             //SunOne 8.0 Specific Stuff
81
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 JavaDoc resolveEntity(String JavaDoc publicID, String JavaDoc systemID) throws SAXException JavaDoc {
119         try{
120             if (publicID==null) {
121                 // unspecified schema
122
if (systemID==null || systemID.lastIndexOf('/')==systemID.length()) {
123                     return null;
124                 }
125
126                 String JavaDoc fileName = getSchemaURLFor(systemID.substring(systemID.lastIndexOf('/')+1));
127                 // if this is not a request for a schema located in our repository,
128
// let's hope that the hint provided by schemaLocation is correct
129
if (fileName==null) {
130                     fileName = systemID;
131                 }
132                 return new InputSource JavaDoc(fileName);
133             }
134
135             if (mapping.containsKey(publicID)) {
136                 return new InputSource JavaDoc(new FileInputStream JavaDoc(new File JavaDoc(getAbsoluteFilenameForDTD((String JavaDoc) mapping.get(publicID)))));
137             }
138         } catch(java.io.IOException JavaDoc ioe) {
139             throw new SAXException JavaDoc(ioe);
140         }
141         return null;
142     }
143
144     public void error(SAXParseException JavaDoc spe) throws SAXParseException JavaDoc {
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 JavaDoc spe) throws SAXParseException JavaDoc {
151         logger.log(Level.FINE,"XML Error line : " + spe.getLineNumber() + " " + spe.getLocalizedMessage());
152         throw spe;
153     }
154
155     protected String JavaDoc getAbsoluteFilenameForDTD(String JavaDoc dtdFilename)
156     {
157         //String j2ee13 = DirLocation.getDTDDirRoot();
158
String JavaDoc j2ee13 = DTDRegistry.DTD_LOCATION;
159         File JavaDoc f = new File JavaDoc(j2ee13 +File.separator+ dtdFilename);
160         return f.getAbsolutePath();
161     }
162
163     protected String JavaDoc getSchemaURLFor(String JavaDoc schemaSystemID) throws IOException JavaDoc {
164         File JavaDoc f = getSchemaFileFor(schemaSystemID);
165         if (f!=null) {
166             return f.toURL().toString();
167         } else {
168             return null;
169         }
170     }
171
172     protected File JavaDoc getSchemaFileFor(String JavaDoc schemaSystemID) throws IOException JavaDoc {
173
174         String JavaDoc schemaLoc = DTDRegistry.SCHEMA_LOCATION.replace('/', File.separatorChar);
175         File JavaDoc f = new File JavaDoc(schemaLoc +File.separatorChar+ schemaSystemID);
176         if (!f.exists()) {
177             return null;
178         }
179         return f;
180     }
181 }
182
Popular Tags