1
19
20 package com.liferay.portal.xml;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.xml.Attribute;
25 import com.liferay.portal.kernel.xml.Document;
26 import com.liferay.portal.kernel.xml.DocumentException;
27 import com.liferay.portal.kernel.xml.Element;
28 import com.liferay.portal.kernel.xml.Entity;
29 import com.liferay.portal.kernel.xml.Namespace;
30 import com.liferay.portal.kernel.xml.Node;
31 import com.liferay.portal.kernel.xml.ProcessingInstruction;
32 import com.liferay.portal.kernel.xml.QName;
33 import com.liferay.portal.kernel.xml.SAXReader;
34 import com.liferay.portal.kernel.xml.Text;
35 import com.liferay.portal.kernel.xml.XPath;
36 import com.liferay.portal.util.EntityResolver;
37 import com.liferay.util.xml.XMLSafeReader;
38
39 import java.io.File;
40 import java.io.InputStream;
41 import java.io.Reader;
42
43 import java.net.MalformedURLException;
44 import java.net.URL;
45
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Map;
49
50 import org.dom4j.DocumentFactory;
51 import org.dom4j.DocumentHelper;
52
53
59 public class SAXReaderImpl implements SAXReader {
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());
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());
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());
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());
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
417
423 org.dom4j.io.SAXReader reader = null;
424
425 try {
426 reader = new org.dom4j.io.SAXReader(_SAX_PARSER_IMPL, validate);
427
428 reader.setEntityResolver(new EntityResolver());
429
430 reader.setFeature(_FEATURES_VALIDATION, validate);
431 reader.setFeature(_FEATURES_VALIDATION_SCHEMA, validate);
432 reader.setFeature(
433 _FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, validate);
434 reader.setFeature(_FEATURES_DYNAMIC, validate);
435 }
436 catch (Exception e) {
437 if (_log.isWarnEnabled()) {
438 _log.warn(
439 "XSD validation is diasabled because " + e.getMessage());
440 }
441
442 reader = new org.dom4j.io.SAXReader(validate);
443
444 reader.setEntityResolver(new EntityResolver());
445 }
446
447 return reader;
448 }
449
450 private static final String _SAX_PARSER_IMPL =
451 "org.apache.xerces.parsers.SAXParser";
452
453 private static final String _FEATURES_VALIDATION =
454 "http://xml.org/sax/features/validation";
455
456 private static final String _FEATURES_VALIDATION_SCHEMA =
457 "http://apache.org/xml/features/validation/schema";
458
459 private static final String _FEATURES_VALIDATION_SCHEMA_FULL_CHECKING =
460 "http://apache.org/xml/features/validation/schema-full-checking";
461
462 private static final String _FEATURES_DYNAMIC =
463 "http://apache.org/xml/features/validation/dynamic";
464
465 private static Log _log = LogFactoryUtil.getLog(SAXReaderImpl.class);
466
467 }