XMLHelper.java :  » Database-ORM » TJDO » com » triactive » jdo » util » Java Open Source

Java Open Source » Database ORM » TJDO 
TJDO » com » triactive » jdo » util » XMLHelper.java
/*
 * Copyright 2003 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: XMLHelper.java,v 1.4 2003/10/20 21:25:59 jackknifebarber Exp $
 */

package com.triactive.jdo.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.jdo.JDOHelper;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


/**
 * A utility class used to obtain <tt>DocumentBuilder</tt>s.
 *
 * @author <a href="mailto:pierreg0@users.sourceforge.net">Kelly Grizzle</a>
 * @version $Revision: 1.4 $
 */

public class XMLHelper
{
    /**
     * The system property that denotes whether to use a validating XML
     * parser when parsing XML files.  Valid values for this property are
     * "true" and "false".  This defaults to "true".  This is the string
     * "com.triactive.jdo.validateTables".
     */
    private static final String USE_VALIDATING_XML_PARSER_PROPERTY =
        "com.triactive.jdo.useValidatingXmlParser";


    /**
     * Private constructor to prevent instantiation.
     */
    private XMLHelper() {}


    /**
     * Obtain a <tt>DocumentBuilder</tt> to parse XML files.  Whether
     * this is validating <tt>DocumentBuilder</tt> depends on the
     * "com.triactive.jdo.useValidatingXmlParser" system propery.
     *
     * @return  A <tt>DocumentBuilder</tt> to parse XML files.
     *
     * @exception ParserConfigurationException
     *      If a <tt>DocumentBuilder</tt> cannot be created.
     */
    public static DocumentBuilder getDocumentBuilder()
        throws ParserConfigurationException
    {
        boolean validating =
            new Boolean(System.getProperty(USE_VALIDATING_XML_PARSER_PROPERTY, "true")).booleanValue();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);

        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new JDOEntityResolver());

        return builder;
    }


    private static class JDOEntityResolver implements EntityResolver 
    {
        private static final String RECOGNIZED_PUBLIC_ID = 
            "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN";

        public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException 
        {
            boolean isJdoDtd;

            if (publicId == null)
                isJdoDtd = systemId.startsWith("file:") && systemId.endsWith("/jdo.dtd");
            else
                isJdoDtd = RECOGNIZED_PUBLIC_ID.equals(publicId);

            if (isJdoDtd)
            {
                InputStream stream = (InputStream)AccessController.doPrivileged(
                    new PrivilegedAction()
                    {
                        public Object run()
                        {
                            return JDOHelper.class.getClassLoader().getResourceAsStream("javax/jdo/jdo.dtd");
                        }
                    }
                );

                return stream == null ? null : new InputSource(stream);
            }
            else
                return null;
        }
    }
}


java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.