List of usage examples for org.w3c.dom Node setTextContent
public void setTextContent(String textContent) throws DOMException;
From source file:org.automagic.deps.doctor.editor.PomWriterImpl.java
@Override public void setVersionProperty(String propertyName, ArtifactVersion version) { Node node = Utils.getNode("/project/properties/" + propertyName, document).get(); node.setTextContent(version.toString()); }
From source file:org.automagic.deps.doctor.editor.PomWriterImpl.java
private void writeDependency(String parent, TransitiveDependency dependency, int indent, boolean full) { Optional<Node> node = Utils.getNode(parent + "/dependencies", document); Node dependenciesNode;/*from w ww . ja va 2 s .c om*/ if (node.isPresent()) { dependenciesNode = node.get(); } else { Node parentNode = Utils.getNode(parent, document).get(); dependenciesNode = document.createElement("dependencies"); addDummyComment(dependenciesNode); addIndentedNode(parentNode, dependenciesNode, indent - 1); dependenciesNode = Utils.getNode(parent + "/dependencies", document).get(); clearDummyComment(dependenciesNode); } Artifact artifact = dependency.getArtifact(); String groupId = trim(artifact.getGroupId()); String artifactId = trim(artifact.getArtifactId()); String type = trim(artifact.getType()); String classifier = trim(artifact.getClassifier()); String version = dependency.getVersion().toString(); Node dependencyNode = document.createElement("dependency"); if (addComments) { String comment = getCommentText(dependency); dependencyNode.appendChild(document.createComment(comment)); } Node groupIdNode = document.createElement("groupId"); groupIdNode.setTextContent(groupId); dependencyNode.appendChild(groupIdNode); Node artifactIdNode = document.createElement("artifactId"); artifactIdNode.setTextContent(artifactId); dependencyNode.appendChild(artifactIdNode); if (StringUtils.isNotBlank(type) && !"jar".equals(type)) { Node typeNode = document.createElement("type"); typeNode.setTextContent(type); dependencyNode.appendChild(typeNode); } if (StringUtils.isNotBlank(classifier)) { Node classifierNode = document.createElement("classifier"); classifierNode.setTextContent(classifier); dependencyNode.appendChild(classifierNode); } if (full) { Node versionNode = document.createElement("version"); versionNode.setTextContent(version); dependencyNode.appendChild(versionNode); } addIndentedNode(dependenciesNode, dependencyNode, indent); }
From source file:org.automagic.deps.doctor.editor.PomWriterImpl.java
private void setDependencyVersion(String parent, TransitiveDependency dependency) { Optional<Node> dependencyNode = Utils.getDependencyNode(dependency.getArtifact(), document, parent); Node versionNode = Utils.getNode("./version", dependencyNode.get()).get(); versionNode.setTextContent(dependency.getVersion().toString()); Optional<Node> comment = Utils.getPluginComment(parent, dependency.getArtifact(), document); if (comment.isPresent()) { comment.get().setNodeValue(getCommentText(dependency)); }/*from w ww. j a v a2s . co m*/ }
From source file:org.commonjava.maven.ext.core.impl.XMLManipulator.java
void internalApplyChanges(Project project, XMLState.XMLOperation operation) throws ManipulationException { File target = new File(project.getPom().getParentFile(), operation.getFile()); logger.info("Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate()); Document doc = xmlIO.parseXML(target); try {//www .j a va 2s . c o m NodeList nodeList = (NodeList) xPath.evaluate(operation.getXPath(), doc, XPathConstants.NODESET); if (nodeList.getLength() == 0) { if (project.isIncrementalPME()) { logger.warn("Did not locate XML using XPath " + operation.getXPath()); return; } else { logger.error("XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile()); throw new ManipulationException("Did not locate XML using XPath " + operation.getXPath()); } } for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (isEmpty(operation.getUpdate())) { // Delete node.getParentNode().removeChild(node); } else { // Update node.setTextContent(operation.getUpdate()); } } xmlIO.writeXML(target, doc); } catch (XPathExpressionException e) { logger.error("Caught XML exception processing file {}, document context {} ", target, doc, e); throw new ManipulationException("Caught XML exception processing file", e); } }
From source file:org.commonjava.maven.ext.io.XMLIOTest.java
@Test public void modifyFile() throws ManipulationException, IOException, XPathExpressionException { String updatePath = "/assembly/includeBaseDirectory"; String newBaseDirectory = "/home/MYNEWBASEDIR"; Document doc = xmlIO.parseXML(xmlFile); XPath xPath = XPathFactory.newInstance().newXPath(); Node node = (Node) xPath.evaluate(updatePath, doc, XPathConstants.NODE); node.setTextContent(newBaseDirectory); File target = tf.newFile();/*from w w w . j a v a 2 s.co m*/ xmlIO.writeXML(target, doc); Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build(); logger.debug("Difference {} ", diff.toString()); String targetXML = FileUtils.readFileToString(target); // XMLUnit only seems to support XPath 1.0 so modify the expression to find the value. String xpathForHamcrest = "/*/*[local-name() = '" + updatePath.substring(updatePath.lastIndexOf('/') + 1) + "']"; assertThat(targetXML, hasXPath(xpathForHamcrest)); assertThat(targetXML, EvaluateXPathMatcher.hasXPath(xpathForHamcrest, equalTo(newBaseDirectory))); assertTrue(diff.toString(), diff.hasDifferences()); }
From source file:org.commonjava.maven.ext.io.XMLIOTest.java
@Test public void modifyMultiple() throws ManipulationException, IOException, XPathExpressionException { String updatePath = "/assembly/formats/format"; Document doc = xmlIO.parseXML(xmlFile); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate(updatePath, doc, XPathConstants.NODESET); logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); logger.debug("Found node {} with type {} and value {} ", node.getNodeName(), node.getNodeType(), node.getTextContent());/* www. j av a2 s .c o m*/ node.setTextContent("NEW-FORMAT-" + i); } File target = tf.newFile(); xmlIO.writeXML(target, doc); Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build(); assertTrue(diff.toString(), diff.hasDifferences()); String xpathForHamcrest = "/*/*/*[starts-with(.,'NEW-FORMAT') and local-name() = '" + updatePath.substring(updatePath.lastIndexOf('/') + 1) + "']"; Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build()); int count = 0; for (Node anI : i) { count++; assertTrue(anI.getTextContent().startsWith("NEW-FORMAT")); } assertEquals(3, count); }
From source file:org.commonjava.maven.ext.io.XMLIOTest.java
@Test public void modifyPartialFile() throws ManipulationException, IOException, XPathExpressionException { String replacementGA = "com.rebuild:servlet-api"; String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]"; Document doc = xmlIO.parseXML(xmlFile); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate(tomcatPath, doc, XPathConstants.NODESET); logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength()); assertTrue(nodeList.getLength() == 1); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); logger.debug("Found node {} with type {} and value {}", node.getNodeName(), node.getNodeType(), node.getTextContent());/*ww w. j a va 2s . c om*/ node.setTextContent(replacementGA); } File target = tf.newFile(); xmlIO.writeXML(target, doc); Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build(); assertTrue(diff.toString(), diff.hasDifferences()); String xpathForHamcrest = "/*/*/*/*/*[starts-with(.,'com.rebuild') and local-name() = 'include']"; Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build()); int count = 0; for (Node anI : i) { count++; assertTrue(anI.getTextContent().startsWith("com.rebuild:servlet-api")); } assertEquals(1, count); }
From source file:org.commonjava.maven.galley.maven.model.view.AbstractMavenElementView.java
public Element getCollapsedElement() { if (isElementStackEmpty(elementsAwaitingCollapse)) { return collapsed; }//from w w w. j a va 2 s. co m addElementToStack(elementsAwaitingCollapse); String textContent = getFirstValueInOverlappingElements(collapsed, elementsAwaitingCollapse); if (null != textContent) { collapsed.setTextContent(textContent); } else { NodeList nodeList = collapsed.getChildNodes(); for (int i = 0; i <= nodeList.getLength(); i++) { Node node = nodeList.item(i); if (!(node instanceof Element)) { continue; } String childText = getFirstValueInOverlappingElements(node, elementsAwaitingCollapse); if (null != childText) { node.setTextContent(childText); } } addToCollapsedChildElements(collapsed, 0); } elementsAwaitingCollapse.clear(); collapsedElementContext = JXPathUtils.newContext(collapsed); return collapsed; }
From source file:org.commonjava.maven.galley.maven.model.view.AbstractMavenElementView.java
protected final Node getNode(final String path) { Node node = (Node) getCollapsedElementContext().selectSingleNode(path); String textContent = getFirstValueInOverlappingElements(node, elements); if (textContent != null) { node.setTextContent(textContent); }//w w w. j a v a 2 s. com return node; }
From source file:org.commonjava.maven.galley.maven.model.view.AbstractMavenElementView.java
@SuppressWarnings("unchecked") protected final List<Node> getNodes(final String path) { List<Node> nodes = getCollapsedElementContext().selectNodes(path); for (Node node : nodes) { String textContent = getFirstValueInOverlappingElements(node, elements); if (textContent != null) { node.setTextContent(textContent); }//from w ww . j a v a 2s . c o m } return nodes; }