Example usage for javax.xml.parsers SAXParserFactory setValidating

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

Introduction

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

Prototype


public void setValidating(boolean validating) 

Source Link

Document

Specifies that the parser produced by this code will validate documents as they are parsed.

Usage

From source file:org.walkmod.util.DomHelper.java

/**
 * Creates a W3C Document that remembers the location of each element in the
 * source file. The location of element nodes can then be retrieved using
 * the {@link #getLocationObject(Element)} method.
 *
 * @param inputSource/* ww  w .j  a  v a  2 s  . co  m*/
 *            the inputSource to read the document from
 * @param dtdMappings
 *            a map of DTD names and public ids
 * @return Document
 */
public static Document parse(InputSource inputSource, Map<String, String> dtdMappings) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating((dtdMappings != null));
    factory.setNamespaceAware(true);
    SAXParser parser = null;
    try {
        parser = factory.newSAXParser();
    } catch (Exception ex) {
        throw new WalkModException("Unable to create SAX parser", ex);
    }
    DOMBuilder builder = new DOMBuilder();
    ContentHandler locationHandler = new LocationAttributes.Pipe(builder);
    try {
        parser.parse(inputSource, new StartHandler(locationHandler, dtdMappings));
    } catch (Exception ex) {
        throw new WalkModException(ex);
    }
    return builder.getDocument();
}

From source file:org.wso2.carbon.sample.tfl.traffic.TrafficPollingTask.java

public void run() {
    try {//from w ww.  ja v a 2  s .  c o m
        URL obj = new URL(streamURL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        ArrayList<Disruption> disruptionsList = new ArrayList<Disruption>();
        try {
            // optional default is GET
            con.setRequestMethod("GET");
            int responseCode = con.getResponseCode();
            log.info("Sending 'GET' request to URL : " + streamURL);
            log.info("Response Code : " + responseCode);

            double t = System.currentTimeMillis();
            // Get SAX Parser Factory
            SAXParserFactory factory = SAXParserFactory.newInstance();
            // Turn on validation, and turn off namespaces
            factory.setValidating(true);
            factory.setNamespaceAware(false);
            SAXParser parser = factory.newSAXParser();
            parser.parse(con.getInputStream(), new TrafficXMLHandler(disruptionsList));
            log.info("Number of Disruptions added to the list: " + disruptionsList.size());
            log.info("Time taken for parsing: " + (System.currentTimeMillis() - t));
        } catch (ParserConfigurationException e) {
            log.info("The underlying parser does not support " + " the requested features.");
        } catch (FactoryConfigurationError e) {
            log.info("Error occurred obtaining SAX Parser Factory.");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.disconnect();
        }

        ArrayList<String> list = new ArrayList<String>();
        int count = 0;
        for (Disruption disruption : disruptionsList) {
            if (disruption.getState().contains("Active")) {
                list.add(disruption.toJson());
            }
            count++;
        }
        TflStream.writeToFile("tfl-traffic-data.out", list, true);
    } catch (IOException e) {
        log.error("Error occurred while getting traffic data: " + e.getMessage(), e);
    }

}

From source file:org.xulux.dataprovider.DictionaryHandler.java

/**
 * Read the dictionary from an inputstream
 * @param stream the stream with the dictionary xml
 *//*  w ww  . jav  a 2  s  . c om*/
public void read(InputStream stream) {
    SAXParser saxParser = null;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    try {
        saxParser = factory.newSAXParser();
        try {
            saxParser.parse(stream, this);
        } catch (SAXException se) {
            se.printStackTrace(System.err);
            se.getException().printStackTrace(System.err);
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
    // and clean up..
    saxParser = null;
    factory = null;
}

From source file:pt.iflow.api.licensing.FileBasedLicenseService.java

private void parseXMLSnapshot(byte[] xml) throws SAXException, IOException, ParserConfigurationException {
    DefaultHandler handler = new DefaultHandler() {
        LicenseEntry currOrg;/*from w w  w.ja  v  a  2 s  .c om*/

        public void startDocument() throws SAXException {
            licenseEntries.clear();
        }

        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if ("org".equals(qName)) {
                currOrg = new LicenseEntry();
                currOrg.available = Long.parseLong(attributes.getValue("available"));
                currOrg.consumed = Long.parseLong(attributes.getValue("consumed"));
                licenseEntries.put(attributes.getValue("id"), currOrg);
            } else if ("flow".equals(qName)) {
                currOrg.flowBased.put(new Integer(attributes.getValue("id")),
                        new Long(attributes.getValue("consumed")));
            }
        }

    };

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    SAXParser parser = factory.newSAXParser();
    parser.parse(new ByteArrayInputStream(xml), handler);

}

From source file:us.paulevans.basicxslt.Utils.java

/**
 * Method to determine if inputted xml file is valid and well-formed.
 * @param saXmlFile/*from   www  .  j a v a 2s  .  c om*/
 * @throws Exception If saXmlFile is invalid or not well-formed.
 */
public void isValidXml(FileContent faXmlFile, boolean aCheckWarning, boolean aCheckError,
        boolean aCheckFatalError) throws SAXNotSupportedException, SAXNotRecognizedException,
        ParserConfigurationException, SAXException, IOException {

    SAXParserFactory factory;

    checkWarning = aCheckWarning;
    checkError = aCheckError;
    checkFatalError = aCheckFatalError;
    factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);
    try {
        factory.setFeature(VALIDATION_FEATURE, true);
        factory.setFeature(SCHEMA_FEATURE, true);
        SAXParser parser = factory.newSAXParser();
        parser.parse(faXmlFile.getInputStream(), this);
    } catch (UnknownHostException aException) {
        // log and re-throw runtime exception...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw new UnknownHostException(stringFactory.getString(LabelStringFactory.ERRORS_NETWORK_CONNECT));
    } catch (SocketException aException) {
        // log and re-throw runtime exception...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw new SocketException(stringFactory.getString(LabelStringFactory.ERRORS_NETWORK_CONNECT));
    } catch (SAXNotSupportedException aException) {
        // log and re-throw...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw aException;
    } catch (SAXNotRecognizedException aException) {
        // log and re-throw...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw aException;
    } catch (ParserConfigurationException aException) {
        // log and re-throw...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw aException;
    } catch (SAXException aException) {
        // log and re-throw...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw aException;
    } catch (IOException aException) {
        // log and re-throw...
        logger.error(ExceptionUtils.getFullStackTrace(aException));
        throw aException;
    }
}