use of javax.xml.parsers.DocumentBuilderFactory in project hbase by apache.
the class TestConfServlet method testWriteXml.
@Test
public void testWriteXml() throws Exception {
StringWriter sw = new StringWriter();
ConfServlet.writeResponse(getTestConf(), sw, "xml");
String xml = sw.toString();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
NodeList nameNodes = doc.getElementsByTagName("name");
boolean foundSetting = false;
for (int i = 0; i < nameNodes.getLength(); i++) {
Node nameNode = nameNodes.item(i);
String key = nameNode.getTextContent();
System.err.println("xml key: " + key);
if (TEST_KEY.equals(key)) {
foundSetting = true;
Element propertyElem = (Element) nameNode.getParentNode();
String val = propertyElem.getElementsByTagName("value").item(0).getTextContent();
assertEquals(TEST_VAL, val);
}
}
assertTrue(foundSetting);
}
use of javax.xml.parsers.DocumentBuilderFactory in project hive by apache.
the class GenHiveTemplate method generateTemplate.
private Document generateTemplate() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.appendChild(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"configuration.xsl\""));
doc.appendChild(doc.createComment("\n" + " Licensed to the Apache Software Foundation (ASF) under one or more\n" + " contributor license agreements. See the NOTICE file distributed with\n" + " this work for additional information regarding copyright ownership.\n" + " The ASF licenses this file to You under the Apache License, Version 2.0\n" + " (the \"License\"); you may not use this file except in compliance with\n" + " the License. You may obtain a copy of the License at\n" + "\n" + " http://www.apache.org/licenses/LICENSE-2.0\n" + "\n" + " Unless required by applicable law or agreed to in writing, software\n" + " distributed under the License is distributed on an \"AS IS\" BASIS,\n" + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + " See the License for the specific language governing permissions and\n" + " limitations under the License.\n"));
Element root = doc.createElement("configuration");
doc.appendChild(root);
root.appendChild(doc.createComment(" WARNING!!! This file is auto generated for documentation purposes ONLY! "));
root.appendChild(doc.createComment(" WARNING!!! Any changes you make to this file will be ignored by Hive. "));
root.appendChild(doc.createComment(" WARNING!!! You must make your changes in hive-site.xml instead. "));
root.appendChild(doc.createComment(" Hive Execution Parameters "));
Thread.currentThread().setContextClassLoader(ShimLoader.class.getClassLoader());
for (HiveConf.ConfVars confVars : HiveConf.ConfVars.values()) {
if (confVars.isExcluded()) {
// thought of creating template for each shims, but I couldn't generate proper mvn script
continue;
}
Element property = appendElement(root, "property", null);
appendElement(property, "name", confVars.varname);
appendElement(property, "value", confVars.getDefaultExpr());
appendElement(property, "description", normalize(confVars.getDescription()));
// wish to add new line here.
}
return doc;
}
use of javax.xml.parsers.DocumentBuilderFactory in project tomcat by apache.
the class WebdavStatus method getDocumentBuilder.
// ------------------------------------------------------ Protected Methods
/**
* Return JAXP document builder instance.
* @return the document builder
* @throws ServletException document builder creation failed
* (wrapped <code>ParserConfigurationException</code> exception)
*/
protected DocumentBuilder getDocumentBuilder() throws ServletException {
DocumentBuilder documentBuilder = null;
DocumentBuilderFactory documentBuilderFactory = null;
try {
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setExpandEntityReferences(false);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.setEntityResolver(new WebdavResolver(this.getServletContext()));
} catch (ParserConfigurationException e) {
throw new ServletException(sm.getString("webdavservlet.jaxpfailed"));
}
return documentBuilder;
}
use of javax.xml.parsers.DocumentBuilderFactory in project checkstyle by checkstyle.
the class CheckUtil method getCheckStyleModulesReferencedInConfig.
/**
* Gets a set of names of checkstyle's checks which are referenced in checkstyle_checks.xml.
*
* @param configFilePath
* file path of checkstyle_checks.xml.
* @return names of checkstyle's checks which are referenced in checkstyle_checks.xml.
*/
private static Set<String> getCheckStyleModulesReferencedInConfig(String configFilePath) {
try {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Validations of XML file make parsing too slow, that is why we
// disable all validations.
factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(new File(configFilePath));
// optional, but recommended
// FYI:
// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-
// how-does-it-work
document.getDocumentElement().normalize();
final NodeList nodeList = document.getElementsByTagName("module");
final Set<String> checksReferencedInCheckstyleChecksXml = new HashSet<>();
for (int i = 0; i < nodeList.getLength(); i++) {
final Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
final Element module = (Element) currentNode;
final String checkName = module.getAttribute("name");
checksReferencedInCheckstyleChecksXml.add(checkName);
}
}
return checksReferencedInCheckstyleChecksXml;
} catch (Exception exception) {
throw new IllegalStateException(exception);
}
}
use of javax.xml.parsers.DocumentBuilderFactory in project checkstyle by checkstyle.
the class XmlUtil method getRawXml.
public static Document getRawXml(String fileName, String code, String unserializedSource) throws ParserConfigurationException {
try {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
final DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(code)));
} catch (IOException | SAXException ex) {
Assert.fail(fileName + " has invalid xml (" + ex.getMessage() + "): " + unserializedSource);
}
return null;
}
Aggregations