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