Example usage for org.jdom2 Element removeNamespaceDeclaration

List of usage examples for org.jdom2 Element removeNamespaceDeclaration

Introduction

In this page you can find the example usage for org.jdom2 Element removeNamespaceDeclaration.

Prototype

public void removeNamespaceDeclaration(final Namespace additionalNamespace) 

Source Link

Document

Removes an additional namespace declarations from this element.

Usage

From source file:com.c4om.autoconf.ulysses.extra.svinchangesetgenerator.SVINChangesetGenerator.java

License:Apache License

/**
 * This method generates a changeset document, which describes what nodes
 * must be added and replaced. It generates it from the SVRLInterpreter
 * report passed at constructor./*from  w w w  .ja  v  a 2 s.  c  o  m*/
 * 
 * @param pathToConfiguration path to the runtime configuration.
 * 
 * @param reportDocument the report document (objective solution description).
 * 
 * @return The generated changeset document.
 * 
 * @throws JDOMException
 *             If there are problems at JDOM2 XML parsings
 * @throws IOException
 *             I/O problems
 * @throws SaxonApiException
 *             problems with Saxon API while transforming metamodel
 *             suggestions into partial autocomplete nodes
 * @throws ParserConfigurationException
 *             problems with javax.xml APIs while transforming metamodel
 *             suggestions into partial autocomplete nodes
 */
public Document getSingleChangesetDocument(String pathToConfiguration, Document reportDocument)
        throws JDOMException, IOException, SaxonApiException, ParserConfigurationException {
    Element resultRoot = new Element("changeset", AutoconfXMLConstants.NAMESPACE_SVINAPPLIER);
    resultRoot.addNamespaceDeclaration(NAMESPACE_AUTOCONF_METADATA); // To
    // prevent
    // several
    // "xmlns:*****"
    // attributes
    // to
    // appear
    // everywhere
    Document result = new Document(resultRoot);
    Element reportElement = reportDocument.getRootElement();
    for (Element currentDiscrepancyElement : reportElement.getChildren()) {
        boolean isCreate = false;
        Element interestingPathsElement = currentDiscrepancyElement.getChild("interestingPaths",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        String searchPathText = interestingPathsElement.getAttributeValue("search-path");
        String basePathText = interestingPathsElement.getAttributeValue("base-path");
        String keySubpathText = interestingPathsElement.getAttributeValue("key-subpath");
        // First, we look for a path to search the element where discrepancy
        // took place (if it exists)
        String[] docAndPath;
        String searchPathInternal;
        if (searchPathText == null) {
            docAndPath = divideDocAndPath(basePathText);
            searchPathInternal = docAndPath[1] + "[" + keySubpathText + "]";
        } else {
            docAndPath = divideDocAndPath(searchPathText);
            searchPathInternal = docAndPath[1];
        }
        if (!documentCache.containsKey(docAndPath[0])) {
            documentCache.put(docAndPath[0],
                    loadJDOMDocumentFromFile(new File(pathToConfiguration + "/" + docAndPath[0])));
        }
        Document currentDoc = documentCache.get(docAndPath[0]);
        List<Element> discordingElementAtDocList = performJAXENXPath(searchPathInternal, currentDoc,
                Filters.element(), xpathNamespaces);
        if (discordingElementAtDocList.size() == 0) {
            isCreate = true;
        }
        if (isCreate) {
            Element nodeToCreate = currentDiscrepancyElement
                    .getChild("suggestedPartialNode", NAMESPACE_SVRL_INTERPETER_REPORT).getChildren().get(0)
                    .clone();
            //Sometimes, svinrep namespace is declared here (it is not clear why). We must remove it.
            nodeToCreate.removeNamespaceDeclaration(NAMESPACE_SVRL_INTERPETER_REPORT);
            boolean thereAreMetamodelSuggestions = currentDiscrepancyElement
                    .getChild("metamodelSuggestions", NAMESPACE_SVRL_INTERPETER_REPORT).getChildren()
                    .size() > 0;
            if (thereAreMetamodelSuggestions) {
                Element metamodelSuggestionUntransformed = currentDiscrepancyElement
                        .getChild("metamodelSuggestions", NAMESPACE_SVRL_INTERPETER_REPORT).getChildren().get(0)
                        .clone();
                Document suggestionMiniDocument = new Document(metamodelSuggestionUntransformed);
                Document suggestionMiniDocumentTransformed = performXSLT(suggestionMiniDocument,
                        xsltTransformMetamodelDocument);
                Element metamodelSuggestion = suggestionMiniDocumentTransformed.getRootElement();
                Attribute metadataAttribute = new Attribute("autogen-from", "metamodel",
                        NAMESPACE_AUTOCONF_METADATA);
                mixTreesRecursive(metamodelSuggestion, nodeToCreate, metadataAttribute,
                        NAMESPACE_AUTOCONF_METADATA.getURI());
            } else {
                Attribute mayNeedManualCompletion = new Attribute("may-need-completion", "true",
                        NAMESPACE_AUTOCONF_METADATA);
                nodeToCreate.setAttribute(mayNeedManualCompletion);
            }
            Element createNodeElement = new Element("add-node", AutoconfXMLConstants.NAMESPACE_SVINAPPLIER);
            final String REGEXP_TO_GET_PARENT_PATH = "(.+)(/[^\\[\\]/]+(\\[.+\\])?)$";
            Pattern patternToGetParentPath = Pattern.compile(REGEXP_TO_GET_PARENT_PATH);
            Matcher matcherToGetParentPath = patternToGetParentPath.matcher(searchPathInternal);
            matcherToGetParentPath.matches();
            String pathToParent = matcherToGetParentPath.group(1);
            Attribute pathToParentAttr = new Attribute("underParentAtPath", pathToParent);
            Attribute documentToChangeAttr = new Attribute("atResource", docAndPath[0]);
            createNodeElement.setAttribute(documentToChangeAttr);
            createNodeElement.setAttribute(pathToParentAttr);
            createNodeElement.addContent(nodeToCreate);
            resultRoot.addContent(createNodeElement);

        } else {
            for (int i = 0; i < discordingElementAtDocList.size(); i++) {
                Element nodeToModify = currentDiscrepancyElement
                        .getChild("suggestedPartialNode", NAMESPACE_SVRL_INTERPETER_REPORT).getChildren().get(0)
                        .clone();
                //Sometimes, svinrep namespace is declared here (it is not clear why). We must remove it.
                nodeToModify.removeNamespaceDeclaration(NAMESPACE_SVRL_INTERPETER_REPORT);
                Element discordingElementAtDoc = discordingElementAtDocList.get(i);
                mixTreesRecursive(discordingElementAtDoc, nodeToModify, null,
                        NAMESPACE_AUTOCONF_METADATA.getURI());
                Element replaceNodeElement = new Element("replace-node",
                        AutoconfXMLConstants.NAMESPACE_SVINAPPLIER);
                Attribute pathToElementAttr = new Attribute("atPath",
                        generateAttributeBasedPath(discordingElementAtDoc));
                Attribute documentToChangeAttr = new Attribute("atResource", docAndPath[0]);
                replaceNodeElement.setAttribute(documentToChangeAttr);
                replaceNodeElement.setAttribute(pathToElementAttr);
                replaceNodeElement.addContent(nodeToModify);
                resultRoot.addContent(replaceNodeElement);
            }
        }
    }
    return result;
}

From source file:com.cybernostics.jsp2thymeleaf.api.elements.CopyElementConverter.java

@Override
public List<Content> process(JSPParser.JspElementContext node, JSPElementNodeConverter context) {
    Optional<Element> maybeElement = createElement(node, context);
    if (maybeElement.isPresent()) {
        Element element = maybeElement.get();
        element.removeNamespaceDeclaration(XMLNS);
        element.addContent(getNewChildContent(node, context));
        addAttributes(element, node, context);
        return Stream.concat(Stream.of(element), getNewAppendedContent(node, context).stream())
                .collect(toList());/* w ww  . j ava 2 s .  co  m*/
    }

    return EMPTY_LIST;
}

From source file:com.cybernostics.jsp2thymeleaf.api.elements.CopyElementConverter.java

protected Optional<Element> createElement(JspElementContext node, JSPElementNodeConverter context) {
    final Element element = new Element(newNameForElement(node));
    element.removeNamespaceDeclaration(element.getNamespace());
    element.setNamespace(newNamespaceForElement(node));
    return Optional.of(element);
}

From source file:de.smartics.maven.plugin.jboss.modules.parser.ModulesDescriptorBuilder.java

License:Apache License

private void adjustNamespaces(final Element element) {
    element.setNamespace(null);//from w w w.  j  a  v  a  2s  . co m
    final List<Namespace> namespaces = new ArrayList<Namespace>(element.getAdditionalNamespaces());
    for (final Namespace namespace : namespaces) {
        element.removeNamespaceDeclaration(namespace);
    }
    element.setNamespace(ModuleXmlBuilder.NS);
    for (final Element child : element.getChildren()) {
        adjustNamespaces(child);
    }
}

From source file:de.smartics.maven.plugin.jboss.modules.util.XmlUtils.java

License:Apache License

public static void adjustNamespaces(final Element element, Namespace ns) {
    element.setNamespace(null);// ww  w .j a va 2s .c om
    final List<Namespace> namespaces = new ArrayList<Namespace>(element.getAdditionalNamespaces());
    for (final Namespace namespace : namespaces) {
        element.removeNamespaceDeclaration(namespace);
    }
    element.setNamespace(ns);
    for (final Element child : element.getChildren()) {
        adjustNamespaces(child, ns);
    }
}

From source file:utils.ParserXML.java

License:Apache License

public String getStringedSubTree(String element) throws JDOMException, IOException {
    String s = "";
    Element elem = rootElement.getChild(element);
    Iterator lst = elem.getDescendants();
    int i;//from w  ww  . jav  a  2 s  .com
    Element e = document.detachRootElement();
    e.removeNamespaceDeclaration(Namespace.NO_NAMESPACE);
    Element e2 = e.getChild(element);
    XMLOutputter xout = new XMLOutputter();
    Format f = Format.getPrettyFormat();
    xout.setFormat(f);
    return ((xout.outputString(e2).replaceAll("<" + element + ">", "")).replaceAll("</" + element + ">", ""));
}