View Javadoc

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;
23  import java.io.InputStream;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.parsers.ParserConfigurationException;
28  
29  import org.apache.xerces.parsers.DOMParser;
30  import org.w3c.dom.Document;
31  import org.xml.sax.InputSource;
32  import org.xml.sax.SAXException;
33  import org.xml.sax.SAXParseException;
34  
35  public class XmlParser
36  {
37  
38  
39      public static org.w3c.dom.Document parsePortletXml(InputStream portletXml) throws IOException, SAXException
40      {
41          
42          DocumentBuilderFactory 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 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 returnDoc = documentBuilder.parse( portletXml );
59              returnDoc.normalize();
60  
61              return returnDoc;
62  
63          } catch (ParserConfigurationException e)
64          {
65              throw new SAXException("Failed creating DocumentBuilder",e);
66          }
67      }
68  
69      public static org.w3c.dom.Document parseWebXml(InputStream webXml) throws IOException, SAXException
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 source = new InputSource( 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
92      {
93  
94          // org.xml.sax.ErrorHandler implementation.
95  
96          public void warning (SAXParseException exception) throws SAXException {
97              throw exception;
98          }
99          public void error   (SAXParseException exception) throws SAXException {
100             throw exception;
101         }
102         public void fatalError (SAXParseException exception) throws SAXException {
103             throw exception;
104         }
105     }
106     
107     
108     public static class EntityResolver implements org.xml.sax.EntityResolver
109     {
110 
111         
112         public String publicDTD         = null;
113         public String[] resourceDTDs    = new String[1];
114         public String[] resourceDTDNames= new String[1];
115 
116         public EntityResolver(String publicDTD,
117                               String resourceDTD,
118                               String resourceDTDName)
119         {
120             this.publicDTD       = publicDTD;
121             this.resourceDTDs[0]     = resourceDTD;
122             this.resourceDTDNames[0] = resourceDTDName;
123         }
124 
125         public EntityResolver(String[] resourceDTDs,
126                               String[] resourceDTDNames)
127         {
128             this.resourceDTDs     = resourceDTDs;
129             this.resourceDTDNames = resourceDTDNames;
130         }
131 
132         public InputSource resolveEntity(String publicId, String systemId) throws SAXException 
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 is = getClass().getResourceAsStream(resourceDTDs[i]);
141 
142                     if (is != null)
143                         return new InputSource(is);
144                     throw new SAXException("XML configuration DTD not found: "+resourceDTDs[i]);
145                 }
146             }
147             
148             // no other external entity permitted
149             throw new SAXException("External entites are not permitted in XML configuration files");
150             
151         }
152 
153     }
154 
155 }