Example usage for org.xml.sax.helpers XMLReaderFactory createXMLReader

List of usage examples for org.xml.sax.helpers XMLReaderFactory createXMLReader

Introduction

In this page you can find the example usage for org.xml.sax.helpers XMLReaderFactory createXMLReader.

Prototype

public static XMLReader createXMLReader() throws SAXException 

Source Link

Document

Obtains a new instance of a org.xml.sax.XMLReader .

Usage

From source file:net.i2cat.netconf.transport.VirtualTransport.java

public VirtualTransport() {
    listeners = new Vector<TransportListener>();

    xmlHandler = new TransportContentParser();

    // populate// w  ww  . j av a  2s .c  o  m
    try {
        parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(xmlHandler);
        parser.setErrorHandler(xmlHandler);
    } catch (SAXException e) {
        e.printStackTrace();
    }
}

From source file:net.sourceforge.vulcan.spring.SpringProjectDomBuilder.java

@Override
protected Transformer createTransformer(String format) throws NoSuchTransformFormatException {
    if (!transformResources.containsKey(format)) {
        throw new NoSuchTransformFormatException();
    }//w  w w.  j  a  va  2 s.  co m

    final Resource resource = applicationContext.getResource(transformResources.get(format));

    try {
        final SAXSource source = new SAXSource(XMLReaderFactory.createXMLReader(),
                new InputSource(resource.getInputStream()));

        try {
            // Try to tell the xsl where it is for the sake of xsl:include
            source.setSystemId(resource.getFile().getAbsolutePath());
        } catch (FileNotFoundException ignore) {
            // Not all resources are backed by files.
        }

        return transformerFactory.newTransformer(source);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RuntimeException(e);
    }

}

From source file:com.mirth.connect.plugins.datatypes.edi.EDISerializer.java

@Override
public String fromXML(String source) throws MessageSerializerException {
    XMLReader xr;/*from   w w w.  ja va2s  .  c om*/
    try {
        xr = XMLReaderFactory.createXMLReader();
    } catch (SAXException e) {
        throw new MessageSerializerException("Error converting XML to EDI", e, ErrorMessageBuilder
                .buildErrorMessage(this.getClass().getSimpleName(), "Error converting XML to EDI", e));
    }
    EDIXMLHandler handler = new EDIXMLHandler();
    xr.setContentHandler(handler);
    xr.setErrorHandler(handler);
    try {
        //Parse, but first replace all spaces between brackets. This fixes pretty-printed XML we might receive
        xr.parse(new InputSource(new StringReader(
                prettyPattern2.matcher(prettyPattern1.matcher(source).replaceAll("<$1>")).replaceAll("<$1>"))));
    } catch (Exception e) {
        throw new MessageSerializerException("Error converting XML to EDI", e, ErrorMessageBuilder
                .buildErrorMessage(this.getClass().getSimpleName(), "Error converting XML to EDI", e));
    }
    return handler.getOutput().toString();
}

From source file:net.sf.janos.model.xml.ResultParser.java

public static TrackMetaData parseTrackMetaData(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    TrackMetaDataHandler handler = new TrackMetaDataHandler();
    reader.setContentHandler(handler);//from   www.jav a2 s  .co  m
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse AV Transport Event: ", e);
    }
    return handler.getMetaData();
}

From source file:de.ks.idnadrev.expimp.xls.SingleSheetImport.java

@Override
public Void call() throws Exception {
    try {/*from w  w w . j  a v  a2  s.c  o  m*/
        XMLReader parser = XMLReaderFactory.createXMLReader();
        ImportSheetHandler importSheetHandler = new ImportSheetHandler(clazz, reader.getSharedStringsTable(),
                columnProvider, this::importEntity);
        parser.setContentHandler(importSheetHandler);

        InputSource inputSource = new InputSource(sheetStream);
        parser.parse(inputSource);

    } catch (SAXException | IOException | InvalidFormatException e) {
        result.generalError("Failed to parse sheet " + clazz.getName(), e);
        throw new RuntimeException(e);
    } finally {
        try {
            sheetStream.close();
        } catch (IOException e) {
            result.generalError("Could not close sheet stream " + clazz.getName(), e);
            throw new RuntimeException(e);
        }
        return null;
    }
}

From source file:XMLUtilities.java

/**
 * Convenience method for parsing an XML file. This method will
 * wrap the resource in an InputSource and set the source's
 * systemId to "jedit.jar" (so the source should be able to
 * handle any external entities by itself).
 *
 * <p>SAX Errors are caught and are not propagated to the caller;
 * instead, an error message is printed to jEdit's activity
 * log. So, if you need custom error handling, <b>do not use
 * this method</b>.//from www .j av a 2  s .  c  o  m
 *
 * <p>The given stream is closed before the method returns,
 * regardless whether there were errors or not.</p>
 *
 * @return true if any error occured during parsing, false if success.
 */
public static boolean parseXML(InputStream in, DefaultHandler handler) throws IOException {
    try {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        InputSource isrc = new InputSource(new BufferedInputStream(in));
        isrc.setSystemId("jedit.jar");
        parser.setContentHandler(handler);
        parser.setDTDHandler(handler);
        parser.setEntityResolver(handler);
        parser.setErrorHandler(handler);
        parser.parse(isrc);
    } catch (SAXParseException se) {
        int line = se.getLineNumber();
        return true;
    } catch (SAXException e) {
        return true;
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException io) {
        }
    }
    return false;
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.SOAPMessageValidatorInterceptor.java

private static Source getSOAPBody(InputStream stream) throws Exception {
    return new SAXSource(new SOAPXMLFilter(XMLReaderFactory.createXMLReader()), new InputSource(stream));
}

From source file:de.faustedition.tei.TeiValidator.java

public List<String> validate(FaustURI uri) throws SAXException, IOException {
    if (logger.isDebugEnabled()) {
        logger.debug("Validating via RelaxNG: " + uri);
    }/*from   w w  w .  j  a v  a  2 s  . co m*/
    final CustomErrorHandler errorHandler = new CustomErrorHandler();
    final Validator validator = schema
            .createValidator(errorHandler.configurationWithErrorHandler().toPropertyMap());

    final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setContentHandler(validator.getContentHandler());
    xmlReader.parse(xml.getInputSource(uri));

    return errorHandler.getErrors();
}

From source file:net.pandoragames.far.ui.MimeConfParser.java

/**
 * Read the mime type definition from xml.
 * @param input xml to be read/*  w ww . jav  a  2  s. co m*/
 * @throws SAXException
 * @throws IOException
 */
public void parse(InputStream input) throws SAXException, IOException {
    Reader reader = new InputStreamReader(input);
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    InputSource xmlInput = new InputSource(reader);
    xmlReader.setContentHandler(new SAXHandler());
    xmlReader.parse(xmlInput);
}

From source file:com.mirth.connect.model.converters.NCPDPSerializer.java

@Override
public String fromXML(String source) throws SerializerException {
    /*/*from   www.  j ava2  s  . c om*/
     * Need to determine the version by looking at the raw message.
     * The transaction header will contain the version ("51" for 5.1 and
     * "D0" for D.0)
     */
    String version = "D0";

    if (source.indexOf("D0") == -1) {
        version = "51";
    } else if (source.indexOf("51") == -1) {
        version = "D0";
    } else if (source.indexOf("51") < source.indexOf("D0")) {
        version = "51";
    }

    try {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        NCPDPXMLHandler handler = new NCPDPXMLHandler(segmentDelimeter, groupDelimeter, fieldDelimeter,
                version);
        reader.setContentHandler(handler);

        if (useStrictValidation) {
            reader.setFeature("http://xml.org/sax/features/validation", true);
            reader.setFeature("http://apache.org/xml/features/validation/schema", true);
            reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
            reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");
            reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
                    "ncpdp" + version + ".xsd");
            reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
                    "/ncpdp" + version + ".xsd");
        }

        /*
         * Parse, but first replace all spaces between brackets. This fixes
         * pretty-printed XML we might receive
         */
        reader.parse(new InputSource(new StringReader(source.replaceAll(">\\s+<", "><"))));
        return handler.getOutput().toString();
    } catch (Exception e) {
        throw new SerializerException("Error converting XML to NCPDP message.", e);
    }
}