Search in sources :

Example 96 with SAXParser

use of javax.xml.parsers.SAXParser in project tomee by apache.

the class JaxbSun method unmarshal.

public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) throws ParserConfigurationException, SAXException, JAXBException {
    // create a parser with validation disabled
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    // Get the JAXB context -- this should be cached
    final JAXBContext ctx = JAXBContextFactory.newInstance(type);
    // get the unmarshaller
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    // log errors?
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            if (logErrors) {
                System.out.println(validationEvent);
            }
            return false;
        }
    });
    // add our XMLFilter which disables dtd downloading
    final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    // Wrap the input stream with our filter
    final SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
    // unmarshal the document
    return unmarshaller.unmarshal(source);
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 97 with SAXParser

use of javax.xml.parsers.SAXParser in project tomee by apache.

the class JaxbWls method unmarshal.

public static <T> Object unmarshal(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    final JAXBContext ctx = JaxbWls.getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    final JaxbWls.NamespaceFilter xmlFilter = new JaxbWls.NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    JaxbWls.currentPublicId.set(new TreeSet<String>());
    try {
        return unmarshaller.unmarshal(source, type);
    } finally {
        JaxbWls.currentPublicId.set(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBContext(javax.xml.bind.JAXBContext) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 98 with SAXParser

use of javax.xml.parsers.SAXParser in project GeoGig by boundlessgeo.

the class XmlReader method run.

/**
     * Reads all data from the file and send it to the sink.
     */
public void run() {
    InputStream inputStream = this.file;
    try {
        SAXParser parser;
        sink.initialize(Collections.<String, Object>emptyMap());
        // make "-" an alias for /dev/stdin
        // if (file.getName().equals("-")) {
        // inputStream = System.in;
        // } else {
        // inputStream = new FileInputStream(file);
        // }
        inputStream = new CompressionActivator(compressionMethod).createCompressionInputStream(inputStream);
        parser = createParser();
        parser.parse(inputStream, new OsmHandler(sink, enableDateParsing));
        sink.complete();
    } catch (SAXParseException e) {
        throw new OsmosisRuntimeException("Unable to parse xml file " + file + ".  publicId=(" + e.getPublicId() + "), systemId=(" + e.getSystemId() + "), lineNumber=" + e.getLineNumber() + ", columnNumber=" + e.getColumnNumber() + ".", e);
    } catch (SAXException e) {
        throw new OsmosisRuntimeException("Unable to parse XML.", e);
    } catch (IOException e) {
        throw new OsmosisRuntimeException("Unable to read XML file " + file + ".", e);
    } finally {
        sink.release();
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.log(Level.SEVERE, "Unable to close input stream.", e);
            }
            inputStream = null;
        }
    }
}
Also used : InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) OsmHandler(org.openstreetmap.osmosis.xml.v0_6.impl.OsmHandler) IOException(java.io.IOException) CompressionActivator(org.openstreetmap.osmosis.xml.common.CompressionActivator) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException) SAXException(org.xml.sax.SAXException)

Example 99 with SAXParser

use of javax.xml.parsers.SAXParser in project Gargoyle by callakrsos.

the class SAXPasrerUtil method getAllQNames.

public static List<String> getAllQNames(InputStream is, SAXHandler defaultHandler) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    sp.parse(is, defaultHandler);
    return defaultHandler.getList();
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 100 with SAXParser

use of javax.xml.parsers.SAXParser in project Gargoyle by callakrsos.

the class SAXPasrerUtil method getAll.

public static <T> List<T> getAll(InputStream is, Handler<T> handler) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    sp.parse(is, handler);
    return handler.getList();
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9