use of javax.xml.parsers.DocumentBuilder in project OpenGrok by OpenGrok.
the class DirectoryListingTest method directoryListing.
/**
* Test directory listing
* @throws java.lang.Exception if an error occurs while generating the
* list.
*/
@Test
public void directoryListing() throws Exception {
StringWriter out = new StringWriter();
out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<start>\n");
DirectoryListing instance = new DirectoryListing();
instance.listTo("ctx", directory, out, directory.getPath(), Arrays.asList(directory.list()));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
assertNotNull("DocumentBuilderFactory is null", factory);
DocumentBuilder builder = factory.newDocumentBuilder();
assertNotNull("DocumentBuilder is null", builder);
out.append("</start>\n");
String str = out.toString();
System.out.println(str);
Document document = builder.parse(new ByteArrayInputStream(str.getBytes()));
NodeList nl = document.getElementsByTagName("tr");
int len = nl.getLength();
// add one extra for header and one for parent directory link
assertEquals(entries.length + 2, len);
// Skip the the header and parent link
for (int i = 2; i < len; ++i) {
validateEntry((Element) nl.item(i));
}
}
use of javax.xml.parsers.DocumentBuilder in project OpenMEAP by OpenMEAP.
the class XmlUtils method getDocument.
/**
* Convenience method to parse an input stream into a document
* @param inputStream
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public static Document getDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(inputStream);
}
use of javax.xml.parsers.DocumentBuilder in project atlas by alibaba.
the class Configuration method readXmlConfig.
/**
* read args from xml
**/
void readXmlConfig(File xmlConfigFile) throws IOException, ParserConfigurationException, SAXException {
if (!xmlConfigFile.exists()) {
return;
}
System.out.printf("reading config file, %s\n", xmlConfigFile.getAbsolutePath());
BufferedInputStream input = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
input = new BufferedInputStream(new FileInputStream(xmlConfigFile));
InputSource source = new InputSource(input);
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(source);
NodeList issues = document.getElementsByTagName(TAG_ISSUE);
for (int i = 0, count = issues.getLength(); i < count; i++) {
Node node = issues.item(i);
Element element = (Element) node;
String id = element.getAttribute(ATTR_ID);
if (id.length() == 0) {
System.err.println("Invalid config file: Missing required issue id attribute");
continue;
}
if (id.equals(PROPERTY_ISSUE)) {
readPropertyFromXml(node);
} else if (id.equals(DEX_ISSUE)) {
readDexPatternsFromXml(node);
} else if (id.equals(SO_ISSUE)) {
readLibPatternsFromXml(node);
} else if (id.equals(RES_ISSUE)) {
readResPatternsFromXml(node);
} else if (id.equals(PACKAGE_CONFIG_ISSUE)) {
readPackageConfigFromXml(node);
} else if (id.equals(SIGN_ISSUE)) {
if (mUseSignAPk) {
readSignFromXml(node);
}
} else {
System.err.println("unknown issue " + id);
}
}
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
System.exit(-1);
}
}
}
}
use of javax.xml.parsers.DocumentBuilder in project Openfire by igniterealtime.
the class UserSchemaValidator method validateAndParse.
/**
* Perform a validate and a parse of the specified source document.
* Validating is done with the specified schemafiles.
*
* @return A Document when the validation s successful.
*/
Document validateAndParse() {
ValidatorErrorHandler handler = new ValidatorErrorHandler();
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setXIncludeAware(true);
documentBuilderFactory.setValidating(false);
// We don't want xml:base and xml:lang attributes in the output.
documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-language", false);
if (schemaSources.length > 0) {
Log.info("Checking Schema's");
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setErrorHandler(handler);
Schema schema = schemaFactory.newSchema(schemaSources);
documentBuilderFactory.setSchema(schema);
Log.info("Start validating document");
} else {
Log.info("Loading document");
}
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
handler.reset();
// Communicate some info about the Xincludes in the imported file. These imports should be resolvable by the server.
documentBuilder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
Log.info(String.format("resolved Entity:%s %s", (publicId != null ? publicId : ""), systemId));
return null;
}
});
documentBuilder.setErrorHandler(handler);
Document result = documentBuilder.parse(source);
if (result != null && handler.isValid()) {
return result;
} else {
Log.warn(String.format("document is invalid. %1$d errors found.", handler.getNrOfErrors()));
return null;
}
} catch (Exception e) {
Log.warn(String.format("document validation failed. %1$d errors found.", handler.getNrOfErrors()));
Log.debug("", e);
return null;
}
}
use of javax.xml.parsers.DocumentBuilder in project Openfire by igniterealtime.
the class GatewayDWR method configure.
@Override
public void configure(ServletConfig servletConfig, Configuration configuration) throws ServletException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
document = builder.newDocument();
Element root = document.createElement("dwr");
document.appendChild(root);
Element allowElement = document.createElement("allow");
allowElement.appendChild(buildCreator("ConfigManager", "net.sf.kraken.web.ConfigManager"));
allowElement.appendChild(buildCreator("ConnectionTester", "net.sf.kraken.web.ConnectionTester"));
root.appendChild(allowElement);
} catch (ParserConfigurationException e) {
Log.error("Error configuring DWR for gateway plugin: ", e);
}
configuration.addConfig(document);
// Specify the path for the js files
Object bean = container.getBean("interface");
if (bean instanceof DefaultInterfaceProcessor) {
DefaultInterfaceProcessor processor = (DefaultInterfaceProcessor) bean;
processor.setOverridePath("/plugins/kraken/dwr");
}
}
Aggregations