KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > xml > XmlParser


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.xml;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 import org.apache.xerces.parsers.DOMParser;
30 import org.w3c.dom.Document JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34
35 public class XmlParser
36 {
37
38
39     public static org.w3c.dom.Document JavaDoc parsePortletXml(InputStream JavaDoc portletXml) throws IOException JavaDoc, SAXException JavaDoc
40     {
41         
42         DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
43
44         documentBuilderFactory.setAttribute("http://xml.org/sax/features/validation", Boolean.TRUE);
45         documentBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", Boolean.TRUE);
46
47         documentBuilderFactory.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
48         documentBuilderFactory.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE);
49
50
51         try{
52
53             // throws ParserConfigurationException if documentBuilder cannot be created
54
DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
55             documentBuilder.setErrorHandler(new ErrorHandler());
56             documentBuilder.setEntityResolver(new EntityResolver(Constants.RES_PORTLET_DTDS, Constants.RES_PORTLET_DTD_NAMES));
57
58             Document JavaDoc returnDoc = documentBuilder.parse( portletXml );
59             returnDoc.normalize();
60
61             return returnDoc;
62
63         } catch (ParserConfigurationException JavaDoc e)
64         {
65             throw new SAXException JavaDoc("Failed creating DocumentBuilder",e);
66         }
67     }
68
69     public static org.w3c.dom.Document JavaDoc parseWebXml(InputStream JavaDoc webXml) throws IOException JavaDoc, SAXException JavaDoc
70     {
71         DOMParser domParser = new DOMParser();
72
73         domParser.setErrorHandler(new ErrorHandler());
74         domParser.setEntityResolver(new EntityResolver(Constants.RES_WEB_PUBLIC_ID,
75                                                        Constants.RES_WEB_DTD,
76                                                        Constants.RES_WEB_DTD_NAME));
77             
78     // modified by YCLI: START :: do not do validation for web.xml
79
domParser.setFeature("http://xml.org/sax/features/validation", false);
80     // modified by YCLI: END
81
domParser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
82
83         InputSource JavaDoc source = new InputSource JavaDoc( webXml );
84         
85         domParser.parse( source );
86         
87         return domParser.getDocument();
88     }
89
90     // private helper classes for parsing
91
public static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc
92     {
93
94         // org.xml.sax.ErrorHandler implementation.
95

96         public void warning (SAXParseException JavaDoc exception) throws SAXException JavaDoc {
97             throw exception;
98         }
99         public void error (SAXParseException JavaDoc exception) throws SAXException JavaDoc {
100             throw exception;
101         }
102         public void fatalError (SAXParseException JavaDoc exception) throws SAXException JavaDoc {
103             throw exception;
104         }
105     }
106     
107     
108     public static class EntityResolver implements org.xml.sax.EntityResolver JavaDoc
109     {
110
111         
112         public String JavaDoc publicDTD = null;
113         public String JavaDoc[] resourceDTDs = new String JavaDoc[1];
114         public String JavaDoc[] resourceDTDNames= new String JavaDoc[1];
115
116         public EntityResolver(String JavaDoc publicDTD,
117                               String JavaDoc resourceDTD,
118                               String JavaDoc resourceDTDName)
119         {
120             this.publicDTD = publicDTD;
121             this.resourceDTDs[0] = resourceDTD;
122             this.resourceDTDNames[0] = resourceDTDName;
123         }
124
125         public EntityResolver(String JavaDoc[] resourceDTDs,
126                               String JavaDoc[] resourceDTDNames)
127         {
128             this.resourceDTDs = resourceDTDs;
129             this.resourceDTDNames = resourceDTDNames;
130         }
131
132         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc
133         {
134
135             for (int i=0; i<resourceDTDNames.length; i++)
136             {
137                 if ((publicId != null && publicId.equals(publicDTD))
138                     || (systemId != null && systemId.endsWith(resourceDTDNames[i])))
139                 {
140                     java.io.InputStream JavaDoc is = getClass().getResourceAsStream(resourceDTDs[i]);
141
142                     if (is != null)
143                         return new InputSource JavaDoc(is);
144                     throw new SAXException JavaDoc("XML configuration DTD not found: "+resourceDTDs[i]);
145                 }
146             }
147             
148             // no other external entity permitted
149
throw new SAXException JavaDoc("External entites are not permitted in XML configuration files");
150             
151         }
152
153     }
154
155 }
156
Popular Tags