1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
100 domParser.setFeature( "http://xml.org/sax/features/validation", false );
101
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
112 public static class ErrorHandler implements org.xml.sax.ErrorHandler
113 {
114
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
173 throw new SAXException( "External entites are not permitted in XML configuration files" );
174 }
175 }
176 }