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));
}
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);
}
}
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());
}
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);
}
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;
}
Aggregations