List of usage examples for org.dom4j Node getNodeType
short getNodeType();
From source file:net.unicon.warlock.fac.AbstractWarlockFactory.java
License:Open Source License
private static ILayoutElement parseLayoutElement(Node n, IWarlockFactory owner) throws WarlockException { // Assertions. if (n == null) { String msg = "Argument 'n [Node]' cannot be null."; throw new IllegalArgumentException(msg); }// w w w. jav a 2 s . co m if (owner == null) { String msg = "Argument 'owner' cannot be null."; throw new IllegalArgumentException(msg); } ILayoutElement rslt = null; // Choose which type to create. switch (n.getNodeType()) { case Node.TEXT_NODE: rslt = IgnoredLayoutText.parse(n); break; case Node.ELEMENT_NODE: Element e = (Element) n; String tagName = e.getName(); if (tagName.equals("value-of")) { rslt = XpathValue.parse(e); } else if (tagName.equals("copy-of")) { rslt = CopyOf.parse(e); } else if (tagName.equals("call-template")) { rslt = TemplateCall.parse(e); } else { rslt = IgnoredLayoutElement.parse(e, owner); } break; default: rslt = EmptyLayoutElement.INSTANCE; break; } return rslt; }
From source file:nl.tue.gale.ae.processor.xmlmodule.CreoleTextHandler.java
License:Open Source License
@SuppressWarnings("unchecked") private void traverse(Element textElement) { boolean flat = ("true".equals(textElement.attributeValue("flat"))); List<Element> elements = ImmutableList.copyOf((List<Element>) textElement.elements()); for (Element element : elements) { textElement.content().add(textElement.content().indexOf(element), DocumentFactory.getInstance().createText("(% " + GaleUtil.serializeXML(element) + " %)")); textElement.remove(element);// w w w . j a va 2s . c o m } textElement.normalize(); List<Node> content = ImmutableList.copyOf((List<Node>) textElement.content()); for (Node node : content) if (node.getNodeType() == Node.TEXT_NODE) { textElement.content().addAll(textElement.content().indexOf(node), parse(node.getText(), flat)); textElement.content().remove(node); } }
From source file:nl.tue.gale.ae.processor.xmlmodule.RepositoryModule.java
License:Open Source License
private void removeTextNodes(Element element) { @SuppressWarnings("unchecked") List<Node> content = (List<Node>) element.content(); for (Node node : ImmutableList.copyOf(content)) if (node.getNodeType() == Node.TEXT_NODE) content.remove(node);//from w w w. ja v a2 s .c o m }
From source file:org.apache.archiva.xml.XMLReader.java
License:Apache License
/** * Remove namespaces from element recursively. *///ww w . j a va 2 s . co m @SuppressWarnings("unchecked") public void removeNamespaces(Element elem) { elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName())); Node n; Iterator<Node> it = elem.elementIterator(); while (it.hasNext()) { n = it.next(); switch (n.getNodeType()) { case Node.ATTRIBUTE_NODE: ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE); break; case Node.ELEMENT_NODE: removeNamespaces((Element) n); break; } } }
From source file:org.apache.maven.archetype.common.DefaultPomManager.java
License:Apache License
public void addModule(File pom, String artifactId) throws IOException, XmlPullParserException, DocumentException, InvalidPackaging { boolean found = false; StringWriter writer = new StringWriter(); Reader fileReader = null;//from w w w . j av a 2 s . c o m try { fileReader = ReaderFactory.newXmlReader(pom); SAXReader reader = new SAXReader(); Document document = reader.read(fileReader); Element project = document.getRootElement(); String packaging = null; Element packagingElement = project.element("packaging"); if (packagingElement != null) { packaging = packagingElement.getStringValue(); } if (!"pom".equals(packaging)) { throw new InvalidPackaging( "Unable to add module to the current project as it is not of packaging type 'pom'"); } Element modules = project.element("modules"); if (modules == null) { modules = project.addText(" ").addElement("modules"); modules.setText("\n "); project.addText("\n"); } // TODO: change to while loop for (@SuppressWarnings("unchecked") Iterator<Element> i = modules.elementIterator("module"); i.hasNext() && !found;) { Element module = i.next(); if (module.getText().equals(artifactId)) { found = true; } } if (!found) { Node lastTextNode = null; for (@SuppressWarnings("unchecked") Iterator<Node> i = modules.nodeIterator(); i.hasNext();) { Node node = i.next(); if (node.getNodeType() == Node.ELEMENT_NODE) { lastTextNode = null; } else if (node.getNodeType() == Node.TEXT_NODE) { lastTextNode = node; } } if (lastTextNode != null) { modules.remove(lastTextNode); } modules.addText("\n "); modules.addElement("module").setText(artifactId); modules.addText("\n "); XMLWriter xmlWriter = new XMLWriter(writer); xmlWriter.write(document); FileUtils.fileWrite(pom.getAbsolutePath(), writer.toString()); } // end if } finally { IOUtil.close(fileReader); } }
From source file:org.apache.maven.archetype.old.DefaultOldArchetype.java
License:Apache License
static boolean addModuleToParentPom(String artifactId, Reader fileReader, Writer fileWriter) throws DocumentException, IOException, ArchetypeTemplateProcessingException { SAXReader reader = new SAXReader(); Document document = reader.read(fileReader); Element project = document.getRootElement(); String packaging = null;/* w ww. j a v a 2s . co m*/ Element packagingElement = project.element("packaging"); if (packagingElement != null) { packaging = packagingElement.getStringValue(); } if (!"pom".equals(packaging)) { throw new ArchetypeTemplateProcessingException( "Unable to add module to the current project as it is not of packaging type 'pom'"); } Element modules = project.element("modules"); if (modules == null) { modules = project.addText(" ").addElement("modules"); modules.setText("\n "); project.addText("\n"); } boolean found = false; for (Iterator<?> i = modules.elementIterator("module"); i.hasNext() && !found;) { Element module = (Element) i.next(); if (module.getText().equals(artifactId)) { found = true; } } if (!found) { Node lastTextNode = null; for (Iterator<?> i = modules.nodeIterator(); i.hasNext();) { Node node = (Node) i.next(); if (node.getNodeType() == Node.ELEMENT_NODE) { lastTextNode = null; } else if (node.getNodeType() == Node.TEXT_NODE) { lastTextNode = node; } } if (lastTextNode != null) { modules.remove(lastTextNode); } modules.addText("\n "); modules.addElement("module").setText(artifactId); modules.addText("\n "); XMLWriter writer = new XMLWriter(fileWriter); writer.write(document); } return !found; }
From source file:org.apereo.portal.io.xml.SpELDataTemplatingStrategy.java
License:Apache License
@Override public Source processTemplates(Document data, String filename) { log.trace("Processing templates for document XML={}", data.asXML()); for (String xpath : XPATH_EXPRESSIONS) { @SuppressWarnings("unchecked") List<Node> nodes = data.selectNodes(xpath); for (Node n : nodes) { String inpt, otpt;/*from w w w . j a v a 2s.com*/ switch (n.getNodeType()) { case org.w3c.dom.Node.ATTRIBUTE_NODE: Attribute a = (Attribute) n; inpt = a.getValue(); otpt = processText(inpt); if (otpt == null) { throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename); } if (!otpt.equals(inpt)) { a.setValue(otpt); } break; case org.w3c.dom.Node.TEXT_NODE: case org.w3c.dom.Node.CDATA_SECTION_NODE: inpt = n.getText(); otpt = processText(inpt); if (otpt == null) { throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename); } if (!otpt.equals(inpt)) { n.setText(otpt); } break; default: String msg = "Unsupported node type: " + n.getNodeTypeName(); throw new RuntimeException(msg); } } } final SAXSource rslt = new DocumentSource(data); rslt.setSystemId(filename); // must be set, else import chokes return rslt; }
From source file:org.codehaus.modello.plugin.utils.Dom4jUtils.java
License:Apache License
/** * Locates and returns ther {@link Element} specified by a passed in XPATH * expression in a target {@link Document}. * // w ww . ja va2 s .c om * @param doc target {@link Document} instance where the element is to be * located. * @param xpathToNode XPATH expression to locate the element. * @return {@link Element} instance that matches the XPATH expression. An * {@link AssertionFailedError} is thrown if no matching Element was * found. */ public static Element findElement(Document doc, String xpathToNode) { if (StringUtils.isEmpty(xpathToNode)) { throw new AssertionFailedError("Unable to assert an attribute using an empty xpath."); } if (doc == null) { throw new AssertionFailedError("Unable to assert an attribute using a null document."); } XPath xpath = doc.createXPath(xpathToNode); Node node = xpath.selectSingleNode(doc); if (node == null) { throw new AssertionFailedError("Expected Node at '" + xpathToNode + "', but was not found."); } if (node.getNodeType() != Node.ELEMENT_NODE) { throw new AssertionFailedError("Node at '" + xpathToNode + "' is not an xml element."); } return (Element) node; }
From source file:org.codehaus.modello.plugin.utils.Dom4jUtils.java
License:Apache License
/** * Asserts that the specified {@link Node} is not a {@link Node#TEXT_NODE} * or {@link Node#CDATA_SECTION_NODE}.//from w w w.j ava2 s .c om * * @param message Assertion message to print. * @param node Node to interrogate for {@link Node#TEXT_NODE} or * {@link Node#CDATA_SECTION_NODE} property. * @param recursive <code>true</code> if the node is to be recursively * searched, else <code>false</code>. * @return <code>true</code> if the specified {@link Node} is not of type * {@link Node#TEXT_NODE} or {@link Node#CDATA_SECTION_NODE} */ public static boolean assertNoTextNode(String message, Node node, boolean recursive) { if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { // Double check that it isn't just whitespace. String text = StringUtils.trim(node.getText()); if (StringUtils.isNotEmpty(text)) { throw new AssertionFailedError(message + " found <" + text + ">"); } } if (recursive) { if (node instanceof Branch) { Iterator it = ((Branch) node).nodeIterator(); while (it.hasNext()) { Node child = (Node) it.next(); assertNoTextNode(message, child, recursive); } } } return false; }
From source file:org.danann.cernunnos.xml.AppendNodeTask.java
License:Apache License
@SuppressWarnings("unchecked") public void perform(TaskRequest req, TaskResponse res) { // Figure out where to put the content... Branch p = null;/*from w w w . j a va 2 s.c o m*/ int index; if (sibling != null) { Node sib = (Node) sibling.evaluate(req, res); p = sib.getParent(); index = p.indexOf(sib) + 1; } else { // Work from the PARENT... p = (Branch) parent.evaluate(req, res); index = p.content().size(); } // Figure out what content to add... List list = null; if (content != null && content.size() > 0) { list = content; } else { list = new LinkedList(); list.add(node.evaluate(req, res)); } // Evaluate phrases & add... for (Object o : list) { Node n = (Node) ((Node) o).clone(); NodeProcessor.evaluatePhrases(n, grammar, req, res); // If the parent is an element, check if we should // carry the parent namespace over to the child... if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) { // We know the parent is an Element w/ a namespace, // is the child (also) an Element w/ none? if (n.getNodeType() == Node.ELEMENT_NODE && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) { // Yes -- we need to port the namespace forward... Namespace nsp = ((Element) p).getNamespace(); if (log.isTraceEnabled()) { StringBuffer msg = new StringBuffer(); msg.append("Adding the following namespace to <").append(n.getName()).append(">: ") .append(nsp); log.trace(msg.toString()); } NodeProcessor.applyNamespace(nsp, (Element) n); } } // Although they *are* nodes, attributes are not technically // content, and therefore they must have special treatment... if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) { // Add attributes by calling addAttribute on the Element contract... ((Element) p).add((Attribute) n); } else { // Add everything else as 'content'... p.content().add(index++, n); } } }