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.collabnet.ccf.core.ga.GenericArtifactHelper.java

License:Open Source License

/**
 * Adds an attribute with the supplied value to the supplied element
 * // www . j  av a  2s .  co m
 * @param element
 *            element in question
 * @param attributeName
 *            attribute name in question
 * @param value
 *            value of the attribute
 */
private static void addAttribute(Element element, String attributeName, String value) {
    element.addAttribute(attributeName, value);
}

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

License:Apache License

/**
 * Creates a generic artifact XML representation out of the Java object
 * //from ww w .  jav  a  2 s.c o 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("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) {
        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());
                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;
}

From source file:com.collabnet.ccf.core.transformer.DynamicXsltProcessor.java

License:Open Source License

/**
 * Tries to load the XSLT from the file defined in the properties
 * /*w ww.  j a  v  a  2 s.co  m*/
 * @throws ValidationException
 *             if the XSLT file is not defined in the properties, the file
 *             cannot be found or there was an error parsing it
 */
private List<Transformer> loadXSLT(File xsltFile, Element element) {
    List<Transformer> transformerList = new ArrayList<Transformer>();
    if (xsltFile == null) {
        String cause = "xsltFile property not set";
        log.error(cause);
        XPathUtils.addAttribute(element, GenericArtifactHelper.ERROR_CODE,
                GenericArtifact.ERROR_TRANSFORMER_FILE);
        throw new CCFRuntimeException(cause);
    }

    try {
        Source source = null;
        if (isOnlyAllowWhiteListedJavaFunctionCalls()) {
            SAXReader reader = new SAXReader();
            Document originalDocument = reader.read(xsltFile);
            Document clonedDocument = (Document) originalDocument.clone();
            Element clonedRootElement = clonedDocument.getRootElement();
            // replace white listed Java functions in XPath expressions with
            // "."
            for (String functionCall : getWhiteListedJavaFunctionCalls()) {
                List<Element> nodes = findFunctionCalls(clonedRootElement, functionCall);
                for (Element e : nodes) {
                    e.addAttribute("select", ".");
                }
            }
            Transformer secureTransform = secureFactory.newTransformer(new DocumentSource(clonedDocument));
            secureTransform.setErrorListener(new XsltValidationErrorListener());
            log.debug("Loaded sanitized version of XSLT [" + xsltFile + "] successfully");
            transformerList.add(secureTransform);
            source = new DocumentSource(originalDocument);
        } else {
            source = new StreamSource(xsltFile);
        }
        Transformer transform = factory.newTransformer(source);
        log.debug("Loaded original XSLT [" + xsltFile + "] successfully");
        transformerList.add(transform);
    } catch (Exception e) {
        String cause = "Failed to load XSLT: [" + xsltFile + " ]" + e.getMessage();
        log.error(cause, e);
        XPathUtils.addAttribute(element, GenericArtifactHelper.ERROR_CODE,
                GenericArtifact.ERROR_TRANSFORMER_FILE);
        throw new CCFRuntimeException(cause, e);
    }

    return transformerList;
}

From source file:com.collabnet.ccf.core.transformer.DynamicXsltProcessor.java

License:Open Source License

/**
 * Copies one attribute from the original element to the new element
 * //from w  ww .jav  a 2  s. co  m
 * @param attributeName
 * @param originalElement
 * @param newElement
 * @throws GenericArtifactParsingException
 */
private static void restoreAttribute(String attributeName, Element originalElement, Element newElement)
        throws GenericArtifactParsingException {
    newElement.addAttribute(attributeName, XPathUtils.getAttributeValue(originalElement, attributeName, false));
}

From source file:com.collabnet.ccf.core.utils.XPathUtils.java

License:Open Source License

/**
 * Adds an attribute with the supplied value to the supplied element
 * //w  w  w  . j av  a2 s.co  m
 * @param element
 *            element in question
 * @param attributeName
 *            attribute name in question
 * @param value
 *            value of the attribute
 */
public static void addAttribute(Element element, String attributeName, String value) {
    element.addAttribute(attributeName, value);
    // element.remove();
}

From source file:com.collabnet.ccf.schemageneration.XSLTInitialMFDGenerator.java

License:Apache License

/**
 * Generates an intial MFD document that can be used for the graphical data mapping
 * @param sourceSchemaName file name of the source schema
 * @param targetSchemaName file name of the target schema
 * @return/*  ww w .ja va 2s . c  o  m*/
 * @throws TransformerException
 */
public Document generateInitialMFD(String sourceSchemaName, String targetSchemaName)
        throws TransformerException {
    Document document = DocumentHelper.createDocument();
    document.setXMLEncoding("UTF-8");
    Element rootElement = document.addElement("CreateInitialMFDDocument");
    rootElement.addAttribute("sourceSchemaName", sourceSchemaName);
    rootElement.addAttribute("targetSchemaName", targetSchemaName);
    return transform(initialMFDFileTransformer, document);
}

From source file:com.cosmosource.common.service.UserMgrManager.java

/**
 * @??: ?xml?/* www  .  j a  va 2s.co m*/
 * @param nodeId
 * @param orgid
 * @return
 */
public String getOrgTreeData(String orgid, String ctx, String type) {
    if ("init".equals(type)) {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("tree");
        root.addAttribute("id", "0");

        TAcOrg org = (TAcOrg) dao.findById(TAcOrg.class, new Long(orgid));
        Element el = root.addElement("item");
        el.addAttribute("text", org.getOrgname());
        el.addAttribute("id", org.getOrgid() + "");
        // el.addAttribute("open", "1");
        el.addAttribute("child", "1");
        Element elx = el.addElement("userdata");
        elx.addAttribute("name", "url");
        elx.addText(ctx + "/common/userMgr/list.act?nodeId=" + org.getOrgid() + "&orgtype=" + org.getOrgtype());

        // getOrgTreeDoc(new Long(orgid), el, ctx, org.getOrgtype());
        return doc.asXML();
    } else {
        Element root = DocumentHelper.createElement("tree");
        root.addAttribute("id", orgid);
        List<TAcOrg> list = dao
                .findByHQL("select t from TAcOrg t where t.parentid=" + orgid + " order by orgcode ");
        if (list.size() <= 500) {
            for (TAcOrg org : list) {
                Element el = root.addElement("item");
                el.addAttribute("text", org.getOrgname());
                el.addAttribute("id", org.getOrgid() + "");
                if ("1".equals(org.getIsbottom())) {
                    el.addAttribute("child", "1");
                } else {
                    el.addAttribute("child", "0");
                }
                // if(org.getParentid()==0){
                // el.addAttribute("open", "1");
                // }
                Element elx = el.addElement("userdata");
                elx.addAttribute("name", "url");
                if (!org.getOrgtype().equals("3")) {
                    elx.addText(ctx + "/common/userMgr/list.act?nodeId=" + org.getOrgid() + "&orgtype="
                            + org.getOrgtype());
                } else {
                    elx.addText(ctx + "/common/userMgr/orgFrame.act?nodeId=" + org.getOrgid());
                }
            }
        }
        return root.asXML();
    }

}

From source file:com.cosmosource.common.service.UserMgrManager.java

/**
 * @??: DOC//from  w ww .  ja  va2  s.  c o m
 * @param orgId
 * @param root
 */
public void getOrgTreeDoc(Long orgId, Element root, String ctx, String orgtype) {

    List<TAcOrg> list = dao.findByHQL("select t from TAcOrg t where t.parentid=" + orgId);
    if (!orgtype.equals("3")) {
        for (TAcOrg org : list) {
            Element el = root.addElement("item");
            el.addAttribute("text", org.getOrgname());
            el.addAttribute("id", org.getOrgid() + "");
            if (org.getParentid() == 0) {
                el.addAttribute("open", "1");
            }
            Element elx = el.addElement("userdata");
            elx.addAttribute("name", "url");
            if (!org.getOrgtype().equals("3")) {
                elx.addText(ctx + "/common/userMgr/list.act?nodeId=" + org.getOrgid() + "&orgtype="
                        + org.getOrgtype());
            } else {
                elx.addText(ctx + "/common/userMgr/orgFrame.act?nodeId=" + org.getOrgid());
            }

            if ("1".equals(org.getIsbottom())) {
                getOrgTreeDoc(org.getOrgid(), el, ctx, org.getOrgtype());
            }
        }
    }
}

From source file:com.ctvit.vdp.services.sysconfiguration.user.UserService.java

/**
 * ?????XML// www.  java  2  s .c om
 **/
public Map<String, String> getXML(Map xmlRightIds, Map thirdRightIds) throws DocumentException {
    String baseXML = systemConfigDao.selectByPrimaryKey("Rights").getValue();//??XML
    Document doc = DocumentHelper.parseText(baseXML);
    Document topDoc = DocumentHelper.createDocument();

    Element baseElement = doc.getRootElement();
    Element topEl = topDoc.addElement("Rights");//?XML
    List<Element> secMenuList = new ArrayList<Element>();
    String topIdFlag = "";

    Iterator elementIter = baseElement.elementIterator();
    while (elementIter.hasNext()) {//??
        Element element01 = (Element) elementIter.next();
        Iterator elementIter01 = element01.elementIterator();
        while (elementIter01.hasNext()) {//??
            Element element02 = (Element) elementIter01.next();
            Iterator elementIter02 = element02.elementIterator();
            String idFlag = "";
            if (xmlRightIds.get(element02.attributeValue("id")) != null) {//??ID?ID
                Element tempEl = element02.getParent();//?
                if (topEl.nodeCount() > 0 && !topIdFlag.equals(tempEl.attributeValue("id"))) {
                    topEl.addElement(tempEl.getName()).addAttribute("id", element01.attributeValue("id"))
                            .addAttribute("name", element01.attributeValue("name"));
                }
                if (topEl.nodeCount() == 0) {
                    topEl.addElement(tempEl.getName()).addAttribute("id", element01.attributeValue("id"))
                            .addAttribute("name", element01.attributeValue("name"));
                }
                topIdFlag = tempEl.attributeValue("id");
                secMenuList.add(element02);
            }
        }
    }

    StringBuffer secXML = new StringBuffer();
    secXML.append("<Rights>");
    Element tempTopEl = topEl.createCopy();
    //      System.out.println("tempTopEl: "+tempTopEl.asXML());
    Iterator secIt = tempTopEl.elementIterator();//????
    String flag = "";
    while (secIt.hasNext()) {
        Element op = (Element) secIt.next();
        for (Element eo : secMenuList) {//eo?? 
            if (eo.attributeValue("id").substring(0, 2).equals(op.attributeValue("id"))
                    && !flag.equals(eo.attributeValue("id"))) {
                flag = eo.attributeValue("id");
                Document secDoc = DocumentHelper.createDocument();
                Element secEle = secDoc.addElement("SecMenu");
                secEle.addAttribute("id", eo.attributeValue("id"));
                secEle.addAttribute("name", eo.attributeValue("name"));
                secEle.addAttribute("source", eo.attributeValue("source"));

                Iterator eoIter = eo.elementIterator();
                while (eoIter.hasNext()) {//??
                    Element thirdEl = (Element) eoIter.next();
                    if (thirdRightIds.get(thirdEl.attributeValue("id")) != null) {
                        Document document = DocumentHelper.createDocument();
                        Element tempEle = document.addElement("ThirdMenu");
                        tempEle.addAttribute("id", thirdEl.attributeValue("id"));
                        tempEle.addAttribute("name", thirdEl.attributeValue("name"));
                        tempEle.addAttribute("source", thirdEl.attributeValue("source"));
                        secEle.add(tempEle);//
                    }
                }
                op.add(secEle);//
            }
            //System.out.println("************ op: "+op.asXML());
        }
        secXML.append(op.asXML());
    }
    secXML.append("</Rights>");
    Map<String, String> xmlMap = new HashMap<String, String>();

    xmlMap.put("topMenu", topEl.asXML());
    xmlMap.put("treeMenu", secXML.toString());
    xmlMap.put("baseXML", baseElement.asXML());
    //      this.getElementList(baseElement,xmlRightIds);
    return xmlMap;
}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.q931.MessageQ931.java

License:Open Source License

public MessageQ931(Element root) throws Exception {
    this.syntax = root.attributeValue("syntax");
    initDictionary(syntax);/*from   w  w  w .  jav  a  2 s.  com*/

    this.header = new HeaderQ931();
    this.header.parseFromXML(root.element("header"), dictionary);

    this.elements = new ArrayList<ElementAbstract>();

    List<Element> elementsInf = root.elements("element");
    ElementAbstract elemInfo = null;
    ElementAbstract elem = null;
    for (Element element : elementsInf) {
        element.addAttribute("coding", "Q931");
        elemInfo = this.dictionary.getElementFromXML(element);
        elem = (ElementQ931) elemInfo.cloneAttribute();
        // FH Manage a new Element like ElementQ931big for id = User-User:126
        elem.parseFromXML(element, this.dictionary, elemInfo);

        this.elements.add(elem);
    }
}