/** * Same as {@link #parse(InputSource, boolean, boolean) parse(source, true, true)}, but loads from a {@link File}; * don't miss the security warnings documented there. */ static public NodeModel parse(File f) throws SAXException, IOException, ParserConfigurationException { return parse(f, true, true); }
/** * Same as {@link #parse(InputSource, boolean, boolean)}, but loads from a {@link File}; don't miss the security * warnings documented there. */ static public NodeModel parse(File f, boolean removeComments, boolean removePIs) throws SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder(); ErrorHandler errorHandler = getErrorHandler(); if (errorHandler != null) builder.setErrorHandler(errorHandler); Document doc = builder.parse(f); if (removeComments && removePIs) { simplify(doc); } else { if (removeComments) { removeComments(doc); } if (removePIs) { removePIs(doc); } mergeAdjacentText(doc); } return wrap(doc); }
useXalanXPathSupport(); } catch (ClassNotFoundException e) { useSunInternalXPathSupport(); } catch (Exception e) { LOG.debug("Failed to use Sun internal XPath support.", e); useJaxenXPathSupport(); } catch (ClassNotFoundException e) {
@Override public TemplateModel get(String key) throws TemplateModelException { if (key.equals("*")) { return getRootElement(); } else if (key.equals("**")) { NodeList nl = ((Document) node).getElementsByTagName("*"); return new NodeListModel(nl, this); } else if (DomStringUtil.isXMLNameLike(key)) { ElementModel em = (ElementModel) NodeModel.wrap(((Document) node).getDocumentElement()); if (em.matchesName(key, Environment.getCurrentEnvironment())) { return em; } else { return new NodeListModel(this); } } return super.get(key); }
public TemplateModel wrap(Object obj) { if (obj instanceof NodeModel) { return (NodeModel) obj; } return NodeModel.wrap((Node) obj); } };
/** * Loads the node model for merging xml. * * @param inputStream The input stream of the xml. * @return The node model. */ protected NodeModel loadMergeXmlModel(InputStream inputStream) throws EnunciateException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(false); //no namespace for the merging... Document doc = builderFactory.newDocumentBuilder().parse(inputStream); NodeModel.simplify(doc); return NodeModel.wrap(doc.getDocumentElement()); } catch (Exception e) { throw new EnunciateException("Error parsing web.xml file for merging", e); } }
@SuppressWarnings("unchecked") @Override public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { String str = getString(0, arguments); if (CommonUtils.notEmpty(str)) { if (uninitialized) { try { DocumentBuilderFactory dbf = NodeModel.getDocumentBuilderFactory(); dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false); dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); dbf.setXIncludeAware(false); dbf.setExpandEntityReferences(false); uninitialized = false; } catch (ParserConfigurationException e) { } } InputSource is = new InputSource(new StringReader(str)); try { return NodeModel.parse(is); } catch (SAXException | IOException | ParserConfigurationException e) { return null; } } return null; }
Document doc = builder.parse(projectFile); projectTemplate = new NodeListModel(builder.parse(projectFile)); projectNode = NodeModel.wrap(doc); TemplateNodeModel docNodeModel = NodeModel.wrap(docNode); HashMap root = new HashMap(); root.put("document", document); vars.put("doc", docNode); if (projectNode != null) { vars.put("project", ((NodeModel) projectNode).getNode()); vars.put("doc", docNode); if (projectNode != null) { vars.put("project", ((NodeModel) projectNode).getNode());
/** * Removes all comments and processing instruction, and unites adjacent text nodes (here CDATA counts as text as * well). This is similar to applying {@link #removeComments(Node)}, {@link #removePIs(Node)}, and finally * {@link #mergeAdjacentText(Node)}, but it does all that somewhat faster. */ static public void simplify(Node parent) { simplify(parent, new StringBuilder(0)); }
/** * Merges adjacent text nodes (where CDATA counts as text node too). Operates recursively on the entire subtree. * The merged node will have the type of the first node of the adjacent merged nodes. * * <p>Because XPath assumes that there are no adjacent text nodes in the tree, not doing this can have * undesirable side effects. Xalan queries like {@code text()} will only return the first of a list of matching * adjacent text nodes instead of all of them, while Jaxen will return all of them as intuitively expected. * * @see #simplify */ static public void mergeAdjacentText(Node parent) { mergeAdjacentText(parent, new StringBuilder(0)); }
NodeModel.useDefaultXPathSupport(); } else if (xpathEngine.equals(Engine.XPATH_ENGINE_XALAN)) { try { NodeModel.useXalanXPathSupport(); } catch (Exception e) { throw new IllegalConfigurationException( NodeModel.useJaxenXPathSupport(); } catch (Exception e) { throw new IllegalConfigurationException( e); NodeModel.setXPathSupportClass(cl);
/** * Recursively removes all comment nodes from the subtree. * * @see #simplify */ static public void removeComments(Node parent) { Node child = parent.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeType() == Node.COMMENT_NODE) { parent.removeChild(child); } else if (child.hasChildNodes()) { removeComments(child); } child = nextSibling; } }
/** * Recursively removes all processing instruction nodes from the subtree. * * @see #simplify */ static public void removePIs(Node parent) { Node child = parent.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { parent.removeChild(child); } else if (child.hasChildNodes()) { removePIs(child); } child = nextSibling; } }
NodeListModel(Node contextNode) { this(NodeModel.wrap(contextNode)); }
private NodeModel loadNodeModel(File xml) throws EnunciateException { Document doc; try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(false); builderFactory.setValidating(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { //we don't want to validate or parse external dtds... return new InputSource(new StringReader("")); } }); doc = builder.parse(new FileInputStream(xml)); } catch (Exception e) { throw new EnunciateException("Error parsing " + xml, e); } NodeModel.simplify(doc); return NodeModel.wrap(doc.getDocumentElement()); }
@SuppressWarnings("unchecked") @Override public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { String str = getString(0, arguments); if (CommonUtils.notEmpty(str)) { if (uninitialized) { try { DocumentBuilderFactory dbf = NodeModel.getDocumentBuilderFactory(); dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false); dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); dbf.setXIncludeAware(false); dbf.setExpandEntityReferences(false); uninitialized = false; } catch (ParserConfigurationException e) { } } InputSource is = new InputSource(new StringReader(str)); try { return NodeModel.parse(is); } catch (SAXException | IOException | ParserConfigurationException e) { return null; } } return null; }
simplify(child, collectorTextChildBuff); } else { int type = child.getNodeType();
static private void mergeAdjacentText(Node parent, StringBuilder collectorBuf) { Node child = parent.getFirstChild(); while (child != null) { Node next = child.getNextSibling(); if (child instanceof Text) { boolean atFirstText = true; while (next instanceof Text) { // if (atFirstText) { collectorBuf.setLength(0); collectorBuf.ensureCapacity(child.getNodeValue().length() + next.getNodeValue().length()); collectorBuf.append(child.getNodeValue()); atFirstText = false; } collectorBuf.append(next.getNodeValue()); parent.removeChild(next); next = child.getNextSibling(); } if (!atFirstText && collectorBuf.length() != 0) { ((CharacterData) child).setData(collectorBuf.toString()); } } else { mergeAdjacentText(child, collectorBuf); } child = next; } }
/** * Recursively removes all comment nodes from the subtree. * * @see #simplify */ static public void removeComments(Node parent) { Node child = parent.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeType() == Node.COMMENT_NODE) { parent.removeChild(child); } else if (child.hasChildNodes()) { removeComments(child); } child = nextSibling; } }
/** * Recursively removes all processing instruction nodes from the subtree. * * @see #simplify */ static public void removePIs(Node parent) { Node child = parent.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { parent.removeChild(child); } else if (child.hasChildNodes()) { removePIs(child); } child = nextSibling; } }