Example usage for org.xml.sax InputSource InputSource

List of usage examples for org.xml.sax InputSource InputSource

Introduction

In this page you can find the example usage for org.xml.sax InputSource InputSource.

Prototype

public InputSource(Reader characterStream) 

Source Link

Document

Create a new input source with a character stream.

Usage

From source file:Main.java

/**
 * Filters an XML document./* www.j a v  a  2s.c  o m*/
 * 
 * @param source the input source for the XML document
 * @param filter the filter
 * 
 * @return an input source for the resulting document
 * 
 * @throws Exception
 */
public static InputSource filterXml(InputSource source, XMLFilter filter) throws Exception {
    // Create filter, which uses a "real" XMLReader but also removes the
    // attributes
    XMLReader reader = XMLReaderFactory.createXMLReader();
    filter.setParent(reader);

    // Create a Transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    // Transform the source tree, applying the filter, and store the result
    // tree in a character array
    CharArrayWriter writer = new CharArrayWriter();
    t.transform(new SAXSource(filter, source), new StreamResult(writer));

    // Return a new InputSource using the result tree
    return new InputSource(new CharArrayReader(writer.toCharArray()));
}

From source file:Main.java

public static Document createDocument(String xml) throws IOException, SAXException {
    return createDocument(new InputSource(new StringReader(xml)));
}

From source file:de.nava.informa.parsers.OPMLParser.java

/**
 * Reads in a news feed definition from the specified URL.
 *
 * @return A collection of <code>FeedIF</code> objects.
 *///w w w. j  a  v a  2s. co  m
public static Collection parse(String url) throws IOException, ParseException {
    URL aURL = null;
    try {
        aURL = new URL(url);
    } catch (java.net.MalformedURLException e) {
        logger.warn("Could not create URL for " + url);
    }
    return parse(new InputSource(url), aURL);
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

/**
 * @param nifiTemplate the nifi template xml string
 * @return the name of the template/*from   w w  w  .j a  v a  2  s .c  o  m*/
 */
public static String getTemplateName(String nifiTemplate)
        throws ParserConfigurationException, XPathExpressionException, IOException, SAXException {

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    InputSource source = new InputSource(new StringReader(nifiTemplate));
    String name = (String) xpath.evaluate("/template/name", source, XPathConstants.STRING);
    return name;
}

From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlResultParser.java

static Object parse(InputStream in) throws IOException {
    try {/*from  ww  w . j  av a 2 s .  c om*/
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
        xmlReader.setContentHandler(sparqlsResultsHandler);
        xmlReader.parse(new InputSource(in));
        return sparqlsResultsHandler.getResults();
    } catch (ParserConfigurationException | SAXException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.castor.jaxb.sourcegeneration.SourceGeneratorTest.java

@Test
public void testSourceGeneration() throws Exception {
    InputSource inputSource = new InputSource(
            getClass().getClassLoader().getResource("test.xsd").toExternalForm());

    Jaxb2SourceGenerator jaxb2SourceGenerator = new Jaxb2SourceGenerator();
    jaxb2SourceGenerator.setSuppressNonFatalWarnings(true);
    //        jaxb2SourceGenerator.set
    jaxb2SourceGenerator.setDestDir("target/test-classes");
    jaxb2SourceGenerator.generateSource(inputSource, "org.castor.jaxb.sourcegeneration.generated");

}

From source file:com.janoz.tvapilib.support.XmlParsingObject.java

protected void parse(AbstractSaxParser parser, InputStream inputStream) {
    try {/*from w w w  .  j a va2  s. c o m*/
        InputSource input = new InputSource(inputStream);
        input.setPublicId("");
        input.setSystemId("");
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(parser);
        reader.parse(input);
    } catch (SAXException e) {
        LOG.info("Error parsing XML data.", e);
        throw new TvApiException(e.getMessage(), e);
    } catch (IOException e) {
        LOG.info("IO error while parsing XML data.", e);
        throw new TvApiException("IO error while parsing XML data.", e);
    }
}

From source file:com.taobao.datasource.LoginConfigParser.java

@SuppressWarnings("unchecked")
public static Map<String, SecureIdentityLoginModule> parse(File file) throws DocumentException {
    SAXReader reader = new SAXReader();
    // Prevent from resolving dtd files
    reader.setEntityResolver(new EntityResolver() {
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(
                    new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
        }//from   www . j a  v a  2  s .c  om
    });
    Document document = reader.read(file);
    List<Node> nodes = applicationPolicyPath.selectNodes(document);

    Map<String, SecureIdentityLoginModule> result = new HashMap<String, SecureIdentityLoginModule>();
    for (Node node : nodes) {
        createSecureIdentityLoginModule(result, node);
    }
    return result;
}

From source file:Main.java

/**
 * Method to create a document based on xml string.
 * //from  www. j  a  v a2s.  c  om
 * @param content (xml)
 * @return Document file for given xml content
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document createDocumentString(String content)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setNamespaceAware(true);
    Document doc = fac.newDocumentBuilder().parse(new InputSource(new StringReader(content)));
    return doc;
}

From source file:eu.optimis.mi.gui.server.XmlUtil.java

@SuppressWarnings("unused") //FIXME Remove?
private static Document getDocument(String xml) {
    try {/*from w w w .  ja v  a  2 s.com*/
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}