Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document 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.noterik.bart.fs.fscommand.ShowSponsorsCommand.java

License:Open Source License

public String execute(String uri, String xml) {
    int numSponsors = 0;
    Element user;/* w w w .j  a  v a 2 s. co m*/
    Document typeDocument = DocumentHelper.createDocument();

    Element fsxml = typeDocument.addElement("fsxml");
    Element properties = fsxml.addElement("properties");

    String domain = URIParser.getDomainFromUri(uri);
    String userUri = "/domain/" + domain + "/user";

    Document userDoc = FSXMLRequestHandler.instance().getNodePropertiesByType(userUri, 0, 0, 999999);
    List<Node> users = userDoc.selectNodes("//user");

    logger.debug("# of users = " + users.size());

    //loop over all users of domain
    for (Iterator<Node> iter = users.iterator(); iter.hasNext();) {
        user = (Element) iter.next();
        String userid = user.selectSingleNode("@id") == null ? "" : user.selectSingleNode("@id").getText();

        logger.debug("user = " + userid);
        String sponsorUri = "/domain/" + domain + "/user/" + userid + "/sponsor";
        Document sponsor = FSXMLRequestHandler.instance().getNodePropertiesByType(sponsorUri, 0, 0, 1);
        int sponsorItems = sponsor.selectSingleNode("//totalResultsAvailable") == null ? 0
                : Integer.parseInt(sponsor.selectSingleNode("//totalResultsAvailable").getText());

        if (sponsorItems > 0) {
            logger.debug(userid + " sponsors " + sponsorItems);
            numSponsors++;
            fsxml.addElement("sponsor").addAttribute("id", userid);
        }
    }
    properties.addElement("totalResultsAvailable").addText(Integer.toString(numSponsors));
    return typeDocument.asXML();
}

From source file:com.noterik.bart.fs.legacy.properties.PropertyHandler.java

License:Open Source License

/**
 * this function creates the Document where the properties will be saved in
 * and the first element to put in it. Then it calls the loopInIt.
 *
 * @param uri//from ww  w.  j a v  a 2 s .c  om
 * @return
 */
public static String getProperties(String uri) {
    int count = 0;
    String xmlProps = "";
    if (hasProperties(uri)) {
        List<String> uris = new ArrayList<String>();
        uris.add(uri);
        String id = uri.substring(uri.lastIndexOf("/") + 1);
        Document doc = DocumentHelper.createDocument();
        String type = getTypefromProp(uri);
        Element root = doc.addElement("fsxml");
        Element current = root.addElement(type).addAttribute("id", id);
        doc = loopInIt(doc, current, uri, count, uris);
        xmlProps = doc.asXML();
    }
    return xmlProps;
}

From source file:com.noterik.bart.fs.legacy.properties.PropertyHandler.java

License:Open Source License

/**
 * This function will get the properties of a uri only, and not of it's
 * childs//ww  w .j  a  v  a2 s .c o  m
 *
 * @param uri
 * @return
 */
public static Document getPropsNoChilds(String uri) {
    String url = uri.substring(0, uri.lastIndexOf("/"));
    logger.debug("url is:" + url);
    Document doc = DocumentHelper.createDocument();
    String id = url.substring(url.lastIndexOf("/") + 1);
    String type = getTypefromProp(url);
    Element root = doc.addElement("fsxml");
    Element current = root.addElement(type).addAttribute("id", id);
    String xml = getXMLfromProp(url);
    Element elem = null;
    // if xml is not empty, unwrapp it and add the node
    if (!xml.equals("")) {
        xml = unwrapXml(xml);
        try {
            elem = (Element) DocumentHelper.parseText(xml).getRootElement().clone();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        current.add(elem);
    } else {
        // if it is empty, just add an empty node
        current.addElement("properties");
    }
    return doc;
}

From source file:com.noterik.bart.fs.legacy.properties.PropertyHandler.java

License:Open Source License

/**
 * This function creates a default property with the specified value
 *
 * @param url/*from   w  w w .j  a  va 2  s. c  o  m*/
 * @param type
 * @param value
 */
public static void createPropertyWithValue(String url, String type, String value) {
    String uri = url.substring(0, url.lastIndexOf("/properties"));
    String property = url.substring(url.lastIndexOf("/properties") + 12);
    Document doc = DocumentHelper.createDocument();
    doc.addElement("fsxml").addElement("properties").addElement(property).addText(value);
    Element fsxml = (Element) doc.selectSingleNode("/fsxml");
    String xml = fsxml.asXML();
    saveProperties(uri, type, xml);
}

From source file:com.noterik.bart.fs.legacy.tools.XmlHelper.java

License:Open Source License

/**
 * This function will extract the props part of the xml String
 * passed as parameter and return it as fsxml compatible properties.
 *
 * If the node is not found, a default (empty) node will be returned
 *
 * @param xml//from ww  w . j  ava2  s .  c  o m
 * @return
 */
public static String getPropsFromXml(String xml) {
    String props = "<fsxml>" + "<properties>" + "</properties>" + "</fsxml>";

    Document doc = null;

    try {
        doc = DocumentHelper.parseText(xml);
    } catch (DocumentException e) {
        logger.error("", e);
    }

    Element properties = (Element) doc.selectSingleNode("/fsxml/ingest/props/properties").clone();

    if (properties != null) {

        Document newDoc = DocumentHelper.createDocument();
        Element fsxml = (Element) newDoc.addElement("fsxml");
        fsxml.add(properties);

        props = fsxml.asXML();
    }
    return props;
}

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

License:Open Source License

/**
 * /*from  w  ww.ja  va  2  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 v a 2s  .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.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;/* w  ww.jav a  2 s  .c  om*/
    }
    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);/* ww w  . j  a v  a2s  .  c om*/

    }
    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.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  www . j  a  v a2s.c om*/
    }
    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);
    }
}