1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
79 domParser.setFeature("http://xml.org/sax/features/validation", false);
80
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
91 public static class ErrorHandler implements org.xml.sax.ErrorHandler
92 {
93
94
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
149 throw new SAXException("External entites are not permitted in XML configuration files");
150
151 }
152
153 }
154
155 }