Example usage for org.xml.sax XMLReader setFeature

List of usage examples for org.xml.sax XMLReader setFeature

Introduction

In this page you can find the example usage for org.xml.sax XMLReader setFeature.

Prototype

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

Source Link

Document

Set the value of a feature flag.

Usage

From source file:org.smartfrog.projects.alpine.xmlutils.ParserHelper.java

/**
 * set a feature on a parser, log any failure but continue This helps us to
 * get past variants in xerces version on the classpath
 *
 * @param parser parser instance/*from w ww.  ja  v  a 2s.  co  m*/
 * @param name   feature name
 * @param flag   flag to set/reset
 */
public static void setFeature(XMLReader parser, String name, boolean flag) {
    try {
        parser.setFeature(name, flag);
    } catch (SAXNotRecognizedException ignored) {
        log.debug("SAXNotRecognizedException setting " + name);
    } catch (SAXNotSupportedException ignored) {
        log.debug("SAXNotSupportedException setting " + name);
    }
}

From source file:org.smartfrog.services.cddlm.cdl.XmlHelper.java

/**
 * set a feature on a parser, log any failure but continue This helps us get
 * past variants in xerces version on the old classpath
 *
 * @param xerces//from  w  w  w. ja v a2 s.co  m
 * @param name
 * @param validate
 */
private static void setFeature(XMLReader xerces, String name, boolean validate) {
    try {
        xerces.setFeature(name, validate);
    } catch (SAXNotRecognizedException e) {
        log.error("SAXNotRecognizedException seting " + name);
    } catch (SAXNotSupportedException e) {
        log.error("SAXNotSupportedException seting " + name);
    }
}

From source file:org.smartfrog.services.xml.test.system.XmlParserTest.java

public void testXercesHandlesSchema() throws SAXException {
    XMLReader xerces;
    xerces = createXerces();/*from w w w . j a  v  a2s.  c o m*/
    xerces.setFeature("http://apache.org/xml/features/validation/schema", true);
}

From source file:org.sonar.plugins.xml.parsers.DetectSchemaParser.java

/**
 * Find the Doctype (DTD or schema).//  www.j  ava 2  s .c  o  m
 */
public Doctype findDoctype(InputStream input) {
    Handler handler = new Handler();

    try {
        SAXParser parser = newSaxParser();
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX + "continue-after-fatal-error", true);
        xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        parser.parse(input, handler);
        return handler.doctype;
    } catch (IOException e) {
        throw new SonarException(e);
    } catch (StopParserException e) {
        return handler.doctype;
    } catch (SAXException e) {
        throw new SonarException(e);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@SuppressWarnings("deprecation") // on JDK 9
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
    if (logger.isDebugEnabled()) {
        logger.debug("Setting validation schema to "
                + StringUtils.arrayToCommaDelimitedString(this.schemaResources));
    }//w  w w .  j a v a  2s .  co  m
    Assert.notEmpty(resources, "No resources given");
    Assert.hasLength(schemaLanguage, "No schema language provided");
    Source[] schemaSources = new Source[resources.length];
    XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    for (int i = 0; i < resources.length; i++) {
        Assert.notNull(resources[i], "Resource is null");
        Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
        InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
        schemaSources[i] = new SAXSource(xmlReader, inputSource);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
    if (this.schemaResourceResolver != null) {
        schemaFactory.setResourceResolver(this.schemaResourceResolver);
    }
    return schemaFactory.newSchema(schemaSources);
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@SuppressWarnings("deprecation") // on JDK 9
private Source processSource(Source source) {
    if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
        return source;
    }/* ww  w.j a va2s . co m*/

    XMLReader xmlReader = null;
    InputSource inputSource = null;

    if (source instanceof SAXSource) {
        SAXSource saxSource = (SAXSource) source;
        xmlReader = saxSource.getXMLReader();
        inputSource = saxSource.getInputSource();
    } else if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        if (streamSource.getInputStream() != null) {
            inputSource = new InputSource(streamSource.getInputStream());
        } else if (streamSource.getReader() != null) {
            inputSource = new InputSource(streamSource.getReader());
        } else {
            inputSource = new InputSource(streamSource.getSystemId());
        }
    }

    try {
        if (xmlReader == null) {
            xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
        }
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        String name = "http://xml.org/sax/features/external-general-entities";
        xmlReader.setFeature(name, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        return new SAXSource(xmlReader, inputSource);
    } catch (SAXException ex) {
        logger.warn("Processing of external entities could not be disabled", ex);
        return source;
    }
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Create an {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}.
 * @return the XMLReader// w w w .j a v  a 2s  .c o  m
 * @throws SAXException if thrown by JAXP methods
 */
@SuppressWarnings("deprecation") // on JDK 9
protected XMLReader createXmlReader() throws SAXException {
    XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
    xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
    if (!isProcessExternalEntities()) {
        xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
    }
    return xmlReader;
}

From source file:org.springframework.ws.server.endpoint.interceptor.PayloadTransformingInterceptor.java

public void afterPropertiesSet() throws Exception {
    if (requestXslt == null && responseXslt == null) {
        throw new IllegalArgumentException("Setting either 'requestXslt' or 'responseXslt' is required");
    }//  ww w  . j  a  va2s.  c om
    TransformerFactory transformerFactory = getTransformerFactory();
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    if (requestXslt != null) {
        Assert.isTrue(requestXslt.exists(), "requestXslt \"" + requestXslt + "\" does not exit");
        if (logger.isInfoEnabled()) {
            logger.info("Transforming request using " + requestXslt);
        }
        Source requestSource = new ResourceSource(xmlReader, requestXslt);
        requestTemplates = transformerFactory.newTemplates(requestSource);
    }
    if (responseXslt != null) {
        Assert.isTrue(responseXslt.exists(), "responseXslt \"" + responseXslt + "\" does not exit");
        if (logger.isInfoEnabled()) {
            logger.info("Transforming response using " + responseXslt);
        }
        Source responseSource = new ResourceSource(xmlReader, responseXslt);
        responseTemplates = transformerFactory.newTemplates(responseSource);
    }
}

From source file:org.xwiki.portlet.view.HTMLStreamFilter.java

/**
 * {@inheritDoc}/*  w w w  .  j a v a 2s  . c  o m*/
 * 
 * @see StreamFilter#filter(Reader, Writer)
 */
public void filter(Reader reader, Writer writer) {
    try {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.cyberneko.html.parsers.SAXParser");
        xmlReader.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
        xmlReader.setProperty("http://cyberneko.org/html/properties/names/elems", "match");
        xmlReader.setProperty("http://cyberneko.org/html/properties/names/attrs", "no-change");

        XMLReader parent = xmlReader;
        for (XMLFilter filter : filters) {
            filter.setParent(parent);
            parent = filter;
        }

        XHTMLWriter xhtmlWriter = new XHTMLWriter(writer);
        xhtmlWriter.setParent(parent);
        xhtmlWriter.parse(new InputSource(reader));
    } catch (Exception e) {
        LOG.error("Failed to rewrite servlet HTML.", e);
    }
}

From source file:org.yawlfoundation.yawl.unmarshal.YawlXMLSpecificationValidator.java

/**
 * Sets the checker up for a run./*from w ww .  j a  v  a2s .  c  om*/
 * @param version the version of the schema
 * @return a reader configured to do the checking.
 * @throws SAXException
 */
private XMLReader setUpChecker(String version) throws SAXException {
    XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    if (YSpecification.isValidVersion(version)) {
        parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
                getSchemaLocation(version));
    } else {
        throw new RuntimeException("Version [" + version + "] is not valid version.");
    }
    parser.setContentHandler(this);
    parser.setErrorHandler(this);
    parser.setFeature("http://xml.org/sax/features/validation", true);
    parser.setFeature("http://apache.org/xml/features/validation/schema", true);
    parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
    return parser;
}