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

/**
 * 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  a  2 s.c o  m
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

/**
 * Metodo para carregar uma string contendo um arquivo xml assinado.
 * //from   www . j  a va 2 s .co m
 * @param xmlString -> XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(String xmlString)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xmlString)));
    return doc;
}

From source file:Main.java

public static Document toXMLDocument(String xmlString) {
    return toXMLDocument(new InputSource(new StringReader(xmlString)));
}

From source file:Main.java

/**
 * Example for retrieving the APAS institutions list
 *
 * @param xml the string representation of the XML
 *
 * @return the Document object created from the XML string representation
 *
 * @throws IOException                  if an I/O error occurs
 * @throws SAXException                 if an XML parsing exception occurs.
 * @throws ParserConfigurationException if a JAXP configuration error
 *                                      occurs.
 *///from www  .  ja  v a2 s  .  co  m
public static Document stringToDocument(final String xml)
        throws SAXException, IOException, ParserConfigurationException {
    return loadXMLFrom(new InputSource(new StringReader(xml)));
}

From source file:Main.java

public static Document getDocument(String payload)
        throws ParserConfigurationException, SAXException, IOException {
    if (payload == null || payload.length() < 1)
        return null;

    StringReader sr = new StringReader(payload);
    InputSource source = new InputSource(sr);

    return getDocument(source, null);
}

From source file:Main.java

private static DocumentBuilder getBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   ww w.  j  a v  a  2 s. co m
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    // prevent DTD entities from being resolved.
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });

    return builder;
}

From source file:Main.java

/** Forms an <code>InputSource</code> for parsing XML documents
 *  from a file. Returns//from   w  w  w. java2 s .  c o  m
 *  <code>null</code> if any of the exceptions is thrown.
 *
 * @param fileName The name of the file.
 * @param enc Character encoding to use.
 * @return An <code>InputSource</code> representation.
 */
public static InputSource getInputSource(String fileName, String enc) {
    InputSource retVal = null;
    try {
        retVal = new InputSource(new BufferedReader(new InputStreamReader(new FileInputStream(fileName), enc)));
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
    return retVal;
}

From source file:Main.java

/**
 * /*from w  ww.jav a  2  s.  c  o  m*/
 * @param xml
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document loadXMLFromString(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

public static Document loadXML(Reader r) {
    try {/*from w w  w . ja  v  a 2  s . c o m*/
        if (r != null)
            return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
    } catch (SAXException | IOException | ParserConfigurationException ex) {
    }
    return null;
}

From source file:Main.java

/**
 *
 *
 * @param obj .../*from w w w .ja va  2s.  co  m*/
 *
 * @return ...
 *
 * @throws SAXException ...
 * @throws IOException ...
 * @throws ParserConfigurationException ...
 */
public static Document parseXml(Object obj) throws SAXException, IOException, ParserConfigurationException {
    if (domBuilderFactory == null) {
        domBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    }

    DocumentBuilder parser = domBuilderFactory.newDocumentBuilder();
    Document doc = null;

    if (obj instanceof String) {
        try {
            // first try to interpret string as URL
            new URL(obj.toString());

            doc = parser.parse(obj.toString());
        } catch (MalformedURLException nourl) {
            // if not a URL, maybe it is the XML itself
            doc = parser.parse(new InputSource(new StringReader(obj.toString())));
        }
    } else if (obj instanceof InputStream) {
        doc = parser.parse(new InputSource((InputStream) obj));
    } else if (obj instanceof Reader) {
        doc = parser.parse(new InputSource((Reader) obj));
    }

    doc.normalize();

    return doc;
}