Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

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

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.buddycloud.channeldirectory.search.utils.FeatureUtils.java

License:Apache License

/**
 * Adds an attribute to the parent element if the corresponding 
 * feature is contained in the options map or this map is empty.
 * /*from  ww  w  . jav  a2 s.  c o m*/
 * @param options
 * @param parentElement
 * @param key
 * @param value
 * @return
 */
public static boolean addAttribute(Set<String> options, Element parentElement, String key, String value) {
    if (options.contains(key) || options.isEmpty()) {
        parentElement.addAttribute(key, value);
        return true;
    }
    return false;
}

From source file:com.buddycloud.friendfinder.handler.MatchContactFromContactProviderHandler.java

License:Apache License

private IQ createResponse(IQ iq, List<MatchedUser> matchedUsers) {
    IQ result = IQ.createResultIQ(iq);/*from w w  w  . j  a  va 2  s  .  c  om*/
    Element queryElement = result.getElement().addElement("query", getNamespace());
    for (MatchedUser user : matchedUsers) {
        Element itemEl = queryElement.addElement("item");
        itemEl.addAttribute("jid", user.getJid());
        itemEl.addAttribute("matched-hash", user.getHash());
    }
    return result;
}

From source file:com.buddycloud.friendfinder.handler.MatchContactHandler.java

License:Apache License

/**
 * @param iq//from  ww  w  . j  ava  2 s  .co m
 * @param matchedUsers
 * @return
 */
private IQ createResponse(IQ iq, List<MatchedUser> matchedUsers) {
    IQ result = IQ.createResultIQ(iq);
    Element queryElement = result.getElement().addElement("query", getNamespace());
    for (MatchedUser user : matchedUsers) {
        Element itemEl = queryElement.addElement("item");
        itemEl.addAttribute("jid", user.getJid());
        itemEl.addAttribute("matched-hash", user.getHash());
    }
    return result;
}

From source file:com.buddycloud.mediaserver.xmpp.MediaServerComponent.java

License:Apache License

protected IQ handleDiscoInfo(IQ iq) {
    IQ disco = super.handleDiscoInfo(iq);
    String endPoint = configuration.getProperty(MediaServerConfiguration.HTTP_ENDPOINT);
    if (endPoint != null) {
        Element queryEl = disco.getElement().element("query");
        Element xEl = queryEl.addElement("x", "jabber:x:data");
        xEl.addAttribute("type", "result");

        addField(xEl, "FORM_TYPE", "hidden", MediaServerConfiguration.BUDDYCLOUD_NS_API);
        addField(xEl, MediaServerConfiguration.API_ENDPOINT_FIELD_VAR, "text-single", endPoint);
    }//  www. j  ava  2 s  .  co m
    return disco;
}

From source file:com.buddycloud.mediaserver.xmpp.MediaServerComponent.java

License:Apache License

private void addField(Element xEl, String var, String type, String value) {
    Element fieldEl = xEl.addElement("field");
    fieldEl.addAttribute("var", var);
    fieldEl.addAttribute("type", type);
    fieldEl.addElement("value").setText(value);
}

From source file:com.buddycloud.mediaserver.xmpp.util.HTTPAuthMessageBuilder.java

License:Apache License

public Message createPacket() {
    Message m = new Message();
    m.setType(Type.normal);/*from w  w w.  j a  va2  s.  c o m*/
    m.setThread(thread);
    m.setBody("Confirmation message for transaction " + id);

    Element rootEl = m.getElement();
    rootEl.addAttribute("xmlns:stream", "http://etherx.jabber.org/streams");

    Element authEl = rootEl.addElement(ELEMENT_NAME, NAMESPACE);
    authEl.addAttribute("id", id);
    authEl.addAttribute("url", url);
    authEl.addAttribute("method", "GET");

    return m;
}

From source file:com.bullx.demo.xml.XMLParser.java

License:Open Source License

public static void bookListToXML(List<Book> books) {
    Document document = DocumentHelper.createDocument();
    // XMLbooks/*from   w  ww  .j a  va 2 s .c o  m*/
    Element booksElement = document.addElement("books");
    //  
    booksElement.addComment("This is a test for dom4j, liubida, 2012.8.11");

    for (Book book : books) {
        // 
        Element bookElement = booksElement.addElement("book");
        // : show
        bookElement.addAttribute("show", book.getShow() ? "yes" : "no");
        // title
        bookElement.addElement("title").setText(book.getTitle());
        // express
        bookElement.addElement("express").setText(book.getExpress());
    }

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    StringWriter out = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(out, format);
    try {
        xmlWriter.write(document);
        xmlWriter.flush();
        String s = out.toString();
        System.out.println(s);
        Log.info("xml done!");
    } catch (Exception e) {
        Log.error("xml error!");
    } finally {
        try {
            if (null != xmlWriter) {
                xmlWriter.close();
            }
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.church.gateway.Global.java

/**
 * Gets the modified menu xml.//from  www .  ja  v a2  s  . c  o  m
 * 
 * @param disallowed_modules
 *            the disallowed_modules
 * 
 * @return the modified menu xml
 * 
 * @throws Exception
 *             the exception
 */
public static String getModifiedMenuXml(String[] disallowed_modules) throws Exception {
    Document doc = DocumentHelper.parseText(Global.MENUS);
    Element root = doc.getRootElement();
    List<Node> list = root.selectNodes("//menu/menu");
    for (Node node : list) {
        Element elmt = (Element) node;
        if (elmt.attribute("class") != null) {
            for (String module : disallowed_modules)
                if (elmt.attribute("class").getValue().trim().equals(module))
                    elmt.addAttribute("visible", "false");
        }
    }
    return doc.asXML();
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
* Sets the Schema Location attribute on the root element
*
* @param document The Exchanger document
* @param schemaType The schema type (either schemaLocation or noNamespaceSchemaLocation)
* @param namespace The namespace// ww  w  .j  a  v a 2s  .co  m
* @param schemaURL The URL or the schema
* 
*/
public static void setSchemaLocation(ExchangerDocument document, String schemaType, String namespace,
        String schemaURL) {
    schemaURL = URLUtilities.encodeURL(schemaURL);

    XDocument xdoc = document.getDocument();
    Element root = xdoc.getRootElement();

    if (schemaType.equals(SCHEMALOCATION)) {
        Attribute attrNoNS = root.attribute(NOSCHEMALOCATION);
        if (attrNoNS != null) {
            root.remove(attrNoNS);
        }

        // need to set both namspace and url
        Attribute attr = root.attribute(SCHEMALOCATION);
        if (attr == null) {
            // does the schema instance already exist
            Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE);
            if (ns != null) {
                String schemaInstancePrefix = ns.getPrefix();
                StringBuffer name = new StringBuffer();
                name.append(schemaInstancePrefix);
                name.append(":");
                name.append(SCHEMALOCATION);

                StringBuffer value = new StringBuffer();
                value.append(namespace);
                value.append(" ");
                value.append(schemaURL);

                root.addAttribute(name.toString(), value.toString());
            } else {
                root.addNamespace("xsi", SCHEMAINSTANCE);

                StringBuffer name = new StringBuffer();
                name.append("xsi");
                name.append(":");
                name.append(SCHEMALOCATION);

                StringBuffer value = new StringBuffer();
                value.append(namespace);
                value.append(" ");
                value.append(schemaURL);

                root.addAttribute(name.toString(), value.toString());
            }
        } else {
            String attrValue = attr.getValue();

            // break up all the namespace and url pairs
            ArrayList stringValues = new ArrayList();

            StringTokenizer st = new StringTokenizer(attrValue);
            while (st.hasMoreTokens()) {
                stringValues.add(st.nextToken());
            }

            // update existing attribute, Note: it may have multiple attribute pairs
            StringBuffer value = new StringBuffer();
            value.append(namespace);
            value.append(" ");
            value.append(schemaURL);

            //need to start at the third value (i.e we only replace the first namespace-url pair)
            for (int i = 2; i < stringValues.size(); i++) {
                value.append(" ");
                value.append((String) stringValues.get(i));
            }

            attr.setValue(value.toString());
        }
    } else {
        // is of type "no schema location"
        Attribute attrSchema = root.attribute(SCHEMALOCATION);
        if (attrSchema != null) {
            root.remove(attrSchema);
        }

        // just need to set the url
        Attribute attr = root.attribute(NOSCHEMALOCATION);
        if (attr == null) {
            // does the schema instance already exist
            Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE);
            if (ns != null) {
                String schemaInstancePrefix = ns.getPrefix();
                StringBuffer name = new StringBuffer();
                name.append(schemaInstancePrefix);
                name.append(":");
                name.append(NOSCHEMALOCATION);

                root.addAttribute(name.toString(), schemaURL);
            } else {
                root.addNamespace("xsi", SCHEMAINSTANCE);

                StringBuffer name = new StringBuffer();
                name.append("xsi");
                name.append(":");
                name.append(NOSCHEMALOCATION);

                root.addAttribute(name.toString(), schemaURL);
            }
        } else {
            // update existing attribute
            attr.setValue(schemaURL);
        }
    }
}

From source file:com.collabnet.ccf.core.ga.GenericArtifactHelper.java

License:Open Source License

/**
 * Creates a generic artifact XML representation out of the Java object
 * /*  ww  w .  j a va 2 s  .  co m*/
 * @param genericArtifact
 *            Java object that will be represented as XML document
 * @return XML representation of generic artifact
 */
public static Document createGenericArtifactXMLDocument(GenericArtifact genericArtifact)
        throws GenericArtifactParsingException {
    Document document = DocumentHelper.createDocument();
    document.setXMLEncoding(EncodingAwareObject.UTF_8);
    // Create XML elements with attributes
    Element root = addRootElement(document, ARTIFACT_ROOT_ELEMENT_NAME, CCF_ARTIFACT_NAMESPACE);

    switch (genericArtifact.getArtifactAction()) {
    case CREATE: {
        addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_CREATE);
        break;
    }
    case DELETE: {
        addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_DELETE);
        break;
    }
    case IGNORE: {
        addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_IGNORE);
        break;
    }
    case UPDATE: {
        addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_UPDATE);
        break;
    }
    case RESYNC: {
        addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_RESYNC);
        break;
    }
    case UNKNOWN: {
        addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_UNKNOWN);
        break;
    }
    default: {
        throw new GenericArtifactParsingException(
                "Non valid value for root-attribute " + ARTIFACT_ACTION + " specified.");
    }
    }

    switch (genericArtifact.getArtifactMode()) {
    case CHANGEDFIELDSONLY: {
        addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_CHANGED_FIELDS_ONLY);
        break;
    }
    case COMPLETE: {
        addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_COMPLETE);
        break;
    }
    case UNKNOWN: {
        addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_UNKNOWN);
        break;
    }
    default: {
        throw new GenericArtifactParsingException(
                "Non valid value for root-attribute " + ARTIFACT_MODE + "specified.");
    }
    }

    ArtifactTypeValue artifactType = genericArtifact.getArtifactType();
    switch (artifactType) {
    case ATTACHMENT: {
        addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_ATTACHMENT);
        String content = genericArtifact.getArtifactValue();
        // TODO BASE64 validation?
        if (content != null)
            // embed content in CDATA section
            setValue(root, content, true);
        break;
    }
    case DEPENDENCY: {
        addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_DEPENDENCY);
        break;
    }
    case PLAINARTIFACT: {
        addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_PLAIN_ARTIFACT);
        break;
    }
    case UNKNOWN: {
        addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_UNKNOWN);
        break;
    }
    default: {
        throw new GenericArtifactParsingException(
                "Non valid value for root-attribute " + ARTIFACT_TYPE + " specified.");
    }
    }

    switch (genericArtifact.getIncludesFieldMetaData()) {
    case TRUE: {
        addAttribute(root, INCLUDES_FIELD_META_DATA, INCLUDES_FIELD_META_DATA_TRUE);
        break;
    }
    case FALSE: {
        addAttribute(root, INCLUDES_FIELD_META_DATA, INCLUDES_FIELD_META_DATA_FALSE);
        break;
    }
    default: {
        throw new GenericArtifactParsingException(
                "Non valid value for root-attribute " + ARTIFACT_MODE + "specified.");
    }
    }

    addAttribute(root, SOURCE_ARTIFACT_LAST_MODIFICATION_DATE,
            genericArtifact.getSourceArtifactLastModifiedDate());
    addAttribute(root, TARGET_ARTIFACT_LAST_MODIFICATION_DATE,
            genericArtifact.getTargetArtifactLastModifiedDate());
    // addAttribute(root, ARTIFACT_LAST_READ_TRANSACTION_ID, genericArtifact
    // .getLastReadTransactionId());
    addAttribute(root, ERROR_CODE, genericArtifact.getErrorCode());
    addAttribute(root, SOURCE_ARTIFACT_VERSION, genericArtifact.getSourceArtifactVersion());
    addAttribute(root, TARGET_ARTIFACT_VERSION, genericArtifact.getTargetArtifactVersion());
    addAttribute(root, CONFLICT_RESOLUTION_PRIORITY, genericArtifact.getConflictResolutionPriority());

    // only create optional attributes if necessary
    if (artifactType == ArtifactTypeValue.DEPENDENCY || artifactType == ArtifactTypeValue.ATTACHMENT
            || artifactType == ArtifactTypeValue.PLAINARTIFACT) {
        addAttribute(root, DEP_PARENT_SOURCE_ARTIFACT_ID, genericArtifact.getDepParentSourceArtifactId());
        addAttribute(root, DEP_PARENT_SOURCE_REPOSITORY_ID, genericArtifact.getDepParentSourceRepositoryId());
        addAttribute(root, DEP_PARENT_SOURCE_REPOSITORY_KIND,
                genericArtifact.getDepParentSourceRepositoryKind());
        addAttribute(root, DEP_PARENT_TARGET_ARTIFACT_ID, genericArtifact.getDepParentTargetArtifactId());
        addAttribute(root, DEP_PARENT_TARGET_REPOSITORY_ID, genericArtifact.getDepParentTargetRepositoryId());
        addAttribute(root, DEP_PARENT_TARGET_REPOSITORY_KIND,
                genericArtifact.getDepParentTargetRepositoryKind());
    }

    // dependencies have even more optional attributes
    if (artifactType == ArtifactTypeValue.DEPENDENCY) {
        addAttribute(root, DEP_CHILD_SOURCE_ARTIFACT_ID, genericArtifact.getDepChildSourceArtifactId());
        addAttribute(root, DEP_CHILD_SOURCE_REPOSITORY_ID, genericArtifact.getDepChildSourceRepositoryId());
        addAttribute(root, DEP_CHILD_SOURCE_REPOSITORY_KIND, genericArtifact.getDepChildSourceRepositoryKind());
        addAttribute(root, DEP_CHILD_TARGET_ARTIFACT_ID, genericArtifact.getDepChildTargetArtifactId());
        addAttribute(root, DEP_CHILD_TARGET_REPOSITORY_ID, genericArtifact.getDepChildTargetRepositoryId());
        addAttribute(root, DEP_CHILD_TARGET_REPOSITORY_KIND, genericArtifact.getDepChildTargetRepositoryKind());
    }

    addAttribute(root, SOURCE_ARTIFACT_ID, genericArtifact.getSourceArtifactId());
    addAttribute(root, SOURCE_REPOSITORY_ID, genericArtifact.getSourceRepositoryId());
    addAttribute(root, SOURCE_REPOSITORY_KIND, genericArtifact.getSourceRepositoryKind());
    addAttribute(root, SOURCE_SYSTEM_ID, genericArtifact.getSourceSystemId());
    addAttribute(root, SOURCE_SYSTEM_KIND, genericArtifact.getSourceSystemKind());
    addAttribute(root, TARGET_ARTIFACT_ID, genericArtifact.getTargetArtifactId());
    addAttribute(root, TARGET_REPOSITORY_ID, genericArtifact.getTargetRepositoryId());
    addAttribute(root, TARGET_REPOSITORY_KIND, genericArtifact.getTargetRepositoryKind());
    addAttribute(root, TARGET_SYSTEM_ID, genericArtifact.getTargetSystemId());
    addAttribute(root, TARGET_SYSTEM_KIND, genericArtifact.getTargetSystemKind());
    addAttribute(root, SOURCE_SYSTEM_TIMEZONE, genericArtifact.getSourceSystemTimezone());
    addAttribute(root, TARGET_SYSTEM_TIMEZONE, genericArtifact.getTargetSystemTimezone());
    // addAttribute(root, SOURCE_SYSTEM_ENCODING, genericArtifact
    // .getSourceSystemEncoding());
    // addAttribute(root, TARGET_SYSTEM_ENCODING, genericArtifact
    // .getTargetSystemEncoding());
    addAttribute(root, TRANSACTION_ID, genericArtifact.getTransactionId());

    if (genericArtifact.getAllGenericArtifactFields() != null) {
        // now add fields
        for (GenericArtifactField genericArtifactField : genericArtifact.getAllGenericArtifactFields()) {
            Element field = addElement(root, ARTIFACT_FIELD_ELEMENT_NAME, CCF_ARTIFACT_NAMESPACE);
            switch (genericArtifactField.getFieldAction()) {
            case APPEND: {
                addAttribute(field, FIELD_ACTION, FIELD_ACTION_APPEND);
                break;
            }
            case DELETE: {
                addAttribute(field, FIELD_ACTION, FIELD_ACTION_DELETE);
                break;
            }
            case REPLACE: {
                addAttribute(field, FIELD_ACTION, FIELD_ACTION_REPLACE);
                break;
            }
            case UNKNOWN: {
                addAttribute(field, FIELD_ACTION, FIELD_ACTION_UNKNOWN);
                break;
            }
            default: {
                throw new GenericArtifactParsingException(
                        "Non valid value for field-attribute " + FIELD_ACTION + " specified.");
            }
            }

            addAttribute(field, FIELD_NAME, genericArtifactField.getFieldName());
            addAttribute(field, FIELD_TYPE, genericArtifactField.getFieldType());
            if (genericArtifactField.getFieldValueHasChanged()) {
                addAttribute(field, FIELD_VALUE_HAS_CHANGED, FIELD_VALUE_HAS_CHANGED_TRUE);
            } else {
                addAttribute(field, FIELD_VALUE_HAS_CHANGED, FIELD_VALUE_HAS_CHANGED_FALSE);
            }

            if (genericArtifact.getIncludesFieldMetaData()
                    .equals(GenericArtifact.IncludesFieldMetaDataValue.TRUE)) {
                addAttribute(field, MIN_OCCURS, genericArtifactField.getMinOccursValue());
                addAttribute(field, MAX_OCCURS, genericArtifactField.getMaxOccursValue());
                addAttribute(field, NULL_VALUE_SUPPORTED, genericArtifactField.getNullValueSupported());
            } else if (!GenericArtifactField.VALUE_UNKNOWN
                    .equals(genericArtifactField.getAlternativeFieldName())
                    && genericArtifactField.getAlternativeFieldName() != null) {
                // if the alternative field name field has been set, we will ship it even if the other meta data has not been populated
                addAttribute(field, ALTERNATIVE_FIELD_NAME, genericArtifactField.getAlternativeFieldName());
            }

            setFieldValue(field, genericArtifactField.getFieldValue(),
                    genericArtifactField.getFieldValueType());
        }
    }
    root.addAttribute(
            new QName(SCHEMA_LOCATION_ATTRIBUTE, new Namespace(SCHEMA_NAMESPACE_PREFIX, SCHEMA_NAMESPACE)),
            CCF_SCHEMA_LOCATION);
    return document;
}