Search in sources :

Example 1 with ElementFilter

use of org.jdom.filter.ElementFilter in project freud by LMAX-Exchange.

the class JavaSourceJdom method parsePackagePath.

///////////////////////////////////////////////////////////////////////////////////////////////////////
public static String[] parsePackagePath(final Element element) {
    SortedSet<Element> packagePathElementSortedSet = new TreeSet<Element>(JdomTreePositionComparator.getInstance());
    for (Iterator iterator = element.getDescendants(new ElementFilter(JavaSourceTokenType.IDENT.getName())); iterator.hasNext(); ) {
        packagePathElementSortedSet.add((Element) iterator.next());
    }
    boolean endsWithDotStar = (element.getChild(JavaSourceTokenType.DOTSTAR.getName()) != null);
    final String[] packagePath = new String[(endsWithDotStar) ? packagePathElementSortedSet.size() + 1 : packagePathElementSortedSet.size()];
    int i = 0;
    for (Element pathElement : packagePathElementSortedSet) {
        packagePath[i++] = pathElement.getTextTrim();
    }
    if (endsWithDotStar) {
        packagePath[i] = "*";
    }
    return packagePath;
}
Also used : TreeSet(java.util.TreeSet) Element(org.jdom.Element) ElementFilter(org.jdom.filter.ElementFilter) Iterator(java.util.Iterator) JdomTreeAdaptor.documentToString(org.freud.core.parser.JdomTreeAdaptor.documentToString)

Example 2 with ElementFilter

use of org.jdom.filter.ElementFilter in project che by eclipse.

the class EffectivePomWriter method addMavenNamespace.

/**
     * method from org.apache.maven.plugins.help.AbstractEffectiveMojo
     * Add a Pom/Settings namespaces to the effective XML content.
     *
     * @param effectiveXml not null the effective POM or Settings
     * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the settings xsd url.
     * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt; with the Maven namespace or
     *         the original <code>effective</code> if an error occurred.
     * @see #POM_XSD_URL
     * @see #SETTINGS_XSD_URL
     */
protected static String addMavenNamespace(String effectiveXml, boolean isPom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(effectiveXml));
        Element rootElement = document.getRootElement();
        // added namespaces
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/4.0.0");
        rootElement.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootElement.addNamespaceDeclaration(xsiNamespace);
        if (rootElement.getAttribute("schemaLocation", xsiNamespace) == null) {
            rootElement.setAttribute("schemaLocation", "http://maven.apache.org/POM/4.0.0 " + (isPom ? POM_XSD_URL : SETTINGS_XSD_URL), xsiNamespace);
        }
        ElementFilter elementFilter = new ElementFilter(Namespace.getNamespace(""));
        for (Iterator<?> i = rootElement.getDescendants(elementFilter); i.hasNext(); ) {
            Element e = (Element) i.next();
            e.setNamespace(pomNamespace);
        }
        StringWriter w = new StringWriter();
        Format format = Format.getPrettyFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);
        return w.toString();
    } catch (JDOMException e) {
        return effectiveXml;
    } catch (IOException e) {
        return effectiveXml;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) StringWriter(java.io.StringWriter) Element(org.jdom.Element) ElementFilter(org.jdom.filter.ElementFilter) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Namespace(org.jdom.Namespace)

Example 3 with ElementFilter

use of org.jdom.filter.ElementFilter in project intellij-community by JetBrains.

the class MavenEffectivePomDumper method addMavenNamespace.

/**
   * Copy/pasted from org.apache.maven.plugins.help.AbstractEffectiveMojo
   */
protected static String addMavenNamespace(String effectiveXml, boolean isPom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(effectiveXml));
        Element rootElement = document.getRootElement();
        // added namespaces
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/4.0.0");
        rootElement.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootElement.addNamespaceDeclaration(xsiNamespace);
        if (rootElement.getAttribute("schemaLocation", xsiNamespace) == null) {
            rootElement.setAttribute("schemaLocation", "http://maven.apache.org/POM/4.0.0 " + (isPom ? POM_XSD_URL : SETTINGS_XSD_URL), xsiNamespace);
        }
        ElementFilter elementFilter = new ElementFilter(Namespace.getNamespace(""));
        for (Iterator i = rootElement.getDescendants(elementFilter); i.hasNext(); ) {
            Element e = (Element) i.next();
            e.setNamespace(pomNamespace);
        }
        addLineBreaks(document, pomNamespace);
        StringWriter w = new StringWriter();
        Format format = Format.getRawFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);
        return w.toString();
    } catch (JDOMException e) {
        return effectiveXml;
    } catch (IOException e) {
        return effectiveXml;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ElementFilter(org.jdom.filter.ElementFilter)

Example 4 with ElementFilter

use of org.jdom.filter.ElementFilter in project maven-plugins by apache.

the class AbstractEffectiveMojo method addMavenNamespace.

/**
     * Add a Pom/Settings namespaces to the effective XML content.
     *
     * @param effectiveXml not null the effective POM or Settings
     * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the settings xsd url.
     * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt; with the Maven namespace or
     *         the original <code>effective</code> if an error occurred.
     * @see #POM_XSD_URL
     * @see #SETTINGS_XSD_URL
     */
protected static String addMavenNamespace(String effectiveXml, boolean isPom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(effectiveXml));
        Element rootElement = document.getRootElement();
        // added namespaces
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/4.0.0");
        rootElement.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootElement.addNamespaceDeclaration(xsiNamespace);
        if (rootElement.getAttribute("schemaLocation", xsiNamespace) == null) {
            rootElement.setAttribute("schemaLocation", "http://maven.apache.org/POM/4.0.0 " + (isPom ? POM_XSD_URL : SETTINGS_XSD_URL), xsiNamespace);
        }
        ElementFilter elementFilter = new ElementFilter(Namespace.getNamespace(""));
        for (Iterator<?> i = rootElement.getDescendants(elementFilter); i.hasNext(); ) {
            Element e = (Element) i.next();
            e.setNamespace(pomNamespace);
        }
        StringWriter w = new StringWriter();
        Format format = Format.getPrettyFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);
        return w.toString();
    } catch (JDOMException e) {
        return effectiveXml;
    } catch (IOException e) {
        return effectiveXml;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) StringWriter(java.io.StringWriter) Element(org.jdom.Element) ElementFilter(org.jdom.filter.ElementFilter) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Namespace(org.jdom.Namespace)

Aggregations

ElementFilter (org.jdom.filter.ElementFilter)4 Element (org.jdom.Element)3 SAXBuilder (org.jdom.input.SAXBuilder)3 Format (org.jdom.output.Format)3 XMLOutputter (org.jdom.output.XMLOutputter)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Document (org.jdom.Document)2 JDOMException (org.jdom.JDOMException)2 Namespace (org.jdom.Namespace)2 Iterator (java.util.Iterator)1 TreeSet (java.util.TreeSet)1 JdomTreeAdaptor.documentToString (org.freud.core.parser.JdomTreeAdaptor.documentToString)1