Example usage for org.dom4j Element getQName

List of usage examples for org.dom4j Element getQName

Introduction

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

Prototype

QName getQName();

Source Link

Document

Returns the QName of this element which represents the local name, the qualified name and the Namespace.

Usage

From source file:nl.tue.gale.conversion.aha3.CLConvert.java

License:Open Source License

private void convertVariable(Element element) {
    if ("gale".equals(element.getQName().getNamespacePrefix()))
        return;//from  w  ww.j a v  a  2s  . c o m
    System.out.println("-- variable found: " + element.asXML());
}

From source file:nl.tue.gale.conversion.aha3.CLConvert.java

License:Open Source License

@SuppressWarnings("unchecked")
private void convertIf(Element element) {
    if ("gale".equals(element.getQName().getNamespacePrefix()))
        return;//from w w  w  .ja  v a  2s  .  c  o m
    element.setQName(DocumentFactory.getInstance().createQName("if", "gale", "http://gale.tue.nl/adaptation"));
    String expr = convertExpr(element.attributeValue("expr"));
    element.addAttribute("expr", expr);
    List<Element> blocks = element.elements("block");
    if (blocks.size() > 0) {
        blocks.get(0).setQName(
                DocumentFactory.getInstance().createQName("then", "gale", "http://gale.tue.nl/adaptation"));
    }
    if (blocks.size() > 1) {
        blocks.get(1).setQName(
                DocumentFactory.getInstance().createQName("else", "gale", "http://gale.tue.nl/adaptation"));
    }
}

From source file:org.alfresco.module.vti.web.ws.GetListEndpoint.java

License:Open Source License

@SuppressWarnings("unchecked")
private void copyElement(Element source, Element target) {
    Element copy = target.addElement(source.getQName());
    for (Attribute attr : (List<Attribute>) source.attributes()) {
        copy.addAttribute(attr.getQName(), attr.getValue());
    }/*from  ww  w .  j  av  a2 s. c  om*/
    for (Element child : (List<Element>) source.elements()) {
        copyElement(child, copy);
    }
    if (source.getText() != null) {
        copy.setText(source.getText());
    }
}

From source file:org.alfresco.repo.search.CannedQueryDefImpl.java

License:Open Source License

public static CannedQueryDefImpl createCannedQuery(Element element, DictionaryService dictionaryService,
        QueryCollection container, NamespacePrefixResolver nspr) {
    if (element.getQName().getName().equals(ELEMENT_QNAME.getName())) {
        QName qName = null;/*ww w  .ja v a  2  s .  co  m*/
        Element qNameElement = element.element(QNAME.getName());
        if (qNameElement != null) {
            qName = QName.createQName(qNameElement.getText(), container.getNamespacePrefixResolver());
        }

        String language = null;
        Element languageElement = element.element(LANGUAGE.getName());
        if (languageElement != null) {
            language = languageElement.getText();
        }

        String query = null;
        Element queryElement = element.element(QUERY.getName());
        if (queryElement != null) {
            query = queryElement.getText();
        }

        List<QueryParameterDefinition> queryParameterDefs = new ArrayList<QueryParameterDefinition>();

        List list = element.elements(QueryParameterDefImpl.getElementQName().getName());
        for (Iterator it = list.iterator(); it.hasNext(); /**/) {
            Element defElement = (Element) it.next();
            NamedQueryParameterDefinition nqpd = QueryParameterDefImpl.createParameterDefinition(defElement,
                    dictionaryService, nspr);
            queryParameterDefs.add(nqpd.getQueryParameterDefinition());
        }

        list = element.elements(QueryParameterRefImpl.getElementQName().getName());
        for (Iterator it = list.iterator(); it.hasNext(); /**/) {
            Element refElement = (Element) it.next();
            NamedQueryParameterDefinition nqpd = QueryParameterRefImpl.createParameterReference(refElement,
                    dictionaryService, container);
            QueryParameterDefinition resolved = nqpd.getQueryParameterDefinition();
            if (resolved == null) {
                throw new AlfrescoRuntimeException("Unable to find refernce parameter : " + nqpd.getQName());
            }
            queryParameterDefs.add(resolved);
        }

        return new CannedQueryDefImpl(qName, language, query, queryParameterDefs, container);

    } else {
        return null;
    }
}

From source file:org.alfresco.repo.search.QueryParameterDefImpl.java

License:Open Source License

public static QueryParameterDefinition createParameterDefinition(Element element,
        DictionaryService dictionaryService, NamespacePrefixResolver nspr) {

    if (element.getQName().getName().equals(ELEMENT_QNAME.getName())) {
        QName qName = null;/*from   ww w .  j  ava 2s .  com*/
        Element qNameElement = element.element(DEF_QNAME.getName());
        if (qNameElement != null) {
            qName = QName.createQName(qNameElement.getText(), nspr);
        }

        PropertyDefinition propDef = null;
        Element propDefElement = element.element(PROPERTY_QNAME.getName());
        if (propDefElement != null) {
            propDef = dictionaryService.getProperty(QName.createQName(propDefElement.getText(), nspr));
        }

        DataTypeDefinition typeDef = null;
        Element typeDefElement = element.element(PROPERTY_TYPE_QNAME.getName());
        if (typeDefElement != null) {
            typeDef = dictionaryService.getDataType(QName.createQName(typeDefElement.getText(), nspr));
        }

        boolean hasDefault = false;
        String defaultValue = null;
        Element defaultValueElement = element.element(DEFAULT_VALUE.getName());
        if (defaultValueElement != null) {
            hasDefault = true;
            defaultValue = defaultValueElement.getText();
        }

        if (propDef != null) {
            return new QueryParameterDefImpl(qName, propDef, hasDefault, defaultValue);
        } else {
            return new QueryParameterDefImpl(qName, typeDef, hasDefault, defaultValue);
        }
    } else {
        return null;
    }
}

From source file:org.alfresco.repo.search.QueryParameterRefImpl.java

License:Open Source License

public static NamedQueryParameterDefinition createParameterReference(Element element,
        DictionaryService dictionaryService, QueryCollection container) {

    if (element.getQName().getName().equals(ELEMENT_QNAME.getName())) {
        QName qName = null;/*  www .  ja v a2 s. co  m*/
        Element qNameElement = element.element(DEF_QNAME.getName());
        if (qNameElement != null) {
            qName = QName.createQName(qNameElement.getText(), container.getNamespacePrefixResolver());
        }

        return new QueryParameterRefImpl(qName, container);
    } else {
        return null;
    }
}

From source file:org.craftercms.core.xml.mergers.impl.cues.impl.MergeParentAndChildMergeCue.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public Element merge(Element parent, Element child, Map<String, String> params) throws XmlMergeException {
    Element merged = DocumentHelper.createElement(child.getQName());
    org.craftercms.core.util.CollectionUtils.move(child.attributes(), merged.attributes());

    if (parent.isTextOnly() && child.isTextOnly()) {
        String parentText = parent.getText();
        String childText = child.getText();

        if (getMergeOrder(params).equalsIgnoreCase("after")) {
            merged.setText(parentText + childText);
        } else {/*from   ww  w  .  j a  v  a2 s. c  o m*/
            merged.setText(childText + parentText);
        }
    } else {
        List<Element> parentElements = parent.elements();
        List<Element> childElements = child.elements();
        List<Element> mergedElements = merged.elements();

        if (CollectionUtils.isNotEmpty(parentElements) && CollectionUtils.isNotEmpty(childElements)) {
            for (Iterator<Element> i = parentElements.iterator(); i.hasNext();) {
                Element parentElement = i.next();
                boolean elementsMerged = false;

                for (Iterator<Element> j = childElements.iterator(); !elementsMerged && j.hasNext();) {
                    Element childElement = j.next();
                    if (elementMergeMatcher.matchForMerge(parentElement, childElement)) {
                        MergeCueContext context = mergeCueResolver.getMergeCue(parentElement, childElement);
                        if (context != null) {
                            i.remove();
                            j.remove();

                            Element mergedElement = context.doMerge();
                            mergedElements.add(mergedElement);

                            elementsMerged = true;
                        } else {
                            throw new XmlMergeException("No merge cue was resolved for matching elements "
                                    + parentElement + " (parent) and " + childElement + " (child)");
                        }
                    }
                }
            }
        }

        if (getMergeOrder(params).equalsIgnoreCase("after")) {
            org.craftercms.core.util.CollectionUtils.move(parentElements, mergedElements);
            org.craftercms.core.util.CollectionUtils.move(childElements, mergedElements);
        } else {
            org.craftercms.core.util.CollectionUtils.move(childElements, mergedElements);
            org.craftercms.core.util.CollectionUtils.move(parentElements, mergedElements);
        }
    }

    return merged;
}

From source file:org.efaps.webdav4vfs.data.AbstractDavResource.java

License:Apache License

/**
 * Get property values. This method expects one of either &lt;allprop&gt;,
 * &lt;propnames&gt; or &lt;prop&gt;. If the element is &lt;prop&gt; it
 * will go through the list of it's children and request the values. For
 * &lt;allprop&gt; it will get values of all known properties and
 * &lt;propnames&gt; will return the names of all known properties.
 *
 * @param root       the root of the result document
 * @param propertyEl the prop, propname or allprop element
 * @return the root of the result document
 *//*from  www.j a  v  a 2  s .  c o  m*/
public Element getPropertyValues(final Element root, final Element propertyEl) {
    // initialize the <propstat> for 200
    final Element okPropStatEl = root.addElement(TAG_PROPSTAT);
    final Element okPropEl = okPropStatEl.addElement(TAG_PROP);

    // initialize the <propstat> element for 404
    final Element failPropStatEl = root.addElement(TAG_PROPSTAT);
    final Element failPropEl = failPropStatEl.addElement(TAG_PROP);

    if (TAG_ALLPROP.equalsIgnoreCase(propertyEl.getName())
            || TAG_PROPNAMES.equalsIgnoreCase(propertyEl.getName())) {

        boolean ignoreValue = TAG_PROPNAMES.equalsIgnoreCase(propertyEl.getName());

        // get all known standard properties
        for (String propName : ALL_PROPERTIES) {
            if (!getPropertyValue(okPropEl, propName, ignoreValue)) {
                failPropEl.addElement(propName);
            }
        }

        // additionally try to add all the custom properties
        try {
            final FileContent objectContent = object.getContent();
            for (String attributeName : objectContent.getAttributeNames()) {
                if (!getPropertyValue(okPropEl, attributeName, ignoreValue)) {
                    failPropEl.addElement(attributeName);
                }
            }
        } catch (final FileSystemException e) {
            LogFactory.getLog(getClass())
                    .error(String.format("can't read attribute properties from '%s'", object.getName()), e);
        }
    } else {
        final List<?> requestedProperties = propertyEl.elements();
        for (Object propertyElObject : requestedProperties) {
            final Element propEl = (Element) propertyElObject;
            final String nameSpace = propEl.getNamespaceURI();
            if (!getPropertyValue(okPropEl, getFQName(nameSpace, propEl.getName()), false)) {
                failPropEl.addElement(propEl.getQName());
            }
        }
    }

    // only add the OK section, if there is content
    if (okPropEl.elements().size() > 0) {
        okPropStatEl.addElement(TAG_STATUS).addText(STATUS_200);
    } else {
        okPropStatEl.detach();
    }

    // only add the failed section, if there is content
    if (failPropEl.elements().size() > 0) {
        failPropStatEl.addElement(TAG_STATUS).addText(STATUS_404);
    } else {
        failPropStatEl.detach();
    }

    return root;
}

From source file:org.efaps.webdav4vfs.data.DavResource.java

License:Apache License

@Override()
protected boolean setPropertyValue(Element root, Element propertyEl) {
    LogFactory.getLog(getClass()).debug(String.format("[%s].set('%s')", object.getName(), propertyEl.asXML()));

    if (!ALL_PROPERTIES.contains(propertyEl.getName())) {
        final String nameSpace = propertyEl.getNamespaceURI();
        final String attributeName = getFQName(nameSpace, propertyEl.getName());
        try {/*w  w  w .  j  ava2  s.c om*/
            FileContent objectContent = object.getContent();
            final String command = propertyEl.getParent().getParent().getName();
            if (TAG_PROP_SET.equals(command)) {
                StringWriter propertyValueWriter = new StringWriter();
                propertyEl.write(propertyValueWriter);
                propertyValueWriter.close();
                objectContent.setAttribute(attributeName, propertyValueWriter.getBuffer().toString());
            } else if (TAG_PROP_REMOVE.equals(command)) {
                objectContent.setAttribute(attributeName, null);
            }
            root.addElement(propertyEl.getQName());
            return true;
        } catch (IOException e) {
            LogFactory.getLog(getClass()).error(String.format("can't store attribute property '%s' = '%s'",
                    attributeName, propertyEl.asXML()), e);
        }
    }
    return false;
}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

@Override
public void setValue(Object value) {
    if (value == null)
        value = "";//null??
    if ((node instanceof org.dom4j.CharacterData || node instanceof Attribute || node instanceof DocumentType
            || node instanceof Entity || node instanceof ProcessingInstruction)) {
        String string = (String) TypeUtils.convert(value, String.class);
        if (string != null && !string.equals("")) {
            ((Node) node).setText(string);
        } else {/*from w w  w .java  2  s .co  m*/
            ((Node) node).getParent().remove((Node) node);
        }
    } else if (node instanceof Document) {
        Document theOriginalDoc = (Document) node;
        Element theOrigialRoot = theOriginalDoc.getRootElement();

        if (value instanceof Document) {//?document

            Document valueDoc = (Document) value;
            Element valueRoot = valueDoc.getRootElement();

            if (theOrigialRoot == null || valueRoot == null
                    || theOrigialRoot.getQName().equals(valueRoot.getQName())) {

                theOriginalDoc.clearContent();

                List content = valueDoc.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        theOriginalDoc.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueRoot.getQName() + " to " + theOrigialRoot.getQName());

            }

        } else if (value instanceof Element) {
            Element valueElem = (Element) value;
            if (valueElem.getQName().equals(theOrigialRoot.getQName())) {
                theOriginalDoc.clearContent();
                Element newValueElem = (Element) valueElem.clone();
                theOriginalDoc.setRootElement(newValueElem);
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElem.getQName() + " to " + theOrigialRoot.getQName());
            }
        } else {
            throw new RuntimeException("Can NOT assign " + value + " to " + theOrigialRoot.getQName());

        }
        //         else if (value instanceof Comment){
        //            Comment cmmt = (Comment)((Comment)value).clone();
        //            theOriginalDoc.add(cmmt);
        //            
        //         }else if (value instanceof ProcessingInstruction){
        //            ProcessingInstruction instru = (ProcessingInstruction)((ProcessingInstruction)value).clone();
        //            theOriginalDoc.add(instru);
        //         }

    } else if (node instanceof Element) {
        Element originalElem = ((Element) node);

        if (value != null && value instanceof Element) {
            Element valueElm = (Element) value;
            if (originalElem.getQName().equals(valueElm.getQName())) {
                originalElem.clearContent();
                List content = valueElm.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        originalElem.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElm.getQName() + " to " + originalElem.getQName());
            }

        } else if (value != null && value instanceof Text) {
            originalElem.clearContent();
            Text txt = (Text) ((Text) value).clone();
            originalElem.add(txt);
        } else if (value != null && value instanceof CDATA) {
            originalElem.clearContent();
            CDATA cdata = (CDATA) ((CDATA) value).clone();
            originalElem.add(cdata);
        } else if (value != null && value instanceof java.util.Date) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format((java.util.Date) value);
            originalElem.clearContent();
            originalElem.addText(dateStr);
        } else {
            String string = (String) TypeUtils.convert(value, String.class);
            originalElem.clearContent();
            originalElem.addText(string);
        }

    }

}