/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*/
/**
* JAXP {@link javax.xml.transform.Source} implementation
* that marshals a JAXB-generated object.
*
* <p>
* This utility class is useful to combine JAXB with
*
* <p>
* The following example shows how to use JAXB to marshal a document
* for transformation by XSLT.
*
* <blockquote>
* <pre>
* MyObject o = // get JAXB content tree
*
* // jaxbContext is a JAXBContext object from which 'o' is created.
* JAXBSource source = new JAXBSource( jaxbContext, o );
*
* // set up XSLT transformation
* TransformerFactory tf = TransformerFactory.newInstance();
* Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
*
* // run transformation
* t.transform(source,new StreamResult(System.out));
* </pre>
* </blockquote>
*
* <p>
* The fact that JAXBSource derives from SAXSource is an implementation
* detail. Thus in general applications are strongly discouraged from
* accessing methods defined on SAXSource. In particular,
* the setXMLReader and setInputSource methods shall never be called.
* The XMLReader object obtained by the getXMLReader method shall
* be used only for parsing the InputSource object returned by
* the getInputSource method.
*
* <p>
* Similarly the InputSource object obtained by the getInputSource
* method shall be used only for being parsed by the XMLReader object
* returned by the getXMLReader.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
/**
* Creates a new {@link javax.xml.transform.Source} for the given content object.
*
* @param context
* JAXBContext that was used to create
* <code>contentObject</code>. This context is used
* to create a new instance of marshaller and must not be null.
* @param contentObject
* An instance of a JAXB-generated class, which will be
* used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
* not be null.
* @throws JAXBException if an error is encountered while creating the
* JAXBSource or if either of the parameters are null.
*/
throws JAXBException {
this(
( contentObject == null ) ?
}
/**
* Creates a new {@link javax.xml.transform.Source} for the given content object.
*
* @param marshaller
* A marshaller instance that will be used to marshal
* <code>contentObject</code> into XML. This must be
* created from a JAXBContext that was used to build
* <code>contentObject</code> and must not be null.
* @param contentObject
* An instance of a JAXB-generated class, which will be
* used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
* not be null.
* @throws JAXBException if an error is encountered while creating the
* JAXBSource or if either of the parameters are null.
*/
throws JAXBException {
if( marshaller == null )
throw new JAXBException(
if( contentObject == null )
throw new JAXBException(
this.marshaller = marshaller;
this.contentObject = contentObject;
super.setXMLReader(pseudoParser);
// pass a dummy InputSource. We don't care
super.setInputSource(new InputSource());
}
// this object will pretend as an XMLReader.
// no matter what parameter is specified to the parse method,
// it just parse the contentObject.
return true;
return false;
throw new SAXNotRecognizedException(name);
}
return;
return;
throw new SAXNotRecognizedException(name);
}
return lexicalHandler;
}
throw new SAXNotRecognizedException(name);
}
return;
}
throw new SAXNotRecognizedException(name);
}
private LexicalHandler lexicalHandler;
// we will store this value but never use it by ourselves.
private EntityResolver entityResolver;
this.entityResolver = resolver;
}
public EntityResolver getEntityResolver() {
return entityResolver;
}
private DTDHandler dtdHandler;
this.dtdHandler = handler;
}
public DTDHandler getDTDHandler() {
return dtdHandler;
}
// SAX allows ContentHandler to be changed during the parsing,
// but JAXB doesn't. So this repeater will sit between those
// two components.
}
public ContentHandler getContentHandler() {
return repeater.getContentHandler();
}
private ErrorHandler errorHandler;
this.errorHandler = handler;
}
public ErrorHandler getErrorHandler() {
return errorHandler;
}
parse();
}
parse();
}
public void parse() throws SAXException {
// parses a content object by using the given marshaller
// SAX events will be sent to the repeater, and the repeater
// will further forward it to an appropriate component.
try {
} catch( JAXBException e ) {
// wrap it to a SAXException
new SAXParseException( e.getMessage(),
// if the consumer sets an error handler, it is our responsibility
// to notify it.
if(errorHandler!=null)
// this is a fatal error. Even if the error handler
// returns, we will abort anyway.
throw se;
}
}
};
/**
* Hook to throw exception from the middle of a contructor chained call
* to this
*/
throws JAXBException {
throw new JAXBException( message );
}
}