Java tutorial
//package com.java2s; /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. * * Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common * Development and Distribution License("CDDL") (collectively, the * "License"). You may not use this file except in compliance with the * License. You can obtain a copy of the License at * http://www.netbeans.org/cddl-gplv2.html * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the * specific language governing permissions and limitations under the * License. When distributing the software, include this License Header * Notice in each file and include the License file at * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the * License Header, with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL * or only the GPL Version 2, indicate your decision by adding * "[Contributor] elects to include this software in this distribution * under the [CDDL or GPL Version 2] license." If you do not indicate a * single choice of license, a recipient has the option to distribute * your version of this file under either the CDDL, the GPL Version 2 or * to extend the choice of license to its licensees as provided above. * However, if you add GPL Version 2 code and therefore, elected the GPL * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. */ import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Main { private static DocumentBuilderFactory[][] doms = new DocumentBuilderFactory[2][2]; /** * Parses an XML document into a DOM tree. * * <div class="nonnormative"> * * <p> * Remember that when parsing XML files you often want to set an explicit * entity resolver. For example, consider a file such as this:</p> * * <pre> * <?xml version="1.0" encoding="UTF-8"?> * <!DOCTYPE root PUBLIC "-//NetBeans//DTD Foo 1.0//EN" "http://www.netbeans.org/dtds/foo-1_0.dtd"> * <root/> * </pre> * * <p> * If you parse this with a null entity resolver, or you use the default * resolver (EntityCatalog.getDefault) but do not do anything special with * this DTD, you will probably find the parse blocking to make a network * connection <em>even when you are not validating</em>. That is because * DTDs can be used to define entities and other XML oddities, and are not a * pure constraint language like Schema or RELAX-NG.</p> * * <p> * There are three basic ways to avoid the network connection.</p> * * <ol> * * <li><p> * Register the DTD. This is generally the best thing to do. See * EntityCatalog's documentation for details, but for example in your layer * use:</p> * * <pre> * <filesystem> * <folder name="xml"> * <folder name="entities"> * <folder name="NetBeans"> * <file name="DTD_Foo_1_0" * url="resources/foo-1_0.dtd"> * <attr name="hint.originalPublicID" * stringvalue="-//NetBeans//DTD Foo 1.0//EN"/> * </file> * </folder> * </folder> * </folder> * </filesystem> * </pre> * * <p> * Now the default system entity catalog will resolve the public ID to the * local copy in your module, not the network copy. Additionally, anyone who * mounts the "NetBeans Catalog" in the XML Entity Catalogs node in the * Runtime tab will be able to use your local copy of the DTD automatically, * for validation, code completion, etc. (The network URL should really * exist, though, for the benefit of other tools!)</p></li> * * <li><p> * You can also set an explicit entity resolver which maps that particular * public ID to some local copy of the DTD, if you do not want to register * it globally in the system for some reason. If handed other public IDs, * just return null to indicate that the system ID should be * loaded.</p></li> * * <li><p> * In some cases where XML parsing is very performance-sensitive, and you * know that you do not need validation and furthermore that the DTD defines * no infoset (there are no entity or character definitions, etc.), you can * speed up the parse. Turn off validation, but also supply a custom entity * resolver that does not even bother to load the DTD at all:</p> * * <pre> * public InputSource resolveEntity(String pubid, String sysid) * throws SAXException, IOException { * if (pubid.equals("-//NetBeans//DTD Foo 1.0//EN")) { * return new InputSource(new ByteArrayInputStream(new byte[0])); * } else { * return EntityCatalog.getDefault().resolveEntity(pubid, sysid); * } * } * </pre></li> * * </ol> * * </div> * * @param input a parser input (for URL users use: * <code>new InputSource(url.toString())</code> * @param validate if true validating parser is used * @param namespaceAware if true DOM is created by namespace aware parser * @param errorHandler a error handler to notify about exception (such as * {@link #defaultErrorHandler}) or <code>null</code> * @param entityResolver SAX entity resolver (such as * EntityCatalog#getDefault) or <code>null</code> * * @throws IOException if an I/O problem during parsing occurs * @throws SAXException is thrown if a parser error occurs * @throws FactoryConfigurationError Application developers should never * need to directly catch errors of this * type. * * @return document representing given input */ public static Document parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException { DocumentBuilder builder = null; DocumentBuilderFactory factory = getFactory(validate, namespaceAware); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N } if (errorHandler != null) { builder.setErrorHandler(errorHandler); } if (entityResolver != null) { builder.setEntityResolver(entityResolver); } return builder.parse(input); } private static synchronized DocumentBuilderFactory getFactory(boolean validate, boolean namespaceAware) { DocumentBuilderFactory factory = doms[validate ? 0 : 1][namespaceAware ? 0 : 1]; if (factory == null) { factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); doms[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory; } return factory; } }