Example usage for org.dom4j QName QName

List of usage examples for org.dom4j QName QName

Introduction

In this page you can find the example usage for org.dom4j QName QName.

Prototype

public QName(String name, Namespace namespace) 

Source Link

Usage

From source file:org.orbeon.oxf.xforms.XFormsUtils.java

License:Open Source License

/**
 * Return the underlying Node from the given NodeInfo, possibly converting it to a Dom4j Node. Changes to the returned Node may or may not
 * reflect on the original, depending on its type.
 *
 * @param nodeInfo      NodeInfo to process
 * @return              Node//w  w w  . ja va2s.c o m
 */
public static Node getNodeFromNodeInfoConvert(NodeInfo nodeInfo) {
    if (nodeInfo instanceof VirtualNode)
        return (Node) ((VirtualNode) nodeInfo).getUnderlyingNode();
    else if (nodeInfo.getNodeKind() == org.w3c.dom.Node.ATTRIBUTE_NODE) {
        return Dom4jUtils.createAttribute(
                new QName(nodeInfo.getLocalPart(), new Namespace(nodeInfo.getPrefix(), nodeInfo.getURI())),
                nodeInfo.getStringValue());
    } else
        return TransformerUtils.tinyTreeToDom4j(
                (nodeInfo.getParent() instanceof DocumentInfo) ? nodeInfo.getParent() : nodeInfo);
}

From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java

License:Open Source License

/**
 * Extract a QName from a string value, given namespace mappings. Return null if the text is empty.
 *
 * @param namespaces                prefix -> URI mappings
 * @param qNameString               QName to analyze
 * @param unprefixedIsNoNamespace   if true, an unprefixed value is in no namespace; if false, it is in the default namespace
 * @return                          a QName object or null if not found
 *///from   w  w  w .  ja v a 2 s.com
public static QName extractTextValueQName(Map<String, String> namespaces, String qNameString,
        boolean unprefixedIsNoNamespace) {
    if (qNameString == null)
        return null;
    qNameString = qNameString.trim();
    if (qNameString.length() == 0)
        return null;

    final int colonIndex = qNameString.indexOf(':');
    final String prefix;
    final String localName;
    final String namespaceURI;
    if (colonIndex == -1) {
        prefix = "";
        localName = qNameString;
        if (unprefixedIsNoNamespace) {
            namespaceURI = "";
        } else {

            final String nsURI = namespaces.get(prefix);
            namespaceURI = nsURI == null ? "" : nsURI;
        }
    } else if (colonIndex == 0) {
        throw new OXFException("Empty prefix for QName: " + qNameString);
    } else {
        prefix = qNameString.substring(0, colonIndex);
        localName = qNameString.substring(colonIndex + 1);
        namespaceURI = namespaces.get(prefix);
        if (namespaceURI == null) {
            throw new OXFException("No namespace declaration found for prefix: " + prefix);
        }
    }
    return new QName(localName, new Namespace(prefix, namespaceURI));
}

From source file:org.orbeon.oxf.xml.XMLUtils.java

License:Open Source License

public static org.dom4j.Document cleanXML(org.dom4j.Document doc, String stylesheetURL) {
    try {//www. j  av  a 2 s.co m
        final org.dom4j.Element element = doc.getRootElement();
        final String systemId = Dom4jUtils.makeSystemId(element);
        // The date to clean
        final DOMGenerator dataToClean = new DOMGenerator(doc, "clean xml", DOMGenerator.ZeroValidity,
                systemId);
        // The stylesheet
        URLGenerator stylesheetGenerator = new URLGenerator(stylesheetURL);
        // The transformation
        // Define the name of the processor (this is a QName)
        final QName processorName = new QName("xslt", XMLConstants.OXF_PROCESSORS_NAMESPACE);
        // Get a factory for this processor
        final ProcessorFactory processorFactory = ProcessorFactoryRegistry.lookup(processorName);
        if (processorFactory == null)
            throw new OXFException("Cannot find processor factory with name '"
                    + processorName.getNamespacePrefix() + ":" + processorName.getName() + "'");

        // Create processor
        final Processor xsltProcessor = processorFactory.createInstance();
        // Where the result goes
        DOMSerializer transformationOutput = new DOMSerializer();

        // Connect
        PipelineUtils.connect(stylesheetGenerator, "data", xsltProcessor, "config");
        PipelineUtils.connect(dataToClean, "data", xsltProcessor, "data");
        PipelineUtils.connect(xsltProcessor, "data", transformationOutput, "data");

        // Run the pipeline
        // Candidate for Scala withPipelineContext
        final PipelineContext pipelineContext = new PipelineContext();
        boolean success = false;
        try {
            final org.dom4j.Document result = transformationOutput.runGetDocument(pipelineContext);
            success = true;
            return result;
        } finally {
            pipelineContext.destroy(success);
        }
    } catch (Exception e) {
        throw new OXFException(e);
    }
}

From source file:org.pentaho.ui.xul.impl.XulParser.java

License:Open Source License

protected XulComponent getElement(org.dom4j.Element srcEle, XulContainer parent) throws XulException {

    String handlerName = srcEle.getName().toUpperCase();
    Attribute att = srcEle/*from  w ww . j  a  v a 2s .c  o  m*/
            .attribute(new QName("customclass", new Namespace("pen", "http://www.pentaho.org/2008/xul")));

    // If the custom handler is registered, use it; otherwise, fall back to the original element handler...
    if (att != null) {
        String potentialHandlerName = att.getValue().toUpperCase();
        if (handlers.get(potentialHandlerName) != null) {
            handlerName = potentialHandlerName;
        }
    }

    Object handler = handlers.get(handlerName);

    if (handler == null) {
        logger.error("handler not found: " + handlerName);
        return null;
        // throw new XulException(String.format("No handler available for input: %s", srcEle.getName()));
    }

    String tagName = srcEle.getName();
    try {

        Constructor<?> constructor = getContructor((String) handler);

        // create a generic element representation of the current Dom4J node
        Element domEle = DocumentFactory.createElement(srcEle.getName().toLowerCase());
        List<Attribute> attrs = srcEle.attributes();
        for (Attribute attr : attrs) {
            domEle.setAttribute(attr.getName(), attr.getValue());
        }

        XulComponent ele = (XulComponent) constructor.newInstance(domEle, parent, xulDomContainer, tagName);

        // preserve atributes in new Generic Dom node
        for (Attribute attr : attrs) {
            ele.setAttribute(attr.getName(), attr.getValue());
        }

        Map<String, String> attributesMap = XulUtil.AttributesToMap(srcEle.attributes());
        BeanUtils.populate(ele, attributesMap);
        return ele;

    } catch (Exception e) {
        throw new XulException(e);
    }

}

From source file:org.talend.mdm.webapp.browserecords.server.util.CommonUtil.java

License:Open Source License

public static void dynamicAssembleByResultOrder(ItemBean itemBean, List<String> viewableXpaths,
        EntityModel entityModel, Map<String, EntityModel> map, String language, boolean isStaging)
        throws Exception {

    if (itemBean.getItemXml() != null) {
        org.dom4j.Document docXml = DocumentHelper.parseText(itemBean.getItemXml());
        int i = 0;
        List<?> els = docXml.getRootElement().elements();
        for (String path : viewableXpaths) {
            String leafPath = path.substring(path.lastIndexOf('/') + 1);
            if (leafPath.startsWith("@")) { //$NON-NLS-1$
                String[] xsiType = leafPath.substring(leafPath.indexOf("@") + 1).split(":"); //$NON-NLS-1$//$NON-NLS-2$
                itemBean.set(path,//from  ww  w .  j  a  v  a  2  s.  c o  m
                        docXml.getRootElement()
                                .element(new QName(xsiType[1],
                                        new Namespace(xsiType[0], "http://www.w3.org/2001/XMLSchema-instance"))) //$NON-NLS-1$
                                .getText());
            } else {
                TypeModel typeModel = entityModel.getMetaDataTypes().get(path);
                org.dom4j.Element el = (org.dom4j.Element) els.get(i);
                if (typeModel != null && typeModel.getForeignkey() != null) {
                    String modelType = el.attributeValue(
                            new QName("type", new Namespace("tmdm", "http://www.talend.com/mdm"))); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
                    itemBean.set(path, path + "-" + el.getText()); //$NON-NLS-1$
                    itemBean.setForeignkeyDesc(path + "-" + el.getText(), //$NON-NLS-1$
                            org.talend.mdm.webapp.browserecords.server.util.CommonUtil.getForeignKeyDesc(
                                    typeModel, el.getText(), false, modelType, map.get(typeModel.getXpath()),
                                    isStaging, language));
                } else if (typeModel != null && DataTypeConstants.BOOLEAN.equals(typeModel.getType())) {
                    if (Constants.BOOLEAN_TRUE_DISPLAY_VALUE.equals(el.getText())
                            || Constants.BOOLEAN_TRUE_VALUE.equals(el.getText())) {
                        itemBean.set(path, Constants.BOOLEAN_TRUE_VALUE);
                    } else if (Constants.BOOLEAN_FALSE_DISPLAY_VALUE.equals(el.getText())
                            || Constants.BOOLEAN_FALSE_VALUE.equals(el.getText())) {
                        itemBean.set(path, Constants.BOOLEAN_FALSE_VALUE);
                    } else {
                        itemBean.set(path, el.getText());
                    }
                } else {
                    itemBean.set(path, el.getText());
                }
            }
            i++;
        }
    }
}

From source file:org.withinsea.izayoi.cortile.template.html.grammar.attr.core.ByContent.java

License:Mozilla Public License

@Override
@SuppressWarnings("unchecked")
@Priority(Priority.HIGHEST)//  w w w  . j  a  v  a  2s  . com
public void processAttr(Attribute attr) throws CortileException {

    Element elem = attr.getParent();
    String prefix = attr.getNamespacePrefix();
    List<Attribute> attrs = new ArrayList<Attribute>((List<Attribute>) elem.attributes());

    try {
        Element scope = elem;
        for (int i = attrs.indexOf(attr); i < attrs.size(); i++) {
            Attribute iAttr = attrs.get(i);
            String iAttrname = iAttr.getName();
            if (iAttr.getNamespacePrefix().equals(prefix) && iAttrname.matches("^\\w+\\-content.*")) {
                Element contentScope = new DefaultElement(HTMLConstants.ANONYMOUS_TAG_NAME);
                DOMUtils.surroundInsideBy(scope, contentScope);
                iAttr.detach();
                contentScope
                        .add(new SurroundableAttr(new QName(iAttrname.replaceFirst("^(\\w+)\\-content", "$1"),
                                iAttr.getQName().getNamespace()), iAttr.getValue()));
                for (int j = i + 1; j < attrs.size(); j++) {
                    Attribute jAttr = attrs.get(j);
                    jAttr.detach();
                    contentScope.add(jAttr);
                }
                scope = contentScope;
            }
        }
    } catch (Exception e) {
        throw new CortileException(e);
    }
}

From source file:org.xmpp.packet.Roster.java

License:Open Source License

/**
 * Adds a new item to the roster. If the roster packet already contains an item
 * using the same JID, the information in the existing item will be overwritten
 * with the new information.<p>//from  ww w  .  ja v  a  2  s . co m
 *
 * The XMPP specification recommends that if the roster item is associated with another
 * instant messaging user (human), that the JID be in bare form (e.g. user@domain).
 * Use the {@link JID#toBareJID() toBareJID()} method for a bare JID.
 *
 * @param jid the JID.
 * @param name the nickname.
 * @param ask the ask type.
 * @param subscription the subscription type.
 * @param groups a Collection of groups.
 * @return the newly created item.
 */
public Item addItem(JID jid, String name, Ask ask, Subscription subscription, Collection<String> groups) {
    if (jid == null) {
        throw new NullPointerException("JID cannot be null");
    }
    if (subscription == null) {
        throw new NullPointerException("Subscription cannot be null");
    }
    Element query = element.element(new QName("query", Namespace.get("jabber:iq:roster")));
    if (query == null) {
        query = element.addElement("query", "jabber:iq:roster");
    }
    Element item = null;
    for (Iterator i = query.elementIterator("item"); i.hasNext();) {
        Element el = (Element) i.next();
        if (el.attributeValue("jid").equals(jid.toString())) {
            item = el;
        }
    }
    if (item == null) {
        item = query.addElement("item");
    }
    item.addAttribute("jid", jid.toBareJID());
    item.addAttribute("name", name);
    if (ask != null) {
        item.addAttribute("ask", ask.toString());
    }
    item.addAttribute("subscription", subscription.toString());
    // Erase existing groups in case the item previously existed.
    for (Iterator i = item.elementIterator("group"); i.hasNext();) {
        item.remove((Element) i.next());
    }
    // Add in groups.
    if (groups != null) {
        for (String group : groups) {
            item.addElement("group").setText(group);
        }
    }
    return new Item(jid, name, ask, subscription, groups);
}

From source file:org.xmpp.packet.Roster.java

License:Open Source License

/**
 * Removes an item from this roster.//from  w w  w  . j a va 2 s  .  co  m
 *
 * @param jid the JID of the item to remove.
 */
public void removeItem(JID jid) {
    Element query = element.element(new QName("query", Namespace.get("jabber:iq:roster")));
    if (query != null) {
        for (Iterator i = query.elementIterator("item"); i.hasNext();) {
            Element item = (Element) i.next();
            if (item.attributeValue("jid").equals(jid.toString())) {
                query.remove(item);
                return;
            }
        }
    }
}

From source file:org.xmpp.packet.Roster.java

License:Open Source License

/**
 * Returns an unmodifiable copy of the {@link Item Items} in the roster packet.
 *
 * @return an unmodifable copy of the {@link Item Items} in the roster packet.
 *//*from  w w w .  j a v a  2s  .  co m*/
public Collection<Item> getItems() {
    Collection<Item> items = new ArrayList<Item>();
    Element query = element.element(new QName("query", Namespace.get("jabber:iq:roster")));
    if (query != null) {
        for (Iterator i = query.elementIterator("item"); i.hasNext();) {
            Element item = (Element) i.next();
            String jid = item.attributeValue("jid");
            String name = item.attributeValue("name");
            String ask = item.attributeValue("ask");
            String subscription = item.attributeValue("subscription");
            Collection<String> groups = new ArrayList<String>();
            for (Iterator j = item.elementIterator("group"); j.hasNext();) {
                Element group = (Element) j.next();
                groups.add(group.getTextTrim());
            }
            Ask askStatus = ask == null ? null : Ask.valueOf(ask);
            Subscription subStatus = subscription == null ? null : Subscription.valueOf(subscription);
            items.add(new Item(new JID(jid), name, askStatus, subStatus, groups));
        }
    }
    return Collections.unmodifiableCollection(items);
}

From source file:se.kb.oai.ore.Metadata.java

License:Apache License

/**
 * Helper method that creates a qualified name with the given namespace and name.
 * //  w ww .j  a v a2 s.  c o  m
 * @param ns a namespace, can only be DC or DCTERMS
 * @param name the name
 * @return a qualified name
 */
public static QName getName(Namespace ns, String name) {
    switch (ns) {
    case DC:
        return new QName(name, DC_NS);
    case DCTERMS:
        return new QName(name, DCTERMS_NS);
    default:
        break;
    }
    return null;
}