List of usage examples for javax.xml.parsers DocumentBuilderFactory setXIncludeAware
public void setXIncludeAware(final boolean state)
From source file:com.esri.geoportal.harvester.migration.MigrationDataBuilder.java
private Document strToDom(String strXml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); factory.setXIncludeAware(false); factory.setExpandEntityReferences(false); factory.setNamespaceAware(true);/* w ww.j a v a 2 s .c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(strXml))); }
From source file:nl.armatiek.xslweb.configuration.WebApp.java
public WebApp(File webAppDefinition) throws Exception { logger.info(String.format("Loading webapp definition \"%s\" ...", webAppDefinition.getAbsolutePath())); Context context = Context.getInstance(); this.definition = webAppDefinition; this.homeDir = webAppDefinition.getParentFile(); this.name = this.homeDir.getName(); this.configuration = new XSLWebConfiguration(this); this.processor = new Processor(this.configuration.getConfiguration()); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w w w .j a v a 2 s .c o m*/ dbf.setSchema(context.getWebAppSchema()); dbf.setXIncludeAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(this); String defXml = IOUtils.toString(new XmlStreamReader(webAppDefinition)); Properties vars = new Properties(System.getProperties()); vars.setProperty("webapp-dir", webAppDefinition.getParentFile().getAbsolutePath().replace('\\', '/')); String resolvedDefXml = XSLWebUtils.resolveProperties(defXml, vars); // Document webAppDoc = db.parse(webAppDefinition); InputSource src = new InputSource(new StringReader(resolvedDefXml)); src.setSystemId(webAppDefinition.getAbsolutePath()); Document webAppDoc = db.parse(src); XPath xpath = new XPathFactoryImpl().newXPath(); xpath.setNamespaceContext(XMLUtils.getNamespaceContext("webapp", Definitions.NAMESPACEURI_XSLWEB_WEBAPP)); Node docElem = webAppDoc.getDocumentElement(); this.title = (String) xpath.evaluate("webapp:title", docElem, XPathConstants.STRING); this.description = (String) xpath.evaluate("webapp:description", docElem, XPathConstants.STRING); String devModeValue = (String) xpath.evaluate("webapp:development-mode", docElem, XPathConstants.STRING); this.developmentMode = XMLUtils.getBooleanValue(devModeValue, false); String maxUploadSizeValue = (String) xpath.evaluate("webapp:max-upload-size", docElem, XPathConstants.STRING); this.maxUploadSize = XMLUtils.getIntegerValue(maxUploadSizeValue, 10); String waitForJobsAtCloseValue = (String) xpath.evaluate("webapp:wait-for-jobs-at-close", docElem, XPathConstants.STRING); this.waitForJobsAtClose = XMLUtils.getBooleanValue(waitForJobsAtCloseValue, true); NodeList resourceNodes = (NodeList) xpath.evaluate("webapp:resources/webapp:resource", docElem, XPathConstants.NODESET); for (int i = 0; i < resourceNodes.getLength(); i++) { resources.add(new Resource((Element) resourceNodes.item(i))); } NodeList paramNodes = (NodeList) xpath.evaluate("webapp:parameters/webapp:parameter", docElem, XPathConstants.NODESET); for (int i = 0; i < paramNodes.getLength(); i++) { parameters.add(new Parameter(processor, (Element) paramNodes.item(i))); } NodeList jobNodes = (NodeList) xpath.evaluate("webapp:jobs/webapp:job", docElem, XPathConstants.NODESET); if (jobNodes.getLength() > 0) { File quartzFile = new File(homeDir, Definitions.FILENAME_QUARTZ); quartzFile = (quartzFile.isFile()) ? quartzFile : new File(context.getHomeDir(), "config" + File.separatorChar + Definitions.FILENAME_QUARTZ); SchedulerFactory sf; if (quartzFile.isFile()) { logger.info(String.format("Initializing Quartz scheduler using properties file \"%s\" ...", quartzFile.getAbsolutePath())); Properties props = XSLWebUtils.readProperties(quartzFile); props.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, name); sf = new StdSchedulerFactory(props); } else { logger.info("Initializing Quartz scheduler ..."); sf = new StdSchedulerFactory(); } scheduler = sf.getScheduler(); for (int i = 0; i < jobNodes.getLength(); i++) { Element jobElem = (Element) jobNodes.item(i); String jobName = XMLUtils.getValueOfChildElementByLocalName(jobElem, "name"); String jobUri = XMLUtils.getValueOfChildElementByLocalName(jobElem, "uri"); String jobCron = XMLUtils.getValueOfChildElementByLocalName(jobElem, "cron"); boolean concurrent = XMLUtils .getBooleanValue(XMLUtils.getValueOfChildElementByLocalName(jobElem, "concurrent"), true); String jobId = "job_" + name + "_" + jobName; JobDetail job = newJob(concurrent ? XSLWebJob.class : NonConcurrentExecutionXSLWebJob.class) .withIdentity(jobId, name).usingJobData("webapp-path", getPath()) .usingJobData("uri", jobUri).build(); Trigger trigger = newTrigger().withIdentity("trigger_" + name + "_" + jobName, name) .withSchedule(cronSchedule(jobCron)).forJob(jobId, name).build(); logger.info(String.format("Scheduling job \"%s\" of webapp \"%s\" ...", jobName, name)); scheduler.scheduleJob(job, trigger); } } NodeList dataSourceNodes = (NodeList) xpath.evaluate("webapp:datasources/webapp:datasource", docElem, XPathConstants.NODESET); for (int i = 0; i < dataSourceNodes.getLength(); i++) { DataSource dataSource = new DataSource((Element) dataSourceNodes.item(i)); dataSources.put(dataSource.getName(), dataSource); } NodeList fopConfigNodes = (NodeList) xpath.evaluate("webapp:fop-configs/webapp:fop-config", docElem, XPathConstants.NODESET); for (int i = 0; i < fopConfigNodes.getLength(); i++) { Element fopConfig = (Element) fopConfigNodes.item(i); Element fopElement = XMLUtils.getFirstChildElement(fopConfig); fopConfigs.put(fopConfig.getAttribute("name"), XMLUtils.nodeToString(fopElement)); } // initClassLoader(); initFileAlterationObservers(); }
From source file:com.fluidops.iwb.provider.XMLProvider.java
/** * Constructs a w3c DOM from an InputStream. Depending on whether the InputStream is constructed from * an html or xml file, it will used JTidy for clean-up and constructing the document DOM. * @param in any input stream that contains (x)html/xml data * @return the DOM of an HTML or XML constructed from any InputStream * @throws SAXException/*from w ww. java2 s . com*/ * @throws IOException * @throws ParserConfigurationException */ protected Document getDocument(InputStream in) throws SAXException, IOException, ParserConfigurationException { Tidy tidy = new Tidy(); tidy.setHideComments(true); tidy.setTidyMark(false); tidy.setQuiet(true); tidy.setShowErrors(1); tidy.setShowWarnings(false); if (StringUtil.isNotNullNorEmpty(config.inputEncoding)) { tidy.setInputEncoding(config.inputEncoding); } tidy.setOutputEncoding("UTF-8"); tidy.setMakeClean(true); Document doc = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setXIncludeAware(false); factory.setExpandEntityReferences(false); DocumentBuilder db = factory.newDocumentBuilder(); // we assume that xml is provided, if no file extension .htm(l) is provided if (!config.xmlfile.toLowerCase().endsWith(".htm") && !config.xmlfile.toLowerCase().endsWith("html")) { // let jtidy know that it needs to handle xml, instead of html tidy.setXmlOut(true); tidy.setXmlTags(true); // need to have another (re)rewrite of the DOM in order to gain a clean w3c.dom ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream is = null; try { doc = tidy.parseDOM(in, outputStream); is = new ByteArrayInputStream(outputStream.toByteArray()); doc = db.parse(is); } finally { IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(is); } } else { doc = tidy.parseDOM(in, null); } return doc; }
From source file:com.idega.xroad.client.business.impl.XRoadServicesImpl.java
@Override public Document getPreffiledDocumentInXML(String applicationID, String taskID, User user, String language) { InputStream documentInputStream = getPreffiledDocument(applicationID, taskID, user, language); if (documentInputStream == null) { return null; }//from ww w .ja va 2 s . c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setXIncludeAware(true); DocumentBuilder documentBuilder; Document document = null; try { documentBuilder = factory.newDocumentBuilder(); document = documentBuilder.parse(documentInputStream); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return document; }
From source file:com.aliyun.odps.conf.Configuration.java
private void loadResource(Properties properties, Object name, boolean quiet) { try {//from w w w .j a va 2 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 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.canova.api.conf.Configuration.java
private void loadResource(Properties properties, Object name, boolean quiet) { try {// w ww . ja v a 2s. 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) { 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 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.error("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 (storeResource) { updatingResource.put(attr, name.toString()); } if (finalParameter) finalParameters.add(attr); } else { LOG.warn(name + ":a attempt to override final parameter: " + attr + "; Ignoring."); } } } } catch (IOException | ParserConfigurationException | SAXException | DOMException e) { LOG.error("error parsing conf file: " + e); throw new RuntimeException(e); } }
From source file:com.glaf.core.config.Configuration.java
private Resource loadResource(Properties properties, Resource wrapper, boolean quiet) { String name = UNKNOWN_RESOURCE; try {/*from w ww . j a v a2 s .c o m*/ Object resource = wrapper.getResource(); name = wrapper.getName(); 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; boolean returnCachedProperties = false; if (resource instanceof URL) { // an URL resource doc = parse(builder, (URL) resource); } else if (resource instanceof String) { // a CLASSPATH resource URL url = getResource((String) resource); doc = parse(builder, url); } else if (resource instanceof InputStream) { doc = parse(builder, (InputStream) resource, null); returnCachedProperties = true; } else if (resource instanceof Properties) { overlay(properties, (Properties) resource); } else if (resource instanceof Element) { root = (Element) resource; } if (root == null) { if (doc == null) { if (quiet) { return null; } throw new RuntimeException(resource + " not found"); } root = doc.getDocumentElement(); } Properties toAddTo = properties; if (returnCachedProperties) { toAddTo = new Properties(); } 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(toAddTo, new Resource(prop, name), 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; LinkedList<String> source = new LinkedList<String>(); 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 = StringInterner.weakIntern(((Text) field.getFirstChild()).getData().trim()); if ("value".equals(field.getTagName()) && field.hasChildNodes()) value = StringInterner.weakIntern(((Text) field.getFirstChild()).getData()); if ("final".equals(field.getTagName()) && field.hasChildNodes()) finalParameter = "true".equals(((Text) field.getFirstChild()).getData()); if ("source".equals(field.getTagName()) && field.hasChildNodes()) source.add(StringInterner.weakIntern(((Text) field.getFirstChild()).getData())); } source.add(name); // Ignore this parameter if it has already been marked as // 'final' if (attr != null) { loadProperty(toAddTo, name, attr, value, finalParameter, source.toArray(new String[source.size()])); } } if (returnCachedProperties) { overlay(properties, toAddTo); return new Resource(toAddTo, name); } return null; } catch (IOException e) { LOG.fatal("error parsing conf " + name, e); throw new RuntimeException(e); } catch (DOMException e) { LOG.fatal("error parsing conf " + name, e); throw new RuntimeException(e); } catch (SAXException e) { LOG.fatal("error parsing conf " + name, e); throw new RuntimeException(e); } catch (ParserConfigurationException e) { LOG.fatal("error parsing conf " + name, e); throw new RuntimeException(e); } }
From source file:co.cask.cdap.common.conf.Configuration.java
private void loadResource(Properties properties, Object name, boolean quiet) { try {//from ww w .j a va 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; 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 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) { if (deprecatedKeyMap.containsKey(attr)) { DeprecatedKeyInfo keyInfo = deprecatedKeyMap.get(attr); keyInfo.accessed = false; for (String key : keyInfo.newKeys) { // update new keys with deprecated key's value loadProperty(properties, name, key, value, finalParameter); } } else { loadProperty(properties, name, attr, value, finalParameter); } } } } 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); } }