Example usage for javax.xml.parsers SAXParser getXMLReader

List of usage examples for javax.xml.parsers SAXParser getXMLReader

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser getXMLReader.

Prototype


public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;

Source Link

Document

Returns the org.xml.sax.XMLReader that is encapsulated by the implementation of this class.

Usage

From source file:net.yacy.cora.document.feed.RSSReader.java

public RSSReader(final int maxsize, InputStream stream) throws IOException {
    this(maxsize);
    if (!(stream instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream))
        stream = new BufferedInputStream(stream);
    try {/*from w w  w . j a  va2  s.  co m*/
        final SAXParser saxParser = getParser();
        // do not look at external dtd - see: http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html
        saxParser.getXMLReader().setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(final String arg0, final String arg1)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        saxParser.parse(stream, this);
    } catch (final SAXException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:net.yacy.cora.document.feed.RSSReader.java

public RSSReader(final int maxsize, final long maxBytes, InputStream stream) throws IOException {
    this(maxsize);

    if (!(stream instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream)) {
        stream = new BufferedInputStream(stream);
    }/* w w w  .j a  v  a  2 s  .  c  o  m*/

    StrictLimitInputStream limitedSource = new StrictLimitInputStream(stream, maxBytes);

    try {
        final SAXParser saxParser = getParser();
        // do not look at external dtd - see: http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html
        saxParser.getXMLReader().setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(final String arg0, final String arg1)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        saxParser.parse(limitedSource, this);
    } catch (final SAXException e) {
        throw new IOException(e.getMessage());
    } catch (StreamLimitException e) {
        this.maxBytesExceeded = true;
    }
}

From source file:nl.b3p.ogc.utils.OgcWfsClient.java

public static AnyNode xmlStringToAnyNode(String xml) throws Exception {
    AnyNode anyNode = null;/*from  w w w  . j  a va 2  s  . c o  m*/
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XMLReader reader = saxParser.getXMLReader();

        org.exolab.castor.xml.util.SAX2ANY handler = new org.exolab.castor.xml.util.SAX2ANY();

        IgnoreEntityResolver r = new IgnoreEntityResolver();
        reader.setEntityResolver(r);

        reader.setContentHandler(handler);
        reader.setErrorHandler(handler);
        InputSource source = new InputSource(new StringReader(xml));
        reader.parse(source);

        anyNode = handler.getStartingNode();
    } catch (Exception e) {
        log.error("error", e);
    }
    return anyNode;

}

From source file:nl.nn.adapterframework.validation.JavaxXmlValidator.java

@Override
public XMLReader createValidatingParser(IPipeLineSession session, ValidationContext context)
        throws XmlValidatorException, PipeRunException {
    SAXParser parser;
    try {//from w w w  .  j a v  a  2  s. co m
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setValidating(false);
        parserFactory.setNamespaceAware(true);
        parserFactory.setFeature(PARSING_FEATURE_SECURE, true);
        //parserFactory.setFeature(PARSING_FEATURE_EXTERNAL_GENERAL_ENTITIES, false);
        //parserFactory.setFeature(PARSING_FEATURE_EXTERNAL_PARAMETER_ENTITIES, false);
        //parserFactory.setFeature(PARSING_FEATURE_DISALLOW_INLINE_DOCTYPE, true);

        Schema schema = getSchemaObject(context.getSchemasId(), schemasProvider.getSchemas(session));
        parserFactory.setSchema(schema);

        parser = parserFactory.newSAXParser();
        return parser.getXMLReader();
    } catch (ParserConfigurationException e) {
        throw new XmlValidatorException(logPrefix + "cannot configure parser", e);
    } catch (ConfigurationException e) {
        throw new XmlValidatorException(logPrefix + "cannot configure parser", e);
    } catch (SAXException e) {
        throw new XmlValidatorException(logPrefix + "cannot create parser", e);
    }
}

From source file:org.ambraproject.wombat.service.ArticleTransformServiceImpl.java

private void transform(Site site, InputStream xml, OutputStream html, TransformerInitializer initialization)
        throws IOException {
    Objects.requireNonNull(site);
    Objects.requireNonNull(xml);// w  w  w  .j av a2s  .c o m
    Objects.requireNonNull(html);

    log.debug("Starting XML transformation");
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader xmlr;
    try {
        SAXParser sp = spf.newSAXParser();
        xmlr = sp.getXMLReader();
    } catch (ParserConfigurationException | SAXException e) {
        throw new RuntimeException(e);
    }

    /*
     * This is a little unorthodox.  Without setting this custom EntityResolver, the transform will
     * make ~50 HTTP calls to nlm.nih.gov to retrieve the DTD and various entity files referenced
     * in the article XML. By setting a custom EntityResolver that just returns an empty string
     * for each of these, we prevent that.  This seems to have no ill effects on the transformation
     * itself.  This is a roundabout way of turning off DTD validation, which is more
     * straightforward to do with a Document/DocumentBuilder, but the saxon library we're using
     * is much faster at XSLT if it uses its own XML parser instead of DocumentBuilder.  See
     * http://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references
     * for a discussion.
     */
    xmlr.setEntityResolver((String publicId, String systemId) -> {
        // Note: returning null here will cause the HTTP request to be made.

        if (VALID_DTDS.contains(systemId)) {
            return new InputSource(new StringReader(""));
        } else {
            throw new IllegalArgumentException("Unexpected entity encountered: " + systemId);
        }
    });
    // build the transformer and add any context-dependent parameters required for the transform
    // NOTE: the XMLReader is passed here for use in creating any required secondary SAX sources
    Transformer transformer = buildTransformer(site, xmlr, initialization);
    SAXSource saxSource = new SAXSource(xmlr, new InputSource(xml));
    try {
        transformer.transform(saxSource, new StreamResult(html));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }

    log.debug("Finished XML transformation");
}

From source file:org.apache.axis.utils.XMLUtils.java

/** Get a SAX parser instance from the JAXP factory.
 *
 * @return a SAXParser instance.//  w w w  . ja v a  2  s  . co m
 */
public static synchronized SAXParser getSAXParser() {
    if (enableParserReuse && !saxParsers.empty()) {
        return (SAXParser) saxParsers.pop();
    }

    try {
        SAXParser parser = saxFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        // parser.getParser().setEntityResolver(new DefaultEntityResolver());
        // The above commented line and the following line are added
        // for preventing XXE (bug #14105).
        // We may need to uncomment the deprecated setting
        // in case that it is considered necessary.
        try {
            reader.setEntityResolver(new DefaultEntityResolver());
        } catch (Throwable t) {
            log.debug("Failed to set EntityResolver on DocumentBuilder", t);
        }
        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
        return parser;
    } catch (ParserConfigurationException e) {
        log.error(Messages.getMessage("parserConfigurationException00"), e);
        return null;
    } catch (SAXException se) {
        log.error(Messages.getMessage("SAXException00"), se);
        return null;
    }
}

From source file:org.apache.axis.utils.XMLUtils.java

/** Return a SAX parser for reuse.
 * @param parser A SAX parser that is available for reuse
 *//*from   w w  w. j a  v a  2s.co  m*/
public static void releaseSAXParser(SAXParser parser) {
    if (!tryReset || !enableParserReuse)
        return;

    //Free up possible ref. held by past contenthandler.
    try {
        XMLReader xmlReader = parser.getXMLReader();
        if (null != xmlReader) {
            xmlReader.setContentHandler(doNothingContentHandler);
            xmlReader.setDTDHandler(doNothingContentHandler);
            try {
                xmlReader.setEntityResolver(doNothingContentHandler);
            } catch (Throwable t) {
                log.debug("Failed to set EntityResolver on DocumentBuilder", t);
            }
            try {
                xmlReader.setErrorHandler(doNothingContentHandler);
            } catch (Throwable t) {
                log.debug("Failed to set ErrorHandler on DocumentBuilder", t);
            }

            synchronized (XMLUtils.class) {
                saxParsers.push(parser);
            }
        } else {
            tryReset = false;
        }
    } catch (org.xml.sax.SAXException e) {
        tryReset = false;
    }
}

From source file:org.apache.fop.fotreetest.FOTreeTestCase.java

/**
 * Runs a test./*from  w ww  .  j  a  v a 2  s .co m*/
 * @throws Exception if a test or FOP itself fails
 */
@Test
public void runTest() throws Exception {
    try {
        ResultCollector collector = ResultCollector.getInstance();
        collector.reset();

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(false);
        SAXParser parser = spf.newSAXParser();
        XMLReader reader = parser.getXMLReader();

        // Resetting values modified by processing instructions
        fopFactory.setBreakIndentInheritanceOnReferenceAreaBoundary(
                FopFactoryConfigurator.DEFAULT_BREAK_INDENT_INHERITANCE);
        fopFactory.setSourceResolution(FopFactoryConfigurator.DEFAULT_SOURCE_RESOLUTION);

        FOUserAgent ua = fopFactory.newFOUserAgent();
        ua.setBaseURL(testFile.getParentFile().toURI().toURL().toString());
        ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
        ua.getEventBroadcaster().addEventListener(new ConsoleEventListenerForTests(testFile.getName()));

        // Used to set values in the user agent through processing instructions
        reader = new PIListener(reader, ua);

        Fop fop = fopFactory.newFop(ua);

        reader.setContentHandler(fop.getDefaultHandler());
        reader.setDTDHandler(fop.getDefaultHandler());
        reader.setErrorHandler(fop.getDefaultHandler());
        reader.setEntityResolver(fop.getDefaultHandler());
        try {
            reader.parse(testFile.toURI().toURL().toExternalForm());
        } catch (Exception e) {
            collector.notifyError(e.getLocalizedMessage());
            throw e;
        }

        List<String> results = collector.getResults();
        if (results.size() > 0) {
            for (int i = 0; i < results.size(); i++) {
                System.out.println((String) results.get(i));
            }
            throw new IllegalStateException((String) results.get(0));
        }
    } catch (Exception e) {
        org.apache.commons.logging.LogFactory.getLog(this.getClass()).info("Error on " + testFile.getName());
        throw e;
    }
}

From source file:org.apache.jmeter.protocol.http.proxy.DefaultSamplerCreatorClassifier.java

/**
 * Tries parsing to see if content is xml
 * //w  w  w .j  a v  a 2 s.  c  o  m
 * @param postData
 *            String
 * @return boolean
 */
private static final boolean isPotentialXml(String postData) {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        ErrorDetectionHandler detectionHandler = new ErrorDetectionHandler();
        xmlReader.setContentHandler(detectionHandler);
        xmlReader.setErrorHandler(detectionHandler);
        xmlReader.parse(new InputSource(new StringReader(postData)));
        return !detectionHandler.isErrorDetected();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

From source file:org.apache.maven.plugin.cxx.utils.svn.SvnService.java

public static SvnInfo getSvnInfo(File basedir, Credential cred, String uri, Log log, boolean noParsingFailure)
        throws MojoExecutionException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    execSvnCommand(basedir, cred, new String[] { "info", uri, "--xml" }, out, log);

    SvnInfo svnInfo = new SvnInfo();
    try {//from   w w  w  . jav a2 s.c  o  m
        SAXParserFactory sfactory = SAXParserFactory.newInstance();
        SAXParser parser = sfactory.newSAXParser();
        XMLReader xmlparser = parser.getXMLReader();
        xmlparser.setContentHandler(svnInfo);
        xmlparser.parse(new InputSource(new ByteArrayInputStream(out.toByteArray())));
    } catch (Exception e) {
        if (noParsingFailure) {
            log.error("svn info xml parsing failed : " + e);
        } else {
            throw new MojoExecutionException("svn info xml parsing failed.", e);
        }
    }
    return svnInfo;
}