feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* <p>To provide customized error handling, implement this interface and
* use the <code>setErrorListener</code> method to register an instance of the
* implmentation with the {@link javax.xml.transform.Transformer}. The
* <code>Transformer</code> then reports all errors and warnings through this
* interface.</p>
*
* <p>If an application does <em>not</em> register its own custom
* <code>ErrorListener</code>, the default <code>ErrorListener</code>
* is used which reports all warnings and errors to <code>System.err</code>
* and does not throw any <code>Exception</code>s.
* Applications are <em>strongly</em> encouraged to register and use
* <code>ErrorListener</code>s that insure proper behavior for warnings and
* errors.</p>
*
* <p>For transformation errors, a <code>Transformer</code> must use this
* interface instead of throwing an <code>Exception</code>: it is up to the
* application to decide whether to throw an <code>Exception</code> for
* different types of errors and warnings. Note however that the
* <code>Transformer</code> is not required to continue with the transformation
* after a call to {@link #fatalError(TransformerException exception)}.</p>
*
* <p><code>Transformer</code>s may use this mechanism to report XML parsing
* errors as well as transformation errors.</p>
*/
public interface ErrorListener {
/**
* Receive notification of a warning.
*
* <p>{@link javax.xml.transform.Transformer} can use this method to report
* conditions that are not errors or fatal errors. The default behaviour
* is to take no action.</p>
*
* <p>After invoking this method, the Transformer must continue with
* the transformation. It should still be possible for the
* application to process the document through to the end.</p>
*
* @param exception The warning information encapsulated in a
* transformer exception.
*
* @throws javax.xml.transform.TransformerException if the application
* chooses to discontinue the transformation.
*
* @see javax.xml.transform.TransformerException
*/
public abstract void warning(TransformerException exception)
throws TransformerException;
/**
* Receive notification of a recoverable error.
*
* <p>The transformer must continue to try and provide normal transformation
* after invoking this method. It should still be possible for the
* application to process the document through to the end if no other errors
* are encountered.</p>
*
* @param exception The error information encapsulated in a
* transformer exception.
*
* @throws javax.xml.transform.TransformerException if the application
* chooses to discontinue the transformation.
*
* @see javax.xml.transform.TransformerException
*/
public abstract void error(TransformerException exception)
throws TransformerException;
/**
* <p>Receive notification of a non-recoverable error.</p>
*
* <p>The processor may choose to continue, but will not normally
* proceed to a successful completion.</p>
*
* <p>The method should throw an exception if it is unable to
* process the error, or if it wishes execution to terminate
* immediately. The processor will not necessarily honor this
* request.</p>
*
* @param exception The error information encapsulated in a
* <code>TransformerException</code>.
*
* @throws javax.xml.transform.TransformerException if the application
* chooses to discontinue the transformation.
*
* @see javax.xml.transform.TransformerException
*/
public abstract void fatalError(TransformerException exception)
throws TransformerException;
}

View File

@@ -0,0 +1,301 @@
/*
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
/**
* <p>Implements pluggable Datatypes.</p>
*
* <p>This class is duplicated for each JAXP subpackage so keep it in
* sync. It is package private for secure class loading.</p>
*
* @author Santiago.PericasGeertsen@sun.com
* @author Huizhe.Wang@oracle.com
*/
class FactoryFinder {
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xalan.internal.";
/**
* Internal debug flag.
*/
private static boolean debug = false;
/**
* Cache for properties in java.home/lib/jaxp.properties
*/
private final static Properties cacheProps = new Properties();
/**
* Flag indicating if properties from java.home/lib/jaxp.properties
* have been cached.
*/
static volatile boolean firstTime = true;
/**
* Security support class use to check access control before
* getting certain system resources.
*/
private final static SecuritySupport ss = new SecuritySupport();
// Define system property "jaxp.debug" to get output
static {
// Use try/catch block to support applets, which throws
// SecurityException out of this code.
try {
String val = ss.getSystemProperty("jaxp.debug");
// Allow simply setting the prop to turn on debug
debug = val != null && !"false".equals(val);
}
catch (SecurityException se) {
debug = false;
}
}
private static void dPrint(String msg) {
if (debug) {
System.err.println("JAXP: " + msg);
}
}
/**
* Attempt to load a class using the class loader supplied. If that fails
* and fall back is enabled, the current (i.e. bootstrap) class loader is
* tried.
*
* If the class loader supplied is <code>null</code>, first try using the
* context class loader followed by the current (i.e. bootstrap) class
* loader.
*
* Use bootstrap classLoader if cl = null and useBSClsLoader is true
*/
static private Class<?> getProviderClass(String className, ClassLoader cl,
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
{
try {
if (cl == null) {
if (useBSClsLoader) {
return Class.forName(className, false, FactoryFinder.class.getClassLoader());
} else {
cl = ss.getContextClassLoader();
if (cl == null) {
throw new ClassNotFoundException();
}
else {
return Class.forName(className, false, cl);
}
}
}
else {
return Class.forName(className, false, cl);
}
}
catch (ClassNotFoundException e1) {
if (doFallback) {
// Use current class loader - should always be bootstrap CL
return Class.forName(className, false, FactoryFinder.class.getClassLoader());
}
else {
throw e1;
}
}
}
/**
* Create an instance of a class. Delegates to method
* <code>getProviderClass()</code> in order to load the class.
*
* @param type Base class / Service interface of the factory to
* instantiate.
*
* @param className Name of the concrete class corresponding to the
* service provider
*
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
* current <code>Thread</code>'s context classLoader is used to load the factory class.
*
* @param doFallback True if the current ClassLoader should be tried as
* a fallback if the class is not found using cl
*
*/
static <T> T newInstance(Class<T> type, String className, ClassLoader cl,
boolean doFallback)
throws TransformerFactoryConfigurationError
{
assert type != null;
boolean useBSClsLoader = false;
// make sure we have access to restricted packages
if (System.getSecurityManager() != null) {
if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
cl = null;
useBSClsLoader = true;
}
}
try {
Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
if (!type.isAssignableFrom(providerClass)) {
throw new ClassCastException(className + " cannot be cast to " + type.getName());
}
Object instance = providerClass.newInstance();
if (debug) { // Extra check to avoid computing cl strings
dPrint("created new instance of " + providerClass +
" using ClassLoader: " + cl);
}
return type.cast(instance);
}
catch (ClassNotFoundException x) {
throw new TransformerFactoryConfigurationError(x,
"Provider " + className + " not found");
}
catch (Exception x) {
throw new TransformerFactoryConfigurationError(x,
"Provider " + className + " could not be instantiated: " + x);
}
}
/**
* Finds the implementation Class object in the specified order. Main
* entry point.
* @return Class object of factory, never null
*
* @param type Base class / Service interface of the
* factory to find.
*
* @param fallbackClassName Implementation class name, if nothing else
* is found. Use null to mean no fallback.
*
* Package private so this code can be shared.
*/
static <T> T find(Class<T> type, String fallbackClassName)
throws TransformerFactoryConfigurationError
{
assert type != null;
final String factoryId = type.getName();
dPrint("find factoryId =" + factoryId);
// Use the system property first
try {
String systemProp = ss.getSystemProperty(factoryId);
if (systemProp != null) {
dPrint("found system property, value=" + systemProp);
return newInstance(type, systemProp, null, true);
}
}
catch (SecurityException se) {
if (debug) se.printStackTrace();
}
// try to read from $java.home/lib/jaxp.properties
try {
if (firstTime) {
synchronized (cacheProps) {
if (firstTime) {
String configFile = ss.getSystemProperty("java.home") + File.separator +
"lib" + File.separator + "jaxp.properties";
File f = new File(configFile);
firstTime = false;
if (ss.doesFileExist(f)) {
dPrint("Read properties file "+f);
cacheProps.load(ss.getFileInputStream(f));
}
}
}
}
final String factoryClassName = cacheProps.getProperty(factoryId);
if (factoryClassName != null) {
dPrint("found in $java.home/jaxp.properties, value=" + factoryClassName);
return newInstance(type, factoryClassName, null, true);
}
}
catch (Exception ex) {
if (debug) ex.printStackTrace();
}
// Try Jar Service Provider Mechanism
T provider = findServiceProvider(type);
if (provider != null) {
return provider;
}
if (fallbackClassName == null) {
throw new TransformerFactoryConfigurationError(null,
"Provider for " + factoryId + " cannot be found");
}
dPrint("loaded from fallback value: " + fallbackClassName);
return newInstance(type, fallbackClassName, null, true);
}
/*
* Try to find provider using the ServiceLoader.
*
* @param type Base class / Service interface of the factory to find.
*
* @return instance of provider class if found or null
*/
private static <T> T findServiceProvider(final Class<T> type)
throws TransformerFactoryConfigurationError
{
try {
return AccessController.doPrivileged(new PrivilegedAction<T>() {
public T run() {
final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);
final Iterator<T> iterator = serviceLoader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
}
});
} catch(ServiceConfigurationError e) {
// It is not possible to wrap an error directly in
// FactoryConfigurationError - so we need to wrap the
// ServiceConfigurationError in a RuntimeException.
// The alternative would be to modify the logic in
// FactoryConfigurationError to allow setting a
// Throwable as the cause, but that could cause
// compatibility issues down the road.
final RuntimeException x = new RuntimeException(
"Provider for " + type + " cannot be created", e);
final TransformerFactoryConfigurationError error =
new TransformerFactoryConfigurationError(x, x.getMessage());
throw error;
}
}
}

View File

@@ -0,0 +1,200 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* Provides string constants that can be used to set
* output properties for a Transformer, or to retrieve
* output properties from a Transformer or Templates object.
* <p>All the fields in this class are read-only.</p>
*
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public class OutputKeys {
/**
* Default constructor is private on purpose. This class is
* only for static variable access, and should never be constructed.
*/
private OutputKeys() { }
/**
* method = "xml" | "html" | "text" | <var>expanded name</var>.
*
* <p>The value of the method property identifies the overall method that
* should be used for outputting the result tree. Other non-namespaced
* values may be used, such as "xhtml", but, if accepted, the handling
* of such values is implementation defined. If any of the method values
* are not accepted and are not namespace qualified,
* then {@link javax.xml.transform.Transformer#setOutputProperty}
* or {@link javax.xml.transform.Transformer#setOutputProperties} will
* throw a {@link java.lang.IllegalArgumentException}.</p>
*
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String METHOD = "method";
/**
* version = <var>nmtoken</var>.
*
* <p><code>version</code> specifies the version of the output
* method.</p>
* <p>When the output method is "xml", the version value specifies the
* version of XML to be used for outputting the result tree. The default
* value for the xml output method is 1.0. When the output method is
* "html", the version value indicates the version of the HTML.
* The default value for the xml output method is 4.0, which specifies
* that the result should be output as HTML conforming to the HTML 4.0
* Recommendation [HTML]. If the output method is "text", the version
* property is ignored.</p>
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String VERSION = "version";
/**
* encoding = <var>string</var>.
*
* <p><code>encoding</code> specifies the preferred character
* encoding that the Transformer should use to encode sequences of
* characters as sequences of bytes. The value of the encoding property should be
* treated case-insensitively. The value must only contain characters in
* the range #x21 to #x7E (i.e., printable ASCII characters). The value
* should either be a <code>charset</code> registered with the Internet
* Assigned Numbers Authority <a href="http://www.iana.org/">[IANA]</a>,
* <a href="http://www.ietf.org/rfc/rfc2278.txt">[RFC2278]</a>
* or start with <code>X-</code>.</p>
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String ENCODING = "encoding";
/**
* omit-xml-declaration = "yes" | "no".
*
* <p><code>omit-xml-declaration</code> specifies whether the XSLT
* processor should output an XML declaration; the value must be
* <code>yes</code> or <code>no</code>.</p>
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String OMIT_XML_DECLARATION = "omit-xml-declaration";
/**
* standalone = "yes" | "no".
*
* <p><code>standalone</code> specifies whether the Transformer
* should output a standalone document declaration; the value must be
* <code>yes</code> or <code>no</code>.</p>
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String STANDALONE = "standalone";
/**
* doctype-public = <var>string</var>.
* <p>See the documentation for the {@link #DOCTYPE_SYSTEM} property
* for a description of what the value of the key should be.</p>
*
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String DOCTYPE_PUBLIC = "doctype-public";
/**
* doctype-system = <var>string</var>.
* <p><code>doctype-system</code> specifies the system identifier
* to be used in the document type declaration.</p>
* <p>If the doctype-system property is specified, the xml output method
* should output a document type declaration immediately before the first
* element. The name following &lt;!DOCTYPE should be the name of the first
* element. If doctype-public property is also specified, then the xml
* output method should output PUBLIC followed by the public identifier
* and then the system identifier; otherwise, it should output SYSTEM
* followed by the system identifier. The internal subset should be empty.
* The value of the doctype-public property should be ignored unless the doctype-system
* property is specified.</p>
* <p>If the doctype-public or doctype-system properties are specified,
* then the html output method should output a document type declaration
* immediately before the first element. The name following &lt;!DOCTYPE
* should be HTML or html. If the doctype-public property is specified,
* then the output method should output PUBLIC followed by the specified
* public identifier; if the doctype-system property is also specified,
* it should also output the specified system identifier following the
* public identifier. If the doctype-system property is specified but
* the doctype-public property is not specified, then the output method
* should output SYSTEM followed by the specified system identifier.</p>
*
* <p><code>doctype-system</code> specifies the system identifier
* to be used in the document type declaration.</p>
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String DOCTYPE_SYSTEM = "doctype-system";
/**
* cdata-section-elements = <var>expanded names</var>.
*
* <p><code>cdata-section-elements</code> specifies a whitespace delimited
* list of the names of elements whose text node children should be output
* using CDATA sections. Note that these names must use the format
* described in the section Qualfied Name Representation in
* {@link javax.xml.transform}.</p>
*
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation.</a>
*/
public static final String CDATA_SECTION_ELEMENTS =
"cdata-section-elements";
/**
* indent = "yes" | "no".
*
* <p><code>indent</code> specifies whether the Transformer may
* add additional whitespace when outputting the result tree; the value
* must be <code>yes</code> or <code>no</code>. </p>
* @see <a href="http://www.w3.org/TR/xslt#output">
* section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String INDENT = "indent";
/**
* media-type = <var>string</var>.
*
* <p><code>media-type</code> specifies the media type (MIME
* content type) of the data that results from outputting the result
* tree. The <code>charset</code> parameter should not be specified
* explicitly; instead, when the top-level media type is
* <code>text</code>, a <code>charset</code> parameter should be added
* according to the character encoding actually used by the output
* method. </p>
* @see <a href="http://www.w3.org/TR/xslt#output">s
* ection 16 of the XSL Transformations (XSLT) W3C Recommendation</a>
*/
public static final String MEDIA_TYPE = "media-type";
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* <p>An object that implements this interface contains the information
* needed to build a transformation result tree.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public interface Result {
/**
* The name of the processing instruction that is sent if the
* result tree disables output escaping.
*
* <p>Normally, result tree serialization escapes & and < (and
* possibly other characters) when outputting text nodes.
* This ensures that the output is well-formed XML. However,
* it is sometimes convenient to be able to produce output that is
* almost, but not quite well-formed XML; for example,
* the output may include ill-formed sections that will
* be transformed into well-formed XML by a subsequent non-XML aware
* process. If a processing instruction is sent with this name,
* serialization should be output without any escaping. </p>
*
* <p>Result DOM trees may also have PI_DISABLE_OUTPUT_ESCAPING and
* PI_ENABLE_OUTPUT_ESCAPING inserted into the tree.</p>
*
* @see <a href="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a>
*/
public static final String PI_DISABLE_OUTPUT_ESCAPING =
"javax.xml.transform.disable-output-escaping";
/**
* The name of the processing instruction that is sent
* if the result tree enables output escaping at some point after having
* received a PI_DISABLE_OUTPUT_ESCAPING processing instruction.
*
* @see <a href="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a>
*/
public static final String PI_ENABLE_OUTPUT_ESCAPING =
"javax.xml.transform.enable-output-escaping";
/**
* Set the system identifier for this Result.
*
* <p>If the Result is not to be written to a file, the system identifier is optional.
* The application may still want to provide one, however, for use in error messages
* and warnings, or to resolve relative output identifiers.</p>
*
* @param systemId The system identifier as a URI string.
*/
public void setSystemId(String systemId);
/**
* Get the system identifier that was set with setSystemId.
*
* @return The system identifier that was set with setSystemId,
* or null if setSystemId was not called.
*/
public String getSystemId();
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
import java.security.*;
import java.io.*;
/**
* This class is duplicated for each JAXP subpackage so keep it in sync.
* It is package private and therefore is not exposed as part of the JAXP
* API.
*
* Security related methods that only work on J2SE 1.2 and newer.
*/
class SecuritySupport {
ClassLoader getContextClassLoader() throws SecurityException{
return (ClassLoader)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
ClassLoader cl = null;
//try {
cl = Thread.currentThread().getContextClassLoader();
//} catch (SecurityException ex) { }
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
return cl;
}
});
}
String getSystemProperty(final String propName) {
return (String)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(propName);
}
});
}
FileInputStream getFileInputStream(final File file)
throws FileNotFoundException
{
try {
return (FileInputStream)
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws FileNotFoundException {
return new FileInputStream(file);
}
});
} catch (PrivilegedActionException e) {
throw (FileNotFoundException)e.getException();
}
}
boolean doesFileExist(final File f) {
return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new Boolean(f.exists());
}
})).booleanValue();
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* An object that implements this interface contains the information
* needed to act as source input (XML source or transformation instructions).
*/
public interface Source {
/**
* Set the system identifier for this Source.
*
* <p>The system identifier is optional if the source does not
* get its data from a URL, but it may still be useful to provide one.
* The application can use a system identifier, for example, to resolve
* relative URIs and to include in error messages and warnings.</p>
*
* @param systemId The system identifier as a URL string.
*/
public void setSystemId(String systemId);
/**
* Get the system identifier that was set with setSystemId.
*
* @return The system identifier that was set with setSystemId, or null
* if setSystemId was not called.
*/
public String getSystemId();
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* This interface is primarily for the purposes of reporting where
* an error occurred in the XML source or transformation instructions.
*/
public interface SourceLocator {
/**
* Return the public identifier for the current document event.
*
* <p>The return value is the public identifier of the document
* entity or of the external parsed entity in which the markup that
* triggered the event appears.</p>
*
* @return A string containing the public identifier, or
* null if none is available.
* @see #getSystemId
*/
public String getPublicId();
/**
* Return the system identifier for the current document event.
*
* <p>The return value is the system identifier of the document
* entity or of the external parsed entity in which the markup that
* triggered the event appears.</p>
*
* <p>If the system identifier is a URL, the parser must resolve it
* fully before passing it to the application.</p>
*
* @return A string containing the system identifier, or null
* if none is available.
* @see #getPublicId
*/
public String getSystemId();
/**
* Return the line number where the current document event ends.
*
* <p><strong>Warning:</strong> The return value from the method
* is intended only as an approximation for the sake of error
* reporting; it is not intended to provide sufficient information
* to edit the character content of the original XML document.</p>
*
* <p>The return value is an approximation of the line number
* in the document entity or external parsed entity where the
* markup that triggered the event appears.</p>
*
* @return The line number, or -1 if none is available.
* @see #getColumnNumber
*/
public int getLineNumber();
/**
* Return the character position where the current document event ends.
*
* <p><strong>Warning:</strong> The return value from the method
* is intended only as an approximation for the sake of error
* reporting; it is not intended to provide sufficient information
* to edit the character content of the original XML document.</p>
*
* <p>The return value is an approximation of the column number
* in the document entity or external parsed entity where the
* markup that triggered the event appears.</p>
*
* @return The column number, or -1 if none is available.
* @see #getLineNumber
*/
public int getColumnNumber();
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
import java.util.Properties;
/**
* An object that implements this interface is the runtime representation of processed
* transformation instructions.
*
* <p>Templates must be threadsafe for a given instance
* over multiple threads running concurrently, and may
* be used multiple times in a given session.</p>
*/
public interface Templates {
/**
* Create a new transformation context for this Templates object.
*
* @return A valid non-null instance of a Transformer.
*
* @throws TransformerConfigurationException if a Transformer can not be created.
*/
Transformer newTransformer() throws TransformerConfigurationException;
/**
* Get the properties corresponding to the effective xsl:output element.
* The object returned will
* be a clone of the internal values. Accordingly, it can be mutated
* without mutating the Templates object, and then handed in to
* {@link javax.xml.transform.Transformer#setOutputProperties}.
*
* <p>The properties returned should contain properties set by the stylesheet,
* and these properties are "defaulted" by default properties specified by
* <a href="http://www.w3.org/TR/xslt#output">section 16 of the
* XSL Transformations (XSLT) W3C Recommendation</a>. The properties that
* were specifically set by the stylesheet should be in the base
* Properties list, while the XSLT default properties that were not
* specifically set should be in the "default" Properties list. Thus,
* getOutputProperties().getProperty(String key) will obtain any
* property in that was set by the stylesheet, <em>or</em> the default
* properties, while
* getOutputProperties().get(String key) will only retrieve properties
* that were explicitly set in the stylesheet.</p>
*
* <p>For XSLT,
* <a href="http://www.w3.org/TR/xslt#attribute-value-templates">Attribute
* Value Templates</a> attribute values will
* be returned unexpanded (since there is no context at this point). The
* namespace prefixes inside Attribute Value Templates will be unexpanded,
* so that they remain valid XPath values.</p>
*
* @return A Properties object, never null.
*/
Properties getOutputProperties();
}

View File

@@ -0,0 +1,339 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
import java.util.Properties;
/**
* An instance of this abstract class can transform a
* source tree into a result tree.
*
* <p>An instance of this class can be obtained with the
* {@link TransformerFactory#newTransformer TransformerFactory.newTransformer}
* method. This instance may then be used to process XML from a
* variety of sources and write the transformation output to a
* variety of sinks.</p>
*
* <p>An object of this class may not be used in multiple threads
* running concurrently. Different Transformers may be used
* concurrently by different threads.</p>
*
* <p>A <code>Transformer</code> may be used multiple times. Parameters and
* output properties are preserved across transformations.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public abstract class Transformer {
/**
* Default constructor is protected on purpose.
*/
protected Transformer() { }
/**
* <p>Reset this <code>Transformer</code> to its original configuration.</p>
*
* <p><code>Transformer</code> is reset to the same state as when it was created with
* {@link TransformerFactory#newTransformer()},
* {@link TransformerFactory#newTransformer(Source source)} or
* {@link Templates#newTransformer()}.
* <code>reset()</code> is designed to allow the reuse of existing <code>Transformer</code>s
* thus saving resources associated with the creation of new <code>Transformer</code>s.</p>
*
* <p>The reset <code>Transformer</code> is not guaranteed to have the same {@link URIResolver}
* or {@link ErrorListener} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
* It is guaranteed to have a functionally equal <code>URIResolver</code>
* and <code>ErrorListener</code>.</p>
*
* @throws UnsupportedOperationException When implementation does not
* override this method.
*
* @since 1.5
*/
public void reset() {
// implementors should override this method
throw new UnsupportedOperationException(
"This Transformer, \"" + this.getClass().getName() + "\", does not support the reset functionality."
+ " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""
+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""
);
}
/**
* <p>Transform the XML <code>Source</code> to a <code>Result</code>.
* Specific transformation behavior is determined by the settings of the
* <code>TransformerFactory</code> in effect when the
* <code>Transformer</code> was instantiated and any modifications made to
* the <code>Transformer</code> instance.</p>
*
* <p>An empty <code>Source</code> is represented as an empty document
* as constructed by {@link javax.xml.parsers.DocumentBuilder#newDocument()}.
* The result of transforming an empty <code>Source</code> depends on
* the transformation behavior; it is not always an empty
* <code>Result</code>.</p>
*
* @param xmlSource The XML input to transform.
* @param outputTarget The <code>Result</code> of transforming the
* <code>xmlSource</code>.
*
* @throws TransformerException If an unrecoverable error occurs
* during the course of the transformation.
*/
public abstract void transform(Source xmlSource, Result outputTarget)
throws TransformerException;
/**
* Add a parameter for the transformation.
*
* <p>Pass a qualified name as a two-part string, the namespace URI
* enclosed in curly braces ({}), followed by the local name. If the
* name has a null URL, the String only contain the local name. An
* application can safely check for a non-null URI by testing to see if the
* first character of the name is a '{' character.</p>
* <p>For example, if a URI and local name were obtained from an element
* defined with &lt;xyz:foo
* xmlns:xyz="http://xyz.foo.com/yada/baz.html"/&gt;,
* then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
* Note that no prefix is used.</p>
*
* @param name The name of the parameter, which may begin with a
* namespace URI in curly braces ({}).
* @param value The value object. This can be any valid Java object. It is
* up to the processor to provide the proper object coersion or to simply
* pass the object on for use in an extension.
*
* @throws NullPointerException If value is null.
*/
public abstract void setParameter(String name, Object value);
/**
* Get a parameter that was explicitly set with setParameter.
*
* <p>This method does not return a default parameter value, which
* cannot be determined until the node context is evaluated during
* the transformation process.
*
* @param name of <code>Object</code> to get
*
* @return A parameter that has been set with setParameter.
*/
public abstract Object getParameter(String name);
/**
* <p>Set a list of parameters.</p>
*
* <p>Note that the list of parameters is specified as a
* <code>Properties</code> <code>Object</code> which limits the parameter
* values to <code>String</code>s. Multiple calls to
* {@link #setParameter(String name, Object value)} should be used when the
* desired values are non-<code>String</code> <code>Object</code>s.
* The parameter names should conform as specified in
* {@link #setParameter(String name, Object value)}.
* An <code>IllegalArgumentException</code> is thrown if any names do not
* conform.</p>
*
* <p>New parameters in the list are added to any existing parameters.
* If the name of a new parameter is equal to the name of an existing
* parameter as determined by {@link java.lang.Object#equals(Object obj)},
* the existing parameter is set to the new value.</p>
*
* @param params Parameters to set.
*
* @throws IllegalArgumentException If any parameter names do not conform
* to the naming rules.
*/
/**
* Clear all parameters set with setParameter.
*/
public abstract void clearParameters();
/**
* Set an object that will be used to resolve URIs used in
* document().
*
* <p>If the resolver argument is null, the URIResolver value will
* be cleared and the transformer will no longer have a resolver.</p>
*
* @param resolver An object that implements the URIResolver interface,
* or null.
*/
public abstract void setURIResolver(URIResolver resolver);
/**
* Get an object that will be used to resolve URIs used in
* document().
*
* @return An object that implements the URIResolver interface,
* or null.
*/
public abstract URIResolver getURIResolver();
/**
* Set the output properties for the transformation. These
* properties will override properties set in the Templates
* with xsl:output.
*
* <p>If argument to this function is null, any properties
* previously set are removed, and the value will revert to the value
* defined in the templates object.</p>
*
* <p>Pass a qualified property key name as a two-part string, the namespace
* URI enclosed in curly braces ({}), followed by the local name. If the
* name has a null URL, the String only contain the local name. An
* application can safely check for a non-null URI by testing to see if the
* first character of the name is a '{' character.</p>
* <p>For example, if a URI and local name were obtained from an element
* defined with &lt;xyz:foo
* xmlns:xyz="http://xyz.foo.com/yada/baz.html"/&gt;,
* then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
* Note that no prefix is used.</p>
* An <code>IllegalArgumentException</code> is thrown if any of the
* argument keys are not recognized and are not namespace qualified.
*
* @param oformat A set of output properties that will be
* used to override any of the same properties in affect
* for the transformation.
*
* @throws IllegalArgumentException When keys are not recognized and
* are not namespace qualified.
*
* @see javax.xml.transform.OutputKeys
* @see java.util.Properties
*
*/
public abstract void setOutputProperties(Properties oformat);
/**
* <p>Get a copy of the output properties for the transformation.</p>
*
* <p>The properties returned should contain properties set by the user,
* and properties set by the stylesheet, and these properties
* are "defaulted" by default properties specified by
* <a href="http://www.w3.org/TR/xslt#output">section 16 of the
* XSL Transformations (XSLT) W3C Recommendation</a>. The properties that
* were specifically set by the user or the stylesheet should be in the base
* Properties list, while the XSLT default properties that were not
* specifically set should be the default Properties list. Thus,
* getOutputProperties().getProperty(String key) will obtain any
* property in that was set by {@link #setOutputProperty},
* {@link #setOutputProperties}, in the stylesheet, <em>or</em> the default
* properties, while
* getOutputProperties().get(String key) will only retrieve properties
* that were explicitly set by {@link #setOutputProperty},
* {@link #setOutputProperties}, or in the stylesheet.</p>
*
* <p>Note that mutation of the Properties object returned will not
* effect the properties that the transformer contains.</p>
*
* <p>If any of the argument keys are not recognized and are not
* namespace qualified, the property will be ignored and not returned.
* In other words the behaviour is not orthogonal with
* {@link #setOutputProperties setOutputProperties}.</p>
*
* @return A copy of the set of output properties in effect for
* the next transformation.
*
* @see javax.xml.transform.OutputKeys
* @see java.util.Properties
* @see <a href="http://www.w3.org/TR/xslt#output">
* XSL Transformations (XSLT) Version 1.0</a>
*/
public abstract Properties getOutputProperties();
/**
* Set an output property that will be in effect for the
* transformation.
*
* <p>Pass a qualified property name as a two-part string, the namespace URI
* enclosed in curly braces ({}), followed by the local name. If the
* name has a null URL, the String only contain the local name. An
* application can safely check for a non-null URI by testing to see if the
* first character of the name is a '{' character.</p>
* <p>For example, if a URI and local name were obtained from an element
* defined with &lt;xyz:foo
* xmlns:xyz="http://xyz.foo.com/yada/baz.html"/&gt;,
* then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
* Note that no prefix is used.</p>
*
* <p>The Properties object that was passed to {@link #setOutputProperties}
* won't be effected by calling this method.</p>
*
* @param name A non-null String that specifies an output
* property name, which may be namespace qualified.
* @param value The non-null string value of the output property.
*
* @throws IllegalArgumentException If the property is not supported, and is
* not qualified with a namespace.
*
* @see javax.xml.transform.OutputKeys
*/
public abstract void setOutputProperty(String name, String value)
throws IllegalArgumentException;
/**
* <p>Get an output property that is in effect for the transformer.</p>
*
* <p>If a property has been set using {@link #setOutputProperty},
* that value will be returned. Otherwise, if a property is explicitly
* specified in the stylesheet, that value will be returned. If
* the value of the property has been defaulted, that is, if no
* value has been set explicitly either with {@link #setOutputProperty} or
* in the stylesheet, the result may vary depending on
* implementation and input stylesheet.</p>
*
* @param name A non-null String that specifies an output
* property name, which may be namespace qualified.
*
* @return The string value of the output property, or null
* if no property was found.
*
* @throws IllegalArgumentException If the property is not supported.
*
* @see javax.xml.transform.OutputKeys
*/
public abstract String getOutputProperty(String name)
throws IllegalArgumentException;
/**
* Set the error event listener in effect for the transformation.
*
* @param listener The new error listener.
*
* @throws IllegalArgumentException if listener is null.
*/
public abstract void setErrorListener(ErrorListener listener)
throws IllegalArgumentException;
/**
* Get the error event handler in effect for the transformation.
* Implementations must provide a default error listener.
*
* @return The current error handler, which should never be null.
*/
public abstract ErrorListener getErrorListener();
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* Indicates a serious configuration error.
*/
public class TransformerConfigurationException extends TransformerException {
private static final long serialVersionUID = 1285547467942875745L;
/**
* Create a new <code>TransformerConfigurationException</code> with no
* detail mesage.
*/
public TransformerConfigurationException() {
super("Configuration Error");
}
/**
* Create a new <code>TransformerConfigurationException</code> with
* the <code>String </code> specified as an error message.
*
* @param msg The error message for the exception.
*/
public TransformerConfigurationException(String msg) {
super(msg);
}
/**
* Create a new <code>TransformerConfigurationException</code> with a
* given <code>Exception</code> base cause of the error.
*
* @param e The exception to be encapsulated in a
* TransformerConfigurationException.
*/
public TransformerConfigurationException(Throwable e) {
super(e);
}
/**
* Create a new <code>TransformerConfigurationException</code> with the
* given <code>Exception</code> base cause and detail message.
*
* @param e The exception to be encapsulated in a
* TransformerConfigurationException
* @param msg The detail message.
*/
public TransformerConfigurationException(String msg, Throwable e) {
super(msg, e);
}
/**
* Create a new TransformerConfigurationException from a message and a Locator.
*
* <p>This constructor is especially useful when an application is
* creating its own exception from within a DocumentHandler
* callback.</p>
*
* @param message The error or warning message.
* @param locator The locator object for the error or warning.
*/
public TransformerConfigurationException(String message,
SourceLocator locator) {
super(message, locator);
}
/**
* Wrap an existing exception in a TransformerConfigurationException.
*
* @param message The error or warning message, or null to
* use the message from the embedded exception.
* @param locator The locator object for the error or warning.
* @param e Any exception.
*/
public TransformerConfigurationException(String message,
SourceLocator locator,
Throwable e) {
super(message, locator, e);
}
}

View File

@@ -0,0 +1,374 @@
/*
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Objects;
/**
* This class specifies an exceptional condition that occurred
* during the transformation process.
*/
public class TransformerException extends Exception {
private static final long serialVersionUID = 975798773772956428L;
/** Field locator specifies where the error occurred */
SourceLocator locator;
/**
* Method getLocator retrieves an instance of a SourceLocator
* object that specifies where an error occurred.
*
* @return A SourceLocator object, or null if none was specified.
*/
public SourceLocator getLocator() {
return this.locator;
}
/**
* Method setLocator sets an instance of a SourceLocator
* object that specifies where an error occurred.
*
* @param location A SourceLocator object, or null to clear the location.
*/
public void setLocator(SourceLocator location) {
this.locator = location;
}
/** Field containedException specifies a wrapped exception. May be null. */
Throwable containedException;
/**
* This method retrieves an exception that this exception wraps.
*
* @return An Throwable object, or null.
* @see #getCause
*/
public Throwable getException() {
return containedException;
}
/**
* Returns the cause of this throwable or <code>null</code> if the
* cause is nonexistent or unknown. (The cause is the throwable that
* caused this throwable to get thrown.)
* @return the cause, or null if unknown
*/
@Override
public Throwable getCause() {
return ((containedException == this)
? null
: containedException);
}
/**
* Initializes the <i>cause</i> of this throwable to the specified value.
* (The cause is the throwable that caused this throwable to get thrown.)
*
* <p>This method can be called at most once. It is generally called from
* within the constructor, or immediately after creating the
* throwable. If this throwable was created
* with {@link #TransformerException(Throwable)} or
* {@link #TransformerException(String,Throwable)}, this method cannot be called
* even once.
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <code>null</code> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @return a reference to this <code>Throwable</code> instance.
* @throws IllegalArgumentException if <code>cause</code> is this
* throwable. (A throwable cannot
* be its own cause.)
* @throws IllegalStateException if this throwable was
* created with {@link #TransformerException(Throwable)} or
* {@link #TransformerException(String,Throwable)}, or this method has already
* been called on this throwable.
*/
@Override
public synchronized Throwable initCause(Throwable cause) {
if (this.containedException != null) {
throw new IllegalStateException("Can't overwrite cause");
}
if (cause == this) {
throw new IllegalArgumentException(
"Self-causation not permitted");
}
this.containedException = cause;
return this;
}
/**
* Create a new TransformerException.
*
* @param message The error or warning message.
*/
public TransformerException(String message) {
this(message, null, null);
}
/**
* Create a new TransformerException wrapping an existing exception.
*
* @param e The exception to be wrapped.
*/
public TransformerException(Throwable e) {
this(null, null, e);
}
/**
* Wrap an existing exception in a TransformerException.
*
* <p>This is used for throwing processor exceptions before
* the processing has started.</p>
*
* @param message The error or warning message, or null to
* use the message from the embedded exception.
* @param e Any exception
*/
public TransformerException(String message, Throwable e) {
this(message, null, e);
}
/**
* Create a new TransformerException from a message and a Locator.
*
* <p>This constructor is especially useful when an application is
* creating its own exception from within a DocumentHandler
* callback.</p>
*
* @param message The error or warning message.
* @param locator The locator object for the error or warning.
*/
public TransformerException(String message, SourceLocator locator) {
this(message, locator, null);
}
/**
* Wrap an existing exception in a TransformerException.
*
* @param message The error or warning message, or null to
* use the message from the embedded exception.
* @param locator The locator object for the error or warning.
* @param e Any exception
*/
public TransformerException(String message, SourceLocator locator,
Throwable e) {
super(((message == null) || (message.length() == 0))
? ((e == null) ? "" : e.toString())
: message);
this.containedException = e;
this.locator = locator;
}
/**
* Get the error message with location information
* appended.
*
* @return A <code>String</code> representing the error message with
* location information appended.
*/
public String getMessageAndLocation() {
StringBuilder sbuffer = new StringBuilder();
sbuffer.append(Objects.toString(super.getMessage(), ""));
sbuffer.append(Objects.toString(getLocationAsString(), ""));
return sbuffer.toString();
}
/**
* Get the location information as a string.
*
* @return A string with location info, or null
* if there is no location information.
*/
public String getLocationAsString() {
if (locator == null) {
return null;
}
if (System.getSecurityManager() == null) {
return getLocationString();
} else {
return (String) AccessController.doPrivileged(
new PrivilegedAction<String>() {
public String run() {
return getLocationString();
}
},
new AccessControlContext(new ProtectionDomain[] {getNonPrivDomain()})
);
}
}
/**
* Constructs the location string.
* @return the location string
*/
private String getLocationString() {
if (locator == null) {
return null;
}
StringBuilder sbuffer = new StringBuilder();
String systemID = locator.getSystemId();
int line = locator.getLineNumber();
int column = locator.getColumnNumber();
if (null != systemID) {
sbuffer.append("; SystemID: ");
sbuffer.append(systemID);
}
if (0 != line) {
sbuffer.append("; Line#: ");
sbuffer.append(line);
}
if (0 != column) {
sbuffer.append("; Column#: ");
sbuffer.append(column);
}
return sbuffer.toString();
}
/**
* Print the the trace of methods from where the error
* originated. This will trace all nested exception
* objects, as well as this object.
*/
@Override
public void printStackTrace() {
printStackTrace(new java.io.PrintWriter(System.err, true));
}
/**
* Print the the trace of methods from where the error
* originated. This will trace all nested exception
* objects, as well as this object.
* @param s The stream where the dump will be sent to.
*/
@Override
public void printStackTrace(java.io.PrintStream s) {
printStackTrace(new java.io.PrintWriter(s));
}
/**
* Print the the trace of methods from where the error
* originated. This will trace all nested exception
* objects, as well as this object.
* @param s The writer where the dump will be sent to.
*/
@Override
public void printStackTrace(java.io.PrintWriter s) {
if (s == null) {
s = new java.io.PrintWriter(System.err, true);
}
try {
String locInfo = getLocationAsString();
if (null != locInfo) {
s.println(locInfo);
}
super.printStackTrace(s);
} catch (Throwable e) {}
Throwable exception = getException();
for (int i = 0; (i < 10) && (null != exception); i++) {
s.println("---------");
try {
if (exception instanceof TransformerException) {
String locInfo =
((TransformerException) exception)
.getLocationAsString();
if (null != locInfo) {
s.println(locInfo);
}
}
exception.printStackTrace(s);
} catch (Throwable e) {
s.println("Could not print stack trace...");
}
try {
Method meth =
((Object) exception).getClass().getMethod("getException",
(Class[]) null);
if (null != meth) {
Throwable prev = exception;
exception = (Throwable) meth.invoke(exception, (Object[]) null);
if (prev == exception) {
break;
}
} else {
exception = null;
}
} catch (InvocationTargetException | IllegalAccessException
| NoSuchMethodException e) {
exception = null;
}
}
// insure output is written
s.flush();
}
/**
* Creates a ProtectionDomain that has no permission.
* @return a ProtectionDomain
*/
private ProtectionDomain getNonPrivDomain() {
CodeSource nullSource = new CodeSource(null, (CodeSigner[]) null);
PermissionCollection noPermission = new Permissions();
return new ProtectionDomain(nullSource, noPermission);
}
}

View File

@@ -0,0 +1,419 @@
/*
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* <p>A TransformerFactory instance can be used to create
* {@link javax.xml.transform.Transformer} and
* {@link javax.xml.transform.Templates} objects.</p>
*
* <p>The system property that determines which Factory implementation
* to create is named <code>"javax.xml.transform.TransformerFactory"</code>.
* This property names a concrete subclass of the
* <code>TransformerFactory</code> abstract class. If the property is not
* defined, a platform default is be used.</p>
*
* @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
* @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a>
*
* @since 1.5
*/
public abstract class TransformerFactory {
/**
* Default constructor is protected on purpose.
*/
protected TransformerFactory() { }
/**
* <p>Obtain a new instance of a <code>TransformerFactory</code>.
* This static method creates a new factory instance.</p>
* <p>This method uses the following ordered lookup procedure to determine
* the <code>TransformerFactory</code> implementation class to
* load:</p>
* <ul>
* <li>
* Use the <code>javax.xml.transform.TransformerFactory</code> system
* property.
* </li>
* <li>
* Use the properties file "lib/jaxp.properties" in the JRE directory.
* This configuration file is in standard <code>java.util.Properties
* </code> format and contains the fully qualified name of the
* implementation class with the key being the system property defined
* above.
* <br>
* The jaxp.properties file is read only once by the JAXP implementation
* and it's values are then cached for future use. If the file does not exist
* when the first attempt is made to read from it, no further attempts are
* made to check for its existence. It is not possible to change the value
* of any property in jaxp.properties after it has been read for the first time.
* </li>
* <li>
* Use the service-provider loading facilities, defined by the
* {@link java.util.ServiceLoader} class, to attempt to locate and load an
* implementation of the service using the {@linkplain
* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:
* the service-provider loading facility will use the {@linkplain
* java.lang.Thread#getContextClassLoader() current thread's context class loader}
* to attempt to load the service. If the context class
* loader is null, the {@linkplain
* ClassLoader#getSystemClassLoader() system class loader} will be used.
* </li>
* <li>
* Otherwise, the system-default implementation is returned.
* </li>
* </ul>
*
* <p>Once an application has obtained a reference to a <code>
* TransformerFactory</code> it can use the factory to configure
* and obtain transformer instances.</p>
*
* @return new TransformerFactory instance, never null.
*
* @throws TransformerFactoryConfigurationError Thrown in case of {@linkplain
* java.util.ServiceConfigurationError service configuration error} or if
* the implementation is not available or cannot be instantiated.
*/
public static TransformerFactory newInstance()
throws TransformerFactoryConfigurationError {
return FactoryFinder.find(
/* The default property name according to the JAXP spec */
TransformerFactory.class,
/* The fallback implementation class name, XSLTC */
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
}
/**
* <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name.
* This function is useful when there are multiple providers in the classpath.
* It gives more control to the application as it can specify which provider
* should be loaded.</p>
*
* <p>Once an application has obtained a reference to a <code>
* TransformerFactory</code> it can use the factory to configure
* and obtain transformer instances.</p>
*
* <h2>Tip for Trouble-shooting</h2>
* <p>Setting the <code>jaxp.debug</code> system property will cause
* this method to print a lot of debug messages
* to <code>System.err</code> about what it is doing and where it is looking at.</p>
*
* <p> If you have problems try:</p>
* <pre>
* java -Djaxp.debug=1 YourProgram ....
* </pre>
*
* @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>.
*
* @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
* current <code>Thread</code>'s context classLoader is used to load the factory class.
*
* @return new TransformerFactory instance, never null.
*
* @throws TransformerFactoryConfigurationError
* if <code>factoryClassName</code> is <code>null</code>, or
* the factory class cannot be loaded, instantiated.
*
* @see #newInstance()
*
* @since 1.6
*/
public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader)
throws TransformerFactoryConfigurationError{
//do not fallback if given classloader can't find the class, throw exception
return FactoryFinder.newInstance(TransformerFactory.class,
factoryClassName, classLoader, false);
}
/**
* <p>Process the <code>Source</code> into a <code>Transformer</code>
* <code>Object</code>. The <code>Source</code> is an XSLT document that
* conforms to <a href="http://www.w3.org/TR/xslt">
* XSL Transformations (XSLT) Version 1.0</a>. Care must
* be taken not to use this <code>Transformer</code> in multiple
* <code>Thread</code>s running concurrently.
* Different <code>TransformerFactories</code> can be used concurrently by
* different <code>Thread</code>s.</p>
*
* @param source <code>Source </code> of XSLT document used to create
* <code>Transformer</code>.
* Examples of XML <code>Source</code>s include
* {@link javax.xml.transform.dom.DOMSource DOMSource},
* {@link javax.xml.transform.sax.SAXSource SAXSource}, and
* {@link javax.xml.transform.stream.StreamSource StreamSource}.
*
* @return A <code>Transformer</code> object that may be used to perform
* a transformation in a single <code>Thread</code>, never
* <code>null</code>.
*
* @throws TransformerConfigurationException Thrown if there are errors when
* parsing the <code>Source</code> or it is not possible to create a
* <code>Transformer</code> instance.
*
* @see <a href="http://www.w3.org/TR/xslt">
* XSL Transformations (XSLT) Version 1.0</a>
*/
public abstract Transformer newTransformer(Source source)
throws TransformerConfigurationException;
/**
* <p>Create a new <code>Transformer</code> that performs a copy
* of the <code>Source</code> to the <code>Result</code>.
* i.e. the "<em>identity transform</em>".</p>
*
* @return A Transformer object that may be used to perform a transformation
* in a single thread, never null.
*
* @throws TransformerConfigurationException When it is not
* possible to create a <code>Transformer</code> instance.
*/
public abstract Transformer newTransformer()
throws TransformerConfigurationException;
/**
* Process the Source into a Templates object, which is a
* a compiled representation of the source. This Templates object
* may then be used concurrently across multiple threads. Creating
* a Templates object allows the TransformerFactory to do detailed
* performance optimization of transformation instructions, without
* penalizing runtime transformation.
*
* @param source An object that holds a URL, input stream, etc.
*
* @return A Templates object capable of being used for transformation
* purposes, never <code>null</code>.
*
* @throws TransformerConfigurationException When parsing to
* construct the Templates object fails.
*/
public abstract Templates newTemplates(Source source)
throws TransformerConfigurationException;
/**
* <p>Get the stylesheet specification(s) associated with the
* XML <code>Source</code> document via the
* <a href="http://www.w3.org/TR/xml-stylesheet/">
* xml-stylesheet processing instruction</a> that match the given criteria.
* Note that it is possible to return several stylesheets, in which case
* they are applied as if they were a list of imports or cascades in a
* single stylesheet.</p>
*
* @param source The XML source document.
* @param media The media attribute to be matched. May be null, in which
* case the prefered templates will be used (i.e. alternate = no).
* @param title The value of the title attribute to match. May be null.
* @param charset The value of the charset attribute to match. May be null.
*
* @return A <code>Source</code> <code>Object</code> suitable for passing
* to the <code>TransformerFactory</code>.
*
* @throws TransformerConfigurationException An <code>Exception</code>
* is thrown if an error occurings during parsing of the
* <code>source</code>.
*
* @see <a href="http://www.w3.org/TR/xml-stylesheet/">
* Associating Style Sheets with XML documents Version 1.0</a>
*/
public abstract Source getAssociatedStylesheet(
Source source,
String media,
String title,
String charset)
throws TransformerConfigurationException;
/**
* Set an object that is used by default during the transformation
* to resolve URIs used in document(), xsl:import, or xsl:include.
*
* @param resolver An object that implements the URIResolver interface,
* or null.
*/
public abstract void setURIResolver(URIResolver resolver);
/**
* Get the object that is used by default during the transformation
* to resolve URIs used in document(), xsl:import, or xsl:include.
*
* @return The URIResolver that was set with setURIResolver.
*/
public abstract URIResolver getURIResolver();
//======= CONFIGURATION METHODS =======
/**
* <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
* or <code>Template</code>s created by this factory.</p>
*
* <p>
* Feature names are fully qualified {@link java.net.URI}s.
* Implementations may define their own features.
* An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
* <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
* It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
* </p>
*
* <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
* When the feature is:</p>
* <ul>
* <li>
* <code>true</code>: the implementation will limit XML processing to conform to implementation limits
* and behave in a secure fashion as defined by the implementation.
* Examples include resolving user defined style sheets and functions.
* If XML processing is limited for security reasons, it will be reported via a call to the registered
* {@link ErrorListener#fatalError(TransformerException exception)}.
* See {@link #setErrorListener(ErrorListener listener)}.
* </li>
* <li>
* <code>false</code>: the implementation will processing XML according to the XML specifications without
* regard to possible implementation limits.
* </li>
* </ul>
*
* @param name Feature name.
* @param value Is feature state <code>true</code> or <code>false</code>.
*
* @throws TransformerConfigurationException if this <code>TransformerFactory</code>
* or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
* @throws NullPointerException If the <code>name</code> parameter is null.
*/
public abstract void setFeature(String name, boolean value)
throws TransformerConfigurationException;
/**
* Look up the value of a feature.
*
* <p>
* Feature names are fully qualified {@link java.net.URI}s.
* Implementations may define their own features.
* <code>false</code> is returned if this <code>TransformerFactory</code> or the
* <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
* It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
* </p>
*
* @param name Feature name.
*
* @return The current state of the feature, <code>true</code> or <code>false</code>.
*
* @throws NullPointerException If the <code>name</code> parameter is null.
*/
public abstract boolean getFeature(String name);
/**
* Allows the user to set specific attributes on the underlying
* implementation. An attribute in this context is defined to
* be an option that the implementation provides.
* An <code>IllegalArgumentException</code> is thrown if the underlying
* implementation doesn't recognize the attribute.
* <p>
* All implementations that implement JAXP 1.5 or newer are required to
* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and
* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} properties.
* </p>
* <ul>
* <li>
* <p>
* Access to external DTDs in the source file is restricted to the protocols
* specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.
* If access is denied during transformation due to the restriction of this property,
* {@link javax.xml.transform.TransformerException} will be thrown by
* {@link javax.xml.transform.Transformer#transform(Source, Result)}.
* </p>
* <p>
* Access to external DTDs in the stylesheet is restricted to the protocols
* specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.
* If access is denied during the creation of a new transformer due to the
* restriction of this property,
* {@link javax.xml.transform.TransformerConfigurationException} will be thrown
* by the {@link #newTransformer(Source)} method.
* </p>
* <p>
* Access to external reference set by the stylesheet processing instruction,
* Import and Include element is restricted to the protocols specified by the
* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} property.
* If access is denied during the creation of a new transformer due to the
* restriction of this property,
* {@link javax.xml.transform.TransformerConfigurationException} will be thrown
* by the {@link #newTransformer(Source)} method.
* </p>
* <p>
* Access to external document through XSLT document function is restricted
* to the protocols specified by the property. If access is denied during
* the transformation due to the restriction of this property,
* {@link javax.xml.transform.TransformerException} will be thrown by the
* {@link javax.xml.transform.Transformer#transform(Source, Result)} method.
* </p>
* </li>
* </ul>
*
* @param name The name of the attribute.
* @param value The value of the attribute.
*
* @throws IllegalArgumentException When implementation does not
* recognize the attribute.
*/
public abstract void setAttribute(String name, Object value);
/**
* Allows the user to retrieve specific attributes on the underlying
* implementation.
* An <code>IllegalArgumentException</code> is thrown if the underlying
* implementation doesn't recognize the attribute.
*
* @param name The name of the attribute.
*
* @return value The value of the attribute.
*
* @throws IllegalArgumentException When implementation does not
* recognize the attribute.
*/
public abstract Object getAttribute(String name);
/**
* Set the error event listener for the TransformerFactory, which
* is used for the processing of transformation instructions,
* and not for the transformation itself.
* An <code>IllegalArgumentException</code> is thrown if the
* <code>ErrorListener</code> listener is <code>null</code>.
*
* @param listener The new error listener.
*
* @throws IllegalArgumentException When <code>listener</code> is
* <code>null</code>
*/
public abstract void setErrorListener(ErrorListener listener);
/**
* Get the error event handler for the TransformerFactory.
*
* @return The current error handler, which should never be null.
*/
public abstract ErrorListener getErrorListener();
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* Thrown when a problem with configuration with the Transformer Factories
* exists. This error will typically be thrown when the class of a
* transformation factory specified in the system properties cannot be found
* or instantiated.
*/
public class TransformerFactoryConfigurationError extends Error {
private static final long serialVersionUID = -6527718720676281516L;
/**
* <code>Exception</code> for the
* <code>TransformerFactoryConfigurationError</code>.
*/
private Exception exception;
/**
* Create a new <code>TransformerFactoryConfigurationError</code> with no
* detail mesage.
*/
public TransformerFactoryConfigurationError() {
super();
this.exception = null;
}
/**
* Create a new <code>TransformerFactoryConfigurationError</code> with
* the <code>String</code> specified as an error message.
*
* @param msg The error message for the exception.
*/
public TransformerFactoryConfigurationError(String msg) {
super(msg);
this.exception = null;
}
/**
* Create a new <code>TransformerFactoryConfigurationError</code> with a
* given <code>Exception</code> base cause of the error.
*
* @param e The exception to be encapsulated in a
* TransformerFactoryConfigurationError.
*/
public TransformerFactoryConfigurationError(Exception e) {
super(e.toString());
this.exception = e;
}
/**
* Create a new <code>TransformerFactoryConfigurationError</code> with the
* given <code>Exception</code> base cause and detail message.
*
* @param e The exception to be encapsulated in a
* TransformerFactoryConfigurationError
* @param msg The detail message.
*/
public TransformerFactoryConfigurationError(Exception e, String msg) {
super(msg);
this.exception = e;
}
/**
* Return the message (if any) for this error . If there is no
* message for the exception and there is an encapsulated
* exception then the message of that exception will be returned.
*
* @return The error message.
*/
public String getMessage() {
String message = super.getMessage();
if ((message == null) && (exception != null)) {
return exception.getMessage();
}
return message;
}
/**
* Return the actual exception (if any) that caused this exception to
* be raised.
*
* @return The encapsulated exception, or null if there is none.
*/
public Exception getException() {
return exception;
}
/**
* use the exception chaining mechanism of JDK1.4
*/
@Override
public Throwable getCause() {
return exception;
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform;
/**
* <p>An object that implements this interface that can be called by the processor
* to turn a URI used in document(), xsl:import, or xsl:include into a Source object.
*/
public interface URIResolver {
/**
* Called by the processor when it encounters
* an xsl:include, xsl:import, or document() function.
*
* @param href An href attribute, which may be relative or absolute.
* @param base The base URI against which the first argument will be made
* absolute if the absolute URI is required.
*
* @return A Source object, or null if the href cannot be resolved,
* and the processor should try to resolve the URI itself.
*
* @throws TransformerException if an error occurs when trying to
* resolve the URI.
*/
public Source resolve(String href, String base)
throws TransformerException;
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.dom;
import javax.xml.transform.SourceLocator;
import org.w3c.dom.Node;
/**
* Indicates the position of a node in a source DOM, intended
* primarily for error reporting. To use a DOMLocator, the receiver of an
* error must downcast the {@link javax.xml.transform.SourceLocator}
* object returned by an exception. A {@link javax.xml.transform.Transformer}
* may use this object for purposes other than error reporting, for instance,
* to indicate the source node that originated a result node.
*/
public interface DOMLocator extends SourceLocator {
/**
* Return the node where the event occurred.
*
* @return The node that is the location for the event.
*/
public Node getOriginatingNode();
}

View File

@@ -0,0 +1,362 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.dom;
import javax.xml.transform.Result;
import org.w3c.dom.Node;
/**
* <p>Acts as a holder for a transformation result tree in the form of a Document Object Model (DOM) tree.</p>
*
* <p>If no output DOM source is set, the transformation will create a Document node as the holder for the result of the transformation,
* which may be retrieved with {@link #getNode()}.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public class DOMResult implements Result {
/** <p>If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns <code>true</code> when passed this value as an argument,
* the <code>Transformer</code> supports <code>Result</code> output of this type.</p>
*/
public static final String FEATURE = "http://javax.xml.transform.dom.DOMResult/feature";
/**
* <p>Zero-argument default constructor.</p>
*
* <p><code>node</code>,
* <code>siblingNode</code> and
* <code>systemId</code>
* will be set to <code>null</code>.</p>
*/
public DOMResult() {
setNode(null);
setNextSibling(null);
setSystemId(null);
}
/**
* <p>Use a DOM node to create a new output target.</p>
*
* <p>In practice, the node should be
* a {@link org.w3c.dom.Document} node,
* a {@link org.w3c.dom.DocumentFragment} node, or
* a {@link org.w3c.dom.Element} node.
* In other words, a node that accepts children.</p>
*
* <p><code>siblingNode</code> and
* <code>systemId</code>
* will be set to <code>null</code>.</p>
*
* @param node The DOM node that will contain the result tree.
*/
public DOMResult(Node node) {
setNode(node);
setNextSibling(null);
setSystemId(null);
}
/**
* <p>Use a DOM node to create a new output target with the specified System ID.<p>
*
* <p>In practice, the node should be
* a {@link org.w3c.dom.Document} node,
* a {@link org.w3c.dom.DocumentFragment} node, or
* a {@link org.w3c.dom.Element} node.
* In other words, a node that accepts children.</p>
*
* <p><code>siblingNode</code> will be set to <code>null</code>.</p>
*
* @param node The DOM node that will contain the result tree.
* @param systemId The system identifier which may be used in association with this node.
*/
public DOMResult(Node node, String systemId) {
setNode(node);
setNextSibling(null);
setSystemId(systemId);
}
/**
* <p>Use a DOM node to create a new output target specifying the child node where the result nodes should be inserted before.</p>
*
* <p>In practice, <code>node</code> and <code>nextSibling</code> should be
* a {@link org.w3c.dom.Document} node,
* a {@link org.w3c.dom.DocumentFragment} node, or
* a {@link org.w3c.dom.Element} node.
* In other words, a node that accepts children.</p>
*
* <p>Use <code>nextSibling</code> to specify the child node
* where the result nodes should be inserted before.
* If <code>nextSibling</code> is not a sibling of <code>node</code>,
* then an <code>IllegalArgumentException</code> is thrown.
* If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,
* then an <code>IllegalArgumentException</code> is thrown.
* If <code>nextSibling</code> is <code>null</code>,
* then the behavior is the same as calling {@link #DOMResult(Node node)},
* i.e. append the result nodes as the last child of the specified <code>node</code>.</p>
*
* <p><code>systemId</code> will be set to <code>null</code>.</p>
*
* @param node The DOM node that will contain the result tree.
* @param nextSibling The child node where the result nodes should be inserted before.
*
* @throws IllegalArgumentException If <code>nextSibling</code> is not a sibling of <code>node</code> or
* <code>node</code> is <code>null</code> and <code>nextSibling</code>
* is not <code>null</code>.
*
* @since 1.5
*/
public DOMResult(Node node, Node nextSibling) {
// does the corrent parent/child relationship exist?
if (nextSibling != null) {
// cannot be a sibling of a null node
if (node == null) {
throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
}
// nextSibling contained by node?
if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
}
}
setNode(node);
setNextSibling(nextSibling);
setSystemId(null);
}
/**
* <p>Use a DOM node to create a new output target specifying the child node where the result nodes should be inserted before and
* the specified System ID.</p>
*
* <p>In practice, <code>node</code> and <code>nextSibling</code> should be
* a {@link org.w3c.dom.Document} node,
* a {@link org.w3c.dom.DocumentFragment} node, or a
* {@link org.w3c.dom.Element} node.
* In other words, a node that accepts children.</p>
*
* <p>Use <code>nextSibling</code> to specify the child node
* where the result nodes should be inserted before.
* If <code>nextSibling</code> is not a sibling of <code>node</code>,
* then an <code>IllegalArgumentException</code> is thrown.
* If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,
* then an <code>IllegalArgumentException</code> is thrown.
* If <code>nextSibling</code> is <code>null</code>,
* then the behavior is the same as calling {@link #DOMResult(Node node, String systemId)},
* i.e. append the result nodes as the last child of the specified node and use the specified System ID.</p>
*
* @param node The DOM node that will contain the result tree.
* @param nextSibling The child node where the result nodes should be inserted before.
* @param systemId The system identifier which may be used in association with this node.
*
* @throws IllegalArgumentException If <code>nextSibling</code> is not a
* sibling of <code>node</code> or
* <code>node</code> is <code>null</code> and <code>nextSibling</code>
* is not <code>null</code>.
*
* @since 1.5
*/
public DOMResult(Node node, Node nextSibling, String systemId) {
// does the corrent parent/child relationship exist?
if (nextSibling != null) {
// cannot be a sibling of a null node
if (node == null) {
throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
}
// nextSibling contained by node?
if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
}
}
setNode(node);
setNextSibling(nextSibling);
setSystemId(systemId);
}
/**
* <p>Set the node that will contain the result DOM tree.<p>
*
* <p>In practice, the node should be
* a {@link org.w3c.dom.Document} node,
* a {@link org.w3c.dom.DocumentFragment} node, or
* a {@link org.w3c.dom.Element} node.
* In other words, a node that accepts children.</p>
*
* <p>An <code>IllegalStateException</code> is thrown if
* <code>nextSibling</code> is not <code>null</code> and
* <code>node</code> is not a parent of <code>nextSibling</code>.
* An <code>IllegalStateException</code> is thrown if <code>node</code> is <code>null</code> and
* <code>nextSibling</code> is not <code>null</code>.</p>
*
* @param node The node to which the transformation will be appended.
*
* @throws IllegalStateException If <code>nextSibling</code> is not
* <code>null</code> and
* <code>nextSibling</code> is not a child of <code>node</code> or
* <code>node</code> is <code>null</code> and
* <code>nextSibling</code> is not <code>null</code>.
*/
public void setNode(Node node) {
// does the corrent parent/child relationship exist?
if (nextSibling != null) {
// cannot be a sibling of a null node
if (node == null) {
throw new IllegalStateException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
}
// nextSibling contained by node?
if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
}
}
this.node = node;
}
/**
* <p>Get the node that will contain the result DOM tree.</p>
*
* <p>If no node was set via
* {@link #DOMResult(Node node)},
* {@link #DOMResult(Node node, String systeId)},
* {@link #DOMResult(Node node, Node nextSibling)},
* {@link #DOMResult(Node node, Node nextSibling, String systemId)} or
* {@link #setNode(Node node)},
* then the node will be set by the transformation, and may be obtained from this method once the transformation is complete.
* Calling this method before the transformation will return <code>null</code>.</p>
*
* @return The node to which the transformation will be appended.
*/
public Node getNode() {
return node;
}
/**
* <p>Set the child node before which the result nodes will be inserted.</p>
*
* <p>Use <code>nextSibling</code> to specify the child node
* before which the result nodes should be inserted.
* If <code>nextSibling</code> is not a descendant of <code>node</code>,
* then an <code>IllegalArgumentException</code> is thrown.
* If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,
* then an <code>IllegalStateException</code> is thrown.
* If <code>nextSibling</code> is <code>null</code>,
* then the behavior is the same as calling {@link #DOMResult(Node node)},
* i.e. append the result nodes as the last child of the specified <code>node</code>.</p>
*
* @param nextSibling The child node before which the result nodes will be inserted.
*
* @throws IllegalArgumentException If <code>nextSibling</code> is not a
* descendant of <code>node</code>.
* @throws IllegalStateException If <code>node</code> is <code>null</code>
* and <code>nextSibling</code> is not <code>null</code>.
*
* @since 1.5
*/
public void setNextSibling(Node nextSibling) {
// does the corrent parent/child relationship exist?
if (nextSibling != null) {
// cannot be a sibling of a null node
if (node == null) {
throw new IllegalStateException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
}
// nextSibling contained by node?
if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
}
}
this.nextSibling = nextSibling;
}
/**
* <p>Get the child node before which the result nodes will be inserted.</p>
*
* <p>If no node was set via
* {@link #DOMResult(Node node, Node nextSibling)},
* {@link #DOMResult(Node node, Node nextSibling, String systemId)} or
* {@link #setNextSibling(Node nextSibling)},
* then <code>null</code> will be returned.</p>
*
* @return The child node before which the result nodes will be inserted.
*
* @since 1.5
*/
public Node getNextSibling() {
return nextSibling;
}
/**
* <p>Set the systemId that may be used in association with the node.</p>
*
* @param systemId The system identifier as a URI string.
*/
public void setSystemId(String systemId) {
this.systemId = systemId;
}
/**
* <p>Get the System Identifier.</p>
*
* <p>If no System ID was set via
* {@link #DOMResult(Node node, String systemId)},
* {@link #DOMResult(Node node, Node nextSibling, String systemId)} or
* {@link #setSystemId(String systemId)},
* then <code>null</code> will be returned.</p>
*
* @return The system identifier.
*/
public String getSystemId() {
return systemId;
}
//////////////////////////////////////////////////////////////////////
// Internal state.
//////////////////////////////////////////////////////////////////////
/**
* <p>The node to which the transformation will be appended.</p>
*/
private Node node = null;
/**
* <p>The child node before which the result nodes will be inserted.</p>
*
* @since 1.5
*/
private Node nextSibling = null;
/**
* <p>The System ID that may be used in association with the node.</p>
*/
private String systemId = null;
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.dom;
import javax.xml.transform.Source;
import org.w3c.dom.Node;
/**
* <p>Acts as a holder for a transformation Source tree in the
* form of a Document Object Model (DOM) tree.</p>
*
* <p>Note that XSLT requires namespace support. Attempting to transform a DOM
* that was not contructed with a namespace-aware parser may result in errors.
* Parsers can be made namespace aware by calling
* {@link javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean awareness)}.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
* @see <a href="http://www.w3.org/TR/DOM-Level-2">Document Object Model (DOM) Level 2 Specification</a>
*/
public class DOMSource implements Source {
/**
* <p><code>Node</code> to serve as DOM source.</p>
*/
private Node node;
/**
* <p>The base ID (URL or system ID) from where URLs
* will be resolved.</p>
*/
private String systemID;
/** If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the Transformer supports Source input of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.dom.DOMSource/feature";
/**
* <p>Zero-argument default constructor. If this constructor is used, and
* no DOM source is set using {@link #setNode(Node node)} , then the
* <code>Transformer</code> will
* create an empty source {@link org.w3c.dom.Document} using
* {@link javax.xml.parsers.DocumentBuilder#newDocument()}.</p>
*
* @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
*/
public DOMSource() { }
/**
* Create a new input source with a DOM node. The operation
* will be applied to the subtree rooted at this node. In XSLT,
* a "/" pattern still means the root of the tree (not the subtree),
* and the evaluation of global variables and parameters is done
* from the root node also.
*
* @param n The DOM node that will contain the Source tree.
*/
public DOMSource(Node n) {
setNode(n);
}
/**
* Create a new input source with a DOM node, and with the
* system ID also passed in as the base URI.
*
* @param node The DOM node that will contain the Source tree.
* @param systemID Specifies the base URI associated with node.
*/
public DOMSource(Node node, String systemID) {
setNode(node);
setSystemId(systemID);
}
/**
* Set the node that will represents a Source DOM tree.
*
* @param node The node that is to be transformed.
*/
public void setNode(Node node) {
this.node = node;
}
/**
* Get the node that represents a Source DOM tree.
*
* @return The node that is to be transformed.
*/
public Node getNode() {
return node;
}
/**
* Set the base ID (URL or system ID) from where URLs
* will be resolved.
*
* @param systemID Base URL for this DOM tree.
*/
public void setSystemId(String systemID) {
this.systemID = systemID;
}
/**
* Get the base ID (URL or system ID) from where URLs
* will be resolved.
*
* @return Base URL for this DOM tree.
*/
public String getSystemId() {
return this.systemID;
}
}

View File

@@ -0,0 +1,144 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.sax;
import javax.xml.transform.Result;
import org.xml.sax.ContentHandler;
import org.xml.sax.ext.LexicalHandler;
/**
* <p>Acts as an holder for a transformation Result.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public class SAXResult implements Result {
/**
* If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the Transformer supports Result output of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.sax.SAXResult/feature";
/**
* Zero-argument default constructor.
*/
public SAXResult() {
}
/**
* Create a SAXResult that targets a SAX2 {@link org.xml.sax.ContentHandler}.
*
* @param handler Must be a non-null ContentHandler reference.
*/
public SAXResult(ContentHandler handler) {
setHandler(handler);
}
/**
* Set the target to be a SAX2 {@link org.xml.sax.ContentHandler}.
*
* @param handler Must be a non-null ContentHandler reference.
*/
public void setHandler(ContentHandler handler) {
this.handler = handler;
}
/**
* Get the {@link org.xml.sax.ContentHandler} that is the Result.
*
* @return The ContentHandler that is to be transformation output.
*/
public ContentHandler getHandler() {
return handler;
}
/**
* Set the SAX2 {@link org.xml.sax.ext.LexicalHandler} for the output.
*
* <p>This is needed to handle XML comments and the like. If the
* lexical handler is not set, an attempt should be made by the
* transformer to cast the {@link org.xml.sax.ContentHandler} to a
* <code>LexicalHandler</code>.</p>
*
* @param handler A non-null <code>LexicalHandler</code> for
* handling lexical parse events.
*/
public void setLexicalHandler(LexicalHandler handler) {
this.lexhandler = handler;
}
/**
* Get a SAX2 {@link org.xml.sax.ext.LexicalHandler} for the output.
*
* @return A <code>LexicalHandler</code>, or null.
*/
public LexicalHandler getLexicalHandler() {
return lexhandler;
}
/**
* Method setSystemId Set the systemID that may be used in association
* with the {@link org.xml.sax.ContentHandler}.
*
* @param systemId The system identifier as a URI string.
*/
public void setSystemId(String systemId) {
this.systemId = systemId;
}
/**
* Get the system identifier that was set with setSystemId.
*
* @return The system identifier that was set with setSystemId, or null
* if setSystemId was not called.
*/
public String getSystemId() {
return systemId;
}
//////////////////////////////////////////////////////////////////////
// Internal state.
//////////////////////////////////////////////////////////////////////
/**
* The handler for parse events.
*/
private ContentHandler handler;
/**
* The handler for lexical events.
*/
private LexicalHandler lexhandler;
/**
* The systemID that may be used in association
* with the node.
*/
private String systemId;
}

View File

@@ -0,0 +1,209 @@
/*
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.sax;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
/**
* <p>Acts as an holder for SAX-style Source.</p>
*
* <p>Note that XSLT requires namespace support. Attempting to transform an
* input source that is not
* generated with a namespace-aware parser may result in errors.
* Parsers can be made namespace aware by calling the
* {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
*
* @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public class SAXSource implements Source {
/**
* If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the Transformer supports Source input of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.sax.SAXSource/feature";
/**
* <p>Zero-argument default constructor. If this constructor is used, and
* no SAX source is set using
* {@link #setInputSource(InputSource inputSource)} , then the
* <code>Transformer</code> will
* create an empty source {@link org.xml.sax.InputSource} using
* {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
*
* @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
*/
public SAXSource() { }
/**
* Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
* and a SAX InputSource. The {@link javax.xml.transform.Transformer}
* or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
* to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
* reader.parse(inputSource).
*
* @param reader An XMLReader to be used for the parse.
* @param inputSource A SAX input source reference that must be non-null
* and that will be passed to the reader parse method.
*/
public SAXSource(XMLReader reader, InputSource inputSource) {
this.reader = reader;
this.inputSource = inputSource;
}
/**
* Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
* The {@link javax.xml.transform.Transformer} or
* {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
* reader via {@link org.xml.sax.helpers.XMLReaderFactory}
* (if setXMLReader is not used), sets itself as
* the reader's {@link org.xml.sax.ContentHandler}, and calls
* reader.parse(inputSource).
*
* @param inputSource An input source reference that must be non-null
* and that will be passed to the parse method of the reader.
*/
public SAXSource(InputSource inputSource) {
this.inputSource = inputSource;
}
/**
* Set the XMLReader to be used for the Source.
*
* @param reader A valid XMLReader or XMLFilter reference.
*/
public void setXMLReader(XMLReader reader) {
this.reader = reader;
}
/**
* Get the XMLReader to be used for the Source.
*
* @return A valid XMLReader or XMLFilter reference, or null.
*/
public XMLReader getXMLReader() {
return reader;
}
/**
* Set the SAX InputSource to be used for the Source.
*
* @param inputSource A valid InputSource reference.
*/
public void setInputSource(InputSource inputSource) {
this.inputSource = inputSource;
}
/**
* Get the SAX InputSource to be used for the Source.
*
* @return A valid InputSource reference, or null.
*/
public InputSource getInputSource() {
return inputSource;
}
/**
* Set the system identifier for this Source. If an input source
* has already been set, it will set the system ID or that
* input source, otherwise it will create a new input source.
*
* <p>The system identifier is optional if there is a byte stream
* or a character stream, but it is still useful to provide one,
* since the application can use it to resolve relative URIs
* and can include it in error messages and warnings (the parser
* will attempt to open a connection to the URI only if
* no byte stream or character stream is specified).</p>
*
* @param systemId The system identifier as a URI string.
*/
public void setSystemId(String systemId) {
if (null == inputSource) {
inputSource = new InputSource(systemId);
} else {
inputSource.setSystemId(systemId);
}
}
/**
* <p>Get the base ID (URI or system ID) from where URIs
* will be resolved.</p>
*
* @return Base URL for the <code>Source</code>, or <code>null</code>.
*/
public String getSystemId() {
if (inputSource == null) {
return null;
} else {
return inputSource.getSystemId();
}
}
/**
* The XMLReader to be used for the source tree input. May be null.
*/
private XMLReader reader;
/**
* <p>The SAX InputSource to be used for the source tree input.
* Should not be <code>null</code>.</p>
*/
private InputSource inputSource;
/**
* Attempt to obtain a SAX InputSource object from a Source
* object.
*
* @param source Must be a non-null Source reference.
*
* @return An InputSource, or null if Source can not be converted.
*/
public static InputSource sourceToInputSource(Source source) {
if (source instanceof SAXSource) {
return ((SAXSource) source).getInputSource();
} else if (source instanceof StreamSource) {
StreamSource ss = (StreamSource) source;
InputSource isource = new InputSource(ss.getSystemId());
isource.setByteStream(ss.getInputStream());
isource.setCharacterStream(ss.getReader());
isource.setPublicId(ss.getPublicId());
return isource;
} else {
return null;
}
}
}

View File

@@ -0,0 +1,150 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.sax;
import javax.xml.transform.*;
import org.xml.sax.XMLFilter;
/**
* This class extends TransformerFactory to provide SAX-specific
* factory methods. It provides two types of ContentHandlers,
* one for creating Transformers, the other for creating Templates
* objects.
*
* <p>If an application wants to set the ErrorHandler or EntityResolver
* for an XMLReader used during a transformation, it should use a URIResolver
* to return the SAXSource which provides (with getXMLReader) a reference to
* the XMLReader.</p>
*/
public abstract class SAXTransformerFactory extends TransformerFactory {
/** If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the TransformerFactory returned from
* {@link javax.xml.transform.TransformerFactory#newInstance} may
* be safely cast to a SAXTransformerFactory.
*/
public static final String FEATURE =
"http://javax.xml.transform.sax.SAXTransformerFactory/feature";
/** If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the {@link #newXMLFilter(Source src)}
* and {@link #newXMLFilter(Templates templates)} methods are supported.
*/
public static final String FEATURE_XMLFILTER =
"http://javax.xml.transform.sax.SAXTransformerFactory/feature/xmlfilter";
/**
* The default constructor is protected on purpose.
*/
protected SAXTransformerFactory() {}
/**
* Get a TransformerHandler object that can process SAX
* ContentHandler events into a Result, based on the transformation
* instructions specified by the argument.
*
* @param src The Source of the transformation instructions.
*
* @return TransformerHandler ready to transform SAX events.
*
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/
public abstract TransformerHandler newTransformerHandler(Source src)
throws TransformerConfigurationException;
/**
* Get a TransformerHandler object that can process SAX
* ContentHandler events into a Result, based on the Templates argument.
*
* @param templates The compiled transformation instructions.
*
* @return TransformerHandler ready to transform SAX events.
*
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/
public abstract TransformerHandler newTransformerHandler(
Templates templates) throws TransformerConfigurationException;
/**
* Get a TransformerHandler object that can process SAX
* ContentHandler events into a Result. The transformation
* is defined as an identity (or copy) transformation, for example
* to copy a series of SAX parse events into a DOM tree.
*
* @return A non-null reference to a TransformerHandler, that may
* be used as a ContentHandler for SAX parse events.
*
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler cannot be created.
*/
public abstract TransformerHandler newTransformerHandler()
throws TransformerConfigurationException;
/**
* Get a TemplatesHandler object that can process SAX
* ContentHandler events into a Templates object.
*
* @return A non-null reference to a TransformerHandler, that may
* be used as a ContentHandler for SAX parse events.
*
* @throws TransformerConfigurationException If for some reason the
* TemplatesHandler cannot be created.
*/
public abstract TemplatesHandler newTemplatesHandler()
throws TransformerConfigurationException;
/**
* Create an XMLFilter that uses the given Source as the
* transformation instructions.
*
* @param src The Source of the transformation instructions.
*
* @return An XMLFilter object, or null if this feature is not supported.
*
* @throws TransformerConfigurationException If for some reason the
* TemplatesHandler cannot be created.
*/
public abstract XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException;
/**
* Create an XMLFilter, based on the Templates argument..
*
* @param templates The compiled transformation instructions.
*
* @return An XMLFilter object, or null if this feature is not supported.
*
* @throws TransformerConfigurationException If for some reason the
* TemplatesHandler cannot be created.
*/
public abstract XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException;
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.sax;
import javax.xml.transform.*;
import org.xml.sax.ContentHandler;
/**
* A SAX ContentHandler that may be used to process SAX
* parse events (parsing transformation instructions) into a Templates object.
*
* <p>Note that TemplatesHandler does not need to implement LexicalHandler.</p>
*/
public interface TemplatesHandler extends ContentHandler {
/**
* When a TemplatesHandler object is used as a ContentHandler
* for the parsing of transformation instructions, it creates a Templates object,
* which the caller can get once the SAX events have been completed.
*
* @return The Templates object that was created during
* the SAX event process, or null if no Templates object has
* been created.
*
*/
public Templates getTemplates();
/**
* Set the base ID (URI or system ID) for the Templates object
* created by this builder. This must be set in order to
* resolve relative URIs in the stylesheet. This must be
* called before the startDocument event.
*
* @param systemID Base URI for this stylesheet.
*/
public void setSystemId(String systemID);
/**
* Get the base ID (URI or system ID) from where relative
* URLs will be resolved.
* @return The systemID that was set with {@link #setSystemId}.
*/
public String getSystemId();
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.sax;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.ext.LexicalHandler;
/**
* A TransformerHandler
* listens for SAX ContentHandler parse events and transforms
* them to a Result.
*/
public interface TransformerHandler
extends ContentHandler, LexicalHandler, DTDHandler {
/**
* <p>Set the <code>Result</code> associated with this
* <code>TransformerHandler</code> to be used for the transformation.</p>
*
* @param result A <code>Result</code> instance, should not be
* <code>null</code>.
*
* @throws IllegalArgumentException if result is invalid for some reason.
*/
public void setResult(Result result) throws IllegalArgumentException;
/**
* Set the base ID (URI or system ID) from where relative
* URLs will be resolved.
* @param systemID Base URI for the source tree.
*/
public void setSystemId(String systemID);
/**
* Get the base ID (URI or system ID) from where relative
* URLs will be resolved.
* @return The systemID that was set with {@link #setSystemId}.
*/
public String getSystemId();
/**
* <p>Get the <code>Transformer</code> associated with this handler, which
* is needed in order to set parameters and output properties.</p>
*
* @return <code>Transformer</code> associated with this
* <code>TransformerHandler</code>.
*/
public Transformer getTransformer();
}

View File

@@ -0,0 +1,183 @@
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.stax;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
/**
* <p>Acts as a holder for an XML {@link Result} in the
* form of a StAX writer,i.e.
* {@link XMLStreamWriter} or {@link XMLEventWriter}.
* <code>StAXResult</code> can be used in all cases that accept
* a <code>Result</code>, e.g. {@link javax.xml.transform.Transformer},
* {@link javax.xml.validation.Validator} which accept
* <code>Result</code> as input.
*
* @author <a href="mailto:Neeraj.Bajaj@Sun.com">Neeraj Bajaj</a>
* @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
*
* @see <a href="http://jcp.org/en/jsr/detail?id=173">
* JSR 173: Streaming API for XML</a>
* @see XMLStreamWriter
* @see XMLEventWriter
*
* @since 1.6
*/
public class StAXResult implements Result {
/** If {@link javax.xml.transform.TransformerFactory#getFeature(String name)}
* returns true when passed this value as an argument,
* the Transformer supports Result output of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.stax.StAXResult/feature";
/**
* <p><code>XMLEventWriter</code> to be used for
* <code>Result</code> output.</p>
*/
private XMLEventWriter xmlEventWriter = null;
/**
* <p><code>XMLStreamWriter</code> to be used for
* <code>Result</code> output.</p>
*/
private XMLStreamWriter xmlStreamWriter = null;
/** <p>System identifier for this <code>StAXResult</code>.<p> */
private String systemId = null;
/**
* <p>Creates a new instance of a <code>StAXResult</code>
* by supplying an {@link XMLEventWriter}.</p>
*
* <p><code>XMLEventWriter</code> must be a
* non-<code>null</code> reference.</p>
*
* @param xmlEventWriter <code>XMLEventWriter</code> used to create
* this <code>StAXResult</code>.
*
* @throws IllegalArgumentException If <code>xmlEventWriter</code> ==
* <code>null</code>.
*/
public StAXResult(final XMLEventWriter xmlEventWriter) {
if (xmlEventWriter == null) {
throw new IllegalArgumentException(
"StAXResult(XMLEventWriter) with XMLEventWriter == null");
}
this.xmlEventWriter = xmlEventWriter;
}
/**
* <p>Creates a new instance of a <code>StAXResult</code>
* by supplying an {@link XMLStreamWriter}.</p>
*
* <p><code>XMLStreamWriter</code> must be a
* non-<code>null</code> reference.</p>
*
* @param xmlStreamWriter <code>XMLStreamWriter</code> used to create
* this <code>StAXResult</code>.
*
* @throws IllegalArgumentException If <code>xmlStreamWriter</code> ==
* <code>null</code>.
*/
public StAXResult(final XMLStreamWriter xmlStreamWriter) {
if (xmlStreamWriter == null) {
throw new IllegalArgumentException(
"StAXResult(XMLStreamWriter) with XMLStreamWriter == null");
}
this.xmlStreamWriter = xmlStreamWriter;
}
/**
* <p>Get the <code>XMLEventWriter</code> used by this
* <code>StAXResult</code>.</p>
*
* <p><code>XMLEventWriter</code> will be <code>null</code>
* if this <code>StAXResult</code> was created with a
* <code>XMLStreamWriter</code>.</p>
*
* @return <code>XMLEventWriter</code> used by this
* <code>StAXResult</code>.
*/
public XMLEventWriter getXMLEventWriter() {
return xmlEventWriter;
}
/**
* <p>Get the <code>XMLStreamWriter</code> used by this
* <code>StAXResult</code>.</p>
*
* <p><code>XMLStreamWriter</code> will be <code>null</code>
* if this <code>StAXResult</code> was created with a
* <code>XMLEventWriter</code>.</p>
*
* @return <code>XMLStreamWriter</code> used by this
* <code>StAXResult</code>.
*/
public XMLStreamWriter getXMLStreamWriter() {
return xmlStreamWriter;
}
/**
* <p>In the context of a <code>StAXResult</code>, it is not appropriate
* to explicitly set the system identifier.
* The <code>XMLEventWriter</code> or <code>XMLStreamWriter</code>
* used to construct this <code>StAXResult</code> determines the
* system identifier of the XML result.</p>
*
* <p>An {@link UnsupportedOperationException} is <strong>always</strong>
* thrown by this method.</p>
*
* @param systemId Ignored.
*
* @throws UnsupportedOperationException Is <strong>always</strong>
* thrown by this method.
*/
public void setSystemId(final String systemId) {
throw new UnsupportedOperationException(
"StAXResult#setSystemId(systemId) cannot set the "
+ "system identifier for a StAXResult");
}
/**
* <p>The returned system identifier is always <code>null</code>.</p>
*
* @return The returned system identifier is always <code>null</code>.
*/
public String getSystemId() {
return null;
}
}

View File

@@ -0,0 +1,236 @@
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.stax;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.Source;
/**
* <p>Acts as a holder for an XML {@link Source} in the
* form of a StAX reader,i.e.
* {@link XMLStreamReader} or {@link XMLEventReader}.
* <code>StAXSource</code> can be used in all cases that accept
* a <code>Source</code>, e.g. {@link javax.xml.transform.Transformer},
* {@link javax.xml.validation.Validator} which accept
* <code>Source</code> as input.
*
* <p><code>StAXSource</code>s are consumed during processing
* and are not reusable.</p>
*
* @author <a href="mailto:Neeraj.Bajaj@Sun.com">Neeraj Bajaj</a>
* @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
*
* @see <a href="http://jcp.org/en/jsr/detail?id=173">
* JSR 173: Streaming API for XML</a>
* @see XMLStreamReader
* @see XMLEventReader
*
* @since 1.6
*/
public class StAXSource implements Source {
/** If {@link javax.xml.transform.TransformerFactory#getFeature(String name)}
* returns true when passed this value as an argument,
* the Transformer supports Source input of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.stax.StAXSource/feature";
/** <p><code>XMLEventReader</code> to be used for source input.</p> */
private XMLEventReader xmlEventReader = null;
/** <p><code>XMLStreamReader</code> to be used for source input.</p> */
private XMLStreamReader xmlStreamReader = null;
/** <p>System identifier of source input.</p> */
private String systemId = null;
/**
* <p>Creates a new instance of a <code>StAXSource</code>
* by supplying an {@link XMLEventReader}.</p>
*
* <p><code>XMLEventReader</code> must be a
* non-<code>null</code> reference.</p>
*
* <p><code>XMLEventReader</code> must be in
* {@link XMLStreamConstants#START_DOCUMENT} or
* {@link XMLStreamConstants#START_ELEMENT} state.</p>
*
* @param xmlEventReader <code>XMLEventReader</code> used to create
* this <code>StAXSource</code>.
*
* @throws XMLStreamException If <code>xmlEventReader</code> access
* throws an <code>Exception</code>.
* @throws IllegalArgumentException If <code>xmlEventReader</code> ==
* <code>null</code>.
* @throws IllegalStateException If <code>xmlEventReader</code>
* is not in <code>XMLStreamConstants.START_DOCUMENT</code> or
* <code>XMLStreamConstants.START_ELEMENT</code> state.
*/
public StAXSource(final XMLEventReader xmlEventReader)
throws XMLStreamException {
if (xmlEventReader == null) {
throw new IllegalArgumentException(
"StAXSource(XMLEventReader) with XMLEventReader == null");
}
// TODO: This is ugly ...
// there is no way to know the current position(event) of
// XMLEventReader. peek() is the only way to know the next event.
// The next event on the input stream should be
// XMLStreamConstants.START_DOCUMENT or
// XMLStreamConstants.START_ELEMENT.
XMLEvent event = xmlEventReader.peek();
int eventType = event.getEventType();
if (eventType != XMLStreamConstants.START_DOCUMENT
&& eventType != XMLStreamConstants.START_ELEMENT) {
throw new IllegalStateException(
"StAXSource(XMLEventReader) with XMLEventReader "
+ "not in XMLStreamConstants.START_DOCUMENT or "
+ "XMLStreamConstants.START_ELEMENT state");
}
this.xmlEventReader = xmlEventReader;
systemId = event.getLocation().getSystemId();
}
/**
* <p>Creates a new instance of a <code>StAXSource</code>
* by supplying an {@link XMLStreamReader}.</p>
*
* <p><code>XMLStreamReader</code> must be a
* non-<code>null</code> reference.</p>
*
* <p><code>XMLStreamReader</code> must be in
* {@link XMLStreamConstants#START_DOCUMENT} or
* {@link XMLStreamConstants#START_ELEMENT} state.</p>
*
* @param xmlStreamReader <code>XMLStreamReader</code> used to create
* this <code>StAXSource</code>.
*
* @throws IllegalArgumentException If <code>xmlStreamReader</code> ==
* <code>null</code>.
* @throws IllegalStateException If <code>xmlStreamReader</code>
* is not in <code>XMLStreamConstants.START_DOCUMENT</code> or
* <code>XMLStreamConstants.START_ELEMENT</code> state.
*/
public StAXSource(final XMLStreamReader xmlStreamReader) {
if (xmlStreamReader == null) {
throw new IllegalArgumentException(
"StAXSource(XMLStreamReader) with XMLStreamReader == null");
}
int eventType = xmlStreamReader.getEventType();
if (eventType != XMLStreamConstants.START_DOCUMENT
&& eventType != XMLStreamConstants.START_ELEMENT) {
throw new IllegalStateException(
"StAXSource(XMLStreamReader) with XMLStreamReader"
+ "not in XMLStreamConstants.START_DOCUMENT or "
+ "XMLStreamConstants.START_ELEMENT state");
}
this.xmlStreamReader = xmlStreamReader;
systemId = xmlStreamReader.getLocation().getSystemId();
}
/**
* <p>Get the <code>XMLEventReader</code> used by this
* <code>StAXSource</code>.</p>
*
* <p><code>XMLEventReader</code> will be <code>null</code>.
* if this <code>StAXSource</code> was created with a
* <code>XMLStreamReader</code>.</p>
*
* @return <code>XMLEventReader</code> used by this
* <code>StAXSource</code>.
*/
public XMLEventReader getXMLEventReader() {
return xmlEventReader;
}
/**
* <p>Get the <code>XMLStreamReader</code> used by this
* <code>StAXSource</code>.</p>
*
* <p><code>XMLStreamReader</code> will be <code>null</code>
* if this <code>StAXSource</code> was created with a
* <code>XMLEventReader</code>.</p>
*
* @return <code>XMLStreamReader</code> used by this
* <code>StAXSource</code>.
*/
public XMLStreamReader getXMLStreamReader() {
return xmlStreamReader;
}
/**
* <p>In the context of a <code>StAXSource</code>, it is not appropriate
* to explicitly set the system identifier.
* The <code>XMLStreamReader</code> or <code>XMLEventReader</code>
* used to construct this <code>StAXSource</code> determines the
* system identifier of the XML source.</p>
*
* <p>An {@link UnsupportedOperationException} is <strong>always</strong>
* thrown by this method.</p>
*
* @param systemId Ignored.
*
* @throws UnsupportedOperationException Is <strong>always</strong>
* thrown by this method.
*/
public void setSystemId(final String systemId) {
throw new UnsupportedOperationException(
"StAXSource#setSystemId(systemId) cannot set the "
+ "system identifier for a StAXSource");
}
/**
* <p>Get the system identifier used by this
* <code>StAXSource</code>.</p>
*
* <p>The <code>XMLStreamReader</code> or <code>XMLEventReader</code>
* used to construct this <code>StAXSource</code> is queried to determine
* the system identifier of the XML source.</p>
*
* <p>The system identifier may be <code>null</code> or
* an empty <code>""</code> <code>String</code>.</p>
*
* @return System identifier used by this <code>StAXSource</code>.
*/
public String getSystemId() {
return systemId;
}
}

View File

@@ -0,0 +1,203 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.stream;
import javax.xml.transform.Result;
import java.io.File;
import java.io.OutputStream;
import java.io.Writer;
import java.net.MalformedURLException;
/**
* <p>Acts as an holder for a transformation result,
* which may be XML, plain Text, HTML, or some other form of markup.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public class StreamResult implements Result {
/** If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the Transformer supports Result output of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.stream.StreamResult/feature";
/**
* Zero-argument default constructor.
*/
public StreamResult() {
}
/**
* Construct a StreamResult from a byte stream. Normally,
* a stream should be used rather than a reader, so that
* the transformer may use instructions contained in the
* transformation instructions to control the encoding.
*
* @param outputStream A valid OutputStream reference.
*/
public StreamResult(OutputStream outputStream) {
setOutputStream(outputStream);
}
/**
* Construct a StreamResult from a character stream. Normally,
* a stream should be used rather than a reader, so that
* the transformer may use instructions contained in the
* transformation instructions to control the encoding. However,
* there are times when it is useful to write to a character
* stream, such as when using a StringWriter.
*
* @param writer A valid Writer reference.
*/
public StreamResult(Writer writer) {
setWriter(writer);
}
/**
* Construct a StreamResult from a URL.
*
* @param systemId Must be a String that conforms to the URI syntax.
*/
public StreamResult(String systemId) {
this.systemId = systemId;
}
/**
* Construct a StreamResult from a File.
*
* @param f Must a non-null File reference.
*/
public StreamResult(File f) {
//convert file to appropriate URI, f.toURI().toASCIIString()
//converts the URI to string as per rule specified in
//RFC 2396,
setSystemId(f.toURI().toASCIIString());
}
/**
* Set the ByteStream that is to be written to. Normally,
* a stream should be used rather than a reader, so that
* the transformer may use instructions contained in the
* transformation instructions to control the encoding.
*
* @param outputStream A valid OutputStream reference.
*/
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
/**
* Get the byte stream that was set with setOutputStream.
*
* @return The byte stream that was set with setOutputStream, or null
* if setOutputStream or the ByteStream constructor was not called.
*/
public OutputStream getOutputStream() {
return outputStream;
}
/**
* Set the writer that is to receive the result. Normally,
* a stream should be used rather than a writer, so that
* the transformer may use instructions contained in the
* transformation instructions to control the encoding. However,
* there are times when it is useful to write to a writer,
* such as when using a StringWriter.
*
* @param writer A valid Writer reference.
*/
public void setWriter(Writer writer) {
this.writer = writer;
}
/**
* Get the character stream that was set with setWriter.
*
* @return The character stream that was set with setWriter, or null
* if setWriter or the Writer constructor was not called.
*/
public Writer getWriter() {
return writer;
}
/**
* Set the systemID that may be used in association
* with the byte or character stream, or, if neither is set, use
* this value as a writeable URI (probably a file name).
*
* @param systemId The system identifier as a URI string.
*/
public void setSystemId(String systemId) {
this.systemId = systemId;
}
/**
* <p>Set the system ID from a <code>File</code> reference.</p>
*
*
* @param f Must a non-null File reference.
*/
public void setSystemId(File f) {
//convert file to appropriate URI, f.toURI().toASCIIString()
//converts the URI to string as per rule specified in
//RFC 2396,
this.systemId = f.toURI().toASCIIString();
}
/**
* Get the system identifier that was set with setSystemId.
*
* @return The system identifier that was set with setSystemId, or null
* if setSystemId was not called.
*/
public String getSystemId() {
return systemId;
}
//////////////////////////////////////////////////////////////////////
// Internal state.
//////////////////////////////////////////////////////////////////////
/**
* The systemID that may be used in association
* with the byte or character stream, or, if neither is set, use
* this value as a writeable URI (probably a file name).
*/
private String systemId;
/**
* The byte stream that is to be written to.
*/
private OutputStream outputStream;
/**
* The character stream that is to be written to.
*/
private Writer writer;
}

View File

@@ -0,0 +1,284 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.xml.transform.stream;
import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import javax.xml.transform.Source;
/**
* <p>Acts as an holder for a transformation Source in the form
* of a stream of XML markup.</p>
*
* <p><em>Note:</em> Due to their internal use of either a {@link Reader} or {@link InputStream} instance,
* <code>StreamSource</code> instances may only be used once.</p>
*
* @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
*/
public class StreamSource implements Source {
/** If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns true when passed this value as an argument,
* the Transformer supports Source input of this type.
*/
public static final String FEATURE =
"http://javax.xml.transform.stream.StreamSource/feature";
/**
* <p>Zero-argument default constructor. If this constructor is used, and
* no Stream source is set using
* {@link #setInputStream(java.io.InputStream inputStream)} or
* {@link #setReader(java.io.Reader reader)}, then the
* <code>Transformer</code> will
* create an empty source {@link java.io.InputStream} using
* {@link java.io.InputStream#InputStream() new InputStream()}.</p>
*
* @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
*/
public StreamSource() { }
/**
* Construct a StreamSource from a byte stream. Normally,
* a stream should be used rather than a reader, so
* the XML parser can resolve character encoding specified
* by the XML declaration.
*
* <p>If this constructor is used to process a stylesheet, normally
* setSystemId should also be called, so that relative URI references
* can be resolved.</p>
*
* @param inputStream A valid InputStream reference to an XML stream.
*/
public StreamSource(InputStream inputStream) {
setInputStream(inputStream);
}
/**
* Construct a StreamSource from a byte stream. Normally,
* a stream should be used rather than a reader, so that
* the XML parser can resolve character encoding specified
* by the XML declaration.
*
* <p>This constructor allows the systemID to be set in addition
* to the input stream, which allows relative URIs
* to be processed.</p>
*
* @param inputStream A valid InputStream reference to an XML stream.
* @param systemId Must be a String that conforms to the URI syntax.
*/
public StreamSource(InputStream inputStream, String systemId) {
setInputStream(inputStream);
setSystemId(systemId);
}
/**
* Construct a StreamSource from a character reader. Normally,
* a stream should be used rather than a reader, so that
* the XML parser can resolve character encoding specified
* by the XML declaration. However, in many cases the encoding
* of the input stream is already resolved, as in the case of
* reading XML from a StringReader.
*
* @param reader A valid Reader reference to an XML character stream.
*/
public StreamSource(Reader reader) {
setReader(reader);
}
/**
* Construct a StreamSource from a character reader. Normally,
* a stream should be used rather than a reader, so that
* the XML parser may resolve character encoding specified
* by the XML declaration. However, in many cases the encoding
* of the input stream is already resolved, as in the case of
* reading XML from a StringReader.
*
* @param reader A valid Reader reference to an XML character stream.
* @param systemId Must be a String that conforms to the URI syntax.
*/
public StreamSource(Reader reader, String systemId) {
setReader(reader);
setSystemId(systemId);
}
/**
* Construct a StreamSource from a URL.
*
* @param systemId Must be a String that conforms to the URI syntax.
*/
public StreamSource(String systemId) {
this.systemId = systemId;
}
/**
* Construct a StreamSource from a File.
*
* @param f Must a non-null File reference.
*/
public StreamSource(File f) {
//convert file to appropriate URI, f.toURI().toASCIIString()
//converts the URI to string as per rule specified in
//RFC 2396,
setSystemId(f.toURI().toASCIIString());
}
/**
* Set the byte stream to be used as input. Normally,
* a stream should be used rather than a reader, so that
* the XML parser can resolve character encoding specified
* by the XML declaration.
*
* <p>If this Source object is used to process a stylesheet, normally
* setSystemId should also be called, so that relative URL references
* can be resolved.</p>
*
* @param inputStream A valid InputStream reference to an XML stream.
*/
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
/**
* Get the byte stream that was set with setByteStream.
*
* @return The byte stream that was set with setByteStream, or null
* if setByteStream or the ByteStream constructor was not called.
*/
public InputStream getInputStream() {
return inputStream;
}
/**
* Set the input to be a character reader. Normally,
* a stream should be used rather than a reader, so that
* the XML parser can resolve character encoding specified
* by the XML declaration. However, in many cases the encoding
* of the input stream is already resolved, as in the case of
* reading XML from a StringReader.
*
* @param reader A valid Reader reference to an XML CharacterStream.
*/
public void setReader(Reader reader) {
this.reader = reader;
}
/**
* Get the character stream that was set with setReader.
*
* @return The character stream that was set with setReader, or null
* if setReader or the Reader constructor was not called.
*/
public Reader getReader() {
return reader;
}
/**
* Set the public identifier for this Source.
*
* <p>The public identifier is always optional: if the application
* writer includes one, it will be provided as part of the
* location information.</p>
*
* @param publicId The public identifier as a string.
*/
public void setPublicId(String publicId) {
this.publicId = publicId;
}
/**
* Get the public identifier that was set with setPublicId.
*
* @return The public identifier that was set with setPublicId, or null
* if setPublicId was not called.
*/
public String getPublicId() {
return publicId;
}
/**
* Set the system identifier for this Source.
*
* <p>The system identifier is optional if there is a byte stream
* or a character stream, but it is still useful to provide one,
* since the application can use it to resolve relative URIs
* and can include it in error messages and warnings (the parser
* will attempt to open a connection to the URI only if
* there is no byte stream or character stream specified).</p>
*
* @param systemId The system identifier as a URL string.
*/
public void setSystemId(String systemId) {
this.systemId = systemId;
}
/**
* Get the system identifier that was set with setSystemId.
*
* @return The system identifier that was set with setSystemId, or null
* if setSystemId was not called.
*/
public String getSystemId() {
return systemId;
}
/**
* Set the system ID from a File reference.
*
* @param f Must a non-null File reference.
*/
public void setSystemId(File f) {
//convert file to appropriate URI, f.toURI().toASCIIString()
//converts the URI to string as per rule specified in
//RFC 2396,
this.systemId = f.toURI().toASCIIString();
}
//////////////////////////////////////////////////////////////////////
// Internal state.
//////////////////////////////////////////////////////////////////////
/**
* The public identifier for this input source, or null.
*/
private String publicId;
/**
* The system identifier as a URL string, or null.
*/
private String systemId;
/**
* The byte stream for this Source, or null.
*/
private InputStream inputStream;
/**
* The character stream for this Source, or null.
*/
private Reader reader;
}