Example usage for javax.xml.parsers SAXParserFactory setFeature

List of usage examples for javax.xml.parsers SAXParserFactory setFeature

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory setFeature.

Prototype

public abstract void setFeature(String name, boolean value)
        throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException;

Source Link

Document

Sets the particular feature in the underlying implementation of org.xml.sax.XMLReader.

Usage

From source file:com.lucidworks.hadoop.ingest.util.EmptyEntityResolver.java

private static void trySetSAXFeature(SAXParserFactory saxFactory, String feature, boolean enabled) {
    try {//from www.j  a v a2  s  .  c o  m
        saxFactory.setFeature(feature, enabled);
    } catch (Exception ex) {
        // ignore
    }
}

From source file:Main.java

public static SAXParserFactory createParserFactory() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//from  ww w .  ja  v a 2 s  .co  m
    factory.setValidating(false);
    try {
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    } catch (ParserConfigurationException ex) {
    } catch (SAXNotRecognizedException ex) {
    } catch (SAXNotSupportedException ex) {
    }
    try {
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (ParserConfigurationException ex) {
    } catch (SAXNotRecognizedException ex) {
    } catch (SAXNotSupportedException ex) {
    }
    return factory;
}

From source file:net.sf.jasperreports.renderers.util.XmlDataSniffer.java

protected static void setParserFeature(SAXParserFactory factory, String feature, boolean value) {
    try {/* w  w  w  .  j  av  a2  s.  c  om*/
        factory.setFeature(feature, value);
    } catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) {
        if (log.isDebugEnabled()) {
            log.debug("Failed to set parser feature " + feature + ", error " + e);
        }
    }
}

From source file:Main.java

/**
 * Constructs a validating secure SAX Parser.
 *
 * @param schemaStream the schema to validate the XML against
 * @return a SAX Parser//  ww  w. j ava  2  s  . c om
 * @throws ParserConfigurationException is thrown if there is a parser
 * configuration exception
 * @throws SAXNotRecognizedException thrown if there is an unrecognized
 * feature
 * @throws SAXNotSupportedException thrown if there is a non-supported
 * feature
 * @throws SAXException is thrown if there is a SAXException
 */
public static SAXParser buildSecureSaxParser(InputStream schemaStream)
        throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    //setting the following unfortunately breaks reading the old suppression files (version 1).
    //factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

    final SAXParser saxParser = factory.newSAXParser();
    saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaStream);
    return saxParser;
}

From source file:eu.project.ttc.test.unit.TestUtil.java

public static String getTeiTxt(String filename)
        throws ParserConfigurationException, SAXException, IOException, FileNotFoundException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/* ww w . j av a 2 s.  co  m*/
    spf.setValidating(false);
    spf.setFeature("http://xml.org/sax/features/namespaces", true);
    spf.setFeature("http://xml.org/sax/features/validation", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    SAXParser saxParser = spf.newSAXParser();
    XMLReader xmlReader = saxParser.getXMLReader();
    TeiToTxtSaxHandler handler = new TeiToTxtSaxHandler();
    xmlReader.setContentHandler(handler);
    xmlReader.parse(new InputSource(TestUtil.getInputStream(filename)));
    String text = handler.getText();
    return text;
}

From source file:Main.java

/**
 * Convenience method: creates and returns a SAXParser.
 * /*from   w w  w .j  a  v a 2  s  . c  om*/
 * @param namespaceAware specifies whether the parser produced 
 * by this code will provide support for XML namespaces
 * @param validating specifies whether the parser produced by 
 * this code will validate documents against their DTD
 * @param xIncludeAware specifies whether the parser produced by 
 * this code will process XIncludes
 * @return newly created SAXParser
 * @exception ParserConfigurationException if a parser cannot be created 
 * which satisfies the requested configuration
 * @exception SAXException for SAX errors
 */
public static SAXParser newSAXParser(boolean namespaceAware, boolean validating, boolean xIncludeAware)
        throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(namespaceAware);
    factory.setValidating(validating);
    factory.setXIncludeAware(xIncludeAware);

    // For Xerces which otherwise, does not support "x-MacRoman".
    try {
        factory.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
    } catch (Exception ignored) {
    }

    return factory.newSAXParser();
}

From source file:ee.ria.xroad.common.message.SaxSoapParserImpl.java

@SneakyThrows
private static SAXParserFactory createSaxParserFactory() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);/*from w  w  w.j  av a 2  s  .  c  om*/
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    // disable external entity parsing to avoid DOS attacks
    factory.setValidating(false);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    return factory;
}

From source file:de.uzk.hki.da.convert.PublishXSLTConversionStrategy.java

/**
 * Creates the xml source./*from  w w w.j a  v a 2s. c o m*/
 *
 * @param file the file
 * @return the source
 */
private Source createXMLSource(File file) {
    // disable validation in order to prevent url resolution of DTDs etc.
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    spf.setValidating(false);
    try {
        spf.setFeature("http://xml.org/sax/features/validation", false);
        spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    } catch (Exception e) {
        logger.warn(e.getMessage());
    }
    XMLReader reader;
    try {
        reader = spf.newSAXParser().getXMLReader();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to create SAXParser", e);
    }

    return new SAXSource(reader, new InputSource(file.getAbsolutePath()));
}

From source file:com.dgwave.osrs.OsrsClient.java

private void initJaxb() throws OsrsException {
    if (jc != null && oj != null)
        return;//ww  w .j a va2  s . c o  m
    try {
        this.jc = JAXBContext.newInstance("com.dgwave.osrs.jaxb");
        this.oj = new ObjectFactory();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        spf.setNamespaceAware(true);
        spf.setValidating(false);
        xmlReader = spf.newSAXParser().getXMLReader();
        xmlReader.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                logger.debug("Ignoring DTD");
                return new InputSource(new StringReader(""));
            }
        });
    } catch (Exception e) {
        throw new OsrsException("JAXB Error", e);
    }
}

From source file:net.sf.jasperreports.engine.xml.BaseSaxParserFactory.java

protected SAXParserFactory createSAXParserFactory() throws ParserConfigurationException, SAXException {
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();

    if (log.isDebugEnabled()) {
        log.debug("Instantiated SAX parser factory of type " + parserFactory.getClass().getName());
    }//from  w  w w.j  a  v a2s. co  m

    parserFactory.setNamespaceAware(true);

    boolean validating = isValidating();
    parserFactory.setValidating(validating);
    parserFactory.setFeature("http://xml.org/sax/features/validation", validating);
    return parserFactory;
}