private Element createModulesElementIfNecessary(final Document pomDocument, final Element root) { Element modulesElement = XmlUtils.findFirstElement("/project/modules", root); if (modulesElement == null) { modulesElement = pomDocument.createElement("modules"); final Element repositories = XmlUtils.findFirstElement("/project/repositories", root); root.insertBefore(modulesElement, repositories); } return modulesElement; }
public static String getTextContent(final String path, final Element parentElement, final String valueIfNull) { final Element element = XmlUtils.findFirstElement(path, parentElement); if (element != null) { return element.getTextContent(); } return valueIfNull; }
/** * Parses the configuration of the given plugin (global, not * execution-scoped) * * @param plugin the XML element to parse (required) * @return <code>null</code> if there isn't one */ private static Configuration getConfiguration(final Element plugin) { return Configuration.getInstance(XmlUtils.findFirstElement("configuration", plugin)); }
private static String getTypeCode(final Element dependencyElement) { if (dependencyElement.hasAttribute("type")) { return dependencyElement.getAttribute("type"); } // Read it from the "type" child element, if any return DomUtils.getTextContent(XmlUtils.findFirstElement("type", dependencyElement), "").trim(); }
/** * Checks in under a given root element whether it can find a child element * which matches the XPath expression supplied. The {@link Element} must * exist. Returns {@link Element} if exists. Please note that the XPath * parser used is NOT namespace aware. So if you want to find a element * <beans><sec:http> you need to use the following XPath expression * '/beans/http'. * * @param xPathExpression the XPath expression (required) * @param root the parent DOM element (required) * @return the Element if discovered (never null; an exception is thrown if * cannot be found) */ public static Element findRequiredElement(final String xPathExpression, final Element root) { Validate.notBlank(xPathExpression, "XPath expression required"); Validate.notNull(root, "Root element required"); final Element element = findFirstElement(xPathExpression, root); Validate.notNull(element, "Unable to obtain required element '" + xPathExpression + "' from element '" + root + "'"); return element; }
/** * 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()); } }
private String getRooProjectVersion() { String homePath = new File(".").getPath(); String pomPath = homePath + "/pom.xml"; File pom = new File(pomPath); try { if (pom.exists()) { InputStream is = new FileInputStream(pom); Document docXml = XmlUtils.readXml(is); Element document = docXml.getDocumentElement(); Element rooVersionElement = XmlUtils.findFirstElement("properties/roo.version", document); String rooVersion = rooVersionElement.getTextContent(); return rooVersion; } return "UNKNOWN"; } catch (FileNotFoundException e) { e.printStackTrace(); } return ""; }
private String getRooProjectVersion() { String homePath = new File(".").getPath(); String pomPath = homePath + "/pom.xml"; File pom = new File(pomPath); try { if (pom.exists()) { InputStream is = new FileInputStream(pom); Document docXml = XmlUtils.readXml(is); Element document = docXml.getDocumentElement(); Element rooVersionElement = XmlUtils.findFirstElement("properties/roo.version", document); String rooVersion = rooVersionElement.getTextContent(); return rooVersion; } return "UNKNOWN"; } catch (FileNotFoundException e) { e.printStackTrace(); } return ""; }
private void resolvePoms(final Element pomRoot, final String pomPath, final Map<String, String> pomSet) { pomSet.put(pomPath, pomSet.get(pomPath)); // ensures this key exists final Element parentElement = XmlUtils.findFirstElement("/project/parent", pomRoot); if (parentElement != null) { resolveParentPom(pomPath, pomSet, parentElement); } resolveChildModulePoms(pomRoot, pomPath, pomSet); }
/** * Returns the child node with the given tag name, creating it if it does * not exist. * * @param tagName the child tag to look for and possibly create (required) * @param parent the parent in which to look for the child (required) * @param document the document containing the parent (required) * @return the existing or created child (never <code>null</code>) * @since 1.2.0 */ public static Element createChildIfNotExists(final String tagName, final Node parent, final Document document) { final Element existingChild = XmlUtils.findFirstElement(tagName, parent); if (existingChild != null) { return existingChild; } // No such child; add it return createChildElement(tagName, parent, document); }
/** * Convenience constructor for creating a repository instance from an XML * Element * * @param element containing the repository definition (required) */ public Repository(final Element element) { Validate.notNull(element, "Element required"); final Element name = XmlUtils.findFirstElement("name", element); final Element snapshotsElement = XmlUtils.findFirstElement("snapshots", element); enableSnapshots = snapshotsElement == null ? false : Boolean.valueOf(XmlUtils.findRequiredElement("enabled", snapshotsElement).getTextContent()); id = XmlUtils.findRequiredElement("id", element).getTextContent(); this.name = name == null ? null : name.getTextContent(); url = XmlUtils.findRequiredElement("url", element).getTextContent(); }
private String getRooProjectVersion() { String homePath = getHome().getPath(); String pomPath = homePath + "/pom.xml"; File pom = new File(pomPath); try { if (pom.exists()) { InputStream is = new FileInputStream(pom); Document docXml = XmlUtils.readXml(is); Element document = docXml.getDocumentElement(); Element rooVersionElement = XmlUtils.findFirstElement("properties/roo.version", document); String rooVersion = rooVersionElement.getTextContent(); return rooVersion; } return "UNKNOWN"; } catch (FileNotFoundException e) { e.printStackTrace(); } return ""; }
private static void insertBefore(final Element element, final String beforeElementName, final Document document) { final Element beforeElement = XmlUtils.findFirstElement(WEB_APP_XPATH + beforeElementName, document.getDocumentElement()); if (beforeElement != null) { document.getDocumentElement().insertBefore(element, beforeElement); addLineBreakBefore(element, document); addLineBreakBefore(element, document); return; } document.getDocumentElement().appendChild(element); addLineBreakBefore(element, document); addLineBreakBefore(element, document); }
private static void insertBetween(final Element element, final String afterElementName, final String beforeElementName, final Document document) { final Element beforeElement = XmlUtils.findFirstElement(WEB_APP_XPATH + beforeElementName, document.getDocumentElement()); if (beforeElement != null) { document.getDocumentElement().insertBefore(element, beforeElement); addLineBreakBefore(element, document); addLineBreakBefore(element, document); return; } final Element afterElement = XmlUtils.findFirstElement(WEB_APP_XPATH + afterElementName, document.getDocumentElement()); if (afterElement != null && afterElement.getNextSibling() != null && afterElement.getNextSibling() instanceof Element) { document.getDocumentElement().insertBefore(element, afterElement.getNextSibling()); addLineBreakBefore(element, document); addLineBreakBefore(element, document); return; } document.getDocumentElement().appendChild(element); addLineBreakBefore(element, document); addLineBreakBefore(element, document); }
private static void insertAfter(final Element element, final String afterElementName, final Document document) { final Element afterElement = XmlUtils.findFirstElement(WEB_APP_XPATH + afterElementName, document.getDocumentElement()); if (afterElement != null && afterElement.getNextSibling() != null && afterElement.getNextSibling() instanceof Element) { document.getDocumentElement().insertBefore(element, afterElement.getNextSibling()); addLineBreakBefore(element, document); addLineBreakBefore(element, document); return; } document.getDocumentElement().appendChild(element); addLineBreakBefore(element, document); addLineBreakBefore(element, document); }
/** * Set session timeout in web.xml document * * @param timeout * @param document (required) * @param comment (optional) */ public static void setSessionTimeout(final int timeout, final Document document, final String comment) { Validate.notNull(document, "Web XML document required"); Validate.notNull(timeout, "Timeout required"); Element sessionConfigElement = XmlUtils.findFirstElement(WEB_APP_XPATH + "session-config", document.getDocumentElement()); if (sessionConfigElement == null) { sessionConfigElement = document.createElement("session-config"); insertBetween(sessionConfigElement, "servlet-mapping[last()]", "welcome-file-list", document); if (StringUtils.isNotBlank(comment)) { addCommentBefore(sessionConfigElement, comment, document); } } appendChildIfNotPresent(sessionConfigElement, new XmlElementBuilder("session-timeout", document).setText(String.valueOf(timeout)).build()); }
/** * Add a welcome file definition to web.xml document * * @param path (required) * @param document (required) * @param comment (optional) */ public static void addWelcomeFile(final String path, final Document document, final String comment) { Validate.notNull(document, "Web XML document required"); Validate.notBlank("Path required"); Element welcomeFileElement = XmlUtils.findFirstElement(WEB_APP_XPATH + "welcome-file-list", document.getDocumentElement()); if (welcomeFileElement == null) { welcomeFileElement = document.createElement("welcome-file-list"); insertBetween(welcomeFileElement, "session-config[last()]", "error-page", document); if (StringUtils.isNotBlank(comment)) { addCommentBefore(welcomeFileElement, comment, document); } } appendChildIfNotPresent(welcomeFileElement, new XmlElementBuilder("welcome-file", document) .setText(path).build()); }
private Parent getParent(final String pomPath, final Element root) { final Element parentElement = XmlUtils.findFirstElement(PARENT_XPATH, root); if (parentElement == null) { return null; } final String relativePath = XmlUtils.getTextContent("/relativePath", parentElement, DEFAULT_RELATIVE_PATH); final String parentPomPath = resolveRelativePath(pomPath, relativePath); return new ParentBuilder(parentElement, parentPomPath).build(); }
/** * Add maven-jar-plugin to provided module. * * @param moduleName the name of the module. */ private void addMavenJarPlugin(String moduleName) { // Add plugin maven-jar-plugin Pom module = projectOperations.getPomFromModuleName(moduleName); // Stop if the plugin is already installed for (final Plugin plugin : module.getBuildPlugins()) { if (plugin.getArtifactId().equals(MAVEN_JAR_PLUGIN)) { return; } } final Element configuration = XmlUtils.getConfiguration(getClass()); final Element plugin = XmlUtils.findFirstElement("/configuration/plugin", configuration); // Now install the plugin itself if (plugin != null) { projectOperations.addBuildPlugin(moduleName, new Plugin(plugin), false); } }
public void addResource(final String moduleName, final Resource resource) { Validate .isTrue(isProjectAvailable(moduleName), "Resource modification prohibited at this time"); Validate.notNull(resource, "Resource to add required"); final Pom pom = getPomFromModuleName(moduleName); Validate.notNull(pom, "The pom is not available, so resource addition cannot be performed"); if (pom.isResourceRegistered(resource)) { return; } final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath())); final Element buildElement = XmlUtils.findFirstElement("/project/build", document.getDocumentElement()); final Element resourcesElement = DomUtils.createChildIfNotExists("resources", buildElement, document); resourcesElement.appendChild(resource.getElement(document)); final String descriptionOfChange = highlight(ADDED + " resource") + " " + resource.getSimpleDescription(); fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false); }