use of javax.xml.xpath.XPath in project camel by apache.
the class SpringBootStarterMojo method fixAdditionalRepositories.
private void fixAdditionalRepositories(Document pom) throws Exception {
if (project.getFile() != null) {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document originalPom = builder.parse(project.getFile());
XPath xpath = XPathFactory.newInstance().newXPath();
Node repositories = (Node) xpath.compile("/project/repositories").evaluate(originalPom, XPathConstants.NODE);
if (repositories != null) {
pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_START));
pom.getDocumentElement().appendChild(pom.importNode(repositories, true));
pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_END));
}
} else {
getLog().warn("Cannot access the project pom file to retrieve repositories");
}
}
use of javax.xml.xpath.XPath in project camel by apache.
the class BaseNexusRepository method indexNexus.
/**
* Runs the task to index nexus for new artifacts
*/
protected void indexNexus() throws Exception {
// must have q parameter so use component to find all component
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
URL url = createNexusUrl();
InputStream is = url.openStream();
try {
Document dom = documentBuilder.parse(is);
XPathFactory xpFactory = XPathFactory.newInstance();
XPath exp = xpFactory.newXPath();
NodeList list = (NodeList) exp.evaluate("//data/artifact", dom, XPathConstants.NODESET);
Set<NexusArtifactDto> newArtifacts = new LinkedHashSet<>();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
String g = getNodeText(node.getChildNodes(), "groupId");
String a = getNodeText(node.getChildNodes(), "artifactId");
String v = getNodeText(node.getChildNodes(), "version");
String l = getNodeText(node.getChildNodes(), "artifactLink");
if (g != null & a != null & v != null & l != null) {
NexusArtifactDto dto = new NexusArtifactDto();
dto.setGroupId(g);
dto.setArtifactId(a);
dto.setVersion(v);
dto.setArtifactLink(l);
log.debug("Found: {}:{}:{}", dto.getGroupId(), dto.getArtifactId(), dto.getVersion());
// is it a new artifact
boolean newArtifact = true;
for (NexusArtifactDto existing : indexedArtifacts) {
if (existing.getGroupId().equals(dto.getGroupId()) && existing.getArtifactId().equals(dto.getArtifactId()) && existing.getVersion().equals(dto.getVersion())) {
newArtifact = false;
break;
}
}
if (newArtifact) {
newArtifacts.add(dto);
}
}
}
// if there is any new artifacts then process them
if (!newArtifacts.isEmpty()) {
onNewArtifacts(newArtifacts);
}
} finally {
close(is);
}
}
use of javax.xml.xpath.XPath in project languagetool by languagetool-org.
the class AtDEvaluator method getRuleMatches.
private List<RuleMatch> getRuleMatches(String resultXml, AnnotatedText text) throws XPathExpressionException {
List<RuleMatch> matches = new ArrayList<>();
Document document = getDocument(resultXml);
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList errors = (NodeList) xPath.evaluate("//error", document, XPathConstants.NODESET);
for (int i = 0; i < errors.getLength(); i++) {
Node error = errors.item(i);
String string = xPath.evaluate("string", error);
String description = xPath.evaluate("description", error);
String preContext = xPath.evaluate("precontext", error);
String errorText = preContext + " " + string;
int fromPos = text.getPlainText().indexOf(errorText) + preContext.length() + 1;
int toPos = fromPos + string.length();
NodeList suggestions = (NodeList) xPath.evaluate("suggestions", error, XPathConstants.NODESET);
RuleMatch ruleMatch = new RuleMatch(new AtdRule(), text.getOriginalTextPositionFor(fromPos), text.getOriginalTextPositionFor(toPos), description);
for (int j = 0; j < suggestions.getLength(); j++) {
Node option = suggestions.item(j);
String optionStr = xPath.evaluate("option", option);
ruleMatch.setSuggestedReplacement(optionStr);
}
matches.add(ruleMatch);
}
return matches;
}
use of javax.xml.xpath.XPath in project languagetool by languagetool-org.
the class PatternRuleXmlCreator method toXML.
/**
* Return the given pattern rule as an indented XML string.
* @since 2.3
*/
public final String toXML(PatternRuleId ruleId, Language language) {
List<String> filenames = language.getRuleFileNames();
XPath xpath = XPathFactory.newInstance().newXPath();
for (String filename : filenames) {
try (InputStream is = this.getClass().getResourceAsStream(filename)) {
Document doc = getDocument(is);
Node ruleNode = (Node) xpath.evaluate("/rules/category/rule[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
if (ruleNode != null) {
return nodeToString(ruleNode);
}
Node ruleNodeInGroup = (Node) xpath.evaluate("/rules/category/rulegroup/rule[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
if (ruleNodeInGroup != null) {
return nodeToString(ruleNodeInGroup);
}
if (ruleId.getSubId() != null) {
NodeList ruleGroupNodes = (NodeList) xpath.evaluate("/rules/category/rulegroup[@id='" + ruleId.getId() + "']/rule", doc, XPathConstants.NODESET);
if (ruleGroupNodes != null) {
for (int i = 0; i < ruleGroupNodes.getLength(); i++) {
if (Integer.toString(i + 1).equals(ruleId.getSubId())) {
return nodeToString(ruleGroupNodes.item(i));
}
}
}
} else {
Node ruleGroupNode = (Node) xpath.evaluate("/rules/category/rulegroup[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
if (ruleGroupNode != null) {
return nodeToString(ruleGroupNode);
}
}
} catch (Exception e) {
throw new RuntimeException("Could not turn rule '" + ruleId + "' for language " + language + " into a string", e);
}
}
throw new RuntimeException("Could not find rule '" + ruleId + "' for language " + language + " in files: " + filenames);
}
use of javax.xml.xpath.XPath in project flyway by flyway.
the class GradleLargeTest method getPomVersion.
/**
* Retrieves the version embedded in the project pom. Useful for running these tests in IntelliJ.
*
* @return The POM version.
*/
private String getPomVersion() {
try {
File pom = new File("pom.xml");
if (!pom.exists()) {
return "unknown";
}
XPath xPath = XPathFactory.newInstance().newXPath();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(false);
Document document = documentBuilderFactory.newDocumentBuilder().parse(pom);
return xPath.evaluate("/project/version", document);
} catch (Exception e) {
throw new IllegalStateException("Unable to read POM version", e);
}
}
Aggregations