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:net.sf.jasperreports.renderers.util.XmlDataSniffer.java

public static XmlSniffResult sniffXml(byte[] data) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(false);/*from   www .  ja v a 2  s .  co  m*/
    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;
    }
}

From source file:eu.annocultor.utils.XmlUtils.java

/**
 * // w w  w. j  a v a  2s  .  com
 * @param fileStream
 * @param handler
 * @param validating
 * @return <code>true</code> on success.
 * @throws Exception
 */
public static int parseXmlFileSAX(File sourceFile, ConverterHandler handler, boolean validating)
        throws Exception {
    // Create a builder factory
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);
    factory.setNamespaceAware(true);

    InputStream fileStream = new BufferedInputStream(new FileInputStream(sourceFile), 1024 * 1024);

    if (fileStream == null) {
        throw new Exception("Null input XML file stream");
    }
    if (handler == null) {
        throw new Exception("Null XML handler");
    }
    try {
        // Create the builder and parse the file
        SAXParser newSAXParser = factory.newSAXParser();
        if (newSAXParser == null) {
            throw new Exception("null SAX parser");
        }
        newSAXParser.parse(fileStream, handler);
    } catch (Exception e) {
        System.err.println("\n" + "*****************************************************\n"
                + "EXCEPTION OCCURRED in file " + sourceFile.getCanonicalPath() + "\n" + "at line "
                + handler.getDocumentLocator().getLineNumber() + ", column "
                + handler.getDocumentLocator().getColumnNumber());
        e.printStackTrace();
        System.err.println("\n" + "TRYING TO CLOSE FILES GRACEFULLY \n"
                + "*****************************************************\n");
        return -1;
    }
    return 0;
}

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//  w ww  . j a v  a 2s  .c o  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);/*from  www. j  a  v a  2  s .c  o 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:com.haulmont.bali.util.Dom4j.java

public static SAXParser getParser() {
    SAXParser parser = saxParserHolder.get();
    if (parser == null) {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(false);
        try {//from w  ww.j ava 2s  .  c  o m
            parser = factory.newSAXParser();
        } catch (ParserConfigurationException | SAXException e) {
            throw new RuntimeException("Unable to create SAX parser", e);
        }
        saxParserHolder.set(parser);
    }
    return parser;
}

From source file:bbcdataservice.BBCProgrammesParser.java

protected static boolean parse(HashMap<Date, MutableChannelDayProgram> dayPrograms, File file, Channel channel,
        Date date) throws Exception {
    mHasNextDay = false;/*  ww w.j a  v a 2s.  c  om*/
    SAXParserFactory fac = SAXParserFactory.newInstance();
    fac.setValidating(false);
    SAXParser sax = fac.newSAXParser();
    sax.parse(file, new BBCProgrammesParser(dayPrograms, channel, date));
    return mHasNextDay;
}

From source file:de.unisb.cs.st.javalanche.rhino.coverage.CoberturaParser.java

public static void parseXmlFile(File file, DefaultHandler handler, boolean validating) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);

    try {//from w w  w .  jav  a  2s .  c o  m
        factory.newSAXParser().parse(file, handler);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a SAX parser.//from ww  w .j  ava  2s.c om
 *
 * <p>
 * See {@link #parse} for hints on setting an entity resolver.
 *
 * @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 synchronized XMLReader createXMLReader(boolean validate, boolean namespaceAware)
        throws SAXException {
    SAXParserFactory factory = saxes[validate ? 0 : 1][namespaceAware ? 0 : 1];
    if (factory == null) {
        try {
            factory = SAXParserFactory.newInstance();
        } catch (FactoryConfigurationError err) {
            throw err;
        }
        factory.setValidating(validate);
        factory.setNamespaceAware(namespaceAware);
        saxes[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory;
    }

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

From source file:Main.java

public void process() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);//from w w w  . j  av  a2  s .c  o m
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");
    try {
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    try {
        parser.parse(new InputSource(new StringReader(getXMLData())), this);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
}

From source file:TrySAX.java

private void process(File file) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);//w w w. j  av  a 2 s  .  c o  m
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");
    try {
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    System.out.println("\nStarting parsing of " + file + "\n");
    try {
        parser.parse(file, this);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
}