001: /*
002: * Copyright 2003 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: XMLHelper.java,v 1.4 2003/10/20 21:25:59 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.util;
012:
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.security.AccessController;
016: import java.security.PrivilegedAction;
017: import javax.jdo.JDOHelper;
018: import javax.xml.parsers.DocumentBuilder;
019: import javax.xml.parsers.DocumentBuilderFactory;
020: import javax.xml.parsers.ParserConfigurationException;
021: import org.xml.sax.EntityResolver;
022: import org.xml.sax.InputSource;
023: import org.xml.sax.SAXException;
024:
025: /**
026: * A utility class used to obtain <tt>DocumentBuilder</tt>s.
027: *
028: * @author <a href="mailto:pierreg0@users.sourceforge.net">Kelly Grizzle</a>
029: * @version $Revision: 1.4 $
030: */
031:
032: public class XMLHelper {
033: /**
034: * The system property that denotes whether to use a validating XML
035: * parser when parsing XML files. Valid values for this property are
036: * "true" and "false". This defaults to "true". This is the string
037: * "com.triactive.jdo.validateTables".
038: */
039: private static final String USE_VALIDATING_XML_PARSER_PROPERTY = "com.triactive.jdo.useValidatingXmlParser";
040:
041: /**
042: * Private constructor to prevent instantiation.
043: */
044: private XMLHelper() {
045: }
046:
047: /**
048: * Obtain a <tt>DocumentBuilder</tt> to parse XML files. Whether
049: * this is validating <tt>DocumentBuilder</tt> depends on the
050: * "com.triactive.jdo.useValidatingXmlParser" system propery.
051: *
052: * @return A <tt>DocumentBuilder</tt> to parse XML files.
053: *
054: * @exception ParserConfigurationException
055: * If a <tt>DocumentBuilder</tt> cannot be created.
056: */
057: public static DocumentBuilder getDocumentBuilder()
058: throws ParserConfigurationException {
059: boolean validating = new Boolean(System.getProperty(
060: USE_VALIDATING_XML_PARSER_PROPERTY, "true"))
061: .booleanValue();
062:
063: DocumentBuilderFactory factory = DocumentBuilderFactory
064: .newInstance();
065: factory.setValidating(validating);
066:
067: DocumentBuilder builder = factory.newDocumentBuilder();
068: builder.setEntityResolver(new JDOEntityResolver());
069:
070: return builder;
071: }
072:
073: private static class JDOEntityResolver implements EntityResolver {
074: private static final String RECOGNIZED_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN";
075:
076: public InputSource resolveEntity(String publicId,
077: String systemId) throws SAXException, IOException {
078: boolean isJdoDtd;
079:
080: if (publicId == null)
081: isJdoDtd = systemId.startsWith("file:")
082: && systemId.endsWith("/jdo.dtd");
083: else
084: isJdoDtd = RECOGNIZED_PUBLIC_ID.equals(publicId);
085:
086: if (isJdoDtd) {
087: InputStream stream = (InputStream) AccessController
088: .doPrivileged(new PrivilegedAction() {
089: public Object run() {
090: return JDOHelper.class.getClassLoader()
091: .getResourceAsStream(
092: "javax/jdo/jdo.dtd");
093: }
094: });
095:
096: return stream == null ? null : new InputSource(stream);
097: } else
098: return null;
099: }
100: }
101: }
|