List of usage examples for javax.xml.parsers DocumentBuilderFactory setFeature
public abstract void setFeature(String name, boolean value) throws ParserConfigurationException;
From source file:org.lockss.plugin.clockss.XPathXmlMetadataParser.java
/** * <p>// ww w . j av a 2s.c o m * Subclasses can override this method to create and configure a * {@link DocumentBuilderFactory} instance. * </p> * <p> * By default, this class uses {@link DocumentBuilderFactory#newInstance()} * and sets all the following to <code>false</code>: * </p> * <ul> * <li>{@link DocumentBuilderFactory#setValidating(boolean)}</li> * <li><code>http://xml.org/sax/features/namespaces</code></li> * <li><code>http://xml.org/sax/features/validation</code></li> * <li><code>http://apache.org/xml/features/nonvalidating/load-dtd-grammar</code></li> * <li><code>http://apache.org/xml/features/nonvalidating/load-external-dtd</code></li> * <li><code>http://apache.org/xml/features/dom/defer-node-expansion</code></li> * </ul> * * @throws ParserConfigurationException * if an error arises while configuring the document builder * factory. * @since 1.66 */ protected DocumentBuilderFactory makeDocumentBuilderFactory() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setFeature("http://xml.org/sax/features/namespaces", false); dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); // The following feature keeps some XML files (see T&Fsource) from causing DB.parse // null pointer exception dbf.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); return dbf; }
From source file:org.nuxeo.ecm.platform.semanticentities.sources.DBpediaEntitySource.java
@Override public List<EntitySuggestion> suggestRemoteEntity(String keywords, String type, int maxSuggestions) throws IOException { Map<String, String> localTypes = descriptor.getReverseMappedTypes(); Set<String> acceptedLocalTypes = new TreeSet<String>(); if (type != null) { acceptedLocalTypes.add(type);//from ww w . j ava 2 s .c o m } else { acceptedLocalTypes.addAll(localTypes.values()); } // remove the overly generic type Entity for now acceptedLocalTypes.remove("Entity"); // fetch more suggestions than requested since we will do type // post-filtering afterwards log.debug("suggestion query for keywords: " + keywords); InputStream bodyStream = fetchSuggestions(keywords, maxSuggestions * 3); if (bodyStream == null) { throw new IOException(String.format("Unable to fetch suggestion response for '%s'", keywords)); } // Fetch the complete payload to make it easier for debugging (should // not be big anyway) String content = IOUtils.toString(bodyStream); log.trace(content); List<EntitySuggestion> suggestions = new ArrayList<EntitySuggestion>(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new ByteArrayInputStream(content.getBytes("utf-8"))); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList resultNodes = (NodeList) xpath.evaluate(RESULT_NODE_XPATH, document, XPathConstants.NODESET); for (int i = 0; i < resultNodes.getLength(); i++) { Node resultNode = resultNodes.item(i); String label = null; String uri = null; Node labelNode = (Node) xpath.evaluate("Label/text()", resultNode, XPathConstants.NODE); if (labelNode != null) { label = labelNode.getNodeValue(); } Node uriNode = (Node) xpath.evaluate("URI/text()", resultNode, XPathConstants.NODE); if (uriNode != null) { uri = uriNode.getNodeValue(); } NodeList typeNodes = (NodeList) xpath.evaluate("Classes/Class/URI/text()", resultNode, XPathConstants.NODESET); String matchingLocalType = null; for (int k = 0; k < typeNodes.getLength(); k++) { Node typeNode = typeNodes.item(k); String localType = localTypes.get(typeNode.getNodeValue()); if (localType != null && acceptedLocalTypes.contains(localType)) { matchingLocalType = localType; break; } } if (matchingLocalType != null && label != null && uri != null) { suggestions.add(new EntitySuggestion(label, uri, matchingLocalType)); if (suggestions.size() >= maxSuggestions) { break; } } } } catch (ParserConfigurationException e) { log.error(e, e); return Collections.emptyList(); } catch (FactoryConfigurationError e) { log.error(e, e); return Collections.emptyList(); } catch (XPathExpressionException e) { log.error(e, e); return Collections.emptyList(); } catch (SAXException e) { throw new IOException( String.format("Invalid suggestion response for '%s' with type '%s'", keywords, type), e); } finally { bodyStream.close(); } return suggestions; }
From source file:org.nuxeo.theme.themes.ThemeParser.java
private static void registerThemeFromInputStream(final ThemeDescriptor themeDescriptor, final InputStream in, boolean preload) throws ThemeIOException, ThemeException { String themeName = null;/*from w ww . j a v a 2s .co m*/ final InputSource is = new InputSource(in); final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e) { log.debug("Could not set DTD non-validation feature"); } DocumentBuilder db; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new ThemeIOException(e); } Document document; try { document = db.parse(is); } catch (SAXException e) { throw new ThemeIOException(e); } catch (IOException e) { throw new ThemeIOException(e); } final org.w3c.dom.Element docElem = document.getDocumentElement(); if (!docElem.getNodeName().equals(DOCROOT_NAME)) { throw new ThemeIOException("No <" + DOCROOT_NAME + "> document tag found in " + in.toString() + ", ignoring the resource."); } themeName = docElem.getAttributes().getNamedItem("name").getNodeValue(); if (!ThemeManager.validateThemeName(themeName)) { throw new ThemeIOException( "Theme names may only contain alpha-numeric characters, underscores and hyphens: " + themeName); } themeDescriptor.setName(themeName); loadTheme(themeDescriptor, docElem, preload); }
From source file:org.nuxeo.theme.themes.ThemeSerializer.java
public Document serialize(final String src) throws Exception { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try {/*from w w w . ja va 2 s. c om*/ dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e) { log.debug("Could not set DTD non-validation feature"); } final DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.newDocument(); elements = new ArrayList<Element>(); final org.w3c.dom.Element root = doc.createElement(DOCROOT_NAME); final ThemeManager themeManager = Manager.getThemeManager(); ThemeElement theme = themeManager.getThemeBySrc(src); // Theme description and name final String description = theme.getDescription(); if (description != null) { root.setAttribute("description", description); } final String themeName = theme.getName(); root.setAttribute("name", themeName); ThemeDescriptor themeDef = ThemeManager.getThemeDescriptor(src); List<String> templateEngines = themeDef.getTemplateEngines(); if (templateEngines != null && templateEngines.size() > 0) { final StringBuilder sb = new StringBuilder(); final Iterator<String> it = templateEngines.iterator(); while (it.hasNext()) { sb.append(it.next()); if (it.hasNext()) { sb.append(","); } } root.setAttribute("template-engines", sb.toString()); } // Resource bank String resourceBankName = themeDef.getResourceBankName(); if (resourceBankName != null) { root.setAttribute("resource-bank", resourceBankName); } doc.appendChild(root); // layout final org.w3c.dom.Element layoutNode = doc.createElement("layout"); root.appendChild(layoutNode); for (Node page : theme.getChildren()) { serializeLayout((Element) page, layoutNode); } // element properties for (Element element : elements) { serializeProperties(element, root); } // presets List<PresetType> customPresets = PresetManager.getCustomPresets(themeName); if (!customPresets.isEmpty()) { final org.w3c.dom.Element presetsNode = doc.createElement("presets"); root.appendChild(presetsNode); for (PresetType preset : customPresets) { final org.w3c.dom.Element presetNode = doc.createElement("preset"); presetNode.setAttribute("name", preset.getName()); presetNode.setAttribute("category", preset.getCategory()); presetNode.setAttribute("label", preset.getLabel()); presetNode.setAttribute("description", preset.getDescription()); presetNode.appendChild(doc.createTextNode(preset.getValue())); presetsNode.appendChild(presetNode); } } // formats final org.w3c.dom.Element formatNode = doc.createElement("formats"); root.appendChild(formatNode); for (String formatTypeName : themeManager.getFormatTypeNames()) { // export named styles for (Identifiable object : themeManager.getNamedObjects(themeName, formatTypeName)) { Format format = (Format) object; // skip unused remote styles if (!format.isCustomized() && ThemeManager.listFormatsDirectlyInheritingFrom(format).isEmpty()) { continue; } serializeFormat(format, formatNode); } for (Format format : themeManager.getFormatsByTypeName(formatTypeName)) { if (format.isNamed()) { continue; } // make sure that the format is used by this theme boolean isUsedByThisTheme = false; for (Element element : ElementFormatter.getElementsFor(format)) { if (element.isChildOf(theme) || element == theme) { isUsedByThisTheme = true; break; } } if (isUsedByThisTheme) { serializeFormat(format, formatNode); } } } return doc; }
From source file:org.nuxeo.theme.webwidgets.Utils.java
public static List<WidgetFieldType> extractSchema(String html) { final List<WidgetFieldType> schema = new ArrayList<WidgetFieldType>(); Matcher matcher = preferencesPattern.matcher(html); if (matcher.find()) { html = matcher.group(0);//from ww w . j a v a2s .c om } else { return schema; } final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e) { log.debug("Could not set DTD non-validation feature"); } final Document document; ByteArrayInputStream in = null; try { in = new ByteArrayInputStream(html.getBytes()); final DocumentBuilder db = dbf.newDocumentBuilder(); document = db.parse(in); } catch (Exception e) { log.error("Could not parse widget code." + e); return schema; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } final String[] attrNames = { "label", "type", "name", "defaultValue", "min", "max", "step", "onchange" }; final NodeList nodes = document.getElementsByTagName("widget:preferences"); if (nodes.getLength() != 1) { return schema; } final Node node = nodes.item(0); final NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { final Node n = childNodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("preference")) { final WidgetFieldType widgetFieldType = new WidgetFieldType(); final NamedNodeMap attrs = n.getAttributes(); final Node typeAttr = attrs.getNamedItem("type"); for (String attrName : attrNames) { Node attrNode = attrs.getNamedItem(attrName); if (attrNode != null) { Field field; try { field = widgetFieldType.getClass().getField(attrName); } catch (Exception e) { log.error("Could not access field: " + attrName + e); continue; } final String value = attrNode.getNodeValue(); try { field.set(widgetFieldType, value); } catch (Exception e) { log.error("Could not set field: " + attrName); continue; } if (typeAttr != null && typeAttr.getNodeValue().equals("list")) { NodeList optionNodes = n.getChildNodes(); List<WidgetFieldType.Option> options = new ArrayList<WidgetFieldType.Option>(); for (int j = 0; j < optionNodes.getLength(); j++) { final Node optionNode = optionNodes.item(j); if (optionNode.getNodeType() == Node.ELEMENT_NODE && optionNode.getNodeName().equals("option")) { final NamedNodeMap optionAttrs = optionNode.getAttributes(); Node optionLabelAttr = optionAttrs.getNamedItem("label"); Node optionValueAttr = optionAttrs.getNamedItem("value"); if (optionLabelAttr != null && optionValueAttr != null) { options.add(widgetFieldType.new Option(optionLabelAttr.getNodeValue(), optionValueAttr.getNodeValue())); } } } widgetFieldType.setOptions(options); } } } schema.add(widgetFieldType); } } return schema; }
From source file:org.openstreetmap.josm.tools.Utils.java
/** * Returns a new secure DOM builder, supporting XML namespaces. * @return a new secure DOM builder, supporting XML namespaces * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration. * @since 10404/*ww w. j a va2 s . c o m*/ */ public static DocumentBuilder newSafeDOMBuilder() throws ParserConfigurationException { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); builderFactory.setNamespaceAware(true); builderFactory.setValidating(false); return builderFactory.newDocumentBuilder(); }
From source file:org.overlord.sramp.common.artifactbuilder.XmlArtifactBuilder.java
@Override public ArtifactBuilder buildArtifacts(BaseArtifactType primaryArtifact, ArtifactContent artifactContent) throws IOException { super.buildArtifacts(primaryArtifact, artifactContent); try {//from w w w.j a v a 2s.co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); //$NON-NLS-1$ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$ DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(getContentStream()); // This *must* be setup prior to calling #configureNamespaceMappings. Most subclasses will need it. rootElement = document.getDocumentElement(); if (primaryArtifact instanceof XmlDocument) { String encoding = document.getXmlEncoding(); if (StringUtils.isBlank(encoding)) { encoding = "UTF-8"; } ((XmlDocument) primaryArtifact).setContentEncoding(encoding); } XPathFactory xPathfactory = XPathFactory.newInstance(); xpath = xPathfactory.newXPath(); StaticNamespaceContext nsCtx = new StaticNamespaceContext(); configureNamespaceMappings(nsCtx); xpath.setNamespaceContext(nsCtx); // Create all derived artifacts derive(); // Set the relatedDocument relationship for all derived artifacts for (BaseArtifactType derivedArtifact : getDerivedArtifacts()) { if (derivedArtifact instanceof DerivedArtifactType) { DerivedArtifactType dat = (DerivedArtifactType) derivedArtifact; if (dat.getRelatedDocument() == null) { DocumentArtifactTarget related = new DocumentArtifactTarget(); related.setValue(primaryArtifact.getUuid()); related.setArtifactType(DocumentArtifactEnum.fromValue(primaryArtifact.getArtifactType())); dat.setRelatedDocument(related); } } else { Relationship genericRelationship = SrampModelUtils.getGenericRelationship(derivedArtifact, "relatedDocument"); //$NON-NLS-1$ if (genericRelationship == null) { SrampModelUtils.addGenericRelationship(derivedArtifact, "relatedDocument", //$NON-NLS-1$ primaryArtifact.getUuid()); } } } return this; } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e); } }
From source file:org.owasp.benchmark.score.BenchmarkScore.java
private static Document getXMLDocument(File f) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); // Prevent XXE = Note, disabling this entirely breaks the parsing of some XML files, like a Burp results // file, so have to use the alternate defense. //dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); docBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); docBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); InputSource is = new InputSource(new FileInputStream(f)); Document doc = docBuilder.parse(is); return doc;//from w w w .ja v a 2 s .c o m }
From source file:org.pentaho.di.core.xml.XMLHandler.java
/** * Load a String into an XML document/*from w w w . ja va2 s .c om*/ * * @param string * The XML text to load into a document * @param Boolean * true to defer node expansion, false to not defer. * @return the Document if all went well, null if an error occurred! */ public static final Document loadXMLString(String string, Boolean namespaceAware, Boolean deferNodeExpansion) throws KettleXMLException { DocumentBuilderFactory dbf; DocumentBuilder db; Document doc; try { // Check and open XML document dbf = DocumentBuilderFactory.newInstance(); dbf.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", deferNodeExpansion); dbf.setNamespaceAware(namespaceAware); // parameterize this as well db = dbf.newDocumentBuilder(); StringReader stringReader = new java.io.StringReader(string); InputSource inputSource = new InputSource(stringReader); try { doc = db.parse(inputSource); } catch (IOException ef) { throw new KettleXMLException("Error parsing XML", ef); } finally { stringReader.close(); } return doc; } catch (Exception e) { throw new KettleXMLException("Error reading information from XML string : " + Const.CR + string, e); } }
From source file:org.pentaho.js.require.RequireJsGenerator.java
public static RequireJsGenerator parsePom(InputStream inputStream) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, ParseException { byte[] bytes = IOUtils.toByteArray(inputStream); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); Document pom = documentBuilderFactory.newDocumentBuilder().parse(new ByteArrayInputStream(bytes)); return new RequireJsGenerator(pom); }