View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  /*
35  
36   */
37  package org.apache.pluto.portalImpl.xml;
38  
39  import com.sun.org.apache.xerces.internal.parsers.DOMParser;
40  
41  import org.w3c.dom.Document;
42  
43  import org.xml.sax.InputSource;
44  import org.xml.sax.SAXException;
45  import org.xml.sax.SAXParseException;
46  
47  import java.io.IOException;
48  import java.io.InputStream;
49  
50  import javax.xml.parsers.DocumentBuilder;
51  import javax.xml.parsers.DocumentBuilderFactory;
52  import javax.xml.parsers.ParserConfigurationException;
53  
54  
55  public class XmlParser
56  {
57      public static org.w3c.dom.Document parsePortletXml( InputStream portletXml )
58          throws IOException, SAXException
59      {
60          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(  );
61  
62          documentBuilderFactory.setAttribute( "http://xml.org/sax/features/validation", Boolean.TRUE );
63          documentBuilderFactory.setAttribute( "http://apache.org/xml/features/validation/schema", Boolean.TRUE );
64  
65          documentBuilderFactory.setAttribute( "http://xml.org/sax/features/namespaces", Boolean.TRUE );
66          documentBuilderFactory.setAttribute( "http://apache.org/xml/features/dom/include-ignorable-whitespace",
67              Boolean.FALSE );
68  
69          try
70          {
71              System.out.println( "JE SUIS LA !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
72  
73              // throws ParserConfigurationException if documentBuilder cannot be created
74              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(  );
75              documentBuilder.setErrorHandler( new ErrorHandler(  ) );
76              documentBuilder.setEntityResolver( new EntityResolver( Constants.RES_PORTLET_DTDS,
77                      Constants.RES_PORTLET_DTD_NAMES ) );
78  
79              Document returnDoc = documentBuilder.parse( portletXml );
80              returnDoc.normalize(  );
81  
82              return returnDoc;
83          }
84          catch ( ParserConfigurationException e )
85          {
86              throw new SAXException( "Failed creating DocumentBuilder", e );
87          }
88      }
89  
90      public static org.w3c.dom.Document parseWebXml( InputStream webXml )
91          throws IOException, SAXException
92      {
93          DOMParser domParser = new DOMParser(  );
94  
95          domParser.setErrorHandler( new ErrorHandler(  ) );
96          domParser.setEntityResolver( new EntityResolver( Constants.RES_WEB_PUBLIC_ID, Constants.RES_WEB_DTD,
97                  Constants.RES_WEB_DTD_NAME ) );
98  
99          // modified by YCLI: START :: do not do validation for web.xml
100         domParser.setFeature( "http://xml.org/sax/features/validation", false );
101         // modified by YCLI: END 
102         domParser.setFeature( "http://apache.org/xml/features/dom/include-ignorable-whitespace", false );
103 
104         InputSource source = new InputSource( webXml );
105 
106         domParser.parse( source );
107 
108         return domParser.getDocument(  );
109     }
110 
111     // private helper classes for parsing
112     public static class ErrorHandler implements org.xml.sax.ErrorHandler
113     {
114         // org.xml.sax.ErrorHandler implementation.
115         public void warning( SAXParseException exception )
116             throws SAXException
117         {
118             throw exception;
119         }
120 
121         public void error( SAXParseException exception )
122             throws SAXException
123         {
124             throw exception;
125         }
126 
127         public void fatalError( SAXParseException exception )
128             throws SAXException
129         {
130             throw exception;
131         }
132     }
133 
134     public static class EntityResolver implements org.xml.sax.EntityResolver
135     {
136         public String publicDTD = null;
137         public String[] resourceDTDs = new String[1];
138         public String[] resourceDTDNames = new String[1];
139 
140         public EntityResolver( String publicDTD, String resourceDTD, String resourceDTDName )
141         {
142             this.publicDTD = publicDTD;
143             this.resourceDTDs[0] = resourceDTD;
144             this.resourceDTDNames[0] = resourceDTDName;
145         }
146 
147         public EntityResolver( String[] resourceDTDs, String[] resourceDTDNames )
148         {
149             this.resourceDTDs = resourceDTDs;
150             this.resourceDTDNames = resourceDTDNames;
151         }
152 
153         public InputSource resolveEntity( String publicId, String systemId )
154             throws SAXException
155         {
156             for ( int i = 0; i < resourceDTDNames.length; i++ )
157             {
158                 if ( ( ( publicId != null ) && publicId.equals( publicDTD ) ) ||
159                         ( ( systemId != null ) && systemId.endsWith( resourceDTDNames[i] ) ) )
160                 {
161                     java.io.InputStream is = getClass(  ).getResourceAsStream( resourceDTDs[i] );
162 
163                     if ( is != null )
164                     {
165                         return new InputSource( is );
166                     }
167 
168                     throw new SAXException( "XML configuration DTD not found: " + resourceDTDs[i] );
169                 }
170             }
171 
172             // no other external entity permitted
173             throw new SAXException( "External entites are not permitted in XML configuration files" );
174         }
175     }
176 }