Example usage for org.dom4j Element attributeValue

List of usage examples for org.dom4j Element attributeValue

Introduction

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

Prototype

String attributeValue(QName qName);

Source Link

Document

This returns the attribute value for the attribute with the given fully qualified name or null if there is no such attribute or the empty string if the attribute value is empty.

Usage

From source file:com.beacon.wlsagent.xml.XmlWorker.java

public Document genMonXml(Document document, BeaconStateCollector fsc) {
    Document resultDoc = null;//w w  w. j  a  v  a  2 s .co m

    if (fsc.isConnectedToAdmin()) {

        resultDoc = DocumentHelper.createDocument();

        Element root = resultDoc.addElement("MONITOR").addAttribute("Date", BeaconUtil.getStringDate());
        Element osEle = root.addElement("OSResource");

        String cpuStr = BeaconUtil.runShell("sh bin/getCPU.sh");
        String memStr = BeaconUtil.runShell("sh bin/getMEM.sh");
        if ((cpuStr == "") || (memStr == "")) {
            cpuStr = (String) BeaconUtil.runShell().get("CPU");
            memStr = (String) BeaconUtil.runShell().get("MEM");
        }
        osEle.addAttribute("CPU", cpuStr).addAttribute("MEM", memStr);

        List mbeanEleList = document.selectNodes("//MBean");

        ObjectName[] svrRt = fsc.getServerRT();

        int svrRtCount = svrRt.length;

        int f = 0;
        for (Iterator i = mbeanEleList.iterator(); i.hasNext();) {
            Element mbeanEle = (Element) i.next();
            String mbeanName = mbeanEle.attributeValue("name");
            log.debug("Currently dealing with mbean: " + mbeanName);
            Iterator k = mbeanEle.elementIterator("attribute");

            List al = new ArrayList();
            for (; k.hasNext(); f++) {
                al.add(((Element) k.next()).getText());
            }
            String mbeanAttrStrs[] = BeaconUtil.listToStrArray(al);

            for (int m = 0; m < svrRtCount; m++) {
                Map mBeanInfoMap[] = fsc.getMBeanInfoMaps(mbeanAttrStrs, mbeanName, svrRt[m]);
                String svrName = svrRt[m].getKeyProperty("Name");
                log.debug("Currently dealing with server: " + svrRt[m]);
                if (mBeanInfoMap != null) {
                    for (int g = 0; g < mBeanInfoMap.length; g++) {
                        if (mBeanInfoMap[g] != null && mbeanAttrStrs.length > 0) {
                            Element currItem = root.addElement(mbeanName);
                            currItem.addAttribute("serverName", svrName);
                            for (int j = 0; j < mbeanAttrStrs.length; j++) {
                                String curAttr = mbeanAttrStrs[j];
                                Object curAttrVal = mBeanInfoMap[g].get(curAttr);
                                if (curAttrVal != null) {
                                    currItem.addAttribute(curAttr, String.valueOf(curAttrVal));
                                } else {
                                    root.remove(currItem);
                                    j = mbeanAttrStrs.length;
                                }
                                log.debug("Attribute: " + curAttr + " Value: "
                                        + String.valueOf(mBeanInfoMap[g].get(curAttr)));
                            }

                        }
                    }
                }
            }

        }
    } else {
        resultDoc = this.genErrXml("Happened to lose connection to WebLogic Admin. maybe shutdown");
    }

    return resultDoc;
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setProperties(String xmlFileName, String itemPath, String ElementName, String keyName,
        String valueName, String key, String value) {
    // ? ??????/*from  w  ww.  j  a v a2 s  . c o m*/
    // ????
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        try {
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(itemPath));
            if (node != null) {
                Iterator<?> it = node.selectNodes(ElementName).iterator();
                while (it.hasNext()) {
                    Element e = (Element) it.next();
                    if (e.attributeValue(keyName).equals(key)) {
                        e.addAttribute(valueName, value);
                        break;
                    }
                }
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            reader = null;
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * /*from ww w . ja v  a  2s  .  c  o m*/
 * @param element
 *            Element
 * @param path
 *            String
 * @param attr
 *            String
 * @return String
 */
public static String getElementContext(Element element, String path, String attr) {
    Element el = element.element(path);
    if (attr == null || attr.trim().equals("")) {
        return el.getText();
    } else {
        return el.attributeValue(attr);
    }
}

From source file:com.bluexml.side.framework.alfresco.sharePortalExtension.PresetsManagerExtension.java

License:Open Source License

/**
 * Construct the model objects for a given preset.
 * Objects persist to the default store for the appropriate object type.
 * /*from ww w  . ja  v  a2  s  . co m*/
 * @param id
 *            Preset ID to use
 * @param tokens
 *            Name value pair tokens to replace in preset definition
 */
public void constructPreset(String id, Map<String, String> tokens) {
    if (id == null) {
        throw new IllegalArgumentException("Preset ID is mandatory.");
    }

    // perform one time init - this cannot be perform in an app handler or by the
    // framework init - as it requires the Alfresco server to be started...
    synchronized (this) {
        if (this.documents == null) {
            init();
        }
    }

    for (Document doc : this.documents) {
        for (Element preset : (List<Element>) doc.getRootElement().elements("preset")) {
            // found preset with matching id?
            if (id.equals(preset.attributeValue("id"))) {
                // any components in the preset?
                Element components = preset.element("components");
                if (components != null) {
                    for (Element c : (List<Element>) components.elements("component")) {
                        // apply token replacement to each value as it is retrieved
                        String title = replace(c.elementTextTrim(Component.PROP_TITLE), tokens);
                        String titleId = replace(c.elementTextTrim(Component.PROP_TITLE_ID), tokens);
                        String description = replace(c.elementTextTrim(Component.PROP_DESCRIPTION), tokens);
                        String descriptionId = replace(c.elementTextTrim(Component.PROP_DESCRIPTION_ID),
                                tokens);
                        String typeId = replace(c.elementTextTrim(Component.PROP_COMPONENT_TYPE_ID), tokens);
                        String scope = replace(c.elementTextTrim(Component.PROP_SCOPE), tokens);
                        String regionId = replace(c.elementTextTrim(Component.PROP_REGION_ID), tokens);
                        String sourceId = replace(c.elementTextTrim(Component.PROP_SOURCE_ID), tokens);
                        String url = replace(c.elementTextTrim(Component.PROP_URL), tokens);
                        String chrome = replace(c.elementTextTrim(Component.PROP_CHROME), tokens);

                        // validate mandatory values
                        if (scope == null || scope.length() == 0) {
                            throw new IllegalArgumentException(
                                    "Scope is a mandatory property for a component preset.");
                        }
                        if (regionId == null || regionId.length() == 0) {
                            throw new IllegalArgumentException(
                                    "RegionID is a mandatory property for a component preset.");
                        }
                        if (sourceId == null || sourceId.length() == 0) {
                            throw new IllegalArgumentException(
                                    "SourceID is a mandatory property for a component preset.");
                        }

                        // generate component
                        Component component = modelObjectService.newComponent(scope, regionId, sourceId);
                        component.setComponentTypeId(typeId);
                        component.setTitle(title);
                        component.setTitleId(titleId);
                        component.setDescription(description);
                        component.setDescriptionId(descriptionId);
                        component.setURL(url);
                        component.setChrome(chrome);

                        // apply arbituary custom properties
                        if (c.element("properties") != null) {
                            for (Element prop : (List<Element>) c.element("properties").elements()) {
                                String propName = replace(prop.getName(), tokens);
                                String propValue = replace(prop.getTextTrim(), tokens);
                                component.setCustomProperty(propName, propValue);
                            }
                        }

                        // persist the object
                        modelObjectService.saveObject(component);
                    }
                }

                // any pages in the preset?
                Element pages = preset.element("pages");
                if (pages != null) {
                    for (Element p : (List<Element>) pages.elements("page")) {
                        // apply token replacement to each value as it is retrieved
                        String pageId = replace(p.attributeValue(Page.PROP_ID), tokens);
                        String title = replace(p.elementTextTrim(Page.PROP_TITLE), tokens);
                        String titleId = replace(p.elementTextTrim(Page.PROP_TITLE_ID), tokens);
                        String description = replace(p.elementTextTrim(Page.PROP_DESCRIPTION), tokens);
                        String descriptionId = replace(p.elementTextTrim(Page.PROP_DESCRIPTION_ID), tokens);
                        String typeId = replace(p.elementTextTrim(Page.PROP_PAGE_TYPE_ID), tokens);
                        String auth = replace(p.elementTextTrim(Page.PROP_AUTHENTICATION), tokens);
                        String template = replace(p.elementTextTrim(Page.PROP_TEMPLATE_INSTANCE), tokens);

                        // validate mandatory values
                        if (pageId == null || pageId.length() == 0) {
                            throw new IllegalArgumentException(
                                    "ID is a mandatory attribute for a page preset.");
                        }
                        if (template == null || template.length() == 0) {
                            throw new IllegalArgumentException(
                                    "Template is a mandatory property for a page preset.");
                        }

                        // generate page
                        Page page = modelObjectService.newPage(pageId);
                        page.setPageTypeId(typeId);
                        page.setTitle(title);
                        page.setTitleId(titleId);
                        page.setDescription(description);
                        page.setDescriptionId(descriptionId);
                        page.setAuthentication(auth);
                        page.setTemplateId(template);

                        // apply arbituary custom properties
                        if (p.element("properties") != null) {
                            for (Element prop : (List<Element>) p.element("properties").elements()) {
                                String propName = replace(prop.getName(), tokens);
                                String propValue = replace(prop.getTextTrim(), tokens);
                                page.setCustomProperty(propName, propValue);
                            }
                        }

                        // persist the object
                        modelObjectService.saveObject(page);
                    }
                }

                // any template instances in the preset?
                Element templates = preset.element("template-instances");
                if (templates != null) {
                    for (Element t : (List<Element>) templates.elements("template-instance")) {
                        // apply token replacement to each value as it is retrieved
                        String templateId = replace(t.attributeValue(TemplateInstance.PROP_ID), tokens);
                        String title = replace(t.elementTextTrim(TemplateInstance.PROP_TITLE), tokens);
                        String titleId = replace(t.elementTextTrim(TemplateInstance.PROP_TITLE_ID), tokens);
                        String description = replace(t.elementTextTrim(TemplateInstance.PROP_DESCRIPTION),
                                tokens);
                        String descriptionId = replace(t.elementTextTrim(TemplateInstance.PROP_DESCRIPTION_ID),
                                tokens);
                        String templateType = replace(t.elementTextTrim(TemplateInstance.PROP_TEMPLATE_TYPE),
                                tokens);

                        // validate mandatory values
                        if (templateId == null || templateId.length() == 0) {
                            throw new IllegalArgumentException(
                                    "ID is a mandatory attribute for a template-instance preset.");
                        }
                        if (templateType == null || templateType.length() == 0) {
                            throw new IllegalArgumentException(
                                    "Template is a mandatory property for a page preset.");
                        }

                        // generate template-instance
                        TemplateInstance template = modelObjectService.newTemplate(templateId);
                        template.setTitle(title);
                        template.setTitleId(titleId);
                        template.setDescription(description);
                        template.setDescriptionId(descriptionId);
                        template.setTemplateTypeId(templateType);

                        // apply arbituary custom properties
                        if (t.element("properties") != null) {
                            for (Element prop : (List<Element>) t.element("properties").elements()) {
                                String propName = replace(prop.getName(), tokens);
                                String propValue = replace(prop.getTextTrim(), tokens);
                                template.setCustomProperty(propName, propValue);
                            }
                        }

                        // persist the object
                        modelObjectService.saveObject(template);
                    }
                }

                // TODO: any chrome, associations, types, themes etc. in the preset...

                // found our preset - no need to process further
                break;
            }
        }
    }
}

From source file:com.bluexml.side.framework.alfresco.sharePortalExtension.PresetsManagerExtension.java

License:Open Source License

public List<String> getPresetsIds() {
    List<String> ids = new ArrayList<String>();
    synchronized (this) {
        if (this.documents == null) {
            init();//from   w  ww. j  a  v a2  s  . c o m
        }
    }
    for (Document doc : this.documents) {
        for (Element preset : (List<Element>) doc.getRootElement().elements("preset")) {
            // found preset with matching id?
            ids.add(preset.attributeValue("id"));
        }
    }
    return ids;
}

From source file:com.buddycloud.channeldirectory.crawler.node.PostCrawler.java

License:Apache License

@SuppressWarnings("unchecked")
private void processPost(String nodeFullJid, String nodeId, Item item) throws Exception {

    PayloadItem<PacketExtension> payloadItem = (PayloadItem<PacketExtension>) item;
    PacketExtension payload = payloadItem.getPayload();

    Element atomEntry = DocumentHelper.parseText(payload.toXML()).getRootElement();

    PostData postData = new PostData();

    postData.setParentFullId(nodeFullJid);
    postData.setParentSimpleId(nodeId);//from   w  w  w .  j a va  2  s .  c  o  m

    Element authorElement = atomEntry.element("author");
    String authorName = authorElement.elementText("name");
    String authorUri = authorElement.elementText("uri");

    postData.setAuthor(authorName);
    postData.setAuthorURI(authorUri);

    String content = atomEntry.elementText("content");
    String updated = atomEntry.elementText("updated");
    String published = atomEntry.elementText("published");
    String id = atomEntry.elementText("id");

    postData.setContent(content);
    postData.setUpdated(DATE_FORMAT.parse(updated));
    postData.setPublished(DATE_FORMAT.parse(published));
    postData.setId(id);

    Element geolocElement = atomEntry.element("geoloc");

    if (geolocElement != null) {
        Geolocation geolocation = new Geolocation();

        String text = geolocElement.elementText("text");
        geolocation.setText(text);

        String lat = geolocElement.elementText("lat");
        String lon = geolocElement.elementText("lon");

        if (lat != null && lon != null) {
            geolocation.setLat(Double.valueOf(lat));
            geolocation.setLng(Double.valueOf(lon));
        }

        postData.setGeolocation(geolocation);
    }

    Element inReplyToElement = atomEntry.element("in-reply-to");
    if (inReplyToElement != null) {
        String replyRef = inReplyToElement.attributeValue("ref");
        postData.setInReplyTo(replyRef);
    }

    insert(postData);
    ActivityHelper.updateActivity(postData, dataSource, configuration);
}

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

License:Apache License

/**
 * Parses options features from an IQ request
 * Returns an empty map if there is no <options> tag
 * //from  ww  w . jav  a 2s .  co m
 * @param request
 * @return
 */
@SuppressWarnings("unchecked")
public static Set<String> parseOptions(Element queryElement) {
    Element optionsElement = queryElement.element("options");
    Set<String> options = new HashSet<String>();

    if (optionsElement == null) {
        return options;
    }

    List<Element> features = optionsElement.elements("feature");
    for (Element element : features) {
        options.add(element.attributeValue("var"));
    }

    return options;
}

From source file:com.buddycloud.friendfinder.provider.Gmail.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//ww w.  jav  a2 s .c  o  m
public ContactProfile getProfile(String accessToken) throws Exception {
    boolean firstPage = true;
    ContactProfile contactProfile = null;
    int startIndex = 1;

    String myEmail = null;

    while (true) {
        StringBuilder urlBuilder = new StringBuilder(GOOGLE_FEED);
        urlBuilder.append("?start-index=").append(startIndex);
        urlBuilder.append("&max-results=").append(PAGE_SIZE);
        urlBuilder.append("&access_token=").append(accessToken);

        String contactsURL = urlBuilder.toString();
        Element xmlContacts = HttpUtils.consumeXML(contactsURL);

        if (firstPage) {
            myEmail = xmlContacts.element("id").getText();
            contactProfile = new ContactProfile(HashUtils.encodeSHA256(PROVIDER_NAME, myEmail));
        }

        if (xmlContacts.element("entry") == null) {
            break;
        }
        Iterator<Element> friendsElements = xmlContacts.elementIterator("entry");
        while (friendsElements.hasNext()) {
            Element element = (Element) friendsElements.next();
            Element emailEl = element.element("email");
            if (emailEl == null) {
                continue;
            }
            String friendEmail = emailEl.attributeValue("address");
            if (friendEmail.equals(myEmail)) {
                continue;
            }
            contactProfile.addFriendHash(HashUtils.encodeSHA256(PROVIDER_NAME, friendEmail));
        }

        startIndex += PAGE_SIZE;
        firstPage = false;
    }

    return contactProfile;
}

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

License:Apache License

private static String getTransactionId(Packet packet) {
    Element confirmEl = packet.getElement().element("confirm");
    return confirmEl.attributeValue("id");
}

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

License:Apache License

public boolean accept(Packet packet) {
    Element confirmEl = packet.getElement().element("confirm");
    if (confirmEl == null) {
        return false;
    }//from   www  .j a v a2s .c  om
    String confirmationIdAttr = confirmEl.attributeValue("id");
    return transactionID.equals(confirmationIdAttr);
}