Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

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

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * //from  w  w  w  . j a  v a2  s .co m
 * <pre>
 * Export the TAC form database by Model.
 * &lt;pre&gt;
 * 
 * @param model
 * @return
 * @throws DMException
 * 
 */
public void exportModelTAC(Model model, String outFile) throws DMException {

    Document document = org.dom4j.DocumentHelper.createDocument();
    Element rootElement = document.addElement("Manufacturers");
    Element manElement = rootElement.addElement("Manufacturer");
    Element manNameElement = manElement.addElement("Name");
    manNameElement.setText(model.getManufacturer().getName());
    Element manexterIDElement = manElement.addElement("ExternalID");
    manexterIDElement.setText(model.getManufacturer().getExternalId());
    Element modelElement = manElement.addElement("Model");
    Element modelNameElement = modelElement.addElement("Name");
    modelNameElement.setText(model.getName());
    Element modelexterIDElement = modelElement.addElement("ExternalID");
    modelexterIDElement.setText(model.getManufacturerModelId());
    Element TACSElement = modelElement.addElement("TACS");

    Set<String> tacSet = model.getModelTAC();
    for (String tac : tacSet) {
        Element TACElement = TACSElement.addElement("TAC");
        TACElement.setText(tac);
    }

    try {
        XMLWriter writer = new XMLWriter(new FileWriter(new File(outFile)));
        writer.write(document);
        writer.close();
        this.formatXMLFile(outFile);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * // w  w w. j a va2s.c  o m
 * <pre>
 * Export the TAC form database by Model.
 * &lt;pre&gt;
 * 
 * @param manufacturer
 * @return
 * @throws DMException
 * 
 */
public void exportModelTAC(Manufacturer manufacturer, String outFile) throws DMException {

    Set<Model> modelSet = manufacturer.getModels();

    Document document = org.dom4j.DocumentHelper.createDocument();
    Element rootElement = document.addElement("Manufacturers");
    Element manElement = rootElement.addElement("Manufacturer");
    Element manNameElement = manElement.addElement("Name");
    manNameElement.setText(manufacturer.getName());
    Element manexterIDElement = manElement.addElement("ExternalID");
    manexterIDElement.setText(manufacturer.getExternalId());

    for (Model model : modelSet) {

        Set<String> tacSet = model.getModelTAC();
        if (tacSet.size() > 0) {
            Element modelElement = manElement.addElement("Model");
            Element modelNameElement = modelElement.addElement("Name");
            modelNameElement.setText(model.getName());
            Element modelexterIDElement = modelElement.addElement("ExternalID");
            modelexterIDElement.setText(model.getManufacturerModelId());
            Element TACSElement = modelElement.addElement("TACS");

            for (String tac : tacSet) {
                Element TACElement = TACSElement.addElement("TAC");
                TACElement.setText(tac);
            }
        } else {
            continue;
        }
    }

    try {

        XMLWriter writer = new XMLWriter(new FileWriter(new File(outFile)));
        writer.write(document);
        writer.close();
        this.formatXMLFile(outFile);

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

From source file:com.oflaherty.webservice.builders.request.RequestBuilder.java

/**
 * builds an individual request element/*from ww  w  .ja va  2 s.c  o  m*/
 *
 * @param requestEl request parent element
 * @param elementName name of the element to create
 * @param value value from userParams
 */
private void buildElement(Element requestEl, String elementName, BigDecimal value) {
    requestEl.addElement(elementName).addText(convertBigDecimalToString(value));
}

From source file:com.oflaherty.webservice.builders.request.RequestBuilder.java

/**
 * Builds the additionalPurcahses element tree (unlimited number of additional purchases)
 *
 * @param requestEl request parent element
 * @param additionalPurchases map containing key (string) and value (BigDecimal)
 */// ww  w.j av  a 2 s. co m
private void buildAdditionalPurchasesEl(Element requestEl, Map<String, BigDecimal> additionalPurchases) {

    Element additionalPurchasesEl = requestEl.addElement(ADDITIONAL_PURCHASES);

    for (Map.Entry<String, BigDecimal> entry : additionalPurchases.entrySet()) {

        Element purchase = additionalPurchasesEl.addElement("purchase");

        String description = entry.getKey();
        String value = entry.getValue().toString();

        purchase.addElement("description").addText(description);
        purchase.addElement("value").addText(value);
    }

}

From source file:com.openedit.users.filesystem.FileSystemUserManager.java

License:Open Source License

public void saveUser(User user) throws UserManagerException {
    if (user.isVirtual()) {
        log.error("Cannot save virtual users: " + user.getUserName());
        return;/*from   ww  w  . ja  v  a 2s  . com*/
    }
    DocumentFactory factory = DocumentFactory.getInstance();
    Document doc = factory.createDocument();
    Element userElem = doc.addElement("user");
    userElem.addAttribute("enabled", Boolean.toString(user.isEnabled()));
    if (user.getUserName() == null) {
        int id = getUserIdCounter().incrementCount();
        String newid = String.valueOf(id);
        user.setId(newid);
    }
    Element userNameElem = userElem.addElement("user-name");
    userNameElem.setText(user.getUserName());

    Element passwordElem = userElem.addElement("password");
    //
    if (user.getPassword() != null && !user.getPassword().equals("")) {
        String ps = user.getPassword();
        ps = encrypt(ps);
        //password may have changed we should set it so it's not in plain text anymore.
        user.setPassword(ps);
        passwordElem.setText(ps);
    }
    Element creationDateElem = userElem.addElement("creation-date");
    if (user.getCreationDate() != null) {
        creationDateElem.setText(String.valueOf(user.getCreationDate().getTime()));
    } else {
        creationDateElem.setText(String.valueOf(System.currentTimeMillis()));
    }

    //Tuan add property lastLogined-Time
    Element lastLoginTime = userElem.addElement("lastLogined-Time");
    lastLoginTime.setText(DateStorageUtil.getStorageUtil().formatForStorage(new Date()));

    MapPropertyContainer map = (MapPropertyContainer) user.getPropertyContainer();
    if (map != null) {
        Element propertiesElem = map.createPropertiesElement("properties");
        userElem.add(propertiesElem);
    }
    if (user.getGroups() != null) {
        for (Iterator iter = user.getGroups().iterator(); iter.hasNext();) {
            Group group = (Group) iter.next();
            Element child = userElem.addElement("group");
            child.addAttribute("id", group.getId());
        }
    }
    File file = loadUserFile(user.getUserName());
    getXmlUtil().saveXml(doc, file);
    getUserNameToUserMap().put(user.getUserName(), user);
}

From source file:com.openedit.users.filesystem.FileSystemUserManager.java

License:Open Source License

public void saveGroup(Group inGroup) throws UserManagerException {
    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("group");
    if (inGroup.getId() == null) {

        int id = getUserIdCounter().incrementCount();
        String inAccount = String.valueOf(id);
        inGroup.setId(inAccount);/*from  w w w  .j  a va  2s . c o  m*/

    }
    root.addAttribute("id", inGroup.getId());
    Element groupNameElem = root.addElement("group-name");
    groupNameElem.setText(inGroup.getName());

    Element permissionsElem = root.addElement("permissions");

    for (Iterator iter = inGroup.getPermissions().iterator(); iter.hasNext();) {
        Object permission = (Object) iter.next();
        Element permissionElem = permissionsElem.addElement("permission");
        permissionElem.setText(permission.toString());
    }

    MapPropertyContainer map = (MapPropertyContainer) inGroup.getPropertyContainer();
    if (map != null) {
        Element propertiesElem = map.createPropertiesElement("properties");
        root.add(propertiesElem);
    }
    File file = loadGroupFile(inGroup.getId());
    getXmlUtil().saveXml(doc, file);

    getGroupIdToGroupMap().put(inGroup.getId(), inGroup);
}

From source file:com.openedit.users.filesystem.MapPropertyContainer.java

License:Open Source License

protected Element createPropertiesElement(String inElementName) {
    Element propertiesElem = DocumentFactory.getInstance().createElement(inElementName);

    for (Iterator iter = getRealProperties().entrySet().iterator(); iter.hasNext();) {
        Map.Entry entry = (Map.Entry) iter.next();
        if (entry.getValue() != null) {
            Element propertyElem = propertiesElem.addElement("property");
            propertyElem.addAttribute("name", entry.getKey().toString());
            propertyElem.addAttribute("value", entry.getValue().toString());
        }/* w  w  w .j  a  va 2s . c o  m*/
    }

    return propertiesElem;
}

From source file:com.openedit.users.filesystem.XmlUserArchive.java

License:Open Source License

public void saveUser(User user) throws UserManagerException {
    if (user.isVirtual()) {
        log.error("Cannot save virtual users: " + user.getUserName());
        return;/*from   w  w  w. j  av a2  s  .  com*/
    }
    DocumentFactory factory = DocumentFactory.getInstance();
    Document doc = factory.createDocument();
    Element userElem = doc.addElement("user");
    userElem.addAttribute("enabled", Boolean.toString(user.isEnabled()));
    if (user.getUserName() == null) {
        int id = getUserIdCounter().incrementCount();
        String newid = String.valueOf(id);
        user.setId(newid);
    }
    Element userNameElem = userElem.addElement("user-name");
    userNameElem.addCDATA(user.getUserName());

    Element passwordElem = userElem.addElement("password");
    //
    if (user.getPassword() != null && !user.getPassword().equals("")) {
        String ps = user.getPassword();
        ps = encrypt(ps);
        // password may have changed we should set it so it's not in plain
        // text anymore.
        user.setPassword(ps);
        passwordElem.addCDATA(ps);
    }
    Element creationDateElem = userElem.addElement("creation-date");
    if (user.getCreationDate() != null) {
        creationDateElem.setText(String.valueOf(user.getCreationDate().getTime()));
    } else {
        creationDateElem.setText(String.valueOf(System.currentTimeMillis()));
    }

    // Tuan add property lastLogined-Time
    Element lastLoginTime = userElem.addElement("lastLogined-Time");
    lastLoginTime.setText(DateStorageUtil.getStorageUtil().formatForStorage(new Date()));

    MapPropertyContainer map = (MapPropertyContainer) user.getPropertyContainer();
    if (map != null) {
        Element propertiesElem = map.createPropertiesElement("properties");
        userElem.add(propertiesElem);
    }
    if (user.getGroups() != null) {
        for (Iterator iter = user.getGroups().iterator(); iter.hasNext();) {
            Group group = (Group) iter.next();
            Element child = userElem.addElement("group");
            child.addAttribute("id", group.getId());
        }
    }
    synchronized (user) {
        // File file = loadUserFile(user.getUserName());
        XmlFile xfile = new XmlFile();
        xfile.setRoot(doc.getRootElement());
        xfile.setPath(getUserDirectory() + "/" + user.getUserName() + ".xml");
        getXmlArchive().saveXml(xfile, null);

        getUserNameToUserMap().put(user.getUserName(), user);
    }
}

From source file:com.openedit.users.filesystem.XmlUserArchive.java

License:Open Source License

public void saveGroup(Group inGroup) throws UserManagerException {
    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("group");
    if (inGroup.getId() == null) {

        int id = getUserIdCounter().incrementCount();
        String inAccount = String.valueOf(id);
        inGroup.setId(inAccount);//w  w w .  jav a 2s  . co m

    }
    root.addAttribute("id", inGroup.getId());
    Element groupNameElem = root.addElement("group-name");
    groupNameElem.setText(inGroup.getName());

    Element permissionsElem = root.addElement("permissions");

    for (Iterator iter = inGroup.getPermissions().iterator(); iter.hasNext();) {
        Object permission = (Object) iter.next();
        Element permissionElem = permissionsElem.addElement("permission");
        permissionElem.setText(permission.toString());
    }

    MapPropertyContainer map = (MapPropertyContainer) inGroup.getPropertyContainer();
    if (map != null) {
        Element propertiesElem = map.createPropertiesElement("properties");
        root.add(propertiesElem);
    }
    File file = loadGroupFile(inGroup.getId());
    getXmlUtil().saveXml(doc, file);

    getGroupIdToGroupMap().remove(inGroup.getId());
}

From source file:com.orange.atk.atkUI.anaHopper.HopperStep.java

License:Apache License

@Override
public void save(Element root, int stepNumber) {
    Element anaElem = root.addElement(TYPE);
    anaElem.addAttribute("name", "hopperstep_" + stepNumber);
    anaElem.addAttribute("file", getFlashFilePath());
    if (getLogin() != null) {
        anaElem.addAttribute("login", getLogin());
        anaElem.addAttribute("password", getPassword());
    }//from w  w  w .java  2s. c om
    if (getUseragent() != null) {
        anaElem.addAttribute("useragent", getUseragent());
    }
    if (getXmlfilepath() != null) {
        anaElem.addAttribute("configfile", getXmlfilepath());
    }
    Enumeration<String> paramKeys = hopperStepParam.keys();
    while (paramKeys.hasMoreElements()) {
        String key = paramKeys.nextElement();
        anaElem.addAttribute(key, hopperStepParam.get(key));
    }
}