📄 domfactory.java
字号:
/** * org/ozone-db/xml/DOMFactory.java * * The contents of this file are subject to the OpenXML Public * License Version 1.0; you may not use this file except in compliance * with the License. You may obtain a copy of the License at * http://www.openxml.org/license.html * * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING * RIGHTS AND LIMITATIONS UNDER THE LICENSE. * * The Initial Developer of this code under the License is Assaf Arkin. * Portions created by Assaf Arkin are Copyright (C) 1998, 1999. * All Rights Reserved. *//** * Changes for Persistent DOM running with ozone are * Copyright 1999 by softwarebuero m&b (SMB). All rights reserved. */package org.ozoneDB.xml;import java.lang.reflect.*;import java.io.*;import java.util.*;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.html.HTMLDocument;import org.ozoneDB.xml.dom.html.HTMLDocumentImpl;import org.openxml.parser.*;import org.openxml.io.*;import org.openxml.source.*;import org.openxml.source.holders.*;import org.openxml.util.Log;/** * Factory for XML, HTML and DTD documents, parsers and printers. The factory * has methods for creating new documents, parsers and printers. The exact * type is determined by the document class, this might be {@link Document} * ({@link #DOCUMENT_XML}), {@link HTMLDocument} ({@link #DOCUMENT_HTML}), * {@link DocumentType} ({@link #DOCUMENT_DTD}) or a user document derived from * {@link XMLDocument}. * <P> * The default document type is controlled by the <TT>openxml.document.class</TT> * propety in the OpenXML properties file ({@link <A HREF="properties.html"> * openxml.prop</A>}). The parser and printer classes for XML, HTML and DTD * documents are also controlled by the property file. * <P> * The method {@link #createDocument} does not guarantee that it will return * {@link Document}, although this is the default behavior. To obtain a * {@link Document} either pass its class as argument, or call {@link * #createXMLDocument}. * <P> * A newly created parser is only guaranteed to extend {@link Parser}, even * if {@link #DOCUMENT_XML} has been specified as the document type. To create * a document from a user class, either use {@link Source}, or the following * code: * <PRE> * Parser parser; * * parser = DOMFactory.createParser( reader, sourceURI, docClass ); * if ( parser instanceof XMLParser ) * doc = ( (XMLParser) parser ).parseDocument( null, docClass ); * else * doc = parser.parseDocument(); * </PRE> * * * @version $Revision: 1.6 $ $Date: 2000/10/28 16:55:22 $ * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a> * @see org.w3c.dom.Document * @see XMLElement * @see XMLCollection */public class DOMFactory extends java.lang.Object { /** * Creates and returns a new XML document. The document type is {@link * Document}. * * @return A new XML document * @see Document */ public static Document createXMLDocument() { return (Document)createDocument( DOCUMENT_XML ); } /** * Creates and returns a new HTML document. The document type is * {@link HTMLDocument}. * * @return A new XML document * @see HTMLDocument */ public static HTMLDocument createHTMLDocument() { return (HTMLDocument)createDocument( DOCUMENT_HTML ); } /** * Creates and returns a new DTD document. The document type is * {@link DTDDocument}. * * @return A new DTD document * @see DTDDocument */ public static DTDDocument createDTDDocument() { return (DTDDocument)createDocument( DOCUMENT_DTD ); } /** * Creates and returns a new XML/HTML/DTD document. The document type is * based on <TT>docClass</TT>, which dictates whether the document is XML, * HTML or DTD. If <TT>docClass</TT> is null, the class type is read from * the property <TT>openxml.document.class</TT>, and if that property is * missing, the default {@link Document} is used. * <P> * Note that the returned document type may or may not be {@link Document}, * but it must extend {@link Document}, and that is also true for non-XML * documents. * * @return A new XML/HTML/DTD document * @see Document */ public static Document createDocument( Class docClass ) { // Returns the document type which is either docClass (must extend // Document), or the class from the properties file, or XMLDocument. docClass = getDocClass( docClass ); // Instantiate a new document, report any error encountered and default // to XMLDocument is necessary. if (docClass != null && docClass != Document.class && docClass != XMLDocument.class) { try { return (Document)docClass.newInstance(); } catch (Exception except) { Log.error( "DOMFactory.createDocument: Could not create new instance of document class [" + docClass.getName() + "] -- defaulting to Document" ); Log.error( except ); } } return new XMLDocument(); } /** * Creates and returns a new XML/HTML/DTD parser. The parser type is * determined by the document class provided in <TT>docClass</TT>, which * dictates whether the parser is XML, HTML or DTD. If <TT>docClass</TT> is * null, the same rules that govern {@link #createDocument} apply here. * <P> * The parser is only guaranteed to extend {@link Parser} and will use * {@link #createDocument} to create an instance of the parsed document. * To create a document of a user class, either use {@link Source}, or * the following code: * <PRE> * Parser parser; * * parser = DOMFactory.createParser( reader, sourceURI, docClass ); * if ( parser instanceof XMLParser ) * doc = ( (XMLParser) parser ).parseDocument( null, docClass ); * else * doc = parser.parseDocument(); * </PRE> * * @param reader A reader to the document source * @param sourceURI The source URI * @param docClass The requested document type * @return A new parser */ public static Parser createParser( Reader reader, String sourceURI, Class docClass ) { String name; Class parserClass; Constructor cnst; // Get the specified document class, or the default document class. // Either one is expected as the output of the parser, so work with it. // Based on the document type decide which is the default parser and // the name of the parser class property. docClass = getDocClass( docClass ); if (HTMLDocument.class.isAssignableFrom( docClass )) { name = "openxml.parser.html.class"; parserClass = HTMLParser.class; } else if (DTDDocument.class.isAssignableFrom( docClass )) { name = "openxml.parser.dtd.class"; parserClass = DTDParser.class; } else { name = "openxml.parser.xml.class"; parserClass = XMLParser.class; } // Given the property name, read its value and if valid, attempt to // load the named parser class. Make sure this class is indeed a // parser. There is no way to check its ability to produce a document // of the requested type: the parser for HTML documents might be rigged // to only produce DTD documents. Such is life. name = getProperty( name ); if (name != null) { try { parserClass = Class.forName( name ); if (!Parser.class.isAssignableFrom( parserClass )) { parserClass = XMLParser.class; Log.error( "DOMFactory.createParser: Parser class [" + name + "] is not a supported parser -- defaulting to XMLParser" ); } } catch (ClassNotFoundException except) { Log.error( "DOMFactory.createParser: Could not locate parser class [" + name + "] -- defaulting to XMLParser" ); } } // Parser class known, find the constructor which accepts a reader and // sourceURI. This is the minimalist constructor for a parser and is // supported by all three parsers. Using that constructor create a new // instance of the parser and return it. if (parserClass != null && parserClass != XMLParser.class) { try { cnst = parserClass.getConstructor( _parserSignature ); if (cnst != null) { return (Parser)cnst.newInstance( new Object[] {reader, sourceURI} ); } } catch (Exception except) { Log.error( "DOMFactory.createParser: Could not create new instance of parser class [" + parserClass.getName() + "] -- defaulting to XMLParser" ); Log.error( except ); } } // Anything fails, or if specifically requested, return the default // XML parser. return new XMLParser( reader, sourceURI ); } /** * Creates and returns a new XML parser. * * @param reader A reader to the document source * @param sourceURI The source URI * @return A new parser */ public static Parser createParser( Reader reader, String sourceName ) throws IOException { return createParser( reader, sourceName, DOCUMENT_XML ); } /** * Creates and returns a new XML/HTML/DTD parser. The parser type is * determined by the document class provided in <TT>docClass</TT>, which * dictates whether the parser is XML, HTML or DTD. If <TT>docClass</TT> is * null, the same rules that govern {@link #createDocument} apply here. * <P> * The parser is only guaranteed to extend {@link Parser} and will use * {@link #createDocument} to create an instance of the parsed document. * To create a document of a user class, either use {@link Source}, or * the following code: * <PRE> * Parser parser; * * parser = DOMFactory.createParser( reader, sourceURI, docClass ); * if ( parser instanceof XMLParser ) * doc = ( (XMLParser) parser ).parseDocument( null, docClass ); * else * doc = parser.parseDocument(); * </PRE> * * @param input An input stream to the document source * @param sourceURI The source URI * @param docClass The requested document type * @return A new parser */ public static Parser createParser( InputStream stream, String sourceName, Class docClass ) throws IOException { return createParser( new BufferedReader( new InputStreamReader( stream ) ), sourceName, docClass ); } /** * Creates and returns a new XML parser. * * @param input An input stream to the document source * @param sourceURI The source URI * @return A new parser */ public static Parser createParser( InputStream stream, String sourceName ) throws IOException { return createParser( new BufferedReader( new InputStreamReader( stream ) ), sourceName, DOCUMENT_XML ); } /** * Creates and returns a new XML/HTML/DTD printer. The printer type is * determined by the document class provided in <TT>docClass</TT>, which * dictates whether the printer is XML, HTML or DTD. If <TT>docClass</TT> is * null, the same rules that govern {@link #createDocument} apply here. * * @param writer A writer for the document output * @param mode The printing mode * @param docClass The document type * @return A new printer * * @deprecated * This method has become obsolete in favor of the <a * href="x3p/package-summary.html">X3P Publisher and Producer APIs</a>. * This method is temporarily provided for backward compatibility but * will not be included in release 1.1. */ public static Printer createPrinter( Writer writer, int mode, Class docClass ) throws IOException { Class printerClass; String name; Constructor cnst; // Get the specified document class, or the default document class. // Either one is expected as the input to the parser, so work with it. // Based on the document type decide which is the default printer and // the name of the printer class property. docClass = getDocClass( docClass ); if (HTMLDocument.class.isAssignableFrom( docClass )) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -