Example usage for org.jdom2 Element getNamespaceURI

List of usage examples for org.jdom2 Element getNamespaceURI

Introduction

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

Prototype

public String getNamespaceURI() 

Source Link

Document

Returns the namespace URI mapped to this element's prefix (or the in-scope default namespace URI if no prefix).

Usage

From source file:ch.kostceco.tools.kostval.validation.modulesiard.impl.ValidationEcolumnModuleImpl.java

License:Open Source License

private boolean prepareXMLAccess(ValidationContext validationContext)
        throws JDOMException, IOException, Exception {
    boolean successfullyCommitted = false;
    Properties properties = validationContext.getValidationProperties();
    File metadataXML = validationContext.getMetadataXML();
    InputStream inputStream = new FileInputStream(metadataXML);
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(inputStream);
    // Assigning JDOM Document to the validation context
    validationContext.setMetadataXMLDocument(document);
    String xmlPrefix = properties.getProperty("module.e.metadata.xml.prefix");
    String xsdPrefix = properties.getProperty("module.e.table.xsd.prefix");
    // Setting the namespaces to access metadata.xml and the different table.xsd
    Element rootElement = document.getRootElement();
    String namespaceURI = rootElement.getNamespaceURI();
    Namespace xmlNamespace = Namespace.getNamespace(xmlPrefix, namespaceURI);
    Namespace xsdNamespace = Namespace.getNamespace(xsdPrefix, namespaceURI);
    // Assigning prefix to the validation context
    validationContext.setXmlPrefix(xmlPrefix);
    validationContext.setXsdPrefix(xsdPrefix);
    // Assigning namespace info to the validation context
    validationContext.setXmlNamespace(xmlNamespace);
    validationContext.setXsdNamespace(xsdNamespace);
    if (validationContext.getXmlNamespace() != null && validationContext.getXsdNamespace() != null
            && validationContext.getXmlPrefix() != null && validationContext.getXsdPrefix() != null
            && validationContext.getMetadataXMLDocument() != null
            && validationContext.getValidationProperties() != null) {
        this.setValidationContext(validationContext);
        successfullyCommitted = true;/*from  www .  j a  va 2 s. c  om*/
    } else {
        successfullyCommitted = false;
        this.setValidationContext(null);
        throw new Exception();
    }
    return successfullyCommitted;
}

From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationFrowModuleImpl.java

License:Open Source License

private boolean prepareXMLAccess(Properties properties, File metadataXML) throws JDOMException, IOException {
    boolean successfullyCommitted = false;
    String me = "[F.0.5] prepareXMLAccess(Properties properties, File metadataXML) ";
    //Initializing validation Logging
    StringBuilder validationLog = new StringBuilder();
    InputStream inputStream = new FileInputStream(metadataXML);
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(inputStream);
    //Assigning JDOM Document to the validation context
    this.setMetadataXMLDocument(document);
    String xmlPrefix = properties.getProperty("module.f.metadata.xml.prefix");
    String xsdPrefix = properties.getProperty("module.f.table.xsd.prefix");
    //Setting the namespaces to access metadata.xml and the different table.xsd
    Element rootElement = document.getRootElement();
    String namespaceURI = rootElement.getNamespaceURI();
    Namespace xmlNamespace = Namespace.getNamespace(xmlPrefix, namespaceURI);
    Namespace xsdNamespace = Namespace.getNamespace(xsdPrefix, namespaceURI);
    //Assigning prefix to the validation context
    this.setXmlPrefix(xmlPrefix);
    this.setXsdPrefix(xsdPrefix);
    //Assigning namespace info to the validation context
    this.setXmlNamespace(xmlNamespace);
    this.setXsdNamespace(xsdNamespace);
    if (this.getXmlNamespace() != null && this.getXsdNamespace() != null && this.getXmlPrefix() != null
            && this.getXsdPrefix() != null && this.getMetadataXMLDocument() != null
            && this.getValidationProperties() != null) {
        //Updating the validation log
        String message = properties.getProperty("successfully.executed");
        validationLog.append(me + message);
        successfullyCommitted = true;//w w w. j  av a 2 s.com
    } else {
        String message = "has failed";
        validationLog.append(me + message);
    }
    this.getValidationLog().append(validationLog);
    return successfullyCommitted;
}

From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationEcolumnModuleImpl.java

License:Open Source License

private boolean prepareXMLAccess(ValidationContext validationContext)
        throws JDOMException, IOException, Exception {
    boolean successfullyCommitted = false;
    Properties properties = validationContext.getValidationProperties();
    File metadataXML = validationContext.getMetadataXML();
    InputStream inputStream = new FileInputStream(metadataXML);
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(inputStream);
    // Assigning JDOM Document to the validation context
    validationContext.setMetadataXMLDocument(document);
    String xmlPrefix = properties.getProperty("module.e.metadata.xml.prefix");
    String xsdPrefix = properties.getProperty("module.e.table.xsd.prefix");
    // Setting the namespaces to access metadata.xml and the different
    // table.xsd/*  w  w w.j ava2 s .  com*/
    Element rootElement = document.getRootElement();
    String namespaceURI = rootElement.getNamespaceURI();
    Namespace xmlNamespace = Namespace.getNamespace(xmlPrefix, namespaceURI);
    Namespace xsdNamespace = Namespace.getNamespace(xsdPrefix, namespaceURI);
    // Assigning prefix to the validation context
    validationContext.setXmlPrefix(xmlPrefix);
    validationContext.setXsdPrefix(xsdPrefix);
    // Assigning namespace info to the validation context
    validationContext.setXmlNamespace(xmlNamespace);
    validationContext.setXsdNamespace(xsdNamespace);
    if (validationContext.getXmlNamespace() != null && validationContext.getXsdNamespace() != null
            && validationContext.getXmlPrefix() != null && validationContext.getXsdPrefix() != null
            && validationContext.getMetadataXMLDocument() != null
            && validationContext.getValidationProperties() != null) {
        this.setValidationContext(validationContext);
        successfullyCommitted = true;
    } else {
        successfullyCommitted = false;
        this.setValidationContext(null);
        throw new Exception();
    }
    return successfullyCommitted;
}

From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationFrowModuleImpl.java

License:Open Source License

private boolean prepareXMLAccess(ValidationContext validationContext)
        throws JDOMException, IOException, Exception {
    boolean successfullyCommitted = false;
    Properties properties = validationContext.getValidationProperties();
    File metadataXML = validationContext.getMetadataXML();
    InputStream inputStream = new FileInputStream(metadataXML);
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(inputStream);
    // Assigning JDOM Document to the validation context
    validationContext.setMetadataXMLDocument(document);
    String xmlPrefix = properties.getProperty("module.f.metadata.xml.prefix");
    String xsdPrefix = properties.getProperty("module.f.table.xsd.prefix");
    // Setting the namespaces to access metadata.xml and the different
    // table.xsd/*  w  ww . j  av  a  2 s . c  o  m*/
    Element rootElement = document.getRootElement();
    String namespaceURI = rootElement.getNamespaceURI();
    Namespace xmlNamespace = Namespace.getNamespace(xmlPrefix, namespaceURI);
    Namespace xsdNamespace = Namespace.getNamespace(xsdPrefix, namespaceURI);
    // Assigning prefix to the validation context
    validationContext.setXmlPrefix(xmlPrefix);
    validationContext.setXsdPrefix(xsdPrefix);
    // Assigning namespace info to the validation context
    validationContext.setXmlNamespace(xmlNamespace);
    validationContext.setXsdNamespace(xsdNamespace);
    if (validationContext.getXmlNamespace() != null && validationContext.getXsdNamespace() != null
            && validationContext.getXmlPrefix() != null && validationContext.getXsdPrefix() != null
            && validationContext.getMetadataXMLDocument() != null
            && validationContext.getValidationProperties() != null) {
        this.setValidationContext(validationContext);
        successfullyCommitted = true;
    } else {
        successfullyCommitted = false;
        this.setValidationContext(null);
        throw new Exception();
    }
    return successfullyCommitted;
}

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

License:Apache License

/**
 * Recursive method that mixes two XML trees in the following way:
 * <ul>/*from   w ww  .j a va 2 s.com*/
 * <li>If a child exists at the source leaf but not at destination, the
 * element is copied as a child to the destination leaf</li>
 * <li>If a child exists at both the source and the destination leafs and
 * the source child has children, this method is recursively called to mix
 * both children</li>
 * </ul>
 * Some important remarks:
 * <ul>
 * <li>Equality comparison is not made via common methods but via
 * {@link JDOMUtils#elementsEqualAtCNameAndAttributes(Element, Element)}
 * .</li>
 * <li>Results of this method are returned as changes to the
 * destinationLeaf.</li>
 * <li>An attribute may be appended to all the elements added to the
 * destination leaf by this method.</li>
 * <li>Elements of a concrete namespace can be ignored by this method, if desired.</li>
 * </ul>
 * 
 * @param sourceLeaf
 *            The source leaf to mix into the destination leaf. It remains
 *            unchanged.
 * @param destinationLeaf
 *            The destination leaf, where the source leaf will be mixed
 *            into. Results will be returned as changes at this element, so
 *            IT WILL NOT REMAIN UNCHANGED AFTER THIS METHOD (normally). You
 *            should consider using {@link Element#clone()} if necessary.
 * @param metadataAttributeToAppend
 *            an attribute to be appended to each element added by this
 *            method to the destinationLeaf. If a node with descendants is
 *            added, the attribute will be added only to the top-level
 *            element (not to all the descendants). If null, no attribute
 *            will be added.
 * @param ignoreNamespaceURIIfTextPresent any element whose namespace URI matches this one 
 *            will be ignored if the source element has text content. 
 *            If null, no element is ignored.
 */
private void mixTreesRecursive(Element sourceLeaf, Element destinationLeaf, Attribute metadataAttributeToAppend,
        String ignoreNamespaceURIIfTextPresent) {
    List<Content> sourceLeafContent = sourceLeaf.getContent();
    //j is the index for "only-element" content
    for (int i = 0, j = 0; i < sourceLeafContent.size(); i++) {
        Content currentContent = sourceLeafContent.get(i);
        if (!(currentContent instanceof Element)) {
            continue;
        }

        Element currentSourceChild = (Element) currentContent;

        Element currentDestinationChild = searchElementEqualAtCNameAndAttributes(destinationLeaf.getChildren(),
                currentSourceChild);

        if (currentDestinationChild == null) {

            if (ignoreNamespaceURIIfTextPresent != null && !destinationLeaf.getTextNormalize().equals("")
                    && ignoreNamespaceURIIfTextPresent.equals(currentSourceChild.getNamespaceURI())) {
                continue;
            }

            // There is not equivalent node at destination, so we copy the
            // whole currentSourceChild.
            Element elementToAdd = currentSourceChild.clone();
            if (metadataAttributeToAppend != null) {
                elementToAdd.setAttribute(metadataAttributeToAppend.clone());
            }
            destinationLeaf.addContent(j, elementToAdd);
        } else {
            // Element exists at destination. If it has children, we recurse
            // to fill them (they might be not completely filled).
            if (currentSourceChild.getChildren().size() > 0) {
                mixTreesRecursive(currentSourceChild, currentDestinationChild, metadataAttributeToAppend,
                        ignoreNamespaceURIIfTextPresent);
            }
        }
        j++;
    }
}

From source file:com.c4om.utils.xmlutils.JDOMUtils.java

License:Apache License

/**
 * Method that checks whether two elements are the same in the following way:
 * <ul>//from ww  w  .  j  av  a  2 s  .c o  m
 * <li>Have the same name</li>
 * <li>Have the same namespace URI</li>
 * <li>Have the same attribute sets (names, namespaces and values)</li>
 * </ul>
 * @param element1 one element
 * @param element2 another element
 * @return the result of the comparison
 */
public static boolean elementsEqualAtCNameAndAttributes(Element element1, Element element2) {
    boolean haveSameName = element1.getName().equals(element2.getName());
    boolean haveSameNamespaceURI = element1.getNamespaceURI().equals(element2.getNamespaceURI());
    Set<Attribute> attrSet1 = new HashSet<>(element1.getAttributes());
    Set<Attribute> attrSet2 = new HashSet<>(element2.getAttributes());
    boolean attrSetsEquals = attrSet1.equals(attrSet2);
    return haveSameName && haveSameNamespaceURI & attrSetsEquals;
}

From source file:com.izforge.izpack.util.xmlmerge.action.FullMergeAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement)
        throws AbstractXmlMergeException {

    logger.fine("Merging: " + originalElement + " (original) and " + patchElement + " (patch)");

    Mapper mapper = (Mapper) m_mapperFactory.getOperation(originalElement, patchElement);

    if (originalElement == null) {
        outputParentElement.addContent(mapper.map(patchElement));
    } else if (patchElement == null) {
        outputParentElement.addContent((Content) originalElement.clone());
    } else {/*from   ww  w  . j  a  va2 s.  c  om*/

        Element workingElement = new Element(originalElement.getName(), originalElement.getNamespacePrefix(),
                originalElement.getNamespaceURI());
        addAttributes(workingElement, originalElement);

        logger.fine("Adding " + workingElement);
        outputParentElement.addContent(workingElement);

        doIt(workingElement, originalElement, patchElement);
    }

}

From source file:com.izforge.izpack.util.xmlmerge.action.OrderedMergeAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement)
        throws AbstractXmlMergeException {

    logger.fine("Merging: " + originalElement + " (original) and " + patchElement + "(patch)");

    Mapper mapper = (Mapper) m_mapperFactory.getOperation(originalElement, patchElement);

    if (originalElement == null) {
        outputParentElement.addContent(mapper.map(patchElement));
    } else if (patchElement == null) {
        outputParentElement.addContent((Content) originalElement.clone());
    } else {/*www .j  a  v a 2  s.c  o  m*/

        Element workingElement = new Element(originalElement.getName(), originalElement.getNamespacePrefix(),
                originalElement.getNamespaceURI());
        addAttributes(workingElement, originalElement);

        logger.fine("Adding " + workingElement);
        outputParentElement.addContent(workingElement);

        doIt(workingElement, originalElement, patchElement);
    }

}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.TypesExtractorImpl.java

License:Apache License

/**
 * Returns a path of the element made of the name of the elements and their prefixes (or namespace URIs). 
 * Prefixes (or URIs) are separated from element names by :, so THEY MUST BE REPLACED BY _ if they are 
 * going to be used to build type names.
 * Note that the : WILL always appear, although there is not any namespace prefix.
 * If a solved namespace-prefix mapping is given, the prefix mapped to the namespace of the element will be used instead of the prefix of the element. 
 * @param element the element//from www .  ja  v a  2  s .  com
 * @param config current inference configuration
 * @param useURI if true, the URI is used to build the path, if false, the prefix is used
 * @param solvedNamespaceToPrefixMapping the solved mappings between the namespace URIs and prefixes
 * @return a list that represents the path. Each element of the list is a path element.
 * @throws NullPointerException if any argument is null
 */
public static List<String> getRealPathOfElementUnfiltered(Element element, XSDInferenceConfiguration config,
        boolean useURI, Map<String, String> solvedNamespaceToPrefixMapping) {
    checkNotNull(element, "'element' must not be null");
    checkNotNull(config, "'config' must not be null");
    LinkedList<String> path = new LinkedList<String>();
    Element currentElement = element;
    do {
        String e;
        if (useURI) {
            e = currentElement.getNamespaceURI() + ":" + currentElement.getName();
        } else {
            String effectivePrefix = solvedNamespaceToPrefixMapping != null
                    ? solvedNamespaceToPrefixMapping.get(currentElement.getNamespaceURI())
                    : currentElement.getNamespacePrefix();
            e = effectivePrefix + ":" + currentElement.getName();
        }
        path.addFirst(e);
    } while ((currentElement = currentElement.getParentElement()) != null);
    return path;
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.TypesExtractorImpl.java

License:Apache License

/**
 * This method traverses all the elements of each input document in order to find all the namespace URI to prefix mappings present at the documents.
 * With that information, the map between namespace URIs and their known prefixes is filled. It is used later to solve which prefix should be bound 
 * to each namespace URI.// ww  w. jav  a 2s. c  om
 */
private void fillKnownNamespaceToPrefixMappings() {
    Filter<Element> elementFilter = Filters.element();
    for (int i = 0; i < xmlDocuments.size(); i++) {
        for (Element element : xmlDocuments.get(i).getDescendants(elementFilter)) {
            for (Namespace namespace : element.getNamespacesInScope()) {
                //We do not add XSI to the known namespaces 
                if (namespace.getURI().equalsIgnoreCase(XSI_NAMESPACE_URI))
                    continue;
                String uri = namespace.getURI();
                String prefix = namespace.getPrefix();
                SortedSet<String> currentPrefixes = prefixNamespaceMapping.get(uri);
                if (currentPrefixes == null) {
                    currentPrefixes = new TreeSet<String>();
                    prefixNamespaceMapping.put(uri, currentPrefixes);
                }
                currentPrefixes.add(prefix);
            }
            //If the element belongs to the empty namespace (empty string) with no prefix, we must add 
            //this to the prefix-namespace mapping explicitly
            if (element.getNamespacePrefix().equals("") && element.getNamespaceURI().equals("")) {
                String uri = "";
                String prefix = "";
                SortedSet<String> currentPrefixes = prefixNamespaceMapping.get(uri);
                if (currentPrefixes == null) {
                    currentPrefixes = new TreeSet<>();
                    prefixNamespaceMapping.put(uri, currentPrefixes);
                }
                currentPrefixes.add(prefix);
            }
        }
    }
}