private boolean isModuleAlreadyPresent(final String moduleName, final Element modulesElement) { for (final Element element : XmlUtils.findElements("module", modulesElement)) { if (element.getTextContent().trim().equals(moduleName)) { return true; } } return false; }
/** * Parses the plugin's version number from the given XML element * * @param plugin the element to parse (required) * @return a non-<code>null</code> version number (might be empty) */ private static String getVersion(final Element plugin) { final List<Element> versionElements = XmlUtils.findElements("./version", plugin); if (!versionElements.isEmpty()) { return versionElements.get(0).getTextContent(); } return ""; }
private List<Dependency> getDependencies(final String xPathExpression, final Element configuration) { final List<Dependency> dependencies = new ArrayList<Dependency>(); for (final Element dependencyElement : XmlUtils.findElements(xPathExpression + "/dependencies/dependency", configuration)) { final Dependency dependency = new Dependency(dependencyElement); dependencies.add(dependency); } return dependencies; }
/** * Removes any elements matching the given XPath expression, relative to the * given Element * * @param xPath the XPath of the element(s) to remove (can be blank) * @param searchBase the element to which the XPath expression is relative */ public static void removeElements(final String xPath, final Element searchBase) { for (final Element elementToDelete : XmlUtils.findElements(xPath, searchBase)) { final Node parentNode = elementToDelete.getParentNode(); parentNode.removeChild(elementToDelete); removeTextNodes(parentNode); } }
/** * Returns the text content of the first child of the given parent that has * the given tag name, if any. * * @param parent the parent in which to search (required) * @param child the child name for which to search (required) * @return <code>null</code> if there is no such child, otherwise the first * such child's text content */ public static String getChildTextContent(final Element parent, final String child) { final List<Element> children = XmlUtils.findElements(child, parent); if (children.isEmpty()) { return null; } return getTextContent(children.get(0), null); }
/** * Parses any elements matching the given XPath expression into instances of * the given type. * * @param <T> the type of object to parse * @param type the type of object to parse; must have a constructor that * accepts an {@link Element} as its sole argument * @param xPath the XPath expression to apply (required) * @param root the root of the XML document being searched (required) * @return a non-<code>null</code> list */ private <T> List<T> parseElements(final Class<T> type, final String xPath, final Element root) { final List<T> results = new ArrayList<T>(); for (final Element element : XmlUtils.findElements(xPath, root)) { try { results.add(type.getConstructor(Element.class).newInstance(element)); } catch (final RuntimeException e) { throw e; } catch (final Exception e) { throw new RuntimeException(e); } } return results; }
private List<Module> getModules(final Element root, final String pomPath, final String packaging) { if (!"pom".equalsIgnoreCase(packaging)) { return null; } final List<Module> modules = new ArrayList<Module>(); for (final Element module : XmlUtils.findElements(MODULE_XPATH, root)) { final String moduleName = module.getTextContent(); if (StringUtils.isNotBlank(moduleName)) { final String modulePath = resolveRelativePath(pomPath, moduleName); modules.add(new Module(moduleName, modulePath)); } } return modules; }
/** * Convenience constructor when an XML element is available that represents * a Maven <resource>. * * @param resource to parse (required) */ public Resource(final Element resource) { final Element directoryElement = XmlUtils.findFirstElement("directory", resource); Validate.notNull(directoryElement, "directory element required"); directory = directoryElement.getTextContent(); final Element filteringElement = XmlUtils.findFirstElement("filtering", resource); filtering = filteringElement == null ? null : Boolean.valueOf(filteringElement.getTextContent()); // Parsing for includes for (final Element include : XmlUtils.findElements("includes/include", resource)) { includes.add(include.getTextContent()); } }
@Override public List<String> getEndPointsFromWsdlFile(String wsdlPath) { List<String> availableEndPoints = new ArrayList<String>(); // Check if provided wsdl file exists if (getFileManager().exists(wsdlPath)) { // Obtain document final Document document = XmlUtils.readXml(getFileManager().getInputStream(wsdlPath)); // Finding service elements List<Element> services = XmlUtils.findElements("service", document.getDocumentElement()); for (Element service : services) { NodeList ports = service.getChildNodes(); for (int i = 0; i < ports.getLength(); i++) { Element port = (Element) ports.item(1); if (port != null && port.getAttribute("name") != null) { availableEndPoints.add(port.getAttribute("name")); } } } } return availableEndPoints; }
@Override public String getServiceUrlForEndpointFromWsdlFile(String endPoint, String wsdlPath) { // Check if provided wsdl file exists if (getFileManager().exists(wsdlPath)) { // Obtain document final Document document = XmlUtils.readXml(getFileManager().getInputStream(wsdlPath)); // Finding service elements List<Element> services = XmlUtils.findElements("service", document.getDocumentElement()); for (Element service : services) { NodeList ports = service.getChildNodes(); for (int i = 0; i < ports.getLength(); i++) { Element port = (Element) ports.item(1); // Check if current endPoint has the same name as the provided one if (port != null && port.getAttribute("name") != null && endPoint.equals(port.getAttribute("name"))) { NodeList addresses = port.getChildNodes(); for (int x = 0; x < addresses.getLength(); x++) { if (addresses.item(x) != null && addresses.item(x) instanceof Element) { Element address = (Element) addresses.item(x); return address.getAttribute("location"); } } } } } } return null; }
private void resolveChildModulePoms(final Element pomRoot, final String pomPath, final Map<String, String> pomSet) { for (final Element module : XmlUtils.findElements("/project/modules/module", pomRoot)) { final String moduleName = module.getTextContent(); if (StringUtils.isNotBlank(moduleName)) { final String modulePath = resolveRelativePath(pomPath, moduleName); final boolean alreadyDiscovered = pomSet.containsKey(modulePath); pomSet.put(modulePath, moduleName); if (!alreadyDiscovered) { final Document pomDocument = XmlUtils.readXml(getFileManager().getInputStream(modulePath)); final Element root = pomDocument.getDocumentElement(); resolvePoms(root, modulePath, pomSet); } } } }
/** * Gets database dependency from config file and adds it with test scope * * @param moduleName * the module which dependency should be added * @param databaseConfigPrefix * the prefix name for choosing the dependency to add * @param configuration * the configuration file with the dependencies to copy from */ private void addTestScopedDependency(String moduleName, String databaseConfigPrefix, final Element configuration) { final List<Element> databaseDependencies = XmlUtils.findElements(databaseConfigPrefix + "/dependencies/dependency", configuration); for (final Element dependencyElement : databaseDependencies) { // Change scope from provided to test NodeList childNodes = dependencyElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { final Node node = childNodes.item(i); if (node != null && node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("scope")) { node.setTextContent("test"); } } // Add dependency getProjectOperations().addDependency(moduleName, new Dependency(dependencyElement)); } }
/** * Parses the given XML plugin element for the plugin's executions * * @param plugin the XML element to parse (required) * @return a non-<code>null</code> list */ private static List<Execution> getExecutions(final Element plugin) { final List<Execution> executions = new ArrayList<Execution>(); // Loop through the "execution" elements in the plugin element for (final Element execution : XmlUtils.findElements("executions/execution", plugin)) { final Element idElement = XmlUtils.findFirstElement("id", execution); final String id = DomUtils.getTextContent(idElement, ""); final Element phaseElement = XmlUtils.findFirstElement("phase", execution); final String phase = DomUtils.getTextContent(phaseElement, ""); final List<String> goals = new ArrayList<String>(); for (final Element goalElement : XmlUtils.findElements("goals/goal", execution)) { goals.add(goalElement.getTextContent()); } final Configuration configuration = Configuration.getInstance(XmlUtils.findFirstElement("configuration", execution)); executions.add(new Execution(id, phase, configuration, goals.toArray(new String[goals.size()]))); } return executions; }
List<Element> viewElements = XmlUtils.findElements("//view-state", documentElement); for (Element element : viewElements) { element.setAttribute("model", modelObjectName);
private void installApplicationConfiguration(String moduleName) { // Add Spring Boot dependences final Element configuration = XmlUtils.getConfiguration(getClass()); final List<Dependency> requiredDependencies = new ArrayList<Dependency>(); final List<Element> dependencies = XmlUtils.findElements("/configuration/dependencies/dependency", configuration); for (final Element dependencyElement : dependencies) { requiredDependencies.add(new Dependency(dependencyElement)); } getProjectOperations().addDependencies(moduleName, requiredDependencies); // Add Plugins List<Element> plugins = XmlUtils.findElements("/configuration/plugins/plugin", configuration); for (Element element : plugins) { Plugin plugin = new Plugin(element); getProjectOperations().addBuildPlugin(moduleName, plugin); } }
/** * Add Spring Cache starter to provided module. * * @param module the Pom where the starter should be installed. */ private void addSpringCacheDependency(Pom module) { // Parse the configuration.xml file final Element configuration = XmlUtils.getConfiguration(getClass()); final List<Dependency> dependencies = new ArrayList<Dependency>(); final List<Element> auditDependencies = XmlUtils.findElements("/configuration/cache/dependencies/dependency", configuration); for (final Element dependencyElement : auditDependencies) { dependencies.add(new Dependency(dependencyElement)); } getProjectOperations().addDependencies(module.getModuleName(), dependencies); }
for (final Element candidate : XmlUtils.findElements("filter", filtersElement)) { if (filter.equals(new Filter(candidate))) { filtersElement.removeChild(candidate); final List<Element> filterElements = XmlUtils.findElements("filter", filtersElement); if (filterElements.isEmpty()) { filtersElement.getParentNode().removeChild(filtersElement);
private void removeRepository(final String moduleName, final Repository repository, final String path) { Validate.isTrue(isProjectAvailable(moduleName), "Repository modification prohibited at this time"); Validate.notNull(repository, "Repository required"); final Pom pom = getPomFromModuleName(moduleName); Validate.notNull(pom, "The pom is not available, so repository removal cannot be performed"); if ("pluginRepository".equals(path)) { if (!pom.isPluginRepositoryRegistered(repository)) { return; } } else { if (!pom.isRepositoryRegistered(repository)) { return; } } final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath())); final Element root = document.getDocumentElement(); String descriptionOfChange = ""; for (final Element candidate : XmlUtils.findElements(path, root)) { if (repository.equals(new Repository(candidate))) { candidate.getParentNode().removeChild(candidate); descriptionOfChange = highlight(REMOVED + " repository") + " " + repository.getUrl(); // We stay in the loop just in case it was in the POM more than // once } } fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false); }
public void removeProperty(final String moduleName, final Property property) { Validate .isTrue(isProjectAvailable(moduleName), "Property modification prohibited at this time"); Validate.notNull(property, "Property to remove required"); final Pom pom = getPomFromModuleName(moduleName); Validate.notNull(pom, "The pom is not available, so property removal cannot be performed"); if (!pom.isPropertyRegistered(property)) { return; } final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath())); final Element root = document.getDocumentElement(); final Element propertiesElement = XmlUtils.findFirstElement("/project/properties", root); String descriptionOfChange = ""; for (final Element candidate : XmlUtils.findElements("/project/properties/*", document.getDocumentElement())) { if (property.equals(new Property(candidate))) { propertiesElement.removeChild(candidate); descriptionOfChange = highlight(REMOVED + " property") + " " + property.getName(); // Stay in the loop just in case it was in the POM more than // once } } DomUtils.removeTextNodes(propertiesElement); fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false); }
XmlUtils.findElements("/configuration/plugins/plugin", configuration);