[jdom-interest] Bringing SAXOutputter closer to XMLReader
Laurent Bihanic
laurent.bihanic at atosorigin.com
Thu Sep 27 07:35:15 PDT 2001
Hi,
Attached is a enhancement to SAXOutputter to bring is closer to XMLReader by
supporting all the methods defined by XMLReader except the 2 methods parse().
With these changes, implementing an XMLReader or XMLFilter from SAXOutputter
is a lot easier: It is now possible to derive from SAXOutputter instead of
writing an adapter class.
JDOMSource has been updated to demonstrate how easy it is!
Changes include:
- new public no-argument constructor
- support for DeclHandler (setter and getter methods)
- getter methods for ContentHandler, DTDHandler, ErrorHandler,
EntityResolver and LexicalHandler
- set/getFeature and set/getProperty methods
Laurent
-------------- next part --------------
/*--
$Id: SAXOutputter.java,v 1.12 2001/08/02 23:56:26 bmclaugh Exp $
Copyright (C) 2000 Brett McLaughlin & Jason Hunter.
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 license at jdom.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 (pm at jdom.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 Brett McLaughlin <brett at jdom.org> and
Jason Hunter <jhunter at jdom.org>. For more information on the
JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom.output;
import java.io.*;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.Locator;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.LocatorImpl;
import org.xml.sax.helpers.AttributesImpl;
import org.jdom.*;
/**
* <p>
* <code>SAXOutputter</code> takes a JDOM tree and fires SAX2 events.
* </p>
*
* Most <code>ContentHandler</code> callbacks are supported. Both
* <code>ignorableWhitespace</code> and <code>skippedEntity</code> have
* not been implemented. The <code>setDocumentLocator</code> callback has
* been implemented, but the locator object always returns -1 for
* <code>getColumnNumber</code> and <code>getLineNumber</code>.
* </p>
*
* The <code>EntityResolver</code> callback <code>resolveEntity</code> has
* been implemented for DTDs.
* </p>
*
* At this time, it is not possible to access notations and unparsed entity
* references in a DTD from a JDOM tree. Therefore, <code>DTDHandler</code>
* callbacks have not been implemented yet.
* </p>
*
* The <code>ErrorHandler</code> callbacks have not been implemented, since
* these are supposed to be invoked when the document is parsed. However, the
* document has already been parsed in order to create the JDOM tree.
* </p>
*
* @author Brett McLaughlin
* @author Jason Hunter
* @author Fred Trimble
* @version 1.0
*/
public class SAXOutputter {
private static final String CVS_ID =
"@(#) $RCSfile: SAXOutputter.java,v $ $Revision: 1.12 $ $Date: 2001/08/02 23:56:26 $ $Name: $";
/** Shortcut for SAX namespaces core feature */
private static final String NAMESPACES_SAX_FEATURE =
"http://xml.org/sax/features/namespaces";
/** Shortcut for SAX namespace-prefixes core feature */
private static final String NS_PREFIXES_SAX_FEATURE =
"http://xml.org/sax/features/namespace-prefixes";
/** Shortcut for SAX-ext. lexical handler property */
private static final String LEXICAL_HANDLER_SAX_PROPERTY =
"http://xml.org/sax/properties/lexical-handler";
/** Shortcut for SAX-ext. declaration handler property */
private static final String DECL_HANDLER_SAX_PROPERTY =
"http://xml.org/sax/properties/declaration-handler";
/**
* Shortcut for SAX-ext. lexical handler alternate property.
* Although this property URI is not the one defined by the SAX
* "standard", some parsers use it instead of the official one.
*/
private static final String LEXICAL_HANDLER_ALT_PROPERTY =
"http://xml.org/sax/handlers/LexicalHandler";
/** Shortcut for SAX-ext. declaration handler alternate property */
private static final String DECL_HANDLER_ALT_PROPERTY =
"http://xml.org/sax/handlers/DeclHandler";
/** registered <code>ContentHandler</code> */
private ContentHandler contentHandler;
/** registered <code>ErrorHandler</code> */
private ErrorHandler errorHandler;
/** registered <code>DTDHandler</code> */
private DTDHandler dtdHandler;
/** registered <code>EntityResolver</code> */
private EntityResolver entityResolver;
/** registered <code>LexicalHandler</code> */
private LexicalHandler lexicalHandler;
/** registered <code>DeclHandler</code> */
private DeclHandler declHandler;
/**
* Whether to report attribute namespace declarations as xmlns attributes.
* Defaults to <code>false</code> as per SAX specifications.
*
* @see <a href="http://www.megginson.com/SAX/Java/namespaces.html">
* SAX namespace specifications</a>
*/
private boolean declareNamespaces = false;
/**
* <p>
* This will create a <code>SAXOutputter</code> without any
* registered handler. The application is then responsible for
* registering them using the <code>setXxxHandler()</code> methods.
* </p>
*/
public SAXOutputter() {
}
/**
* <p>
* This will create a <code>SAXOutputter</code> with the
* specified <code>ContentHandler</code>.
* </p>
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods
*/
public SAXOutputter(ContentHandler contentHandler) {
this(contentHandler, null, null, null, null);
}
/**
* <p>
* This will create a <code>SAXOutputter</code> with the
* specified SAX2 handlers. At this time, only <code>ContentHandler</code>
* and <code>EntityResolver</code> are supported.
* </p>
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods
* @param errorHandler contains <code>ErrorHandler</code> callback methods
* @param dtdHandler contains <code>DTDHandler</code> callback methods
* @param entityResolver contains <code>EntityResolver</code>
* callback methods
*/
public SAXOutputter(ContentHandler contentHandler,
ErrorHandler errorHandler,
DTDHandler dtdHandler,
EntityResolver entityResolver) {
this(contentHandler, errorHandler, dtdHandler, entityResolver, null);
}
/**
* <p>
* This will create a <code>SAXOutputter</code> with the
* specified SAX2 handlers. At this time, only <code>ContentHandler</code>
* and <code>EntityResolver</code> are supported.
* </p>
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods
* @param errorHandler contains <code>ErrorHandler</code> callback methods
* @param dtdHandler contains <code>DTDHandler</code> callback methods
* @param entityResolver contains <code>EntityResolver</code>
* callback methods
* @param lexicalHandler contains <code>LexicalHandler</code> callbacks.
*/
public SAXOutputter(ContentHandler contentHandler,
ErrorHandler errorHandler,
DTDHandler dtdHandler,
EntityResolver entityResolver,
LexicalHandler lexicalHandler) {
this.contentHandler = contentHandler;
this.errorHandler = errorHandler;
this.dtdHandler = dtdHandler;
this.entityResolver = entityResolver;
this.lexicalHandler = lexicalHandler;
}
/**
* <p>
* This will set the <code>ContentHandler</code>.
* </p>
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods.
*/
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
/**
* <p>
* Returns the registered <code>ContentHandler</code>.
* </p>
*
* @return the current <code>ContentHandler</code> or
* <code>null</code> if none was registered.
*/
public ContentHandler getContentHandler() {
return this.contentHandler;
}
/**
* <p>
* This will set the <code>ErrorHandler</code>.
* </p>
*
* @param errorHandler contains <code>ErrorHandler</code> callback methods.
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
/**
* <p>
* Return the registered <code>ErrorHandler</code>.
* </p>
*
* @return the current <code>ErrorHandler</code> or
* <code>null</code> if none was registered.
*/
public ErrorHandler getErrorHandler() {
return this.errorHandler;
}
/**
* <p>
* This will set the <code>DTDHandler</code>.
* </p>
*
* @param dtdHandler contains <code>DTDHandler</code> callback methods.
*/
public void setDTDHandler(DTDHandler dtdHandler) {
this.dtdHandler = dtdHandler;
}
/**
* <p>
* Return the registered <code>DTDHandler</code>.
* </p>
*
* @return the current <code>DTDHandler</code> or
* <code>null</code> if none was registered.
*/
public DTDHandler getDTDHandler() {
return this.dtdHandler;
}
/**
* <p>
* This will set the <code>EntityResolver</code>.
* </p>
*
* @param entityResolver contains EntityResolver callback methods.
*/
public void setEntityResolver(EntityResolver entityResolver) {
this.entityResolver = entityResolver;
}
/**
* <p>
* Return the registered <code>EntityResolver</code>.
* </p>
*
* @return the current <code>EntityResolver</code> or
* <code>null</code> if none was registered.
*/
public EntityResolver getEntityResolver() {
return this.entityResolver;
}
/**
* <p>
* This will set the <code>LexicalHandler</code>.
* </p>
*
* @param lexicalHandler contains lexical callback methods.
*/
public void setLexicalHandler(LexicalHandler lexicalHandler) {
this.lexicalHandler = lexicalHandler;
}
/**
* <p>
* Return the registered <code>LexicalHandler</code>.
* </p>
*
* @return the current <code>LexicalHandler</code> or
* <code>null</code> if none was registered.
*/
public LexicalHandler getLexicalHandler() {
return this.lexicalHandler;
}
/**
* <p>
* This will set the <code>DeclHandler</code>.
* </p>
*
* @param declHandler contains declaration callback methods.
*/
public void setDeclHandler(DeclHandler declHandler) {
this.declHandler = declHandler;
}
/**
* <p>
* Return the registered <code>DeclHandler</code>.
* </p>
*
* @return the current <code>DeclHandler</code> or
* <code>null</code> if none was registered.
*/
public DeclHandler getDeclHandler() {
return this.declHandler;
}
/**
* <p>
* This will define whether attribute namespace declarations shall be
* reported as "xmlns" attributes. This flag defaults to <code>false</code>
* and behaves as the "namespace-prefixes" SAX core feature.
* </p>
*
* @param reportDecl whether attribute namespace declarations shall be
* reported as "xmlns" attributes.
*/
public void setReportNamespaceDeclarations(boolean declareNamespaces) {
this.declareNamespaces = declareNamespaces;
}
/**
* <p>
* This will set the state of a SAX feature.
* </p>
* <p>
* All XMLReaders are required to support setting to true and to false.
* </p>
* <p>
* SAXOutputter currently supports the following SAX core features:
* <dl>
* <dt><code>http://xml.org/sax/features/namespaces</code></dt>
* <dd><strong>description:</strong> An optional extension handler for
* lexical events like comments.</dd>
* <dd><strong>access:</strong> read/write, but always
* <code>true</code>!</dd>
* <dt><code>http://xml.org/sax/features/namespace-prefixes</code></dt>
* <dd><strong>description:</strong> An optional extension handler
* for DTD-related events other than notations and unparsed
* entities.</dd>
* <dd><strong>access:</strong> read/write</dd>
* </dl>
* </p>
*
* @param name <code>String</code> the feature name, which is a
* fully-qualified URI.
* @param value <code>boolean</code> the requested state of the
* feature (true or false).
*
* @throws SAXNotRecognizedException When SAXOutputter does not
* recognize the feature name.
* @throws SAXNotSupportedException When SAXOutputter recognizes
* the feature name but cannot set the requested value.
*/
public void setFeature(String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException {
if (NS_PREFIXES_SAX_FEATURE.equals(name)) {
// Namespace prefix declarations.
this.setReportNamespaceDeclarations(value);
}
else {
if (NAMESPACES_SAX_FEATURE.equals(name)) {
if (value != true) {
// Namespaces feature always supported by SAXOutputter.
throw new SAXNotSupportedException(name);
}
// Else: true is OK!
}
else {
// Not a supported feature.
throw new SAXNotRecognizedException(name);
}
}
}
/**
* <p>
* This will look up the value of a SAX feature.
* </p>
*
* @param name <code>String</code> the feature name, which is a
* fully-qualified URI.
* @return <code>boolean</code> the current state of the feature
* (true or false).
*
* @throws SAXNotRecognizedException When SAXOutputter does not
* recognize the feature name.
* @throws SAXNotSupportedException When SAXOutputter recognizes
* the feature name but determine its value at this time.
*/
public boolean getFeature(String name)
throws SAXNotRecognizedException, SAXNotSupportedException {
if (NS_PREFIXES_SAX_FEATURE.equals(name)) {
// Namespace prefix declarations.
return (this.declareNamespaces);
}
else {
if (NAMESPACES_SAX_FEATURE.equals(name)) {
// Namespaces feature always supported by SAXOutputter.
return (true);
}
else {
// Not a supported feature.
throw new SAXNotRecognizedException(name);
}
}
}
/**
* <p>
* This will set the value of a SAX property.
* This method is also the standard mechanism for setting extended
* handlers.
* </p>
* <p>
* SAXOutputter currently supports the following SAX properties:
* <dl>
* <dt><code>http://xml.org/sax/properties/lexical-handler</code></dt>
* <dd><strong>data type:</strong>
* <code>org.xml.sax.ext.LexicalHandler</code></dd>
* <dd><strong>description:</strong> An optional extension handler for
* lexical events like comments.</dd>
* <dd><strong>access:</strong> read/write</dd>
* <dt><code>http://xml.org/sax/properties/declaration-handler</code></dt>
* <dd><strong>data type:</strong>
* <code>org.xml.sax.ext.DeclHandler</code></dd>
* <dd><strong>description:</strong> An optional extension handler for
* DTD-related events other than notations and unparsed entities.</dd>
* <dd><strong>access:</strong> read/write</dd>
* </dl>
* </p>
*
* @param name <code>String</code> the property name, which is a
* fully-qualified URI.
* @param value <code>Object</code> the requested value for the property.
*
* @throws SAXNotRecognizedException When SAXOutputter does not recognize
* the property name.
* @throws SAXNotSupportedException When SAXOutputter recognizes the
* property name but cannot set the requested value.
*/
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException {
if ((LEXICAL_HANDLER_SAX_PROPERTY.equals(name)) ||
(LEXICAL_HANDLER_ALT_PROPERTY.equals(name))) {
this.setLexicalHandler((LexicalHandler)value);
}
else {
if ((DECL_HANDLER_SAX_PROPERTY.equals(name)) ||
(DECL_HANDLER_ALT_PROPERTY.equals(name))) {
this.setDeclHandler((DeclHandler)value);
}
else {
throw new SAXNotRecognizedException(name);
}
}
}
/**
* <p>
* This will look up the value of a SAX property.
* </p>
*
* @param name <code>String</code> the property name, which is a
* fully-qualified URI.
* @return <code>Object</code> the current value of the property.
*
* @throws SAXNotRecognizedException When SAXOutputter does not recognize
* the property name.
* @throws SAXNotSupportedException When SAXOutputter recognizes the
* property name but cannot determine its value at this time.
*/
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException {
if ((LEXICAL_HANDLER_SAX_PROPERTY.equals(name)) ||
(LEXICAL_HANDLER_ALT_PROPERTY.equals(name))) {
return this.getLexicalHandler();
}
else {
if ((DECL_HANDLER_SAX_PROPERTY.equals(name)) ||
(DECL_HANDLER_ALT_PROPERTY.equals(name))) {
return this.getDeclHandler();
}
else {
throw new SAXNotRecognizedException(name);
}
}
}
/**
* <p>
* This will output the <code>JDOM Document</code>, firing off the
* SAX events that have been registered.
* </p>
*
* @param document <code>JDOM Document</code> to output.
*/
public void output(Document document) throws JDOMException {
if (document == null) {
return;
}
// contentHandler.setDocumentLocator()
documentLocator(document);
// contentHandler.startDocument()
startDocument();
// entityResolver.resolveEntity()
entityResolver(document);
// JDOM cannot read unparsed entities and notations in DTD (yet)
// dtdHandler(document);
// Handle root element, as well as any root level
// processing instructions and CDATA sections
Iterator i = document.getContent().iterator();
while (i.hasNext()) {
Object obj = i.next();
if (obj instanceof Element) {
// process root element and its content
element(document.getRootElement(), new NamespaceStack());
}
else if (obj instanceof ProcessingInstruction) {
// contentHandler.processingInstruction()
processingInstruction((ProcessingInstruction) obj);
}
else if (obj instanceof CDATA) {
// contentHandler.characters()
characters(((CDATA) obj).getText());
}
else if (obj instanceof Comment) {
// lexicalHandler.comment()
comment(((Comment)obj).getText());
}
}
// contentHandler.endDocument()
endDocument();
}
/**
* <p>
* This enables an application to resolve external entities.
* </p>
*
* @param document JDOM <code>Document</code>.
*/
private void entityResolver(Document document) throws JDOMException {
if (entityResolver != null) {
DocType docType = document.getDocType();
String publicID = null;
String systemID = null;
if (docType != null) {
publicID = docType.getPublicID();
systemID = docType.getSystemID();
}
if ((publicID != null) || (systemID != null)) {
try {
entityResolver.resolveEntity(publicID, systemID);
}
catch (SAXException se) {
throw new JDOMException("Exception in entityResolver", se);
}
catch (IOException ioe) {
throw new JDOMException("Exception in entityResolver", ioe);
}
}
}
}
// XXX Not implemented yet, since a JDOM Document does not
// have access to the unparsed entities and notations
// defined in a DTD (yet)
// private void dtdHandler(Document document) {
// }
/**
* <p>
* This method tells you the line of the XML file being parsed.
* For an in-memory document, it's meaningless. The location
* is only valid for the current parsing lifecycle, but
* the document has already been parsed. Therefore, it returns
* -1 for both line and column numbers.
* </p>
*
* @param document JDOM <code>Document</code>.
*/
private void documentLocator(Document document) {
LocatorImpl locator = new LocatorImpl();
String publicID = null;
String systemID = null;
DocType docType = document.getDocType();
if (docType != null) {
publicID = docType.getPublicID();
systemID = docType.getSystemID();
}
locator.setPublicId(publicID);
locator.setSystemId(systemID);
locator.setLineNumber(-1);
locator.setColumnNumber(-1);
contentHandler.setDocumentLocator((Locator) locator);
}
/**
* <p>
* This method is always the second method of all callbacks in
* all handlers to be invoked (setDocumentLocator is always first).
* </p>
*/
private void startDocument() throws JDOMException {
try {
contentHandler.startDocument();
}
catch (SAXException se) {
throw new JDOMException("Exception in startDocument", se);
}
}
/**
* <p>
* Always the last method of all callbacks in all handlers
* to be invoked.
* </p>
*/
private void endDocument() throws JDOMException {
try {
contentHandler.endDocument();
}
catch (SAXException se) {
throw new JDOMException("Exception in endDocument", se);
}
}
/**
* <p>
* This will invoke the <code>ContentHandler.processingInstruction</code>
* callback when a processing instruction is encountered.
* </p>
*
* @param pi <code>ProcessingInstruction</code> containing target and data.
*/
private void processingInstruction(ProcessingInstruction pi)
throws JDOMException {
if (pi != null) {
String target = pi.getTarget();
String data = pi.getData();
try {
contentHandler.processingInstruction(target, data);
}
catch (SAXException se) {
throw new JDOMException(
"Exception in processingInstruction", se);
}
}
}
/**
* <p>
* This will recursively invoke all of the callbacks for a particular
* element.
* </p>
*
* @param element <code>Element</code> used in callbacks.
* @param namespaces <code>List</code> stack of Namespaces in scope.
*/
private void element(Element element, NamespaceStack namespaces)
throws JDOMException {
// used to check endPrefixMapping
int previouslyDeclaredNamespaces = namespaces.size();
// contentHandler.startPrefixMapping()
Attributes nsAtts = startPrefixMapping(element, namespaces);
// contentHandler.startElement()
startElement(element, nsAtts);
// handle content in the element
elementContent(element, namespaces);
// contentHandler.endElement()
endElement(element);
// contentHandler.endPrefixMapping()
endPrefixMapping(namespaces, previouslyDeclaredNamespaces);
}
/**
* <p>
* This will invoke the <code>ContentHandler.startPrefixMapping</code>
* callback
* when a new namespace is encountered in the <code>Document</code>.
* </p>
*
* @param element <code>Element</code> used in callbacks.
* @param namespaces <code>List</code> stack of Namespaces in scope.
*
* @return <code>Attributes</code> declaring the namespaces local to
* <code>element</code> or <code>null</code>.
*/
private Attributes startPrefixMapping(Element element,
NamespaceStack namespaces)
throws JDOMException {
AttributesImpl nsAtts = null; // The namespaces as xmlns attributes
Namespace ns = element.getNamespace();
if (ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
String prefix = ns.getPrefix();
String uri = namespaces.getURI(prefix);
if (!ns.getURI().equals(uri)) {
namespaces.push(ns);
nsAtts = this.addNsAttribute(nsAtts, ns);
try {
contentHandler.startPrefixMapping(prefix, ns.getURI());
}
catch (SAXException se) {
throw new JDOMException(
"Exception in startPrefixMapping", se);
}
}
}
// Fire additional namespace declarations
List additionalNamespaces = element.getAdditionalNamespaces();
if (additionalNamespaces != null) {
Iterator itr = additionalNamespaces.iterator();
while (itr.hasNext()) {
ns = (Namespace)itr.next();
String prefix = ns.getPrefix();
String uri = namespaces.getURI(prefix);
if (!ns.getURI().equals(uri)) {
namespaces.push(ns);
nsAtts = this.addNsAttribute(nsAtts, ns);
try {
contentHandler.startPrefixMapping(prefix, ns.getURI());
}
catch (SAXException se) {
throw new JDOMException(
"Exception in startPrefixMapping", se);
}
}
}
}
return nsAtts;
}
/**
* <p>
* This will invoke the <code>endPrefixMapping</code> callback in the
* <code>ContentHandler</code> when a namespace is goes out of scope
* in the <code>Document</code>.
* </p>
*
* @param namespaces <code>List</code> stack of Namespaces in scope.
* @param previouslyDeclaredNamespaces number of previously declared
* namespaces
*/
private void endPrefixMapping(NamespaceStack namespaces,
int previouslyDeclaredNamespaces)
throws JDOMException {
while (namespaces.size() > previouslyDeclaredNamespaces) {
String prefix = namespaces.pop();
try {
contentHandler.endPrefixMapping(prefix);
}
catch (SAXException se) {
throw new JDOMException("Exception in endPrefixMapping", se);
}
}
}
/**
* <p>
* This will invoke the <code>startElement</code> callback
* in the <code>ContentHandler</code>.
* </p>
*
* @param element <code>Element</code> used in callbacks.
* @param eltNamespaces <code>List</code> of namespaces to declare with
* the element or <code>null</code>.
*/
private void startElement(Element element, Attributes nsAtts)
throws JDOMException {
String namespaceURI = element.getNamespaceURI();
String localName = element.getName();
String rawName = element.getQualifiedName();
// Allocate attribute list.
AttributesImpl atts = (nsAtts != null)?
new AttributesImpl(nsAtts): new AttributesImpl();
List attributes = element.getAttributes();
Iterator i = attributes.iterator();
while (i.hasNext()) {
Attribute a = (Attribute) i.next();
atts.addAttribute(a.getNamespaceURI(),
a.getName(),
a.getQualifiedName(),
"CDATA",
a.getValue());
}
try {
contentHandler.startElement(namespaceURI, localName, rawName, atts);
}
catch (SAXException se) {
throw new JDOMException("Exception in startElement", se);
}
}
/**
* <p>
* This will invoke the <code>endElement</code> callback
* in the <code>ContentHandler</code>.
* </p>
*
* @param element <code>Element</code> used in callbacks.
*/
private void endElement(Element element) throws JDOMException {
String namespaceURI = element.getNamespaceURI();
String localName = element.getName();
String rawName = element.getQualifiedName();
try {
contentHandler.endElement(namespaceURI, localName, rawName);
}
catch (SAXException se) {
throw new JDOMException("Exception in endElement", se);
}
}
/**
* <p>
* This will invoke the callbacks for the content of an element.
* </p>
*
* @param element <code>Element</code> used in callbacks.
* @param namespaces <code>List</code> stack of Namespaces in scope.
*/
private void elementContent(Element element, NamespaceStack namespaces)
throws JDOMException {
List eltContent = element.getContent();
boolean empty = eltContent.size() == 0;
boolean stringOnly =
!empty &&
eltContent.size() == 1 &&
eltContent.get(0) instanceof String;
if (stringOnly) {
// contentHandler.characters()
characters(element.getText());
}
else {
Object content = null;
for (int i = 0, size = eltContent.size(); i < size; i++) {
content = eltContent.get(i);
if (content instanceof Element) {
element((Element) content, namespaces);
}
else if (content instanceof String) {
// contentHandler.characters()
characters((String) content);
}
else if (content instanceof CDATA) {
// contentHandler.characters()
characters(((CDATA) content).getText());
}
else if (content instanceof ProcessingInstruction) {
// contentHandler.processingInstruction()
processingInstruction((ProcessingInstruction) content);
}
}
}
}
/**
* <p>
* This will be called for each chunk of character data encountered.
* </p>
*
* @param elementText all text in an element, including whitespace.
*/
private void characters(String elementText) throws JDOMException {
char[] c = elementText.toCharArray();
try {
contentHandler.characters(c, 0, c.length);
}
catch (SAXException se) {
throw new JDOMException("Exception in characters", se);
}
}
/**
* <p>
* This will be called for each chunk of comment data encontered.
* </p>
*
* @param commentText all text in a comment, including whitespace.
*/
private void comment(String commentText) throws JDOMException {
if (lexicalHandler != null) {
char[] c = commentText.toCharArray();
try {
lexicalHandler.comment(c, 0, c.length);
} catch (SAXException se) {
throw new JDOMException("Exception in comment", se);
}
}
}
/**
* <p>
* Appends a namespace declaration in the form of a xmlns attribute to
* an attribute list, crerating this latter if needed.
* </p>
*
* @param atts <code>AttributeImpl</code> where to add the attribute.
* @param ns <code>Namespace</code> the namespace to declare.
*
* @return <code>AttributeImpl</code> the updated attribute list.
*/
private AttributesImpl addNsAttribute(AttributesImpl atts, Namespace ns) {
if (this.declareNamespaces) {
if (atts == null) {
atts = new AttributesImpl();
}
atts.addAttribute("", // namespace
"", // local name
"xmlns:" + ns.getPrefix(), // qualified name
"CDATA", // type
ns.getURI()); // value
}
return atts;
}
}
-------------- next part --------------
/*--
$Id: JDOMSource.java,v 1.4 2001/06/19 20:33:19 jhunter Exp $
Copyright (C) 2001 Brett McLaughlin & Jason Hunter.
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 license at jdom.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 (pm at jdom.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 Brett McLaughlin <brett at jdom.org> and
Jason Hunter <jhunter at jdom.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 org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;
import org.jdom.*;
import org.jdom.output.*;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.TransformerFactory; // workaround for @link bug
/**
* Acts as an holder for JDOM document sources.
* <p>
* This class shall be used to wrap a JDOM Document to provide it
* as input to a JAXP Transformer</p>
* <p>
* The following example shows how to apply an XSL Transformation
* to a JDOM document and get the transformation result in the form
* of another JDOM Document:</p>
* <blockquote><pre>
* public static Document transform(Document in, String stylesheet)
* throws JDOMException {
* try {
* Transformer transformer = TransformerFactory.newInstance()
* .newTransformer(new StreamSource(stylesheet));
*
* JDOMResult out = new JDOMResult();
* transformer.transform(new JDOMSource(in), out);
* return out.getDocument();
* }
* catch (TransformerException e) {
* throw new JDOMException("XSLT Trandformation failed", e);
* }
* }
* </pre></blockquote>
*
* @see org.jdom.transform.JDOMResult
*
* @author Laurent Bihanic
* @author Jason Hunter
*/
public class JDOMSource extends SAXSource {
/**
* 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";
/**
* An optional (chain of) SAX XMLFilter to apply to the SAX events
* describing the JDOM document.
*
* @see #getXMLReader
*/
private XMLFilter xmlFilter = null;
/**
* Creates a JDOM TRaX source wrapping a JDOM document.
*
* @param source the JDOM document to use as source for the
* transformations
*
* @throws NullPointerException if <code>source</code> is
* <code>null</code>.
*
* @see #setDocument
*/
public JDOMSource(Document source) {
setDocument(source);
}
/**
* Sets the source document used by this TRaX source.
*
* @param source the JDOM document to use as source for the
* transformations
*
* @throws NullPointerException 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 none has been set.
*
* @see #setDocument
*/
public Document getDocument() {
return ((JDOMInputSource) getInputSource()).getDocument();
}
//-------------------------------------------------------------------------
// SAXSource overwritten methods
//-------------------------------------------------------------------------
/**
* Sets the SAX InputSource to be used for the Source.
* <p>
* As this implementation only supports JDOM document as data
* source, this method always throws an
* {@link UnsupportedOperationException}.
*
* @param inputSource a valid InputSource reference.
*
* @throws UnsupportedOperationException always!
*/
public void setInputSource(InputSource inputSource)
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* Set the XMLReader to be used for the Source.
* <p>
* As this implementation only supports JDOM document as data
* source, this method throws an
* {@link UnsupportedOperationException} if the provided reader
* object does not implement the SAX {@link XMLFilter}
* interface. Otherwise, the JDOM document reader will be
* attached as parent of the filter chain.</p>
*
* @param reader a valid XMLReader or XMLFilter reference.
*
* @throws UnsupportedOperationException always!
*
* @see #getXMLReader
*/
public void setXMLReader(XMLReader reader)
throws UnsupportedOperationException {
if (reader instanceof XMLFilter) {
this.xmlFilter = (XMLFilter)reader;
}
else {
throw new UnsupportedOperationException();
}
}
/**
* Returns the XMLReader to be used for the Source.
* <p>
* This implementation returns a specific XMLReader reading
* the XML data from the source JDOM document.</p>
*
* @return an XMLReader reading the XML data from the source
* JDOM document.
*/
public XMLReader getXMLReader() {
XMLReader documentReader = new DocumentReader();
// Install filter (if any)
if (this.xmlFilter != null) {
// Connect the filter chain to the document reader.
XMLFilter root = this.xmlFilter;
while (root.getParent() instanceof XMLFilter) {
root = (XMLFilter) root.getParent();
}
root.setParent(documentReader);
// Read from filter
documentReader = this.xmlFilter;
}
return documentReader;
}
//=========================================================================
// JDOMInputSource nested class
//=========================================================================
/**
* A subclass of the SAX InputSource interface that wraps a JDOM
* Document.
* <p>
* This class is nested in JDOMSource as it is not intented to
* be used independently of its friend: DocumentReader.</p>
*
* @see org.jdom.Document
*/
private static class JDOMInputSource extends InputSource {
/**
* The source document.
*/
private Document document = null;
/**
* Builds a Input Source wrapping the specified JDOM Document.
*
* @param source the source document.
*
* @see #setDocument
*/
public JDOMInputSource(Document source) {
setDocument(source);
}
/**
* Sets the source document.
*
* @param source the JDOM document to use as source.
*
* @throws NullPointerException if <code>source</code> is
* <code>null</code>.
*
* @see #getDocument
*/
public void setDocument(Document source) {
if (source == null) {
throw new NullPointerException("source");
}
document = source;
}
/**
* Returns the source document.
*
* @return the source document or <code>null</code> if none
* has been set.
*
* @see #setDocument
*/
public Document getDocument() {
return document;
}
//-------------------------------------------------------------------------
// InputSource overwritten methods
//-------------------------------------------------------------------------
/**
* Sets the character stream for this input source.
* <p>
* This implementation always throws an
* {@link UnsupportedOperationException} as the only source
* stream supported is the source JDOM document.</p>
*
* @param characterStream a character stream containing
* an XML document.
*
* @throws UnsupportedOperationException always!
*/
public void setCharacterStream(Reader characterStream)
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* Gets the character stream for this input source.
* <p>
* Note that this method is only provided to make this
* InputSource implementation acceptable by any XML
* parser. As it generates an in-memory string representation
* of the JDOM document, it is quite inefficient from both
* speed and memory consumption points of view.</p>
*
* @return a Reader to a string representation of the
* source JDOM document.
*/
public Reader getCharacterStream() {
Document doc = this.getDocument();
Reader reader = null;
if (doc != null) {
//try {
// Get an in-memory string representation of the document
// and return a reader on it.
reader = new StringReader(new XMLOutputter().outputString(doc));
//}
//catch (final IOException outputterError) {
// // Oops! Can't stringify document.
// // => Return a dummy reader implementation that will
// // notify of the error on every call.
// reader = new Reader() {
// public int read(char cbuf[], int off, int len)
// throws IOException {
// throw outputterError;
// }
// public void close() throws IOException {
// return;
// }
// };
// }
}
// Else: No document, no reader!
return reader;
}
}
//=========================================================================
// DocumentReader nested class
//=========================================================================
/**
* An implementation of the SAX2 XMLReader interface that presents
* a SAX view of a JDOM Document. The actual generation of the
* SAX events is delegated to JDOM's SAXOutputter.
*
* @see org.jdom.Document
* @see org.jdom.output.SAXOutputter
*/
private static class DocumentReader extends SAXOutputter
implements XMLReader {
/**
* Public default constructor.
*/
public DocumentReader() {
super();
}
//----------------------------------------------------------------------
// SAX XMLReader interface support
//----------------------------------------------------------------------
/**
* Parses an XML document from a system identifier (URI).
* <p>
* This implementation does not support reading XML data from
* system identifiers, only from JDOM documents. Hence,
* this method always throws a {@SAXNotSupportedException}.</p>
*
* @param systemId the system identifier (URI).
*
* @throws SAXNotSupportedException always!
*/
public void parse(String systemId) throws SAXNotSupportedException {
throw new SAXNotSupportedException(
"Only JDOM Documents are supported as input");
}
/**
* Parses an XML document.
* <p>
* The methods accepts only <code>JDOMInputSource</code>s
* instances as input sources.</p>
*
* @param source the input source for the top-level of the
* XML document.
*
* @throws SAXException any SAX exception,
* possibly wrapping
* another exception.
* @throws SAXNotSupportedException if the input source does
* not wrap a JDOM document.
*/
public void parse(InputSource input) throws SAXException {
if (input instanceof JDOMInputSource) {
try {
this.output(((JDOMInputSource)input).getDocument());
}
catch (JDOMException e) {
throw new SAXException(e.getMessage(), e);
}
}
else {
throw new SAXNotSupportedException(
"Only JDOM Documents are supported as input");
}
}
}
}
-------------- next part --------------
Index: output/SAXOutputter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/output/SAXOutputter.java,v
retrieving revision 1.12
diff -r1.12 SAXOutputter.java
63a64
> import org.xml.sax.ext.DeclHandler;
105a107,135
> /** Shortcut for SAX namespaces core feature */
> private static final String NAMESPACES_SAX_FEATURE =
> "http://xml.org/sax/features/namespaces";
>
> /** Shortcut for SAX namespace-prefixes core feature */
> private static final String NS_PREFIXES_SAX_FEATURE =
> "http://xml.org/sax/features/namespace-prefixes";
>
> /** Shortcut for SAX-ext. lexical handler property */
> private static final String LEXICAL_HANDLER_SAX_PROPERTY =
> "http://xml.org/sax/properties/lexical-handler";
>
> /** Shortcut for SAX-ext. declaration handler property */
> private static final String DECL_HANDLER_SAX_PROPERTY =
> "http://xml.org/sax/properties/declaration-handler";
>
> /**
> * Shortcut for SAX-ext. lexical handler alternate property.
> * Although this property URI is not the one defined by the SAX
> * "standard", some parsers use it instead of the official one.
> */
> private static final String LEXICAL_HANDLER_ALT_PROPERTY =
> "http://xml.org/sax/handlers/LexicalHandler";
>
> /** Shortcut for SAX-ext. declaration handler alternate property */
> private static final String DECL_HANDLER_ALT_PROPERTY =
> "http://xml.org/sax/handlers/DeclHandler";
>
>
119a150,152
>
> /** registered <code>DeclHandler</code> */
> private DeclHandler declHandler;
131a165,174
> * This will create a <code>SAXOutputter</code> without any
> * registered handler. The application is then responsible for
> * registering them using the <code>setXxxHandler()</code> methods.
> * </p>
> */
> public SAXOutputter() {
> }
>
> /**
> * <p>
201a245,256
>
> /**
> * <p>
> * Returns the registered <code>ContentHandler</code>.
> * </p>
> *
> * @return the current <code>ContentHandler</code> or
> * <code>null</code> if none was registered.
> */
> public ContentHandler getContentHandler() {
> return this.contentHandler;
> }
215a271,282
> * Return the registered <code>ErrorHandler</code>.
> * </p>
> *
> * @return the current <code>ErrorHandler</code> or
> * <code>null</code> if none was registered.
> */
> public ErrorHandler getErrorHandler() {
> return this.errorHandler;
> }
>
> /**
> * <p>
226a294,305
> * Return the registered <code>DTDHandler</code>.
> * </p>
> *
> * @return the current <code>DTDHandler</code> or
> * <code>null</code> if none was registered.
> */
> public DTDHandler getDTDHandler() {
> return this.dtdHandler;
> }
>
> /**
> * <p>
237a317,328
> * Return the registered <code>EntityResolver</code>.
> * </p>
> *
> * @return the current <code>EntityResolver</code> or
> * <code>null</code> if none was registered.
> */
> public EntityResolver getEntityResolver() {
> return this.entityResolver;
> }
>
> /**
> * <p>
246c337,372
<
---
>
> /**
> * <p>
> * Return the registered <code>LexicalHandler</code>.
> * </p>
> *
> * @return the current <code>LexicalHandler</code> or
> * <code>null</code> if none was registered.
> */
> public LexicalHandler getLexicalHandler() {
> return this.lexicalHandler;
> }
>
> /**
> * <p>
> * This will set the <code>DeclHandler</code>.
> * </p>
> *
> * @param declHandler contains declaration callback methods.
> */
> public void setDeclHandler(DeclHandler declHandler) {
> this.declHandler = declHandler;
> }
>
> /**
> * <p>
> * Return the registered <code>DeclHandler</code>.
> * </p>
> *
> * @return the current <code>DeclHandler</code> or
> * <code>null</code> if none was registered.
> */
> public DeclHandler getDeclHandler() {
> return this.declHandler;
> }
>
259a386,554
>
> /**
> * <p>
> * This will set the state of a SAX feature.
> * </p>
> * <p>
> * All XMLReaders are required to support setting to true and to false.
> * </p>
> * <p>
> * SAXOutputter currently supports the following SAX core features:
> * <dl>
> * <dt><code>http://xml.org/sax/features/namespaces</code></dt>
> * <dd><strong>description:</strong> An optional extension handler for
> * lexical events like comments.</dd>
> * <dd><strong>access:</strong> read/write, but always
> * <code>true</code>!</dd>
> * <dt><code>http://xml.org/sax/features/namespace-prefixes</code></dt>
> * <dd><strong>description:</strong> An optional extension handler
> * for DTD-related events other than notations and unparsed
> * entities.</dd>
> * <dd><strong>access:</strong> read/write</dd>
> * </dl>
> * </p>
> *
> * @param name <code>String</code> the feature name, which is a
> * fully-qualified URI.
> * @param value <code>boolean</code> the requested state of the
> * feature (true or false).
> *
> * @throws SAXNotRecognizedException When SAXOutputter does not
> * recognize the feature name.
> * @throws SAXNotSupportedException When SAXOutputter recognizes
> * the feature name but cannot set the requested value.
> */
> public void setFeature(String name, boolean value)
> throws SAXNotRecognizedException, SAXNotSupportedException {
> if (NS_PREFIXES_SAX_FEATURE.equals(name)) {
> // Namespace prefix declarations.
> this.setReportNamespaceDeclarations(value);
> }
> else {
> if (NAMESPACES_SAX_FEATURE.equals(name)) {
> if (value != true) {
> // Namespaces feature always supported by SAXOutputter.
> throw new SAXNotSupportedException(name);
> }
> // Else: true is OK!
> }
> else {
> // Not a supported feature.
> throw new SAXNotRecognizedException(name);
> }
> }
> }
>
> /**
> * <p>
> * This will look up the value of a SAX feature.
> * </p>
> *
> * @param name <code>String</code> the feature name, which is a
> * fully-qualified URI.
> * @return <code>boolean</code> the current state of the feature
> * (true or false).
> *
> * @throws SAXNotRecognizedException When SAXOutputter does not
> * recognize the feature name.
> * @throws SAXNotSupportedException When SAXOutputter recognizes
> * the feature name but determine its value at this time.
> */
> public boolean getFeature(String name)
> throws SAXNotRecognizedException, SAXNotSupportedException {
> if (NS_PREFIXES_SAX_FEATURE.equals(name)) {
> // Namespace prefix declarations.
> return (this.declareNamespaces);
> }
> else {
> if (NAMESPACES_SAX_FEATURE.equals(name)) {
> // Namespaces feature always supported by SAXOutputter.
> return (true);
> }
> else {
> // Not a supported feature.
> throw new SAXNotRecognizedException(name);
> }
> }
> }
>
> /**
> * <p>
> * This will set the value of a SAX property.
> * This method is also the standard mechanism for setting extended
> * handlers.
> * </p>
> * <p>
> * SAXOutputter currently supports the following SAX properties:
> * <dl>
> * <dt><code>http://xml.org/sax/properties/lexical-handler</code></dt>
> * <dd><strong>data type:</strong>
> * <code>org.xml.sax.ext.LexicalHandler</code></dd>
> * <dd><strong>description:</strong> An optional extension handler for
> * lexical events like comments.</dd>
> * <dd><strong>access:</strong> read/write</dd>
> * <dt><code>http://xml.org/sax/properties/declaration-handler</code></dt>
> * <dd><strong>data type:</strong>
> * <code>org.xml.sax.ext.DeclHandler</code></dd>
> * <dd><strong>description:</strong> An optional extension handler for
> * DTD-related events other than notations and unparsed entities.</dd>
> * <dd><strong>access:</strong> read/write</dd>
> * </dl>
> * </p>
> *
> * @param name <code>String</code> the property name, which is a
> * fully-qualified URI.
> * @param value <code>Object</code> the requested value for the property.
> *
> * @throws SAXNotRecognizedException When SAXOutputter does not recognize
> * the property name.
> * @throws SAXNotSupportedException When SAXOutputter recognizes the
> * property name but cannot set the requested value.
> */
> public void setProperty(String name, Object value)
> throws SAXNotRecognizedException, SAXNotSupportedException {
> if ((LEXICAL_HANDLER_SAX_PROPERTY.equals(name)) ||
> (LEXICAL_HANDLER_ALT_PROPERTY.equals(name))) {
> this.setLexicalHandler((LexicalHandler)value);
> }
> else {
> if ((DECL_HANDLER_SAX_PROPERTY.equals(name)) ||
> (DECL_HANDLER_ALT_PROPERTY.equals(name))) {
> this.setDeclHandler((DeclHandler)value);
> }
> else {
> throw new SAXNotRecognizedException(name);
> }
> }
> }
>
> /**
> * <p>
> * This will look up the value of a SAX property.
> * </p>
> *
> * @param name <code>String</code> the property name, which is a
> * fully-qualified URI.
> * @return <code>Object</code> the current value of the property.
> *
> * @throws SAXNotRecognizedException When SAXOutputter does not recognize
> * the property name.
> * @throws SAXNotSupportedException When SAXOutputter recognizes the
> * property name but cannot determine its value at this time.
> */
> public Object getProperty(String name)
> throws SAXNotRecognizedException, SAXNotSupportedException {
> if ((LEXICAL_HANDLER_SAX_PROPERTY.equals(name)) ||
> (LEXICAL_HANDLER_ALT_PROPERTY.equals(name))) {
> return this.getLexicalHandler();
> }
> else {
> if ((DECL_HANDLER_SAX_PROPERTY.equals(name)) ||
> (DECL_HANDLER_ALT_PROPERTY.equals(name))) {
> return this.getDeclHandler();
> }
> else {
> throw new SAXNotRecognizedException(name);
> }
> }
> }
>
Index: transform/JDOMSource.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/transform/JDOMSource.java,v
retrieving revision 1.4
diff -r1.4 JDOMSource.java
376,428c376,377
< private static class DocumentReader implements XMLReader {
< /**
< * The JDOM SAXOutputter used to get a SAX2 view of the source
< * JDOM document.
< * <p>
< * As SAXOutputter requires at least a ContentHandler as
< * constructor argument, a dummy
< * {@link XMLFilterImpl empty XMLFilter} is used.</p>
< */
< private SAXOutputter saxOutputter = new SAXOutputter(new XMLFilterImpl());
<
< /**
< * The SAX ContentHandler associated to this XMLReader.
< * <p>
< * We have to maintain a reference on the ContentHandler as
< * JDOM's SAXOutputter does not provide any getter method.</p>
< */
< private ContentHandler contentHandler = null;
<
< /**
< * The SAX DTDHandler associated to this XMLReader.
< * <p>
< * We have to maintain a reference on the DTDHandler as
< * JDOM's SAXOutputter does not provide any getter method.</p>
< */
< private DTDHandler dtdHandler = null;
<
< /**
< * The SAX ErrorHandler associated to this XMLReader.
< * <p>
< * We have to maintain a reference on the ErrorHandler as
< * JDOM's SAXOutputter does not provide any getter method.</p>
< */
< private ErrorHandler errorHandler = null;
<
< /**
< * The SAX EntityResolver associated to this XMLReader.
< * <p>
< * We have to maintain a reference on the EntityResolver as
< * JDOM's SAXOutputter does not provide any getter method.</p>
< */
< private EntityResolver entityResolver = null;
<
< /**
< * The SAX features defined for this XMLReader.
< * <p>
< * This class does not define any feature (yet) and ignores
< * the SAX mandatory feature. Thus, this member is present
< * only to support the mandatory feature setting and retrieval
< * logic defined by SAX.</p>
< */
< private Map features = new HashMap();
<
---
> private static class DocumentReader extends SAXOutputter
> implements XMLReader {
433,437c382
< // Default value for SAX core features.
< this.features.put("http://xml.org/sax/features/namespaces",
< Boolean.TRUE);
< this.features.put("http://xml.org/sax/features/namespace-prefixes",
< Boolean.FALSE);
---
> super();
444,697d388
< //----------------------------------------------------------------------
< // Configuration
< //----------------------------------------------------------------------
<
< /**
< * Sets the state of a feature.
< * <p>
< * The feature name is any fully-qualified URI.</p>
< * <p>
< * All XMLReaders are required to support setting
< * <code>http://xml.org/sax/features/namespaces</code> to
< * <code>true</code> and
< * <code>http://xml.org/sax/features/namespace-prefixes</code> to
< * <code>false</code>.</p>
< * <p>
< * This implementation only recognizes the above listed
< * mandatory features but ignores them.</p>
< *
< * @param name the feature name, which is a fully-qualified URI.
< * @param state the requested state of the feature (true or false).
< *
< * @throws SAXNotRecognizedException when the XMLReader does not
< * recognize the feature name.
< * @throws SAXNotSupportedException when the XMLReader recognizes
< * the feature name but cannot set
< * the requested value.
< *
< * @see #getFeature
< */
< public void setFeature(String name, boolean value)
< throws SAXNotRecognizedException,
< SAXNotSupportedException {
< if (name.equals("http://xml.org/sax/features/namespace-prefixes")) {
< // Update outputter feature support.
< this.saxOutputter.setReportNamespaceDeclarations(value);
< // Memorize current value.
< this.features.put(name, new Boolean(value));
< }
< else {
< if (name.equals("http://xml.org/sax/features/namespaces")) {
< if (value != true) {
< // namespaces feature always supported.
< throw new SAXNotSupportedException(name);
< }
< // Else: true is OK!
< }
< else {
< throw new SAXNotRecognizedException(name);
< }
< }
< }
<
< /**
< * Looks up the value of a feature.
< * <p>
< * All XMLReaders are required to recognize the
< * <code>http://xml.org/sax/features/namespaces</code> and the
< * <code>http://xml.org/sax/features/namespace-prefixes</code> feature
< * names.</p>
< * <p>
< * This implementation only recognizes the above listed
< * mandatory features.</p>
< *
< * @param name the feature name, which is a fully-qualified URI.
< *
< * @return the current state of the feature (true or false).
< *
< * @throws SAXNotRecognizedException when the XMLReader does not
< * recognize the feature name.
< * @throws SAXNotSupportedException when the XMLReader recognizes
< * the feature name but cannot
< * determine its value at this time.
< *
< * @see #setFeature
< */
< public boolean getFeature(String name) throws SAXNotRecognizedException,
< SAXNotSupportedException {
< Boolean value = (Boolean) this.features.get(name);
< if (value != null) {
< return value.booleanValue();
< }
< else {
< throw new SAXNotRecognizedException(name);
< }
< }
<
< /**
< * Sets the value of a property.
< * <p>
< * The property name is any fully-qualified URI. It is
< * possible for an XMLReader to recognize a property name but
< * to be unable to set its value.</p>
< * <p>
< * This implementation does not recognized nor support any
< * property and thus always throws a
< * {@link SAXNotRecognizedException}.</p>
< *
< * @param name the property name, which is a fully-qualified URI.
< * @param state the requested value for the property.
< *
< * @throws SAXNotRecognizedException always!
< *
< * @see #getProperty
< */
< public void setProperty(String name, Object value)
< throws SAXNotRecognizedException {
< throw new SAXNotRecognizedException(name);
< }
<
< /**
< * Looks up the value of a property.
< * <p>
< * The property name is any fully-qualified URI. It is
< * possible for an XMLReader to recognize a property name but
< * to be unable to return its state.</p>
< * <p>
< * This implementation does not recognized nor support any
< * property and thus always throws a
< * {@link SAXNotRecognizedException}.</p>
< *
< * @param name the property name, which is a fully-qualified URI.
< *
< * @return the current value of the property.
< *
< * @throws SAXNotRecognizedException always!
< *
< * @see #getProperty
< */
< public Object getProperty(String name) throws SAXNotRecognizedException {
< throw new SAXNotRecognizedException(name);
< }
<
< //----------------------------------------------------------------------
< // Event handlers
< //----------------------------------------------------------------------
<
< /**
< * Allows an application to register a content event handler.
< *
< * @param handler the content handler.
< *
< * @throws NullPointerException if the handler argument
< * is <code>null</code>.
< *
< * @see #getContentHandler
< */
< public void setContentHandler(ContentHandler handler) {
< this.saxOutputter.setContentHandler(handler);
< this.contentHandler = handler;
< }
<
< /**
< * Returns the current content handler.
< *
< * @return the current content handler, or
< * <code>null</code> if none has been registered.
< *
< * @see #setContentHandler
< */
< public ContentHandler getContentHandler() {
< return this.contentHandler;
< }
<
< /**
< * Allows an application to register a DTD event handler.
< *
< * @param handler the DTD handler.
< *
< * @throws NullPointerException if the handler argument
< * is <code>null</code>.
< *
< * @see #getDTDHandler
< */
< public void setDTDHandler(DTDHandler handler) {
< if (handler == null) {
< throw new NullPointerException("handler");
< }
< this.saxOutputter.setDTDHandler(handler);
< this.dtdHandler = handler;
< }
<
< /**
< * Returns the current DTD handler.
< *
< * @return the current DTD handler, or
< * <code>null</code> if none has been registered.
< *
< * @see #setDTDHandler
< */
< public DTDHandler getDTDHandler() {
< return this.dtdHandler;
< }
<
< /**
< * Allows an application to register an error event handler.
< *
< * @param handler the error handler.
< *
< * @throws NullPointerException if the handler argument
< * is <code>null</code>.
< *
< * @see #getErrorHandler
< */
< public void setErrorHandler(ErrorHandler handler) {
< this.saxOutputter.setErrorHandler(handler);
< this.errorHandler = handler;
< }
<
< /**
< * Returns the current error handler.
< *
< * @return the current error handler, or
< * <code>null</code> if none has been registered.
< *
< * @see #setErrorHandler
< */
< public ErrorHandler getErrorHandler() {
< return errorHandler;
< }
<
< /**
< * Allows an application to register an entity resolver.
< *
< * @param resolver the entity resolver.
< *
< * @throws NullPointerException if the resolver argument
< * is <code>null</code>.
< *
< * @see #getEntityResolver
< */
< public void setEntityResolver(EntityResolver resolver) {
< if (resolver == null) {
< throw new NullPointerException("resolver");
< }
< this.saxOutputter.setEntityResolver(resolver);
< this.entityResolver = resolver;
< }
<
< /**
< * Returns the current entity resolver.
< *
< * @return the current entity resolver, or
< * <code>null</code> if none has been registered.
< *
< * @see #setEntityResolver
< */
< public EntityResolver getEntityResolver() {
< return entityResolver;
< }
<
< //----------------------------------------------------------------------
< // Parsing
< //----------------------------------------------------------------------
<
732c423
< this.saxOutputter.output(((JDOMInputSource)input).getDocument());
---
> this.output(((JDOMInputSource)input).getDocument());
More information about the jdom-interest
mailing list