📄 domfactory.java
字号:
name = "openxml.printer.html.class"; printerClass = HTMLPrinter.class; } else if (DTDDocument.class.isAssignableFrom( docClass )) { name = "openxml.printer.dtd.class"; printerClass = DTDPrinter.class; } else { name = "openxml.printer.xml.class"; printerClass = XMLPrinter.class; } // Given the property name, read its value and if valid, attempt to // load the named printer class. Make sure this class is indeed a // printer. There is no way to check its ability to generate a document // of the requested type: the printer for HTML documents might be rigged // to only generate DTD documents. Such is life. name = getProperty( name ); if (name != null) { try { printerClass = Class.forName( name ); if (!Printer.class.isAssignableFrom( printerClass )) { printerClass = XMLPrinter.class; Log.error( "DOMFactory.createPrinter: Printer class [" + name + "] is not a supported printer -- defaulting to XMLPrinter" ); } } catch (ClassNotFoundException except) { Log.error( "DOMFactory.createPrinter: Could not locate printer class [" + name + "] -- defaulting to XMLPrinter" ); } } // Printer class known, find the constructor which accepts a writer and // printing mode. This is the minimalist constructor for a printer and // is supported by all three printers. Using that constructor create a // new instance of the printer and return it. if (printerClass != null && printerClass != XMLPrinter.class) { try { cnst = printerClass.getConstructor( _printerSignature ); return (Printer)cnst.newInstance( new Object[] {writer, new Integer( mode )} ); } catch (Exception except) { Log.error( "DOMFactory.createPrinter: Could not create new instance of printer class [" + printerClass.getName() + "] -- defaulting to XMLPrinter" ); Log.error( except ); } } // Anything fails, or if specifically requested, return the default // XML printer. return new XMLPrinter( writer, mode ); } /** * Creates and returns a new XML printer. * * @param writer A writer for the document output * @param mode The printing mode * @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 ) throws IOException { return createPrinter( writer, mode, 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 output A stream 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( OutputStream stream, int mode, Class docClass ) throws IOException { return createPrinter( new XMLStreamWriter( stream ), mode, docClass ); } /** * Creates and returns a new XML printer. * * @param output A stream for the document output * @param mode The printing mode * @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( OutputStream stream, int mode ) throws IOException { return createPrinter( new XMLStreamWriter( stream ), mode, DOCUMENT_XML ); } /** * Returns the property from the OpenXML properties file. * * @param name The property name * @return Property value or null */ public static String getProperty( String name ) { return getProperties().getProperty( name ); } /** * Returns the properties list from the OpenXML properties file. If this * property list is changed, changes will affect the behavior of the factory * and other OpenXML elements. * * @return The properties list */ public static Properties getProperties() { String className; Class docClass; if (_xmlProps == null) { _xmlProps = new Properties(); try { _xmlProps.load( DOMFactory.class.getResourceAsStream( RESOURCE_PROPS ) ); } catch (Exception except) { Log.error( "DOMFactory.getProperties: Failed to load properties from resource [" + RESOURCE_PROPS + "]" ); Log.error( except ); } } return _xmlProps; } /** * Returns the specified document class, or the properties file specified * class, or the default. If the specified document class is not valid, a * runtime exception is thrown. If the specified class is null, the name * is read from the properties file and used as the based class. If that * property is missing or not a valid class, the default document class * ({@link Document}) is used. * * @param docClass The specified document class, or null * @return A valid document class, extending {@link Document} */ public static Class getDocClass( Class docClass ) { String prop; // If the specified document class is invalid, throw an exception, // as we do not want to assume default behavior. if (docClass != null) { if (docClass == DocumentType.class || docClass == DTDDocument.class) { return DTDDocument.class; } if (!Document.class.isAssignableFrom( docClass )) { throw new IllegalArgumentException( "Requested document class is not a valid class." ); } } // Read the property from the properties file and if not missing, // attempt to load the named class. If the named class does not extend // document, sadly it must be disposed of. prop = getProperty( "openxml.document.class" ); if (prop != null) { try { docClass = Class.forName( prop ); if (!Document.class.isAssignableFrom( docClass )) { docClass = null; Log.error( "DOMFactory.getDocClass: Document class [" + prop + "] is not a supported document class -- defaulting to Document" ); } } catch (ClassNotFoundException except) { Log.error( "DOMFactory.getDocClass: Could not locate document class [" + prop + "] -- defaulting to Document" ); } } // The default is Document. if (docClass == null) { docClass = DOCUMENT_XML; } return docClass; } public static Source newSource() { getHolderFinder(); return new SourceImpl(); } /** * Returns a singleton holder finder. This finder has default holder factories * registered for handling network, file, JAR and CLASSPATH document sources, * and mapping for built-in DTDs. Additional holder factories and Xcatalogs * may be specified in the properties file and are loaded and registered the * first time this method is called. * * @return An holder finder */ public static HolderFinder getHolderFinder() { XCatalog catalog; String prop; StringTokenizer tokenizer; if (_finder == null) { _finder = HolderFinderImpl.getHolderFinder(); catalog = XCatalogFactory.findCatalog( MAIN_CATALOG ); if (catalog != null) { _finder.registerFactory( XCatalogFactory.asHolderFactory( catalog ) ); } prop = DOMFactory.getProperty( "openxml.holder.factories" ); if (prop != null) { tokenizer = new StringTokenizer( prop, ";" ); while (tokenizer.hasMoreTokens()) { prop = tokenizer.nextToken(); try { _finder.registerFactory( (HolderFactory)Class.forName( prop ).newInstance() ); Log.info( "DOMHolderFactory.<init>: Registered holder factory [" + prop + "]" ); } catch (Exception except) { Log.error( "DOMHolderFactory.<init>: Failed to register holder factory [" + prop + "] -- class not found or could not be instantiated" ); } } } prop = DOMFactory.getProperty( "openxml.holder.catalogs" ); if (prop != null) { tokenizer = new StringTokenizer( prop, ";" ); while (tokenizer.hasMoreTokens()) { prop = tokenizer.nextToken(); try { catalog = XCatalogFactory.findCatalog( prop ); _finder.registerFactory( XCatalogFactory.asHolderFactory( catalog ) ); Log.info( "DOMHolderFactory.<init>: Registered XCatalog from [" + prop + "]" ); } catch (Exception except) { Log.error( "DOMHolderFactory.<init>: Failed to register XCatalog from [" + prop + "] -- catalog not found or could not be loaded" ); } } } } return _finder; } /** * XML document class. Can be used to request a new XML document, XML * parser or XML printer. Will produce a document of type {@link * Document}. */ public final static Class DOCUMENT_XML = Document.class; /** * HTML document class. Can be used to request a new HTML document, HTML * parser or HTML printer. Will produce a document of type {@link * HTMLDocument}. */ public final static Class DOCUMENT_HTML = HTMLDocumentImpl.class; /** * DTD document class. Can be used to request a new DTD document, DTD * parser or DTD printer. Will produce a document of type {@link * DTDDocument}. */ public final static Class DOCUMENT_DTD = DocumentType.class; /** * Holds the properties for the factory. This object is created on-demand * when a property is first accessed. */ private static Properties _xmlProps; /** * Holds a singleton holder finder. */ private static HolderFinder _finder; /** * The signature for the constructor of a parser class (class that * implemented {@link Parser}). Accepts <TT>reader</TT> and * <TT>docClass</TT>. */ private final static Class[] _parserSignature = new Class[] {Reader.class, String.class}; /** * The signature for the constructor of a printer class (class that * implemented {@link Printer}). Accepts <TT>writer</TT> and <TT>mode</TT>. */ private final static Class[] _printerSignature = new Class[] {Writer.class, int.class}; /** * Identifies the properties file as a resource. The named resource will * be loaded with the DOMFactory class loader. If the resource exists in * a different package, the full package path must be included, preceded * with '/'. */ private final static String RESOURCE_PROPS = "openxml.properties"; private final static String MAIN_CATALOG = "res:/org/openxml/source/dtd-catalog/catalog.xml"; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -