Example usage for javax.xml.parsers SAXParserFactory setNamespaceAware

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

Introduction

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

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:Main.java

public static SAXParser getSAXParser() throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    return saxParser;
}

From source file:Main.java

public static boolean validateWithDTDUsingSAX(String xml) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//  w ww.j av  a  2s  .  com
    factory.setNamespaceAware(true);

    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    reader.parse(new InputSource(xml));
    return true;
}

From source file:Main.java

public static final XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
    final SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(false);/*ww w .  j a  va2  s  . c o m*/
    spf.setNamespaceAware(true);
    return spf.newSAXParser().getXMLReader();
}

From source file:Main.java

public static XMLReader xmlReader() throws ParserConfigurationException, SAXException {
    SAXParserFactory spf = SAX_PARSER_FACTORY.get();
    if (spf == null) {
        spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAX_PARSER_FACTORY.set(spf);/*from  w ww .ja  v a 2 s . co  m*/
    }
    return spf.newSAXParser().getXMLReader();
}

From source file:Main.java

/**
 * Constructs a validating secure SAX Parser.
 *
 * @param schemaStream the schema to validate the XML against
 * @return a SAX Parser//from w  w  w .jav  a  2  s  .c o m
 * @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:Main.java

/** Create a SAX parser from the JAXP factory.
 * The result can be used to parse XML files.
 * /*from   www .  jav  a 2 s. c  o  m*/
 * <p>See class Javadoc for hints on setting an entity resolver.
 * This parser has its entity resolver set to the system entity resolver chain.
 *
 * @param validate if true, a validating parser is returned
 * @param namespaceAware if true, a namespace aware parser is returned
 *
 * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
 * @throws SAXException if a parser fulfilling given parameters can not be created
 *
 * @return XMLReader configured according to passed parameters
 */
public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException {

    SAXParserFactory factory = SAXParserFactory.newInstance();

    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);

    try {
        return factory.newSAXParser().getXMLReader();
    } catch (ParserConfigurationException ex) {
        throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N                        
    }

}

From source file:Main.java

private static XMLReader makeXMLReader() throws ParserConfigurationException, SAXException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    final XMLReader reader = factory.newSAXParser().getXMLReader();
    return reader;
}

From source file:org.epics.archiverappliance.retrieval.channelarchiver.XMLRPCClient.java

/**
 * Internal method to make a XML_RPC post call and call the SAX handler on the returned document.
 * @param serverURL The Server URL//from   w  ww  . java 2s  .  co  m
 * @param handler  DefaultHandler 
 * @param postEntity StringEntity 
 * @throws IOException  &emsp; 
 * @throws SAXException  &emsp; 
 */
private static void doHTTPPostAndCallSAXHandler(String serverURL, DefaultHandler handler,
        StringEntity postEntity) throws IOException, SAXException {
    logger.debug("Executing doHTTPPostAndCallSAXHandler with the server URL " + serverURL);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost postMethod = new HttpPost(serverURL);
    postMethod.addHeader("Content-Type", "text/xml");
    postMethod.setEntity(postEntity);
    logger.debug("Executing the HTTP POST" + serverURL);
    HttpResponse response = httpclient.execute(postMethod);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        logger.debug("Obtained a HTTP entity of length " + entity.getContentLength());
        try (InputStream is = entity.getContent()) {
            SAXParserFactory sfac = SAXParserFactory.newInstance();
            sfac.setNamespaceAware(false);
            sfac.setValidating(false);
            SAXParser parser = sfac.newSAXParser();
            parser.parse(is, handler);
        } catch (ParserConfigurationException pex) {
            throw new IOException(pex);
        }
    } else {
        throw new IOException("HTTP response did not have an entity associated with it");
    }
}

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);
    spf.setValidating(false);/*w ww . j  ava 2s.com*/
    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:net.sf.jasperreports.renderers.util.XmlDataSniffer.java

public static XmlSniffResult sniffXml(byte[] data) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);//from ww w.j a v a2  s .co m
    factory.setNamespaceAware(false);
    factory.setXIncludeAware(false);
    setParserFeature(factory, FEATURE_EXTERNAL_GENERAL_ENTITIES, false);
    setParserFeature(factory, FEATURE_EXTERNAL_PARAMETER_ENTITIES, false);
    setParserFeature(factory, FEATURE_LOAD_EXTERNAL_DTD, false);

    SaxHandler handler = new SaxHandler();

    ByteArrayInputStream bais = new ByteArrayInputStream(data);

    try {
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(bais, handler);
        return new XmlSniffResult(handler.rootElementName);
    } catch (ValidXmlSAXException e) {
        return new XmlSniffResult(handler.rootElementName);
    } catch (SAXException | ParserConfigurationException | IOException e) {
        return null;
    }
}