下面列出了怎么用org.xml.sax.XMLFilter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
throw new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Unit test for contentHandler setter/getter along reader as handler's
* parent.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase10() throws Exception {
String outputFile = USER_DIR + "saxtf010.out";
String goldFile = GOLDEN_DIR + "saxtf010GF.out";
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter =
saxTFactory.newXMLFilter(new StreamSource(XSLT_FILE));
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(XML_FILE));
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for contentHandler setter/getter.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase12() throws Exception {
String outputFile = USER_DIR + "saxtf012.out";
String goldFile = GOLDEN_DIR + "saxtf012GF.out";
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
InputSource is = new InputSource(new FileInputStream(XSLT_FILE));
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(is);
SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter = saxTFactory.newXMLFilter(saxSource);
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(XML_FILE));
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for TemplatesHandler setter/getter.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase13() throws Exception {
String outputFile = USER_DIR + "saxtf013.out";
String goldFile = GOLDEN_DIR + "saxtf013GF.out";
try(FileInputStream fis = new FileInputStream(XML_FILE)) {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
// I have put this as it was complaining about systemid
thandler.setSystemId("file:///" + USER_DIR);
reader.setContentHandler(thandler);
reader.parse(XSLT_FILE);
XMLFilter filter
= saxTFactory.newXMLFilter(thandler.getTemplates());
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
filter.parse(new InputSource(fis));
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Create an XMLFilter that uses the given source as the
* transformation instructions.
*
* @param templates The source of the transformation instructions.
* @return An XMLFilter object, or null if this feature is not supported.
* @throws TransformerConfigurationException
*/
@Override
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException
{
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch (TransformerConfigurationException e1) {
if (_errorListener != null) {
try {
_errorListener.fatalError(e1);
return null;
}
catch (TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
/**
* Create an XMLFilter that uses the given source as the
* transformation instructions. Uses
* com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
*/
public XMLFilter newXMLFilter(Source src)
throws TransformerConfigurationException {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
if (_errorlistener != null) {
_xsltcFactory.setErrorListener(_errorlistener);
}
if (_uriresolver != null) {
_xsltcFactory.setURIResolver(_uriresolver);
}
Templates templates = _xsltcFactory.newTemplates(src);
if (templates == null ) return null;
return newXMLFilter(templates);
}
public XMLFilter newXMLFilter(Templates templates)
throws TransformerConfigurationException {
try {
return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
}
catch(TransformerConfigurationException e1) {
if (_xsltcFactory == null) {
createXSLTCTransformerFactory();
}
ErrorListener errorListener = _xsltcFactory.getErrorListener();
if(errorListener != null) {
try {
errorListener.fatalError(e1);
return null;
}
catch( TransformerException e2) {
new TransformerConfigurationException(e2);
}
}
throw e1;
}
}
public LoadingModelEvaluatorBuilder load(InputStream is, String modelName) throws SAXException, JAXBException {
Schema schema = getSchema();
ValidationEventHandler validationEventHandler = getValidationEventHandler();
List<? extends XMLFilter> filters = getFilters();
boolean locatable = getLocatable();
VisitorBattery visitors = getVisitors();
Unmarshaller unmarshaller = JAXBUtil.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(validationEventHandler);
if(filters == null){
filters = Collections.singletonList(new ImportFilter());
}
Source source = SAXUtil.createFilteredSource(is, filters.toArray(new XMLFilter[filters.size()]));
PMML pmml = (PMML)unmarshaller.unmarshal(source);
Visitor locatorHandler = (locatable ? new LocatorTransformer() : new LocatorNullifier());
locatorHandler.applyTo(pmml);
if(visitors != null && !visitors.isEmpty()){
visitors.applyTo(pmml);
}
Model model = PMMLUtil.findModel(pmml, modelName);
setPMML(pmml);
setModel(model);
return this;
}
/**
* Parses a RELAX NG grammar into an annotated grammar.
*/
private Model loadRELAXNG() throws SAXException {
// build DOM forest
final DOMForest forest = buildDOMForest( new RELAXNGInternalizationLogic() );
// use JAXP masquerading to validate the input document.
// DOMForest -> ExtensionBindingChecker -> RNGOM
XMLReaderCreator xrc = new XMLReaderCreator() {
public XMLReader createXMLReader() {
// foreset parser cannot change the receivers while it's working,
// so we need to have one XMLFilter that works as a buffer
XMLFilter buffer = new XMLFilterImpl() {
@Override
public void parse(InputSource source) throws IOException, SAXException {
forest.createParser().parse( source, this, this, this );
}
};
XMLFilter f = new ExtensionBindingChecker(Const.RELAXNG_URI,opt,errorReceiver);
f.setParent(buffer);
f.setEntityResolver(opt.entityResolver);
return f;
}
};
Parseable p = new SAXParseable( opt.getGrammars()[0], errorReceiver, xrc );
return loadRELAXNG(p);
}
/**
* Parses a RELAX NG grammar into an annotated grammar.
*/
private Model loadRELAXNG() throws SAXException {
// build DOM forest
final DOMForest forest = buildDOMForest( new RELAXNGInternalizationLogic() );
// use JAXP masquerading to validate the input document.
// DOMForest -> ExtensionBindingChecker -> RNGOM
XMLReaderCreator xrc = new XMLReaderCreator() {
public XMLReader createXMLReader() {
// foreset parser cannot change the receivers while it's working,
// so we need to have one XMLFilter that works as a buffer
XMLFilter buffer = new XMLFilterImpl() {
@Override
public void parse(InputSource source) throws IOException, SAXException {
forest.createParser().parse( source, this, this, this );
}
};
XMLFilter f = new ExtensionBindingChecker(Const.RELAXNG_URI,opt,errorReceiver);
f.setParent(buffer);
f.setEntityResolver(opt.entityResolver);
return f;
}
};
Parseable p = new SAXParseable( opt.getGrammars()[0], errorReceiver, xrc );
return loadRELAXNG(p);
}