Search in sources :

Example 51 with InputSource

use of org.xml.sax.InputSource in project jslint4java by happygiraffe.

the class XmlResultFormatterTest method assertValid.

private void assertValid(File output) throws FileNotFoundException, SAXException {
    InputSource xml = new InputSource(new FileInputStream(output));
    URL dtdUrl = getClass().getClassLoader().getResource(DTD_RESOURCE);
    assertThat("resource " + DTD_RESOURCE + " exists", dtdUrl, notNullValue());
    // Specify a validator as the documents don't have <!DOCTYPE jslint> in them.
    Validator validator = new Validator(xml, dtdUrl.toString(), "jslint");
    assertThat(validator.toString(), validator.isValid(), is(true));
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Validator(org.custommonkey.xmlunit.Validator)

Example 52 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class ManualGenerator method grabBodyContent.

private String grabBodyContent() throws MalformedURLException, IOException {
    URL url = new URL(page);
    File file = new File(targetDir, ".manualCache-" + url.getFile().substring(1));
    try {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        XMLReader parser = new Parser();
        parser.setFeature(Parser.namespacesFeature, false);
        parser.setFeature(Parser.namespacePrefixesFeature, false);
        parser.setProperty(Parser.schemaProperty, new org.ccil.cowan.tagsoup.HTMLSchema() {

            {
                //problem with nested lists that the confluence {toc} macro creates
                elementType("ul", M_LI, M_BLOCK | M_LI, 0);
            }
        });
        StringWriter w = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(w) {

            int inDiv = Integer.MAX_VALUE;

            int count;

            public void characters(char[] ch, int start, int len) throws SAXException {
                if (inDiv <= count) {
                    super.characters(ch, start, len);
                }
            }

            public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
                count++;
                if ("div".equalsIgnoreCase(qName) && "wiki-content maincontent".equalsIgnoreCase(atts.getValue("class"))) {
                    inDiv = count;
                }
                if (inDiv <= count) {
                    super.startElement(uri, localName, qName, atts);
                }
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (inDiv <= count) {
                    super.endElement(uri, localName, qName);
                }
                count--;
                if (inDiv > count) {
                    inDiv = Integer.MAX_VALUE;
                }
            }
        };
        xmlWriter.setOutputProperty(XMLWriter.OMIT_XML_DECLARATION, "yes");
        xmlWriter.setOutputProperty(XMLWriter.METHOD, "html");
        parser.setContentHandler(xmlWriter);
        long date = con.getLastModified();
        parser.parse(new InputSource(new BufferedInputStream(con.getInputStream())));
        FileWriter writer = new FileWriter(file);
        writer.write(Long.toString(date));
        writer.close();
        return w.toString();
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException("Failed", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) FileWriter(java.io.FileWriter) Attributes(org.xml.sax.Attributes) XMLWriter(org.ccil.cowan.tagsoup.XMLWriter) URL(java.net.URL) Parser(org.ccil.cowan.tagsoup.Parser) HttpURLConnection(java.net.HttpURLConnection) StringWriter(java.io.StringWriter) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) XMLReader(org.xml.sax.XMLReader)

Example 53 with InputSource

use of org.xml.sax.InputSource in project buck by facebook.

the class XmlDomParserTest method testXmlDomParserClosesReader.

/**
   * Checks that when creating an {@link InputSource} from a {@link Reader} and passing that
   * through {@link XmlDomParser#parse(InputSource,boolean)}, it is closed before the method
   * returns.
   * @see <a href="http://fburl.com/8289364">DocumentBuilder.parse(InputStream)</a>
   * @throws IOException
   */
@Test
public void testXmlDomParserClosesReader() throws IOException, SAXException {
    StringReaderForCloseCheck reader = new StringReaderForCloseCheck("<?xml version='1.0'?> <a><b><c></c></b></a>");
    assertFalse(reader.isClosed());
    XmlDomParser.parse(new InputSource(reader), false);
    assertTrue(reader.isClosed());
}
Also used : InputSource(org.xml.sax.InputSource) Test(org.junit.Test)

Example 54 with InputSource

use of org.xml.sax.InputSource in project buck by facebook.

the class XmlUtils method parseDocument.

/**
     * Parses the given {@link Reader} as a DOM document, using the JDK parser. The parser does not
     * validate, and is optionally namespace aware.
     *
     * @param xml            a reader for the XML content to be parsed (must be well formed)
     * @param namespaceAware whether the parser is namespace aware
     * @return the DOM document
     */
@NonNull
public static Document parseDocument(@NonNull Reader xml, boolean namespaceAware) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    InputSource is = new InputSource(xml);
    factory.setNamespaceAware(namespaceAware);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(is);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NonNull(com.android.annotations.NonNull)

Example 55 with InputSource

use of org.xml.sax.InputSource in project twitterdroid by fbrunel.

the class TwitterResponse method parse.

public TwitterResponse parse(String xmlString) throws SAXException, IOException, ParseException, MalformedURLException {
    Document d = builder.parse(new InputSource(new StringReader(xmlString)));
    nodes = d.getElementsByTagName(TOP_LEVEL_NODE_NAME);
    readEntries();
    return this;
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Aggregations

InputSource (org.xml.sax.InputSource)1124 StringReader (java.io.StringReader)401 IOException (java.io.IOException)304 Document (org.w3c.dom.Document)282 SAXException (org.xml.sax.SAXException)281 DocumentBuilder (javax.xml.parsers.DocumentBuilder)262 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)213 XMLReader (org.xml.sax.XMLReader)194 Test (org.junit.Test)160 InputStream (java.io.InputStream)158 NodeList (org.w3c.dom.NodeList)145 Element (org.w3c.dom.Element)144 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)143 ByteArrayInputStream (java.io.ByteArrayInputStream)103 SAXParser (javax.xml.parsers.SAXParser)103 SAXSource (javax.xml.transform.sax.SAXSource)95 SAXParserFactory (javax.xml.parsers.SAXParserFactory)90 File (java.io.File)82 Node (org.w3c.dom.Node)82 URL (java.net.URL)65