1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.xml;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.xml.Attribute;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.DocumentException;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.Entity;
24  import com.liferay.portal.kernel.xml.Namespace;
25  import com.liferay.portal.kernel.xml.Node;
26  import com.liferay.portal.kernel.xml.ProcessingInstruction;
27  import com.liferay.portal.kernel.xml.QName;
28  import com.liferay.portal.kernel.xml.SAXReader;
29  import com.liferay.portal.kernel.xml.Text;
30  import com.liferay.portal.kernel.xml.XPath;
31  import com.liferay.portal.util.EntityResolver;
32  import com.liferay.util.xml.XMLSafeReader;
33  
34  import java.io.File;
35  import java.io.InputStream;
36  import java.io.Reader;
37  
38  import java.net.MalformedURLException;
39  import java.net.URL;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  import java.util.Map;
44  
45  import org.apache.xerces.parsers.SAXParser;
46  
47  import org.dom4j.DocumentFactory;
48  import org.dom4j.DocumentHelper;
49  
50  /**
51   * <a href="SAXReaderImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   */
55  public class SAXReaderImpl implements SAXReader {
56  
57      public static SAXReaderImpl getInstance() {
58          return _instance;
59      }
60  
61      public static List<Attribute> toNewAttributes(
62          List<org.dom4j.Attribute> oldAttributes) {
63  
64          List<Attribute> newAttributes = new ArrayList<Attribute>(
65              oldAttributes.size());
66  
67          for (org.dom4j.Attribute oldAttribute : oldAttributes) {
68              newAttributes.add(new AttributeImpl(oldAttribute));
69          }
70  
71          return new NodeList<Attribute, org.dom4j.Attribute>(
72              newAttributes, oldAttributes);
73      }
74  
75      public static List<Element> toNewElements(
76          List<org.dom4j.Element> oldElements) {
77  
78          List<Element> newElements = new ArrayList<Element>(oldElements.size());
79  
80          for (org.dom4j.Element oldElement : oldElements) {
81              newElements.add(new ElementImpl(oldElement));
82          }
83  
84          return new NodeList<Element, org.dom4j.Element>(
85              newElements, oldElements);
86      }
87  
88      public static List<Namespace> toNewNamespaces(
89          List<org.dom4j.Namespace> oldNamespaces) {
90  
91          List<Namespace> newNamespaces = new ArrayList<Namespace>(
92              oldNamespaces.size());
93  
94          for (org.dom4j.Namespace oldNamespace : oldNamespaces) {
95              newNamespaces.add(new NamespaceImpl(oldNamespace));
96          }
97  
98          return new NodeList<Namespace, org.dom4j.Namespace>(
99              newNamespaces, oldNamespaces);
100     }
101 
102     public static List<Node> toNewNodes(List<org.dom4j.Node> oldNodes) {
103         List<Node> newNodes = new ArrayList<Node>(oldNodes.size());
104 
105         for (org.dom4j.Node oldNode : oldNodes) {
106             if (oldNode instanceof org.dom4j.Element) {
107                 newNodes.add(new ElementImpl((org.dom4j.Element)oldNode));
108             }
109             else {
110                 newNodes.add(new NodeImpl(oldNode));
111             }
112         }
113 
114         return new NodeList<Node, org.dom4j.Node>(newNodes, oldNodes);
115     }
116 
117     public static List<ProcessingInstruction> toNewProcessingInstructions(
118         List<org.dom4j.ProcessingInstruction> oldProcessingInstructions) {
119 
120         List<ProcessingInstruction> newProcessingInstructions =
121             new ArrayList<ProcessingInstruction>(
122                 oldProcessingInstructions.size());
123 
124         for (org.dom4j.ProcessingInstruction oldProcessingInstruction :
125                 oldProcessingInstructions) {
126 
127             newProcessingInstructions.add(
128                 new ProcessingInstructionImpl(oldProcessingInstruction));
129         }
130 
131         return new NodeList
132             <ProcessingInstruction, org.dom4j.ProcessingInstruction>(
133                 newProcessingInstructions, oldProcessingInstructions);
134     }
135 
136     public static List<org.dom4j.Attribute> toOldAttributes(
137         List<Attribute> newAttributes) {
138 
139         List<org.dom4j.Attribute> oldAttributes =
140             new ArrayList<org.dom4j.Attribute>(newAttributes.size());
141 
142         for (Attribute newAttribute : newAttributes) {
143             AttributeImpl newAttributeImpl = (AttributeImpl)newAttribute;
144 
145             oldAttributes.add(newAttributeImpl.getWrappedAttribute());
146         }
147 
148         return oldAttributes;
149     }
150 
151     public static List<org.dom4j.Node> toOldNodes(List<Node> newNodes) {
152         List<org.dom4j.Node> oldNodes = new ArrayList<org.dom4j.Node>(
153             newNodes.size());
154 
155         for (Node newNode : newNodes) {
156             NodeImpl newNodeImpl = (NodeImpl)newNode;
157 
158             oldNodes.add(newNodeImpl.getWrappedNode());
159         }
160 
161         return oldNodes;
162     }
163 
164     public static List<org.dom4j.ProcessingInstruction>
165         toOldProcessingInstructions(
166             List<ProcessingInstruction> newProcessingInstructions) {
167 
168         List<org.dom4j.ProcessingInstruction> oldProcessingInstructions =
169             new ArrayList<org.dom4j.ProcessingInstruction>(
170                 newProcessingInstructions.size());
171 
172         for (ProcessingInstruction newProcessingInstruction :
173                 newProcessingInstructions) {
174 
175             ProcessingInstructionImpl newProcessingInstructionImpl =
176                 (ProcessingInstructionImpl)newProcessingInstruction;
177 
178             oldProcessingInstructions.add(
179                 newProcessingInstructionImpl.getWrappedProcessingInstruction());
180         }
181 
182         return oldProcessingInstructions;
183     }
184 
185     public Attribute createAttribute(
186         Element element, QName qName, String value) {
187 
188         ElementImpl elementImpl = (ElementImpl)element;
189         QNameImpl qNameImpl = (QNameImpl)qName;
190 
191         DocumentFactory documentFactory = DocumentFactory.getInstance();
192 
193         return new AttributeImpl(
194             documentFactory.createAttribute(
195                 elementImpl.getWrappedElement(), qNameImpl.getWrappedQName(),
196                 value));
197     }
198 
199     public Attribute createAttribute(
200         Element element, String name, String value) {
201 
202         ElementImpl elementImpl = (ElementImpl)element;
203 
204         DocumentFactory documentFactory = DocumentFactory.getInstance();
205 
206         return new AttributeImpl(
207             documentFactory.createAttribute(
208                 elementImpl.getWrappedElement(), name, value));
209     }
210 
211     public Document createDocument() {
212         return new DocumentImpl(DocumentHelper.createDocument());
213     }
214 
215     public Document createDocument(Element rootElement) {
216         ElementImpl rootElementImpl = (ElementImpl)rootElement;
217 
218         return new DocumentImpl(
219             DocumentHelper.createDocument(rootElementImpl.getWrappedElement()));
220     }
221 
222     public Document createDocument(String encoding) {
223         DocumentFactory documentFactory = DocumentFactory.getInstance();
224 
225         return new DocumentImpl(documentFactory.createDocument(encoding));
226     }
227 
228     public Element createElement(QName qName) {
229         QNameImpl qNameImpl = (QNameImpl)qName;
230 
231         return new ElementImpl(
232             DocumentHelper.createElement(qNameImpl.getWrappedQName()));
233     }
234 
235     public Element createElement(String name) {
236         return new ElementImpl(DocumentHelper.createElement(name));
237     }
238 
239     public Entity createEntity(String name, String text) {
240         return new EntityImpl(DocumentHelper.createEntity(name, text));
241     }
242 
243     public Namespace createNamespace(String uri) {
244         return new NamespaceImpl(org.dom4j.Namespace.get(uri));
245     }
246 
247     public Namespace createNamespace(String prefix, String uri) {
248         return new NamespaceImpl(DocumentHelper.createNamespace(prefix, uri));
249     }
250 
251     public ProcessingInstruction createProcessingInstruction(
252         String target, Map<String, String> data) {
253 
254         org.dom4j.ProcessingInstruction processingInstruction =
255             DocumentHelper.createProcessingInstruction(target, data);
256 
257         if (processingInstruction == null) {
258             return null;
259         }
260         else {
261             return new ProcessingInstructionImpl(processingInstruction);
262         }
263     }
264 
265     public ProcessingInstruction createProcessingInstruction(
266         String target, String data) {
267 
268         org.dom4j.ProcessingInstruction processingInstruction =
269             DocumentHelper.createProcessingInstruction(target, data);
270 
271         if (processingInstruction == null) {
272             return null;
273         }
274         else {
275             return new ProcessingInstructionImpl(processingInstruction);
276         }
277     }
278 
279     public QName createQName(String localName) {
280         return new QNameImpl(DocumentHelper.createQName(localName));
281     }
282 
283     public QName createQName(String localName, Namespace namespace) {
284         NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
285 
286         return new QNameImpl(
287             DocumentHelper.createQName(
288                 localName, namespaceImpl.getWrappedNamespace()));
289     }
290 
291     public Text createText(String text) {
292         return new TextImpl(DocumentHelper.createText(text));
293     }
294 
295     public XPath createXPath(String xpathExpression) {
296         return new XPathImpl(DocumentHelper.createXPath(xpathExpression));
297     }
298 
299     public List<Node> selectNodes(
300         String xpathFilterExpression, List<Node> nodes) {
301 
302         return toNewNodes(
303             DocumentHelper.selectNodes(
304                 xpathFilterExpression, toOldNodes(nodes)));
305     }
306 
307     public List<Node> selectNodes(
308         String xpathFilterExpression, Node node) {
309 
310         NodeImpl nodeImpl = (NodeImpl)node;
311 
312         return toNewNodes(
313             DocumentHelper.selectNodes(
314                 xpathFilterExpression, nodeImpl.getWrappedNode()));
315     }
316 
317     public void sort(List<Node> nodes, String xpathExpression) {
318         DocumentHelper.sort(toOldNodes(nodes), xpathExpression);
319     }
320 
321     public void sort(
322         List<Node> nodes, String xpathExpression, boolean distinct) {
323 
324         DocumentHelper.sort(toOldNodes(nodes), xpathExpression, distinct);
325     }
326 
327     public Document read(File file) throws DocumentException {
328         return read(file, false);
329     }
330 
331     public Document read(File file, boolean validate)
332         throws DocumentException {
333 
334         try {
335             org.dom4j.io.SAXReader saxReader = getSAXReader(validate);
336 
337             return new DocumentImpl(saxReader.read(file));
338         }
339         catch (org.dom4j.DocumentException de) {
340             throw new DocumentException(de.getMessage(), de);
341         }
342     }
343 
344     public Document read(InputStream is) throws DocumentException {
345         return read(is, false);
346     }
347 
348     public Document read(InputStream is, boolean validate)
349         throws DocumentException {
350 
351         try {
352             org.dom4j.io.SAXReader saxReader = getSAXReader(validate);
353 
354             return new DocumentImpl(saxReader.read(is));
355         }
356         catch (org.dom4j.DocumentException de) {
357             throw new DocumentException(de.getMessage(), de);
358         }
359     }
360 
361     public Document read(Reader reader) throws DocumentException {
362         return read(reader, false);
363     }
364 
365     public Document read(Reader reader, boolean validate)
366         throws DocumentException {
367 
368         try {
369             org.dom4j.io.SAXReader saxReader = getSAXReader(validate);
370 
371             return new DocumentImpl(saxReader.read(reader));
372         }
373         catch (org.dom4j.DocumentException de) {
374             throw new DocumentException(de.getMessage(), de);
375         }
376     }
377 
378     public Document read(String xml) throws DocumentException {
379         return read(new XMLSafeReader(xml));
380     }
381 
382     public Document read(String xml, boolean validate)
383         throws DocumentException {
384 
385         return read(new XMLSafeReader(xml), validate);
386     }
387 
388     public Document read(URL url) throws DocumentException {
389         return read(url, false);
390     }
391 
392     public Document read(URL url, boolean validate) throws DocumentException {
393         try {
394             org.dom4j.io.SAXReader saxReader = getSAXReader(validate);
395 
396             return new DocumentImpl(saxReader.read(url));
397         }
398         catch (org.dom4j.DocumentException de) {
399             throw new DocumentException(de.getMessage(), de);
400         }
401     }
402 
403     public Document readURL(String url)
404         throws DocumentException, MalformedURLException {
405 
406         return read(new URL(url), false);
407     }
408 
409     public Document readURL(String url, boolean validate)
410         throws DocumentException, MalformedURLException {
411 
412         return read(new URL(url), validate);
413     }
414 
415     protected org.dom4j.io.SAXReader getSAXReader(boolean validate) {
416         org.dom4j.io.SAXReader reader = null;
417 
418         try {
419             reader = new org.dom4j.io.SAXReader(new SAXParser(), validate);
420 
421             reader.setEntityResolver(new EntityResolver());
422 
423             reader.setFeature(_FEATURES_VALIDATION, validate);
424             reader.setFeature(_FEATURES_VALIDATION_SCHEMA, validate);
425             reader.setFeature(
426                 _FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, validate);
427             reader.setFeature(_FEATURES_DYNAMIC, validate);
428         }
429         catch (Exception e) {
430             if (_log.isWarnEnabled()) {
431                 _log.warn(
432                     "XSD validation is diasabled because " + e.getMessage());
433             }
434 
435             reader = new org.dom4j.io.SAXReader(false);
436 
437             reader.setEntityResolver(new EntityResolver());
438         }
439 
440         return reader;
441     }
442 
443     private static final String _FEATURES_VALIDATION =
444         "http://xml.org/sax/features/validation";
445 
446     private static final String _FEATURES_VALIDATION_SCHEMA =
447         "http://apache.org/xml/features/validation/schema";
448 
449     private static final String _FEATURES_VALIDATION_SCHEMA_FULL_CHECKING =
450         "http://apache.org/xml/features/validation/schema-full-checking";
451 
452     private static final String _FEATURES_DYNAMIC =
453         "http://apache.org/xml/features/validation/dynamic";
454 
455     private static Log _log = LogFactoryUtil.getLog(SAXReaderImpl.class);
456 
457     private static SAXReaderImpl _instance = new SAXReaderImpl();
458 
459 }