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.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Sets a property to an array of values. Multiple values matching the same property
 * is mapped to an XML file as multiple elements containing each value.
 * For example, using the name "foo.bar.prop", and the value string array containing
 * {"some value", "other value", "last value"} would produce the following XML:
 * <pre>//from   w  w w .j a v a2s . com
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name   the name of the property.
 * @param values the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values, boolean isEncrypt) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator<Element> iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add(iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator<Node> it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            String propValue = StringEscapeUtils.escapeXml(value);
            // check to see if the property is marked as encrypted
            if (isEncrypt) {
                propValue = EncryptUtil.desEncrypt(encryptKey, value);
                childElement.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
            }
            childElement.setText(propValue);
        }
    }
    saveProperties();

}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name      the name of the property to set.
 * @param value     the new value for the property.
 * @param isEncrypt ?// w w w  .  jav a2 s.  co m
 */
public synchronized void setProperty(String name, String value, boolean isEncrypt) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException("Property name cannot contain XML entities.");
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        String propValue = StringEscapeUtils.escapeXml(value);
        // check to see if the property is marked as encrypted
        if (isEncrypt) {
            propValue = EncryptUtil.desEncrypt(encryptKey, value);
            element.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
        }
        element.setText(propValue);
    }
    // Write the XML properties to disk
    saveProperties();

}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * /*from  w  w  w  . j  a va  2 s  .  co m*/
 * @param document
 */
private Element generateRoot(Document document) {
    Element root = document.addElement(ROOT_ELEMENT_NAME);
    root.addAttribute("namespace", daoClazz.getName());
    return root;
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * ???/* www  .j  a  va 2 s.c  o  m*/
 * @param document
 */
private void generateInsert(Element root) {
    root.addComment("");
    Element insert = root.addElement("insert");
    insert.addAttribute("id", SAVE_ID);
    insert.addAttribute("parameterType", this.domainSimpleName);
    StringBuilder sb = new StringBuilder();
    //sb.append("\n");
    //sb.append("\t\t");
    sb.append("INSERT INTO {tableName} (");
    int i = 0;
    int fileNameSize = fieldNames.size();
    //?
    for (String fieldName : fieldNames) {
        i++;
        sb.append(fieldName.toUpperCase());
        if (i != fileNameSize) {
            sb.append(", ");
        }
    }
    sb.append(") VALUES (");
    i = 0;
    //
    for (String fieldName : fieldNames) {
        i++;
        sb.append("#{");
        sb.append(fieldName);
        sb.append("}");
        if (i != fileNameSize) {
            sb.append(", ");
        }
    }
    sb.append(")");
    //sb.append("\n");
    insert.setText(sb.toString());
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * ??//from   www  .j  ava 2s .  c  o m
 * @param root
 */
private void generateGet(Element root) {
    root.addComment("?ID?");
    Element get = root.addElement("select");
    get.addAttribute("id", GET_ID);
    get.addAttribute("parameterType", "int");
    get.addAttribute("resultMap", this.domainSimpleName.toLowerCase() + "Map");
    StringBuilder sb = new StringBuilder();
    //sb.append("\n");
    //sb.append("\t\t");
    sb.append("SELECT ");
    int i = 0;
    int fileNameSize = fieldNames.size();
    //
    for (String fieldName : fieldNames) {
        i++;
        sb.append(fieldName.toUpperCase());
        if (i != fileNameSize) {
            sb.append(", ");
        }
    }
    sb.append(" FROM {tableName} WHERE ID = #{param2}");
    //sb.append("\n");
    get.setText(sb.toString());
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * ?//from ww w . jav  a 2  s.c  o m
 * @param root
 */
private void generateUpdate(Element root) {
    root.addComment("");
    Element update = root.addElement("update");
    update.addAttribute("id", UPDATE_ID);
    update.addAttribute("parameterType", this.domainSimpleName);
    //sb.append("\n");
    //sb.append("\t\t");
    update.addText("\n\t\tUPDATE {tableName} ");

    Element set = update.addElement("set");

    //
    int i = 0;
    int fileNameSize = fieldNames.size();
    for (String fieldName : fieldNames) {
        i++;
        Element ifElement = set.addElement("if");
        ifElement.addAttribute("test", fieldName + " != null and " + fieldName + " != ''");
        String text = fieldName.toUpperCase() + " = #{" + fieldName + "}";
        if (i != fileNameSize) {
            text += ",";
        }
        ifElement.setText(text);
    }
    update.addText("\n\t\t WHERE ID = #{id}");
    //sb.append("\n");
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * ?//from w ww . j  av a2  s  . c o  m
 * @param root
 */
private void generateList(Element root) {
    root.addComment("");
    Element list = root.addElement("select");
    list.addAttribute("id", LIST_ID);
    list.addAttribute("resultMap", this.domainSimpleName.toLowerCase() + "Map");
    StringBuilder sb = new StringBuilder();
    //sb.append("\n");
    //sb.append("\t\t");
    sb.append("SELECT ");
    int i = 0;
    int fileNameSize = fieldNames.size();
    //
    for (String fieldName : fieldNames) {
        i++;
        sb.append(fieldName.toUpperCase());
        if (i != fileNameSize) {
            sb.append(", ");
        }
    }
    sb.append(" FROM {tableName} ");
    //sb.append("\n");
    list.setText(sb.toString());

    Element where = list.addElement("where");

    for (String fieldName : fieldNames) {
        Element ifElement = where.addElement("if");
        ifElement.addAttribute("test", fieldName + " != null and " + fieldName + " != ''");
        ifElement.setText(fieldName.toUpperCase() + " = #{" + fieldName + "}");
    }
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * ?// w ww  . java 2 s . c  o  m
 * @param root
 */
private void generateDelete(Element root) {
    root.addComment("");
    Element delete = root.addElement("delete");
    delete.addAttribute("id", DELETE_ID);
    delete.addAttribute("parameterType", "int");
    delete.setText("DELETE FROM {tableName} WHERE ID = #{param2}");
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * ?/*  w  w  w  . j  a v a 2  s.  c om*/
 * @param root
 */
private void generateResultMap(Element root) {
    root.addComment("");
    Element resultMap = root.addElement("resultMap");
    resultMap.addAttribute("id", this.domainSimpleName.toLowerCase() + "Map");
    resultMap.addAttribute("type", this.domainSimpleName);

    for (String fieldName : fieldNames) {
        Element result = null;
        if (fieldName.equalsIgnoreCase("id")) {
            result = resultMap.addElement("id");
        } else {
            result = resultMap.addElement("result");
        }
        result.addAttribute("column", fieldName.toUpperCase());
        result.addAttribute("property", fieldName);
    }
}

From source file:com.jonschang.ai.ga.GenericGene.java

License:LGPL

@Override
public Element getXml() {
    DocumentFactory f = DocumentFactory.getInstance();
    Element toRet = f.createElement("gene");
    toRet.addAttribute("name", this.getName());
    toRet.addAttribute("expressiveness", this.getExpressiveness().toString());
    return toRet;
}