KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > util > XMLHelper


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: XMLHelper.java,v 1.4 2003/10/20 21:25:59 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17 import javax.jdo.JDOHelper;
18 import javax.xml.parsers.DocumentBuilder JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21 import org.xml.sax.EntityResolver JavaDoc;
22 import org.xml.sax.InputSource JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25
26 /**
27  * A utility class used to obtain <tt>DocumentBuilder</tt>s.
28  *
29  * @author <a HREF="mailto:pierreg0@users.sourceforge.net">Kelly Grizzle</a>
30  * @version $Revision: 1.4 $
31  */

32
33 public class XMLHelper
34 {
35     /**
36      * The system property that denotes whether to use a validating XML
37      * parser when parsing XML files. Valid values for this property are
38      * "true" and "false". This defaults to "true". This is the string
39      * "com.triactive.jdo.validateTables".
40      */

41     private static final String JavaDoc USE_VALIDATING_XML_PARSER_PROPERTY =
42         "com.triactive.jdo.useValidatingXmlParser";
43
44
45     /**
46      * Private constructor to prevent instantiation.
47      */

48     private XMLHelper() {}
49
50
51     /**
52      * Obtain a <tt>DocumentBuilder</tt> to parse XML files. Whether
53      * this is validating <tt>DocumentBuilder</tt> depends on the
54      * "com.triactive.jdo.useValidatingXmlParser" system propery.
55      *
56      * @return A <tt>DocumentBuilder</tt> to parse XML files.
57      *
58      * @exception ParserConfigurationException
59      * If a <tt>DocumentBuilder</tt> cannot be created.
60      */

61     public static DocumentBuilder JavaDoc getDocumentBuilder()
62         throws ParserConfigurationException JavaDoc
63     {
64         boolean validating =
65             new Boolean JavaDoc(System.getProperty(USE_VALIDATING_XML_PARSER_PROPERTY, "true")).booleanValue();
66
67         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
68         factory.setValidating(validating);
69
70         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
71         builder.setEntityResolver(new JDOEntityResolver());
72
73         return builder;
74     }
75
76
77     private static class JDOEntityResolver implements EntityResolver JavaDoc
78     {
79         private static final String JavaDoc RECOGNIZED_PUBLIC_ID =
80             "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN";
81
82         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
83             throws SAXException JavaDoc, IOException JavaDoc
84         {
85             boolean isJdoDtd;
86
87             if (publicId == null)
88                 isJdoDtd = systemId.startsWith("file:") && systemId.endsWith("/jdo.dtd");
89             else
90                 isJdoDtd = RECOGNIZED_PUBLIC_ID.equals(publicId);
91
92             if (isJdoDtd)
93             {
94                 InputStream JavaDoc stream = (InputStream JavaDoc)AccessController.doPrivileged(
95                     new PrivilegedAction JavaDoc()
96                     {
97                         public Object JavaDoc run()
98                         {
99                             return JDOHelper.class.getClassLoader().getResourceAsStream("javax/jdo/jdo.dtd");
100                         }
101                     }
102                 );
103
104                 return stream == null ? null : new InputSource JavaDoc(stream);
105             }
106             else
107                 return null;
108         }
109     }
110 }
111
112
113
Popular Tags