Example usage for org.jdom2 Element getNamespacePrefix

List of usage examples for org.jdom2 Element getNamespacePrefix

Introduction

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

Prototype

public String getNamespacePrefix() 

Source Link

Document

Returns the namespace prefix of the element or an empty string if none exists.

Usage

From source file:ca.nrc.cadc.xml.JsonOutputter.java

License:Open Source License

private void writeElement(Element e, PrintWriter w, int i, boolean listItem) throws IOException {
    boolean childListItem = listElementNames.contains(e.getName());

    indent(w, i);/* ww w. j av a  2s  .c  o  m*/

    if (!listItem) {
        // write key
        w.print(QUOTE);
        if (StringUtil.hasText(e.getNamespacePrefix())) {
            w.print(e.getNamespacePrefix());
            w.print(":");
        }
        w.print(e.getName());
        w.print(QUOTE);
        w.print(" : ");
    }

    // write value
    w.print("{");
    boolean multiLine = true;
    boolean children = false;
    boolean attrs = writeAttributes(e, w, i + 1);

    if (childListItem) {
        // in badgerfish, this would be the name of child elements but prefer $ since [] 
        // is the value of e and e is a list of children; $ is also consistent with how
        // we (and badgerfish) handle leaf values inside elements: { "$" : value }
        if (attrs) {
            w.print(",");
        }
        indent(w, i + 1);
        w.print(QUOTE);
        w.print("$");
        w.print(QUOTE);
        w.print(" : ");
        w.print("[");
        children = writeChildElements(e, w, i + 2, childListItem, attrs);
        indent(w, i + 1);
        w.print("]");
    } else
        children = writeChildElements(e, w, i + 1, childListItem, attrs);

    if (!children && !childListItem) // plain value
    {
        if (attrs) {
            w.print(",");
            indent(w, i + 1);
        } else
            multiLine = false;
        String sval = e.getTextNormalize();
        w.print(QUOTE);
        w.print("$");
        w.print(QUOTE);
        w.print(" : ");
        if (isBoolean(e.getName(), sval) || isNumeric(e.getName(), sval))
            w.print(sval);
        else {
            w.print(QUOTE);
            w.print(sval);
            w.print(QUOTE);
        }
    }
    if (multiLine)
        indent(w, i);
    w.print("}");
}

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  ava2 s  .  com

        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 {//from  w  w  w .  j  a v a  2s  .  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.kixeye.kixmpp.KixmppWebSocketCodec.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
    WebSocketFrame frame = null;//w  ww  .  j  a  v  a2 s .c om

    if (msg instanceof Element) {
        Element element = (Element) msg;

        if (element.getNamespace() == null || element.getNamespace() == Namespace.NO_NAMESPACE) {
            if ("stream".equals(element.getNamespacePrefix())) {
                element.setNamespace(Namespace.getNamespace("http://etherx.jabber.org/streams"));
            } else {
                element.setNamespace(Namespace.getNamespace("jabber:client"));

                IteratorIterable<Content> descendants = element.getDescendants();

                while (descendants.hasNext()) {
                    Content content = descendants.next();

                    if (content instanceof Element) {
                        Element descendantElement = (Element) content;
                        if (descendantElement.getNamespace() == null
                                || descendantElement.getNamespace() == Namespace.NO_NAMESPACE) {
                            descendantElement.setNamespace(element.getNamespace());
                        }
                    }
                }
            }
        }

        ByteBuf binaryData = ctx.alloc().buffer();
        new XMLOutputter().output((Element) msg, new ByteBufOutputStream(binaryData));

        frame = new TextWebSocketFrame(binaryData);
    } else if (msg instanceof KixmppStreamStart) {
        KixmppStreamStart streamStart = (KixmppStreamStart) msg;

        StringWriter writer = new StringWriter();

        if (streamStart.doesIncludeXmlHeader()) {
            writer.append("<?xml version='1.0' encoding='UTF-8'?>");
        }
        writer.append("<stream:stream ");
        if (streamStart.getId() != null) {
            writer.append(String.format("id=\"%s\" ", streamStart.getId()));
        }
        if (streamStart.getFrom() != null) {
            writer.append(String.format("from=\"%s\" ", streamStart.getFrom().getFullJid()));
        }
        if (streamStart.getTo() != null) {
            writer.append(String.format("to=\"%s\" ", streamStart.getTo()));
        }
        writer.append(
                "version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">");

        frame = new TextWebSocketFrame(writer.toString());
    } else if (msg instanceof KixmppStreamEnd) {
        frame = new TextWebSocketFrame("</stream:stream>");
    } else if (msg instanceof String) {
        frame = new TextWebSocketFrame((String) msg);
    } else if (msg instanceof ByteBuf) {
        frame = new TextWebSocketFrame((ByteBuf) msg);
    }

    if (frame != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Sending: [{}]", frame.content().toString(StandardCharsets.UTF_8));
        }

        out.add(frame);
    }
}

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/*  w  w w  .  j  av a  2  s  .  co  m*/
 * @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./*from   ww w  .  ja  v  a  2  s  .  co m*/
 */
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);
            }
        }
    }
}

From source file:org.yawlfoundation.yawl.resourcing.util.DataSchemaBuilder.java

License:Open Source License

/**
 * Clones a set of attributes. Needs to be done this way to (i) break the
 * parental attachment to the attribute; and (ii) to fix any errant namespace
 * prefixes/*from  w  w  w .  ja  v a2s.com*/
 * @param element the element with the attributes to clone
 * @param defNS the default namespace
 * @return the List of clone attributes
 */
private List<Attribute> cloneAttributes(Element element, Namespace defNS) {
    String prefix = element.getNamespacePrefix();
    List<Attribute> cloned = new ArrayList<Attribute>();
    for (Attribute attribute : element.getAttributes()) {
        String value = getAttributeValue(attribute, prefix, defNS);
        Attribute copy = new Attribute(attribute.getName(), value);
        cloned.add(copy);
    }
    return cloned;
}