List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringComments
public void setIgnoringComments(boolean ignoreComments)
From source file:org.apache.jxtadoop.conf.Configuration.java
private void loadResource(Properties properties, Object name, boolean quiet) { try {// w ww.j a va2 s. co m DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); //ignore all comments inside the xml file docBuilderFactory.setIgnoringComments(true); //allow includes in the xml file docBuilderFactory.setNamespaceAware(true); try { docBuilderFactory.setXIncludeAware(true); } catch (UnsupportedOperationException e) { LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e); } DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = null; Element root = null; if (name instanceof URL) { // an URL resource URL url = (URL) name; if (url != null) { if (!quiet) { LOG.info("parsing " + url); } doc = builder.parse(url.toString()); } } else if (name instanceof String) { // a CLASSPATH resource URL url = getResource((String) name); if (url != null) { if (!quiet) { LOG.info("parsing " + url); } doc = builder.parse(url.toString()); } } else if (name instanceof Path) { // a file resource // Can't use FileSystem API or we get an infinite loop // since FileSystem uses Configuration API. Use java.io.File instead. File file = new File(((Path) name).toUri().getPath()).getAbsoluteFile(); if (file.exists()) { if (!quiet) { LOG.info("parsing " + file); } InputStream in = new BufferedInputStream(new FileInputStream(file)); try { doc = builder.parse(in); } finally { in.close(); } } } else if (name instanceof InputStream) { try { doc = builder.parse((InputStream) name); } finally { ((InputStream) name).close(); } } else if (name instanceof Element) { root = (Element) name; } if (doc == null && root == null) { if (quiet) return; throw new RuntimeException(name + " not found"); } if (root == null) { root = doc.getDocumentElement(); } if (!"configuration".equals(root.getTagName())) LOG.fatal("bad conf file: top-level element not <configuration>"); NodeList props = root.getChildNodes(); for (int i = 0; i < props.getLength(); i++) { Node propNode = props.item(i); if (!(propNode instanceof Element)) continue; Element prop = (Element) propNode; if ("configuration".equals(prop.getTagName())) { loadResource(properties, prop, quiet); continue; } if (!"property".equals(prop.getTagName())) LOG.warn("bad conf file: element not <property>"); NodeList fields = prop.getChildNodes(); String attr = null; String value = null; boolean finalParameter = false; for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) continue; Element field = (Element) fieldNode; if ("name".equals(field.getTagName()) && field.hasChildNodes()) attr = ((Text) field.getFirstChild()).getData().trim(); if ("value".equals(field.getTagName()) && field.hasChildNodes()) value = ((Text) field.getFirstChild()).getData(); if ("final".equals(field.getTagName()) && field.hasChildNodes()) finalParameter = "true".equals(((Text) field.getFirstChild()).getData()); } // Ignore this parameter if it has already been marked as 'final' if (attr != null && value != null) { if (!finalParameters.contains(attr)) { properties.setProperty(attr, value); if (finalParameter) finalParameters.add(attr); } else { LOG.warn(name + ":a attempt to override final parameter: " + attr + "; Ignoring."); } } } } catch (IOException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (DOMException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (SAXException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (ParserConfigurationException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } }
From source file:org.apache.myfaces.shared_impl.webapp.webxml.WebXmlParser.java
public WebXml parse() { _webXml = new WebXml(); try {/*from www.jav a 2s . com*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); dbf.setIgnoringComments(true); dbf.setNamespaceAware(true); dbf.setValidating(false); // dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver(new _EntityResolver()); db.setErrorHandler(new MyFacesErrorHandler(log)); InputSource is = createContextInputSource(null, WEB_XML_PATH); if (is == null) { URL url = _context.getResource(WEB_XML_PATH); log.debug("No web-xml found at : " + (url == null ? " null " : url.toString())); return _webXml; } Document document = db.parse(is); Element webAppElem = document.getDocumentElement(); if (webAppElem == null || !webAppElem.getNodeName().equals("web-app")) { throw new FacesException("No valid web-app root element found!"); } readWebApp(webAppElem); return _webXml; } catch (Exception e) { log.fatal("Unable to parse web.xml", e); throw new FacesException(e); } }
From source file:org.apache.oozie.cli.OozieCLI.java
private Properties parse(InputStream is, Properties conf) throws IOException { try {/*from www .ja v a 2 s. c om*/ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); // support for includes in the xml file docBuilderFactory.setXIncludeAware(true); // ignore all comments inside the xml file docBuilderFactory.setIgnoringComments(true); docBuilderFactory.setExpandEntityReferences(false); docBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = builder.parse(is); return parseDocument(doc, conf); } catch (SAXException e) { throw new IOException(e); } catch (ParserConfigurationException e) { throw new IOException(e); } }
From source file:org.apache.rahas.impl.util.SAMLUtilsTest.java
private static boolean equals(String element1, String element2) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// ww w.j a v a 2 s. c om dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setIgnoringComments(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc1 = db.parse(new ByteArrayInputStream(element1.getBytes("UTF-8"))); doc1.normalizeDocument(); Document doc2 = db.parse(new ByteArrayInputStream(element1.getBytes("UTF-8"))); doc2.normalizeDocument(); return doc1.isEqualNode(doc2); }
From source file:org.apache.safe.service.Configuration.java
private void loadResource(Properties properties, Object name) { try {/*from w w w. j a va2 s .c o m*/ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); //ignore all comments inside the xml file docBuilderFactory.setIgnoringComments(true); //allow includes in the xml file docBuilderFactory.setNamespaceAware(true); try { docBuilderFactory.setXIncludeAware(true); } catch (UnsupportedOperationException e) { LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e); } DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = null; Element root = null; if (name instanceof URL) { // an URL resource URL url = (URL) name; if (url != null) { doc = builder.parse(url.toString()); } } else if (name instanceof String) { // a CLASSPATH resource URL url = getResource((String) name); if (url != null) { doc = builder.parse(url.toString()); } } else if (name instanceof InputStream) { try { doc = builder.parse((InputStream) name); } finally { ((InputStream) name).close(); } } else if (name instanceof Element) { root = (Element) name; } if (doc == null && root == null) { throw new RuntimeException(name + " not found"); } if (root == null) { root = doc.getDocumentElement(); } if (!"configuration".equals(root.getTagName())) LOG.fatal("bad conf file: top-level element not <configuration>"); NodeList props = root.getChildNodes(); for (int i = 0; i < props.getLength(); i++) { Node propNode = props.item(i); if (!(propNode instanceof Element)) continue; Element prop = (Element) propNode; if ("configuration".equals(prop.getTagName())) { loadResource(properties, prop); continue; } if (!"property".equals(prop.getTagName())) LOG.warn("bad conf file: element not <property>"); NodeList fields = prop.getChildNodes(); String attr = null; String value = null; boolean finalParameter = false; for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) continue; Element field = (Element) fieldNode; if ("name".equals(field.getTagName()) && field.hasChildNodes()) attr = ((Text) field.getFirstChild()).getData().trim(); if ("value".equals(field.getTagName()) && field.hasChildNodes()) value = ((Text) field.getFirstChild()).getData(); if ("final".equals(field.getTagName()) && field.hasChildNodes()) finalParameter = "true".equals(((Text) field.getFirstChild()).getData()); } // Ignore this parameter if it has already been marked as 'final' if (attr != null) { if (value != null) { properties.setProperty(attr, value); } } } } catch (IOException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (DOMException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (SAXException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (ParserConfigurationException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } }
From source file:org.apache.safe.service.FileBasedAuthorizationService.java
void loadResource(InputStream input) { try {//from w w w. j av a 2 s. c o m DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); //ignore all comments inside the xml file docBuilderFactory.setIgnoringComments(true); //allow includes in the xml file docBuilderFactory.setNamespaceAware(true); try { docBuilderFactory.setXIncludeAware(true); } catch (UnsupportedOperationException e) { LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e); } DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = null; Element root = null; try { doc = builder.parse((InputStream) input); } finally { ((InputStream) input).close(); } if (doc == null && root == null) { throw new RuntimeException(input + " not found"); } if (root == null) { root = doc.getDocumentElement(); } if (!"acls".equals(root.getTagName())) LOG.fatal("bad conf file: top-level element not <acls>"); NodeList props = root.getChildNodes(); for (int i = 0; i < props.getLength(); i++) { Node propNode = props.item(i); if (!(propNode instanceof Element)) continue; Element prop = (Element) propNode; if (!"acl".equals(prop.getTagName())) LOG.warn("bad conf file: element not <acl>"); NodeList fields = prop.getChildNodes(); String key = null; String users = null; String groups = null; for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) continue; Element field = (Element) fieldNode; if ("key".equals(field.getTagName()) && field.hasChildNodes()) key = ((Text) field.getFirstChild()).getData().trim(); if ("users".equals(field.getTagName()) && field.hasChildNodes()) users = ((Text) field.getFirstChild()).getData().trim(); if ("groups".equals(field.getTagName()) && field.hasChildNodes()) groups = ((Text) field.getFirstChild()).getData(); if (key != null) { Set<String> userSet = null; Set<String> groupSet = null; if (users != null && !users.isEmpty()) { userSet = new HashSet<String>(Arrays.asList(users.split(","))); } if (groups != null && !groups.isEmpty()) { groupSet = new HashSet<String>(Arrays.asList(groups.split(","))); } ACL acl = new ACL(userSet, groupSet); acls.put(key, acl); } } } } catch (IOException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (DOMException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (SAXException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } catch (ParserConfigurationException e) { LOG.fatal("error parsing conf file: " + e); throw new RuntimeException(e); } }
From source file:org.apache.servicemix.jbi.deployer.utils.ManagementSupport.java
private static Document parse(String result) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from www . ja v a 2 s.c om factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(result))); }
From source file:org.apache.sling.urlrewriter.internal.SlingUrlRewriteFilter.java
private Document createDocument(final String rules) { if (StringUtils.isBlank(rules)) { logger.warn("given rules are blank"); return null; }/*w w w. jav a2 s . c om*/ final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); try { final String systemId = ""; final ConfHandler confHandler = new ConfHandler(systemId); final DocumentBuilder documentBuilder = factory.newDocumentBuilder(); documentBuilder.setErrorHandler(confHandler); documentBuilder.setEntityResolver(confHandler); final InputStream inputStream = new ByteArrayInputStream(rules.getBytes("UTF-8")); final Document document = documentBuilder.parse(inputStream); // , systemId); IOUtils.closeQuietly(inputStream); return document; } catch (Exception e) { logger.error("error creating document from rules property", e); return null; } }
From source file:org.apache.sysml.conf.DMLConfig.java
/** * Method to parse configuration/*from w ww . j a va 2s .co m*/ * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ private void parseConfig() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); //ignore XML comments DocumentBuilder builder = factory.newDocumentBuilder(); Document domTree = null; if (_fileName.startsWith("hdfs:") || _fileName.startsWith("gpfs:")) // config file from DFS { if (!LocalFileUtils.validateExternalFilename(_fileName, true)) throw new IOException("Invalid (non-trustworthy) hdfs config filename."); FileSystem DFS = FileSystem.get(ConfigurationManager.getCachedJobConf()); Path configFilePath = new Path(_fileName); domTree = builder.parse(DFS.open(configFilePath)); } else // config from local file system { if (!LocalFileUtils.validateExternalFilename(_fileName, false)) throw new IOException("Invalid (non-trustworthy) local config filename."); domTree = builder.parse(_fileName); } _xmlRoot = domTree.getDocumentElement(); }
From source file:org.apache.sysml.conf.DMLConfig.java
/** * Method to update the key value/*from w w w . j av a 2s . c om*/ * @param paramName parameter name * @param paramValue parameter value * @throws DMLRuntimeException if DMLRuntimeException occurs */ public void setTextValue(String paramName, String paramValue) throws DMLRuntimeException { if (_xmlRoot != null) DMLConfig.setTextValue(_xmlRoot, paramName, paramValue); else { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); //ignore XML comments DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); String configString = "<root><" + paramName + ">" + paramValue + "</" + paramName + "></root>"; Document domTree = builder.parse(new ByteArrayInputStream(configString.getBytes("UTF-8"))); _xmlRoot = domTree.getDocumentElement(); } catch (Exception e) { throw new DMLRuntimeException("Unable to set config value", e); } } }