Example usage for javax.xml.parsers SAXParserFactory newInstance

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

Introduction

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

Prototype


public static SAXParserFactory newInstance() 

Source Link

Document

Obtain a new instance of a SAXParserFactory .

Usage

From source file:Main.java

public static XMLReader createXmlReader() throws SAXException {
    try {/*ww  w  .j a  v  a2s . c om*/
        // use Xerces to ensure XML 1.1 is handled correctly
        Class<?> clazz = Class.forName("org.apache.xerces.parsers.SAXParser"); //$NON-NLS-1$
        return (XMLReader) clazz.newInstance();
    } catch (Throwable e) {
        SAXParser saxParser;
        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            saxParserFactory.setNamespaceAware(true);
            saxParser = saxParserFactory.newSAXParser();
        } catch (ParserConfigurationException e2) {
            throw new SAXException(e2);
        }
        return saxParser.getXMLReader();
    }
}

From source file:Main.java

/**
 * Create a new SAXParser which processes XML securely.
 *
 * @return a SAXParser/*  w w  w .  j ava 2s  . c om*/
 */
public static SAXParser createSaxParser() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
        spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        return spf.newSAXParser();
    } catch (ParserConfigurationException | SAXException e) {
        throw new IllegalStateException(e);
    }
}

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);//from   w  w w . ja  va 2 s .c om
        SAX_PARSER_FACTORY.set(spf);
    }
    return spf.newSAXParser().getXMLReader();
}

From source file:Main.java

/**
 * Convenience method: creates and returns a SAXParser.
 * //from   ww w. j a  v a2  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:Main.java

/**
 * Helper method to unmarshall a xml doc
 * @see http://docs.oracle.com/javase/tutorial/jaxb/intro/basic.html
 * http://jaxb.java.net/nonav/2.2.6/docs/ch03.html#unmarshalling
 * this uses <T> JAXBElement<T> unmarshal(Source source,
                    Class<T> declaredType)
                  throws JAXBException/*ww w  .  j  a v  a 2 s  .  c  o  m*/
 * 
 */
/*   public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException{
 String packageName = docClass.getPackage().getName();
 JAXBContext jc = JAXBContext.newInstance( packageName );
 Unmarshaller u = jc.createUnmarshaller();
 JAXBElement<T> root = u.unmarshal(new StreamSource(inputStream),docClass);
 return root.getValue();
 }*/

public static <T> T unmarshall(Class<T> docClass, InputStream inputStream)
        throws JAXBException, ParserConfigurationException, SAXException {
    String packageName = docClass.getPackage().getName();
    JAXBContext jc = JAXBContext.newInstance(packageName);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/validation/schema", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    InputSource inputSource = new InputSource(inputStream);
    SAXSource source = new SAXSource(xmlReader, inputSource);

    Unmarshaller u = jc.createUnmarshaller();
    JAXBElement<T> root = u.unmarshal(source, docClass);
    return root.getValue();
}

From source file:Main.java

/** Creates a namespace-aware {@link SAXParserFactory} */
private static SAXParserFactory createNamespaceAwareSAXParserFactory() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from  w w w  . j a  v a2  s  . c  o m*/
    return spf;
}

From source file:Main.java

/**
 * Get a SAX Parser Factory. This method implements a thread-relative
 * singleton./*  w w w. ja  va2  s. co m*/
 * 
 * NOTE: Need sax parser factory per thread for thread safety.
 * See: http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/SAXParserFactory.html
 * @return
 */
public static SAXParserFactory getSAXParserFactory() {
    SAXParserFactory threadLocalSAXParserFactory = SAX_FACTORY.get();
    if (null == threadLocalSAXParserFactory) {
        threadLocalSAXParserFactory = SAXParserFactory.newInstance();
        SAX_FACTORY.set(threadLocalSAXParserFactory);
    }
    return threadLocalSAXParserFactory;
}

From source file:Main.java

public static boolean validateWithDTDUsingSAX(String xml) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);/*from w  w w .j av a2s.  co  m*/
    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

/**
 * Returns a {@link SAXSource} for the provided {@link InputStream} that explicitly forfeit external DTD validation
 *
 * @param in the {@link InputStream} for the {@link SAXSource}
 * @return a {@link SAXSource} for the provided {@link InputStream} that explicitly forfeit external DTD validation
 * @throws SAXException                 if a SAX error occurs
 * @throws ParserConfigurationException if a configuration error occurs
 *//*from  w ww  .  j av a2 s .  com*/
public static Source toNonValidatingSAXSource(InputStream in)
        throws SAXException, ParserConfigurationException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    InputSource inputSource = new InputSource(in);
    return new SAXSource(xmlReader, inputSource);
}

From source file:Main.java

public void process() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*w  w  w.  ja va  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);
    }
    try {
        parser.parse(new InputSource(new StringReader(getXMLData())), this);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
}