Example usage for org.dom4j QName get

List of usage examples for org.dom4j QName get

Introduction

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

Prototype

public static QName get(String localName, Namespace namespace, String qualifiedName) 

Source Link

Usage

From source file:org.apache.archiva.xml.XMLReader.java

License:Apache License

/**
 * Remove namespaces from element recursively.
 *//*from   ww  w.j  a  v a2s  .c o m*/
@SuppressWarnings("unchecked")
public void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));

    Node n;

    Iterator<Node> it = elem.elementIterator();
    while (it.hasNext()) {
        n = it.next();

        switch (n.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
            break;
        case Node.ELEMENT_NODE:
            removeNamespaces((Element) n);
            break;
        }
    }
}

From source file:org.eclipse.ecr.core.io.impl.ExportedDocumentImpl.java

License:Open Source License

protected void readProperty(Element parent, Namespace targetNs, Field field, Object value, boolean inlineBlobs)
        throws IOException {
    Type type = field.getType();/*from ww w .j a  v a 2  s.com*/
    QName name = QName.get(field.getName().getLocalName(), targetNs.prefix, targetNs.uri);
    Element element = parent.addElement(name);
    if (value == null) {
        return; // have no content
    }

    // extract the element content
    if (type.isSimpleType()) {
        element.addText(type.encode(value));
    } else if (type.isComplexType()) {
        ComplexType ctype = (ComplexType) type;
        if (TypeConstants.isContentType(ctype)) {
            readBlob(element, ctype, (Blob) value, inlineBlobs);
        } else {
            readComplex(element, ctype, (Map) value, inlineBlobs);
        }
    } else if (type.isListType()) {
        if (value instanceof List) {
            readList(element, (ListType) type, (List) value, inlineBlobs);
        } else if (value.getClass().getComponentType() != null) {
            readList(element, (ListType) type, PrimitiveArrays.toList(value), inlineBlobs);
        } else {
            throw new IllegalArgumentException("A value of list type is neither list neither array: " + value);
        }
    }
}

From source file:org.eclipse.ecr.core.io.impl.TypedExportedDocumentImpl.java

License:Open Source License

/**
 * Here we do what super does but add the "type" attribute to the XML
 * elements./* ww w  .  j a v a2s  . c  o m*/
 */
@Override
@SuppressWarnings("rawtypes")
protected void readProperty(Element parent, Namespace targetNs, Field field, Object value, boolean inlineBlobs)
        throws IOException {
    Type type = field.getType();
    QName name = QName.get(field.getName().getLocalName(), targetNs.prefix, targetNs.uri);
    Element element = parent.addElement(name);

    // extract the element content
    if (type.isSimpleType()) {
        element.addAttribute(TYPE_ATTRIBUTE, getSimpleTypeId(type));
        if (value != null) {
            element.addText(type.encode(value));
        }
    } else if (type.isComplexType()) {
        ComplexType ctype = (ComplexType) type;
        if (TypeConstants.isContentType(ctype)) {
            element.addAttribute(TYPE_ATTRIBUTE, TypeConstants.CONTENT);
            if (value != null) {
                readBlob(element, ctype, (Blob) value, inlineBlobs);
            }
        } else {
            element.addAttribute(TYPE_ATTRIBUTE, COMPLEX_TYPE_ID);
            if (value != null) {
                readComplex(element, ctype, (Map) value, inlineBlobs);
            }
        }
    } else if (type.isListType()) {
        element.addAttribute(TYPE_ATTRIBUTE,
                ((ListType) type).isScalarList() ? SCALAR_LIST_TYPE_ID : COMPLEX_LIST_TYPE_ID);
        if (value != null) {
            if (value instanceof List) {
                readList(element, (ListType) type, (List) value, inlineBlobs);
            } else if (value.getClass().getComponentType() != null) {
                readList(element, (ListType) type, PrimitiveArrays.toList(value), inlineBlobs);
            } else {
                throw new IllegalArgumentException(
                        "A value of list type is neither list neither array: " + value);
            }
        }
    }
}

From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java

License:Apache License

private void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));
    Node n = null;// ww w . jav a  2 s . co  m
    for (int i = 0; i < elem.content().size(); i++) {
        n = (Node) elem.content().get(i);
        if (n.getNodeType() == Node.ATTRIBUTE_NODE)
            ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
        if (n.getNodeType() == Node.ELEMENT_NODE)
            removeNamespaces((Element) n);
    }
}

From source file:org.intalio.tempo.workflow.fds.core.UserProcessMessageConvertor.java

License:Open Source License

/**
 * Converts a SOAP message from a user process to the WorkflowProcesses
 * format. <br>/*from  ww  w . j a va2s  . com*/
 * The conversion is done in-place. The passed <code>Document</code>
 * instance gets converted to the Workflow Processes format and its previous
 * format is lost.
 * 
 * @param message
 *            The SOAP message from a user process to convert to the
 *            Workflow Processes format.
 * @throws MessageFormatException
 *             If the specified message has an invalid format. Note that if
 *             this exception is thrown, <code>message</code> may have
 *             already been partly processed and therefore should be assumed
 *             to be corrupted.
 */
@SuppressWarnings("unchecked")
public void convertMessage(Document message) throws MessageFormatException, AxisFault {
    FormDispatcherConfiguration config = FormDispatcherConfiguration.getInstance();

    XPath xpath = null;
    xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body/soapenv:Fault");
    List<Node> fault = xpath.selectNodes(message);
    if (fault.size() != 0)
        throw new RuntimeException(fault.toString());

    // Check SOAP action
    xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body");
    xpath.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> bodyQueryResult = xpath.selectNodes(message);
    if (bodyQueryResult.size() != 0) {
        Element root = (Element) bodyQueryResult.get(0);
        if (root.asXML().indexOf("createTaskRequest") != -1) {
            _soapAction = "createTask";
            xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Header/addr:Action");
            xpath.setNamespaceURIs(MessageConstants.get_nsMap());
            List<Node> wsaActionQueryResult = xpath.selectNodes(message);
            if (wsaActionQueryResult.size() != 0) {
                Element wsaToElement = (Element) wsaActionQueryResult.get(0);
                wsaToElement.setText(_soapAction);
            } else
                _log.warn("Did not find addr:Action in SOAP header");
        }
    }
    _log.debug("Converted SOAP Action: " + _soapAction);

    /*
     * Change the wsa:To endpoint to Workflow Processes, if a wsa:To header
     * is present.
     */
    xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Header/addr:To");
    xpath.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> wsaToQueryResult = xpath.selectNodes(message);
    if (wsaToQueryResult.size() != 0) {
        Element wsaToElement = (Element) wsaToQueryResult.get(0);
        String workflowProcessesUrl = config.getPxeBaseUrl() + config.getWorkflowProcessesRelativeUrl();
        wsaToElement.setText(workflowProcessesUrl);
    } else
        _log.debug("Did not find addr:To in SOAP header");

    /*
     * Change the session address to be FDS endpoint and retrieve sender
     * endpoint
     */
    xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Header/intalio:callback/addr:Address");
    xpath.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> callbackToQueryResult = xpath.selectNodes(message);
    if (callbackToQueryResult.size() != 0) {
        Element wsaToElement = (Element) callbackToQueryResult.get(0);
        _userProcessEndpoint = wsaToElement.getText();
        wsaToElement.setText(config.getFdsUrl());
    } else
        _log.debug("Did not find intalio:callback/addr:Address in SOAP header");

    /* Next, fetch the user process namespace URI from the task metadata */
    /*
     * 1. fetch the first element of SOAP envelope body.
     */
    List<Node> allSoapBodyElements = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body//*")
            .selectNodes(message);
    if (allSoapBodyElements.size() == 0) {
        throw new MessageFormatException("No elements found inside soapenv:Body.");
    }
    Element firstPayloadElement = (Element) allSoapBodyElements.get(0);

    /*
     * 2. fetch its namespace and use it to fetch the userProcessEndpoint
     * and userProcessNamespaceURI element (which should be in the same
     * namespace). If those elements are not found, nothing is reported.
     * This is necessary for converting responses, where this information is
     * not present.
     */
    String messageNamespace = firstPayloadElement.getNamespaceURI();
    _userProcessNamespaceUri = messageNamespace;

    Map<String, String> namespaceURIs = new HashMap<String, String>(MessageConstants.get_nsMap());
    namespaceURIs.put(REQUEST_PREFIX, _userProcessNamespaceUri);

    /*
     * Add session in task meta data so that it can be retrieved when
     * workflow process needs to send a message to the user process
     */
    xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Header/intalio:callback/intalio:session");
    xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
    List<Node> sessionQueryResult = xpath.selectNodes(message);
    if (sessionQueryResult.size() != 0) {
        Element wsaToElement = (Element) sessionQueryResult.get(0);
        String session = wsaToElement.getText();
        xpath = DocumentHelper.createXPath("//" + REQUEST_PREFIX + ":taskMetaData");
        xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
        List<Node> tmdQueryResult = xpath.selectNodes(message);
        Element tmdElement = (Element) tmdQueryResult.get(0);
        Element sessionElement = tmdElement.addElement("session", MessageConstants.IB4P_NS);
        sessionElement.setText(session);
    }

    // retrieve userProcessEndpoint from task meta data
    // or put sender endpoint in task meta data if not defined
    xpath = DocumentHelper
            .createXPath("//" + REQUEST_PREFIX + ":taskMetaData/" + REQUEST_PREFIX + ":userProcessEndpoint");
    xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
    List<Node> endpointQueryResult = xpath.selectNodes(message);
    if (endpointQueryResult.size() != 0) {
        Element userProcessEndpointElement = (Element) endpointQueryResult.get(0);
        String value = userProcessEndpointElement.getText();
        if (value != null && value.length() > 0)
            _userProcessEndpoint = value;
        else if (_userProcessEndpoint != null) {
            _log.info("User process endpoint is empty in task metadata, adding " + _userProcessEndpoint);
            userProcessEndpointElement.setText(_userProcessEndpoint);
        }
    } else if (_userProcessEndpoint != null) {
        _log.info("User process endpoint is not defined in task metadata, adding " + _userProcessEndpoint);
        xpath = DocumentHelper.createXPath("//" + REQUEST_PREFIX + ":taskMetaData");
        xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
        List<Node> tmdQueryResult = xpath.selectNodes(message);
        if (tmdQueryResult.size() > 0) {
            Element wsaToElement = (Element) tmdQueryResult.get(0);
            Element nsElement = wsaToElement.addElement("userProcessEndpoint", MessageConstants.IB4P_NS);
            nsElement.setText(_userProcessEndpoint);
        }
    }

    // Add user process namespace to taskmetadata if not already defined
    xpath = DocumentHelper.createXPath(
            "//" + REQUEST_PREFIX + ":taskMetaData/" + REQUEST_PREFIX + ":userProcessNamespaceURI");
    xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
    List<Node> nsQueryResult = xpath.selectNodes(message);
    if (nsQueryResult.size() == 0 && _userProcessNamespaceUri != null) {
        xpath = DocumentHelper.createXPath("//" + REQUEST_PREFIX + ":taskMetaData");
        xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
        List<Node> tmdQueryResult = xpath.selectNodes(message);
        if (tmdQueryResult.size() > 0) {
            _log.info("User process namespace is not defined in task metadata, adding "
                    + _userProcessNamespaceUri);
            Element wsaToElement = (Element) tmdQueryResult.get(0);
            Element nsElement = wsaToElement.addElement("userProcessNamespaceURI", MessageConstants.IB4P_NS);
            nsElement.setText(_userProcessNamespaceUri);
        }
    } else {
        Element wsaToElement = (Element) nsQueryResult.get(0);
        if (wsaToElement.getTextTrim().length() == 0) {
            _log.info("User process namespace is empty in task metadata, adding " + _userProcessNamespaceUri);
            wsaToElement.setText(_userProcessNamespaceUri);
        }
    }

    /*
     * Now, change the namespace of all soapenv:Body elements, except the
     * task input and the attachments, to ib4p.
     */
    xpath = DocumentHelper.createXPath("//" + REQUEST_PREFIX + ":taskInput//*");
    xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
    List<Node> allTaskInputElements = xpath.selectNodes(message);
    xpath = DocumentHelper.createXPath("//" + REQUEST_PREFIX + ":attachments//*");
    xpath.setNamespaceURIs(namespaceURIs/* MessageConstants.get_nsMap() */);
    List<Node> allAttachmentsElements = xpath.selectNodes(message);
    for (int i = 0; i < allSoapBodyElements.size(); ++i) {
        Node node = (Node) allSoapBodyElements.get(i);
        if (!allTaskInputElements.contains(node) && !allAttachmentsElements.contains(node)) {

            Element element = (Element) node;
            element.remove(element.getNamespace());
            element.setQName(QName.get(element.getName(), "ib4p", MessageConstants.IB4P_NS));
        }
    }
}

From source file:org.intalio.tempo.workflow.fds.core.WorkflowProcessesMessageConvertor.java

License:Open Source License

/**
 * Converts a SOAP message from the Workflow Processes format to the format
 * of the user process the message is targetted for. <br>
 * The target user process is figured out from the message payload. <br>
 * The conversion is done in-place. The passed <code>Document</code>
 * instance gets converted to the user process format and its previous
 * format is lost./*from   w  w w .  j a  v  a 2s  . c o m*/
 * 
 * @param message
 *            The SOAP message coming from the Workflow Processes to convert
 *            to the user process format.
 * @param userProcessNamespaceUri
 *            The user process namespace URI. Should be <code>null</code>
 *            when converting the <i>requests</i>. Must be specified when
 *            converting the <i>replies</i>, since in this case no
 *            information about the target user process is specified inside
 *            the message.
 * @throws MessageFormatException
 *             If the specified message has an invalid format. Note that if
 *             this exception is thrown, <code>message</code> may have
 *             already been partly processed and therefore should be assumed
 *             to be corrupted.
 */
@SuppressWarnings("unchecked")
public void convertMessage(Document message, String userProcessNamespaceUri) throws MessageFormatException {
    XPath xpathSelector = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body/soapenv:Fault");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> fault = xpathSelector.selectNodes(message);
    if (fault.size() != 0) {
        // return fault as-is
        LOG.error("Fault in response:\n" + message.asXML());
        return;
    }

    //retrieve session
    xpathSelector = DocumentHelper
            .createXPath("/soapenv:Envelope/soapenv:Body/*[1]/ib4p:taskMetaData/ib4p:session");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> sessionNodes = xpathSelector.selectNodes(message);
    if (sessionNodes.size() > 0) {
        Element sessionElement = (Element) sessionNodes.get(0);
        String session = sessionElement.getText();

        //remove callback
        xpathSelector = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Header/intalio:callback");
        xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
        List<Node> callbackNodes = xpathSelector.selectNodes(message);
        if (callbackNodes.size() != 0) {
            Element wsaTo = (Element) callbackNodes.get(0);
            Element header = (Element) wsaTo.getParent();
            header.remove(wsaTo);
            sessionElement = header.addElement("session", MessageConstants.INTALIO_NS);
            sessionElement.setText(session);
        }
    }

    /* fetch the user process endpoint element from the task metadata */
    xpathSelector = DocumentHelper
            .createXPath("/soapenv:Envelope/soapenv:Body/*[1]/ib4p:taskMetaData/ib4p:userProcessEndpoint");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> userProcessEndpointNodes = xpathSelector.selectNodes(message);
    if (userProcessEndpointNodes.size() > 0) {
        /* found the user process endpoint element */
        Element userProcessEndpointElement = (Element) userProcessEndpointNodes.get(0);
        /* save it for later use */
        _userProcessEndpoint = userProcessEndpointElement.getText();

        /* do we have a wsa:To element? */
        xpathSelector = DocumentHelper.createXPath("//wsa:To");
        xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
        List<Node> wsaToNodes = xpathSelector.selectNodes(message);
        if (wsaToNodes.size() != 0) {
            /* We have the wsa:To element. Set the correct target endpoint */
            Element wsaTo = (Element) wsaToNodes.get(0);
            wsaTo.setText(_userProcessEndpoint);
        }

        /* do we have a addr:To element? */
        xpathSelector = DocumentHelper.createXPath("//addr:To");
        xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
        List<Node> addrToNodes = xpathSelector.selectNodes(message);
        if (addrToNodes.size() != 0) {
            /* We have the wsa:To element. Set the correct target endpoint */
            Element wsaTo = (Element) addrToNodes.get(0);
            //wsaTo.removeChildren();
            wsaTo.setText(_userProcessEndpoint);
        }
    }

    /*
     * If the user process namespace URI is not specified explicitly, the
     * userProcessNamespaceURI element must be present in the metadata
     * section.
     */
    if (userProcessNamespaceUri == null) {
        xpathSelector = DocumentHelper.createXPath(
                "/soapenv:Envelope/soapenv:Body/*[1]/ib4p:taskMetaData/ib4p:userProcessNamespaceURI");
        xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
        List<Node> namespaceElementQueryResult = xpathSelector.selectNodes(message);
        if (namespaceElementQueryResult.size() == 0) {
            throw new MessageFormatException("No user process namespace specified "
                    + "and no ib4p:userProcessNamespaceURI element present to determine those.");
        }
        Element userProcessNamespaceUriElement = (Element) namespaceElementQueryResult.get(0);
        userProcessNamespaceUri = userProcessNamespaceUriElement.getText();
        _userProcessNamespaceUri = userProcessNamespaceUri;
    }

    xpathSelector = DocumentHelper.createXPath(
            "/soapenv:Envelope/soapenv:Body/*[1]/ib4p:taskMetaData/ib4p:userProcessCompleteSOAPAction");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> soapActionQueryResult = xpathSelector.selectNodes(message);
    if (soapActionQueryResult.size() > 0) {
        Element soapActionElement = (Element) soapActionQueryResult.get(0);
        _soapAction = soapActionElement.getText();

        xpathSelector = DocumentHelper.createXPath("//addr:Action");
        xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
        List<Node> actionNodes = xpathSelector.selectNodes(message);
        if (actionNodes.size() > 0) {
            Element wsaTo = (Element) actionNodes.get(0);
            //wsaTo.removeChildren();
            wsaTo.setText(_soapAction);
        }
    }

    // TODO: generate a unique namespace prefix?
    String userProcessNamespace = "userProcess";

    /* Select all elements inside the soap envelope body. */
    xpathSelector = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body//*");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> bodyNodes = xpathSelector.selectNodes(message);
    /* Select all elements inside the task output payload. */
    xpathSelector = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body/*[1]/ib4p:taskOutput//*");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> taskOutputNodes = xpathSelector.selectNodes(message);
    /* Select all the attachments. */
    xpathSelector = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body//ib4p:attachments//*");
    xpathSelector.setNamespaceURIs(MessageConstants.get_nsMap());
    List<Node> attachementsNode = xpathSelector.selectNodes(message);

    /*
     * Change namespace for all the elements which are inside the soap
     * envelope body but not inside the task output payload.
     */
    for (int i = 0; i < bodyNodes.size(); ++i) {
        Node node = (Node) bodyNodes.get(i);

        if (!taskOutputNodes.contains(node) && !attachementsNode.contains(node)) {
            Element element = (Element) node;
            element.remove(element.getNamespace());
            element.setQName(QName.get(element.getName(), userProcessNamespace, userProcessNamespaceUri));
        }
    }
}

From source file:org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl.java

License:Apache License

protected void readProperty(Element parent, Namespace targetNs, Field field, Object value, boolean inlineBlobs)
        throws IOException {
    if (value == null) {
        return; // have no content
    }/*from  www .  jav  a 2s  . c  o  m*/
    Type type = field.getType();
    QName name = QName.get(field.getName().getLocalName(), targetNs.prefix, targetNs.uri);
    Element element = parent.addElement(name);

    // extract the element content
    if (type.isSimpleType()) {
        // use CDATA to avoid any bad interaction between content and envelope
        String encodedValue = type.encode(value);
        if (encodedValue != null) {
            // workaround embedded CDATA
            encodedValue = encodedValue.replaceAll("]]>", "]]]]><![CDATA[>");
        }
        element.addCDATA(encodedValue);
    } else if (type.isComplexType()) {
        ComplexType ctype = (ComplexType) type;
        if (TypeConstants.isContentType(ctype)) {
            readBlob(element, ctype, (Blob) value, inlineBlobs);
        } else {
            readComplex(element, ctype, (Map) value, inlineBlobs);
        }
    } else if (type.isListType()) {
        if (value instanceof List) {
            readList(element, (ListType) type, (List) value, inlineBlobs);
        } else if (value.getClass().getComponentType() != null) {
            readList(element, (ListType) type, PrimitiveArrays.toList(value), inlineBlobs);
        } else {
            throw new IllegalArgumentException("A value of list type is neither list neither array: " + value);
        }
    }
}

From source file:org.nuxeo.ecm.core.io.impl.TypedExportedDocumentImpl.java

License:Apache License

/**
 * Here we do what super does but add the "type" attribute to the XML elements.
 *///w w w  . j a  v a2  s.  c om
@Override
@SuppressWarnings("rawtypes")
protected void readProperty(Element parent, Namespace targetNs, Field field, Object value, boolean inlineBlobs)
        throws IOException {
    Type type = field.getType();
    QName name = QName.get(field.getName().getLocalName(), targetNs.prefix, targetNs.uri);
    Element element = parent.addElement(name);

    // extract the element content
    if (type.isSimpleType()) {
        element.addAttribute(TYPE_ATTRIBUTE, getSimpleTypeId(type));
        if (value != null) {
            element.addText(type.encode(value));
        }
    } else if (type.isComplexType()) {
        ComplexType ctype = (ComplexType) type;
        if (TypeConstants.isContentType(ctype)) {
            element.addAttribute(TYPE_ATTRIBUTE, TypeConstants.CONTENT);
            if (value != null) {
                readBlob(element, ctype, (Blob) value, inlineBlobs);
            }
        } else {
            element.addAttribute(TYPE_ATTRIBUTE, COMPLEX_TYPE_ID);
            if (value != null) {
                readComplex(element, ctype, (Map) value, inlineBlobs);
            }
        }
    } else if (type.isListType()) {
        String typeId;
        ListType listType = ((ListType) type);
        // Scalar list
        if (listType.isScalarList()) {
            typeId = SCALAR_LIST_TYPE_ID;
        }
        // Content list
        else if (TypeConstants.isContentType(listType.getFieldType())) {
            typeId = CONTENT_LIST_TYPE_ID;
        }
        // Complex list
        else {
            typeId = COMPLEX_LIST_TYPE_ID;
        }
        element.addAttribute(TYPE_ATTRIBUTE, typeId);
        if (value != null) {
            if (value instanceof List) {
                readList(element, (ListType) type, (List) value, inlineBlobs);
            } else if (value.getClass().getComponentType() != null) {
                readList(element, (ListType) type, PrimitiveArrays.toList(value), inlineBlobs);
            } else {
                throw new IllegalArgumentException(
                        "A value of list type is neither list neither array: " + value);
            }
        }
    }
}

From source file:org.nuxeo.export.doc.xml.ExportedDocumentAsXMLWithVocLabels.java

License:Open Source License

@Override
protected void readProperty(Element parent, Namespace targetNs, Field field, Object value, boolean inlineBlobs)
        throws IOException {

    // In all cases, first return the field value
    super.readProperty(parent, targetNs, field, value, inlineBlobs);

    // Then, add the label if the field declares it uses a vocabulary
    // by following the expected naming convention
    if (value != null && field.getType().isSimpleType()) {
        String fieldName = field.getName().toString();
        int pos = fieldName.indexOf(kFIELD_TAG_FOR_VOCABULARY);
        if (pos > 0) {
            String vocName = fieldName.substring(pos + kFIELD_TAG_FOR_VOCABULARY.length());
            if (vocName.isEmpty()) {
                log.error("Found tag \"" + kFIELD_TAG_FOR_VOCABULARY + "\" in field name " + fieldName
                        + ", but no vocabulary label afetr the tag");
            } else {
                String fieldValue = value.toString();
                org.nuxeo.ecm.directory.Session session = null;
                try {
                    session = getDirService().open(vocName);
                    if (session == null) { // Vocabulary not found
                        log.error("Vocabulary '" + vocName + "' not found");
                    } else {
                        DocumentModel directoryDoc = session.getEntry(fieldValue);
                        if (directoryDoc == null) {
                            log.error("Entry id <" + fieldValue + "> not found in vocabulary " + vocName);
                        } else {
                            DataModel dm = directoryDoc.getDataModels().values().iterator().next();
                            String label = (String) dm.getData("label");

                            QName name = QName.get(field.getName().getLocalName() + kFIELD_EXTENSION_FOR_XML,
                                    targetNs.prefix, targetNs.uri);
                            Element element = parent.addElement(name);
                            element.addCDATA(label);// Should be encoded?
                        }/*from   w  ww  .  j a  va2s .c om*/
                    }

                } catch (Exception e) {
                    log.error(e);
                } finally {
                    if (session != null) {
                        try {
                            session.close();
                        } catch (DirectoryException e) {
                            // We ignore this one
                        }
                    }
                }
            }
        }
    }
}

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

License:Open Source License

private void setDataType(DatatypeRef datatypeRef, Node node) {
    if (datatypeRef.types != null && datatypeRef.types.length > 0) {
        // This element is valid and has at least one assigned datatype

        // Attempt to set datatype name
        final Datatype datatype = datatypeRef.types[0];
        if (datatype instanceof XSDatatype) {
            final XSDatatype xsDatatype = (XSDatatype) datatype;
            final String dataTypeURI = xsDatatype.getNamespaceUri();
            final String dataTypeName = xsDatatype.getName();

            if (dataTypeName != null && !dataTypeName.equals(""))
                InstanceData.setSchemaType(node, QName.get(dataTypeName, "", dataTypeURI));
        }// w  w w  .  j  a  v  a2 s  .c o m
    }
}