KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilXml


1 /*
2  * $Id: UtilXml.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import javax.xml.parsers.DocumentBuilder JavaDoc;
38 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
39 import javax.xml.parsers.ParserConfigurationException JavaDoc;
40
41 import javolution.util.FastList;
42
43 import org.apache.xml.serialize.OutputFormat;
44 import org.apache.xml.serialize.XMLSerializer;
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48 import org.xml.sax.EntityResolver JavaDoc;
49 import org.xml.sax.ErrorHandler JavaDoc;
50 import org.xml.sax.InputSource JavaDoc;
51 import org.xml.sax.SAXException JavaDoc;
52 import org.xml.sax.SAXParseException JavaDoc;
53 import org.xml.sax.helpers.DefaultHandler JavaDoc;
54
55 /**
56  * Utilities methods to simplify dealing with JAXP & DOM XML parsing
57  *
58  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
59  * @version $Rev: 5462 $
60  * @since 2.0
61  */

62 public class UtilXml {
63
64     public static final String JavaDoc module = UtilXml.class.getName();
65     
66     public static String JavaDoc writeXmlDocument(Document JavaDoc document) throws java.io.IOException JavaDoc {
67         if (document == null) {
68             Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module);
69             return null;
70         }
71         return writeXmlDocument(document.getDocumentElement());
72     }
73
74     public static String JavaDoc writeXmlDocument(Element JavaDoc element) throws java.io.IOException JavaDoc {
75         if (element == null) {
76             Debug.logWarning("[UtilXml.writeXmlDocument] Element was null, doing nothing", module);
77             return null;
78         }
79
80         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
81         writeXmlDocument(bos, element);
82         String JavaDoc outString = bos.toString("UTF-8");
83
84         if (bos != null) bos.close();
85         return outString;
86     }
87
88     public static void writeXmlDocument(String JavaDoc filename, Document JavaDoc document)
89         throws java.io.FileNotFoundException JavaDoc, java.io.IOException JavaDoc {
90         if (document == null) {
91             Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module);
92             return;
93         }
94         writeXmlDocument(filename, document.getDocumentElement());
95     }
96
97     public static void writeXmlDocument(String JavaDoc filename, Element JavaDoc element)
98         throws java.io.FileNotFoundException JavaDoc, java.io.IOException JavaDoc {
99         if (element == null) {
100             Debug.logWarning("[UtilXml.writeXmlDocument] Element was null, doing nothing", module);
101             return;
102         }
103         if (filename == null) {
104             Debug.logWarning("[UtilXml.writeXmlDocument] Filename was null, doing nothing", module);
105             return;
106         }
107
108         File JavaDoc outFile = new File JavaDoc(filename);
109         FileOutputStream JavaDoc fos = null;
110         fos = new FileOutputStream JavaDoc(outFile);
111
112         try {
113             writeXmlDocument(fos, element);
114         } finally {
115             if (fos != null) fos.close();
116         }
117     }
118
119     public static void writeXmlDocument(OutputStream JavaDoc os, Document JavaDoc document) throws java.io.IOException JavaDoc {
120         if (document == null) {
121             Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module);
122             return;
123         }
124         writeXmlDocument(os, document.getDocumentElement());
125     }
126     public static void writeXmlDocument(OutputStream JavaDoc os, Element JavaDoc element) throws java.io.IOException JavaDoc {
127         if (element == null) {
128             Debug.logWarning("[UtilXml.writeXmlDocument] Element was null, doing nothing", module);
129             return;
130         }
131         if (os == null) {
132             Debug.logWarning("[UtilXml.writeXmlDocument] OutputStream was null, doing nothing", module);
133             return;
134         }
135
136         // if(document instanceof XmlDocument) {
137
// Crimson writer
138
// XmlDocument xdoc = (XmlDocument) document;
139
// xdoc.write(os);
140
// }
141
// else {
142
// Xerces writer
143
OutputFormat format = new OutputFormat(element.getOwnerDocument());
144         format.setIndent(2);
145         
146         XMLSerializer serializer = new XMLSerializer(os, format);
147         serializer.asDOMSerializer();
148         serializer.serialize(element);
149         // }
150
}
151
152     public static Document JavaDoc readXmlDocument(String JavaDoc content)
153             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
154         return readXmlDocument(content, true);
155     }
156
157     public static Document JavaDoc readXmlDocument(String JavaDoc content, boolean validate)
158             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
159         if (content == null) {
160             Debug.logWarning("[UtilXml.readXmlDocument] content was null, doing nothing", module);
161             return null;
162         }
163         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(content.getBytes("UTF-8"));
164         return readXmlDocument(bis, validate, "Internal Content");
165     }
166
167     public static Document JavaDoc readXmlDocument(URL JavaDoc url)
168             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
169         return readXmlDocument(url, true);
170     }
171
172     public static Document JavaDoc readXmlDocument(URL JavaDoc url, boolean validate)
173             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
174         if (url == null) {
175             Debug.logWarning("[UtilXml.readXmlDocument] URL was null, doing nothing", module);
176             return null;
177         }
178         return readXmlDocument(url.openStream(), validate, url.toString());
179     }
180
181     /**
182      * @deprecated
183      */

184     public static Document JavaDoc readXmlDocument(InputStream JavaDoc is)
185             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
186         return readXmlDocument(is, true, null);
187     }
188
189     public static Document JavaDoc readXmlDocument(InputStream JavaDoc is, String JavaDoc docDescription)
190             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
191         return readXmlDocument(is, true, docDescription);
192     }
193
194     public static Document JavaDoc readXmlDocument(InputStream JavaDoc is, boolean validate, String JavaDoc docDescription)
195             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
196         if (is == null) {
197             Debug.logWarning("[UtilXml.readXmlDocument] InputStream was null, doing nothing", module);
198             return null;
199         }
200
201         long startTime = System.currentTimeMillis();
202         
203         // DON'T do this: seems to be causing problems with Catalina/Tomcat, maybe it is expecting a different parser?
204
//System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
205

206         Document JavaDoc document = null;
207
208         /* Xerces DOMParser direct interaction; the other seems to be working better than this, so we'll stay with the standard JAXP stuff
209         DOMParser parser = new DOMParser();
210         try {
211             parser.setFeature("http://xml.org/sax/features/validation", true);
212             parser.setFeature("http://apache.org/xml/features/validation/schema", true);
213         } catch (SAXException e) {
214             Debug.logWarning("Could not set parser feature: " + e.toString(), module);
215         }
216         parser.parse(new InputSource(is));
217         document = parser.getDocument();
218         */

219         
220         /* Standard JAXP (mostly), but doesn't seem to be doing XML Schema validation, so making sure that is on... */
221         DocumentBuilderFactory JavaDoc factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
222         factory.setValidating(validate);
223         factory.setNamespaceAware(true);
224
225         factory.setAttribute("http://xml.org/sax/features/validation", Boolean.TRUE);
226         factory.setAttribute("http://apache.org/xml/features/validation/schema", Boolean.TRUE);
227         
228         // with a SchemaUrl, a URL object
229
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
230
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", SchemaUrl);
231
DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
232         if (validate) {
233             LocalResolver lr = new LocalResolver(new DefaultHandler JavaDoc());
234             ErrorHandler JavaDoc eh = new LocalErrorHandler(docDescription, lr);
235
236             builder.setEntityResolver(lr);
237             builder.setErrorHandler(eh);
238         }
239         document = builder.parse(is);
240         
241         double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
242         if (Debug.timingOn()) Debug.logTiming("XML Read " + totalSeconds + "s: " + docDescription, module);
243         return document;
244     }
245
246     public static Document JavaDoc makeEmptyXmlDocument() {
247         return makeEmptyXmlDocument(null);
248     }
249
250     public static Document JavaDoc makeEmptyXmlDocument(String JavaDoc rootElementName) {
251         Document JavaDoc document = null;
252         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
253
254         factory.setValidating(true);
255         // factory.setNamespaceAware(true);
256
try {
257             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
258
259             document = builder.newDocument();
260         } catch (Exception JavaDoc e) {
261             Debug.logError(e, module);
262         }
263
264         if (document == null) return null;
265         
266         if (rootElementName != null) {
267             Element JavaDoc rootElement = document.createElement(rootElementName);
268             document.appendChild(rootElement);
269         }
270
271         return document;
272     }
273
274     /** Creates a child element with the given name and appends it to the element child node list. */
275     public static Element JavaDoc addChildElement(Element JavaDoc element, String JavaDoc childElementName, Document JavaDoc document) {
276         Element JavaDoc newElement = document.createElement(childElementName);
277
278         element.appendChild(newElement);
279         return newElement;
280     }
281
282     /** Creates a child element with the given name and appends it to the element child node list.
283      * Also creates a Text node with the given value and appends it to the new elements child node list.
284      */

285     public static Element JavaDoc addChildElementValue(Element JavaDoc element, String JavaDoc childElementName,
286             String JavaDoc childElementValue, Document JavaDoc document) {
287         Element JavaDoc newElement = addChildElement(element, childElementName, document);
288
289         newElement.appendChild(document.createTextNode(childElementValue));
290         return newElement;
291     }
292
293     /** Creates a child element with the given name and appends it to the element child node list.
294      * Also creates a CDATASection node with the given value and appends it to the new elements child node list.
295      */

296     public static Element JavaDoc addChildElementCDATAValue(Element JavaDoc element, String JavaDoc childElementName,
297             String JavaDoc childElementValue, Document JavaDoc document) {
298         Element JavaDoc newElement = addChildElement(element, childElementName, document);
299
300         newElement.appendChild(document.createCDATASection(childElementValue));
301         return newElement;
302     }
303
304     /** Return a List of Element objects that are children of the given element */
305     public static List JavaDoc childElementList(Element JavaDoc element) {
306         if (element == null) return null;
307
308         List JavaDoc elements = FastList.newInstance();
309         Node JavaDoc node = element.getFirstChild();
310
311         if (node != null) {
312             do {
313                 if (node.getNodeType() == Node.ELEMENT_NODE) {
314                     Element JavaDoc childElement = (Element JavaDoc) node;
315
316                     elements.add(childElement);
317                 }
318             } while ((node = node.getNextSibling()) != null);
319         }
320         return elements;
321     }
322
323     /** Return a List of Element objects that have the given name and are
324      * immediate children of the given element; if name is null, all child
325      * elements will be included. */

326     public static List JavaDoc childElementList(Element JavaDoc element, String JavaDoc childElementName) {
327         if (element == null) return null;
328
329         List JavaDoc elements = FastList.newInstance();
330         Node JavaDoc node = element.getFirstChild();
331
332         if (node != null) {
333             do {
334                 if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null ||
335                         childElementName.equals(node.getNodeName()))) {
336                     Element JavaDoc childElement = (Element JavaDoc) node;
337
338                     elements.add(childElement);
339                 }
340             } while ((node = node.getNextSibling()) != null);
341         }
342         return elements;
343     }
344
345     /** Return a List of Element objects that have the given name and are
346      * immediate children of the given element; if name is null, all child
347      * elements will be included. */

348     public static List JavaDoc childElementList(Element JavaDoc element, Set JavaDoc childElementNames) {
349         if (element == null) return null;
350
351         List JavaDoc elements = FastList.newInstance();
352         if (childElementNames == null) return elements;
353         Node JavaDoc node = element.getFirstChild();
354
355         if (node != null) {
356             do {
357                 if (node.getNodeType() == Node.ELEMENT_NODE && childElementNames.contains(node.getNodeName())) {
358                     Element JavaDoc childElement = (Element JavaDoc) node;
359
360                     elements.add(childElement);
361                 }
362             } while ((node = node.getNextSibling()) != null);
363         }
364         return elements;
365     }
366
367     /** Return the first child Element
368      * returns the first element. */

369     public static Element JavaDoc firstChildElement(Element JavaDoc element, Set JavaDoc childElementNames) {
370         if (element == null) return null;
371         // get the first element with the given name
372
Node JavaDoc node = element.getFirstChild();
373
374         if (node != null) {
375             do {
376                 if (node.getNodeType() == Node.ELEMENT_NODE && childElementNames.contains(node.getNodeName())) {
377                     Element JavaDoc childElement = (Element JavaDoc) node;
378
379                     return childElement;
380                 }
381             } while ((node = node.getNextSibling()) != null);
382         }
383         return null;
384     }
385
386     /** Return the first child Element
387      * returns the first element. */

388     public static Element JavaDoc firstChildElement(Element JavaDoc element) {
389         if (element == null) return null;
390         // get the first element with the given name
391
Node JavaDoc node = element.getFirstChild();
392
393         if (node != null) {
394             do {
395                 if (node.getNodeType() == Node.ELEMENT_NODE) {
396                     Element JavaDoc childElement = (Element JavaDoc) node;
397
398                     return childElement;
399                 }
400             } while ((node = node.getNextSibling()) != null);
401         }
402         return null;
403     }
404
405     /** Return the first child Element with the given name; if name is null
406      * returns the first element. */

407     public static Element JavaDoc firstChildElement(Element JavaDoc element, String JavaDoc childElementName) {
408         if (element == null) return null;
409         // get the first element with the given name
410
Node JavaDoc node = element.getFirstChild();
411
412         if (node != null) {
413             do {
414                 if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null ||
415                         childElementName.equals(node.getNodeName()))) {
416                     Element JavaDoc childElement = (Element JavaDoc) node;
417
418                     return childElement;
419                 }
420             } while ((node = node.getNextSibling()) != null);
421         }
422         return null;
423     }
424
425     /** Return the first child Element with the given name; if name is null
426      * returns the first element. */

427     public static Element JavaDoc firstChildElement(Element JavaDoc element, String JavaDoc childElementName, String JavaDoc attrName, String JavaDoc attrValue) {
428         if (element == null) return null;
429         // get the first element with the given name
430
Node JavaDoc node = element.getFirstChild();
431
432         if (node != null) {
433             do {
434                 if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null ||
435                         childElementName.equals(node.getNodeName()))) {
436                     Element JavaDoc childElement = (Element JavaDoc) node;
437
438                     String JavaDoc value = childElement.getAttribute(attrName);
439
440                     if (value != null && value.equals(attrValue)) {
441                         return childElement;
442                     }
443                 }
444             } while ((node = node.getNextSibling()) != null);
445         }
446         return null;
447     }
448
449     /** Return the text (node value) contained by the named child node. */
450     public static String JavaDoc childElementValue(Element JavaDoc element, String JavaDoc childElementName) {
451         if (element == null) return null;
452         // get the value of the first element with the given name
453
Element JavaDoc childElement = firstChildElement(element, childElementName);
454
455         return elementValue(childElement);
456     }
457
458     /** Return the text (node value) contained by the named child node or a default value if null. */
459     public static String JavaDoc childElementValue(Element JavaDoc element, String JavaDoc childElementName, String JavaDoc defaultValue) {
460         if (element == null) return defaultValue;
461         // get the value of the first element with the given name
462
Element JavaDoc childElement = firstChildElement(element, childElementName);
463         String JavaDoc elementValue = elementValue(childElement);
464
465         if (elementValue == null || elementValue.length() == 0)
466             return defaultValue;
467         else
468             return elementValue;
469     }
470
471     /** Return the text (node value) of the first node under this, works best if normalized. */
472     public static String JavaDoc elementValue(Element JavaDoc element) {
473         if (element == null) return null;
474         // make sure we get all the text there...
475
element.normalize();
476         Node JavaDoc textNode = element.getFirstChild();
477
478         if (textNode == null) return null;
479
480         StringBuffer JavaDoc valueBuffer = new StringBuffer JavaDoc();
481         do {
482             if (textNode.getNodeType() == Node.CDATA_SECTION_NODE || textNode.getNodeType() == Node.TEXT_NODE) {
483                 valueBuffer.append(textNode.getNodeValue());
484             }
485         } while ((textNode = textNode.getNextSibling()) != null);
486         return valueBuffer.toString();
487     }
488
489     public static String JavaDoc checkEmpty(String JavaDoc string) {
490         if (string != null && string.length() > 0)
491             return string;
492         else
493             return "";
494     }
495
496     public static String JavaDoc checkEmpty(String JavaDoc string1, String JavaDoc string2) {
497         if (string1 != null && string1.length() > 0)
498             return string1;
499         else if (string2 != null && string2.length() > 0)
500             return string2;
501         else
502             return "";
503     }
504
505     public static String JavaDoc checkEmpty(String JavaDoc string1, String JavaDoc string2, String JavaDoc string3) {
506         if (string1 != null && string1.length() > 0)
507             return string1;
508         else if (string2 != null && string2.length() > 0)
509             return string2;
510         else if (string3 != null && string3.length() > 0)
511             return string3;
512         else
513             return "";
514     }
515
516     public static boolean checkBoolean(String JavaDoc str) {
517         return checkBoolean(str, false);
518     }
519
520     public static boolean checkBoolean(String JavaDoc str, boolean defaultValue) {
521         if (defaultValue) {
522             //default to true, ie anything but false is true
523
return !"false".equals(str);
524         } else {
525             //default to false, ie anything but true is false
526
return "true".equals(str);
527         }
528     }
529
530     /**
531      * Local entity resolver to handle J2EE DTDs. With this a http connection
532      * to sun is not needed during deployment.
533      * Function boolean hadDTD() is here to avoid validation errors in
534      * descriptors that do not have a DOCTYPE declaration.
535      */

536     public static class LocalResolver implements EntityResolver JavaDoc {
537
538         private boolean hasDTD = false;
539         private EntityResolver JavaDoc defaultResolver;
540
541         public LocalResolver(EntityResolver JavaDoc defaultResolver) {
542             this.defaultResolver = defaultResolver;
543         }
544
545         /**
546          * Returns DTD inputSource. If DTD was found in the dtds Map and inputSource was created
547          * flag hasDTD is set to true.
548          * @param publicId - Public ID of DTD
549          * @param systemId - System ID of DTD
550          * @return InputSource of DTD
551          */

552         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
553             //Debug.logInfo("resolving XML entity with publicId [" + publicId + "], systemId [" + systemId + "]", module);
554
hasDTD = false;
555             String JavaDoc dtd = UtilProperties.getSplitPropertyValue(UtilURL.fromResource("localdtds.properties"), publicId);
556             if (UtilValidate.isNotEmpty(dtd)) {
557                 if (Debug.verboseOn()) Debug.logVerbose("[UtilXml.LocalResolver.resolveEntity] resolving DTD with publicId [" + publicId +
558                         "], systemId [" + systemId + "] and the dtd file is [" + dtd + "]", module);
559                 try {
560                     URL JavaDoc dtdURL = UtilURL.fromResource(dtd);
561                     if (dtdURL == null) {
562                         throw new GeneralException("Local DTD not found - " + dtd);
563                     }
564                     InputStream JavaDoc dtdStream = dtdURL.openStream();
565                     InputSource JavaDoc inputSource = new InputSource JavaDoc(dtdStream);
566
567                     inputSource.setPublicId(publicId);
568                     hasDTD = true;
569                     if (Debug.verboseOn()) Debug.logVerbose("[UtilXml.LocalResolver.resolveEntity] got LOCAL DTD input source with publicId [" +
570                             publicId + "] and the dtd file is [" + dtd + "]", module);
571                     return inputSource;
572                 } catch (Exception JavaDoc e) {
573                     Debug.logWarning(e, module);
574                 }
575             } else {
576                 // nothing found by the public ID, try looking at the systemId, or at least the filename part of it and look for that on the classpath
577
int lastSlash = systemId.lastIndexOf("/");
578                 String JavaDoc filename = null;
579                 if (lastSlash == -1) {
580                     filename = systemId;
581                 } else {
582                     filename = systemId.substring(lastSlash + 1);
583                 }
584                 
585                 URL JavaDoc resourceUrl = UtilURL.fromResource(filename);
586                 
587                 if (resourceUrl != null) {
588                     InputStream JavaDoc resStream = resourceUrl.openStream();
589                     InputSource JavaDoc inputSource = new InputSource JavaDoc(resStream);
590     
591                     if (UtilValidate.isNotEmpty(publicId)) {
592                         inputSource.setPublicId(publicId);
593                     }
594                     hasDTD = true;
595                     if (Debug.verboseOn()) Debug.logVerbose("[UtilXml.LocalResolver.resolveEntity] got LOCAL DTD/Schema input source with publicId [" +
596                             publicId + "] and the file/resource is [" + filename + "]", module);
597                     return inputSource;
598                 } else {
599                     Debug.logWarning("[UtilXml.LocalResolver.resolveEntity] could not find LOCAL DTD/Schema with publicId [" +
600                             publicId + "] and the file/resource is [" + filename + "]", module);
601                     return null;
602                 }
603             }
604             //Debug.logInfo("[UtilXml.LocalResolver.resolveEntity] local resolve failed for DTD with publicId [" +
605
// publicId + "] and the dtd file is [" + dtd + "], trying defaultResolver", module);
606
return defaultResolver.resolveEntity(publicId, systemId);
607         }
608
609         /**
610          * Returns the boolean value to inform id DTD was found in the XML file or not
611          * @return boolean - true if DTD was found in XML
612          */

613         public boolean hasDTD() {
614             return hasDTD;
615         }
616     }
617
618
619     /** Local error handler for entity resolver to DocumentBuilder parser.
620      * Error is printed to output just if DTD was detected in the XML file.
621      */

622     public static class LocalErrorHandler implements ErrorHandler JavaDoc {
623
624         private String JavaDoc docDescription;
625         private LocalResolver localResolver;
626
627         public LocalErrorHandler(String JavaDoc docDescription, LocalResolver localResolver) {
628             this.docDescription = docDescription;
629             this.localResolver = localResolver;
630         }
631
632         public void error(SAXParseException JavaDoc exception) {
633             if (localResolver.hasDTD()) {
634                 Debug.logError("XmlFileLoader: File "
635                     + docDescription
636                     + " process error. Line: "
637                     + String.valueOf(exception.getLineNumber())
638                     + ". Error message: "
639                     + exception.getMessage(), module
640                 );
641             }
642         }
643
644         public void fatalError(SAXParseException JavaDoc exception) {
645             if (localResolver.hasDTD()) {
646                 Debug.logError("XmlFileLoader: File "
647                     + docDescription
648                     + " process fatal error. Line: "
649                     + String.valueOf(exception.getLineNumber())
650                     + ". Error message: "
651                     + exception.getMessage(), module
652                 );
653             }
654         }
655
656         public void warning(SAXParseException JavaDoc exception) {
657             if (localResolver.hasDTD()) {
658                 Debug.logError("XmlFileLoader: File "
659                     + docDescription
660                     + " process warning. Line: "
661                     + String.valueOf(exception.getLineNumber())
662                     + ". Error message: "
663                     + exception.getMessage(), module
664                 );
665             }
666         }
667     }
668 }
669
Popular Tags