📄 jdomsource.java
字号:
/*--
$Id: JDOMSource.java,v 1.20 2007/11/10 05:29:02 jhunter Exp $
Copyright (C) 2001-2007 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom.transform;
import java.io.*;
import java.util.*;
import javax.xml.transform.sax.*;
import org.jdom.*;
import org.jdom.output.*;
import org.xml.sax.*;
/**
* A holder for an XML Transformation source: a Document, Element, or list of
* nodes.
* <p>
* The is provides input to a
* {@link javax.xml.transform.Transformer JAXP TrAX Transformer}.
* <p>
* The following example shows how to apply an XSL Transformation
* to a JDOM document and get the transformation result in the form
* of a list of JDOM nodes:
* <pre><code>
* public static List transform(Document doc, String stylesheet)
* throws JDOMException {
* try {
* Transformer transformer = TransformerFactory.newInstance()
* .newTransformer(new StreamSource(stylesheet));
* JDOMSource in = new JDOMSource(doc);
* JDOMResult out = new JDOMResult();
* transformer.transform(in, out);
* return out.getResult();
* }
* catch (TransformerException e) {
* throw new JDOMException("XSLT Transformation failed", e);
* }
* }
* </code></pre>
*
* @see org.jdom.transform.JDOMResult
*
* @version $Revision: 1.20 $, $Date: 2007/11/10 05:29:02 $
* @author Laurent Bihanic
* @author Jason Hunter
*/
public class JDOMSource extends SAXSource {
private static final String CVS_ID =
"@(#) $RCSfile: JDOMSource.java,v $ $Revision: 1.20 $ $Date: 2007/11/10 05:29:02 $ $Name: jdom_1_1 $";
/**
* If {@link javax.xml.transform.TransformerFactory#getFeature}
* returns <code>true</code> when passed this value as an
* argument, the Transformer natively supports JDOM.
* <p>
* <strong>Note</strong>: This implementation does not override
* the {@link SAXSource#FEATURE} value defined by its superclass
* to be considered as a SAXSource by Transformer implementations
* not natively supporting JDOM.
* </p>
*/
public final static String JDOM_FEATURE =
"http://org.jdom.transform.JDOMSource/feature";
/**
* The XMLReader object associated to this source or
* <code>null</code> if no XMLReader has yet been requested.
*
* @see #getXMLReader
*/
private XMLReader xmlReader = null;
/**
* Optional entity resolver associated to the source of
* this document or <code>null</code> if no EntityResolver
* was supplied with this JDOMSource.
*
* @see #buildDocumentReader()
*/
private EntityResolver resolver = null;
/**
* Creates a JDOM TrAX source wrapping a JDOM document.
*
* @param source the JDOM document to use as source for the
* transformations
*
* @throws IllegalArgumentException if <code>source</code> is
* <code>null</code>.
*/
public JDOMSource(Document source) {
setDocument(source);
}
/**
* Creates a JDOM TrAX source wrapping a list of JDOM nodes.
*
* @param source the JDOM nodes to use as source for the
* transformations
*
* @throws IllegalArgumentException if <code>source</code> is
* <code>null</code>.
*/
public JDOMSource(List source) {
setNodes(source);
}
/**
* Creates a JDOM TrAX source wrapping a JDOM element.
*
* @param source the JDOM element to use as source for the
* transformations
*
* @throws IllegalArgumentException if <code>source</code> is
* <code>null</code>.
*/
public JDOMSource(Element source) {
List nodes = new ArrayList();
nodes.add(source);
setNodes(nodes);
}
/**
* Creates a JDOM TrAX source wrapping a JDOM element with an
* associated EntityResolver to resolve external entities.
*
* @param source The JDOM Element to use as source for the
* transformations
*
* @param resolver Entity resolver to use for the source
* transformation
*
* @throws IllegalArgumentException if<code>source</code> is
* <code>null</code>
*/
public JDOMSource(Document source, EntityResolver resolver) {
setDocument(source);
this.resolver = resolver;
}
/**
* Sets the source document used by this TrAX source.
*
* @param source the JDOM document to use as source for the
* transformations
*
* @throws IllegalArgumentException if <code>source</code> is
* <code>null</code>.
*
* @see #getDocument
*/
public void setDocument(Document source) {
super.setInputSource(new JDOMInputSource(source));
}
/**
* Returns the source document used by this TrAX source.
*
* @return the source document used by this TrAX source or
* <code>null</code> if the source is a node list.
*
* @see #setDocument
*/
public Document getDocument() {
Object src = ((JDOMInputSource)getInputSource()).getSource();
Document doc = null;
if (src instanceof Document) {
doc = (Document)src;
}
return doc;
}
/**
* Sets the source node list used by this TrAX source.
*
* @param source the JDOM nodes to use as source for the
* transformations
*
* @throws IllegalArgumentException if <code>source</code> is
* <code>null</code>.
*
* @see #getNodes
*/
public void setNodes(List source) {
super.setInputSource(new JDOMInputSource(source));
}
/**
* Returns the source node list used by this TrAX source.
*
* @return the source node list used by this TrAX source or
* <code>null</code> if the source is a JDOM document.
*
* @see #setDocument
*/
public List getNodes() {
Object src = ((JDOMInputSource)getInputSource()).getSource();
List nodes = null;
if (src instanceof List) {
nodes = (List)src;
}
return nodes;
}
//-------------------------------------------------------------------------
// SAXSource overwritten methods
//-------------------------------------------------------------------------
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -