Example usage for org.dom4j Element attribute

List of usage examples for org.dom4j Element attribute

Introduction

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

Prototype

Attribute attribute(QName qName);

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.log4ic.compressor.utils.MemcachedUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
private static MemcachedConfig getMemcachedConfig(InputStream in) throws DocumentException {
    SAXReader reader = new SAXReader();

    MemcachedConfig config = new MemcachedConfig();

    Document doc = reader.read(in);

    logger.debug("??...");
    List<Node> nodeList = doc.selectNodes("/memcached/servers/server");
    if (nodeList.isEmpty()) {
        throw new DocumentException(CONFIG_FILE_PATH + " file memcached.servers server element empty!");
    } else {/*from w ww  . j  ava2  s.co m*/
        logger.debug("???" + nodeList.size() + ".");
    }

    AddressConfig addrConf = biludAddrMapConfig(nodeList);

    config.builder = new XMemcachedClientBuilder(addrConf.addressMap, addrConf.widgets);

    Element el = (Element) doc.selectSingleNode("/memcached");
    logger.debug("??...");
    Attribute attr = el.attribute("connectionPoolSize");
    if (attr != null) {
        String connPoolSize = attr.getValue();
        if (StringUtils.isNotBlank(connPoolSize)) {
            try {
                config.builder.setConnectionPoolSize(Integer.parseInt(connPoolSize));
                logger.debug("?" + connPoolSize);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("??...");
    attr = el.attribute("enableHeartBeat");
    if (attr != null) {
        String enableHeartBeatS = attr.getValue();
        if (StringUtils.isNotBlank(enableHeartBeatS)) {
            try {
                config.enableHeartBeat = Boolean.parseBoolean(enableHeartBeatS);
                logger.debug("???" + enableHeartBeatS);
            } catch (Exception e) {
                logger.error("?????", e);
            }
        } else {
            logger.error("?????");
        }
    } else {
        logger.warn("????");
    }
    logger.debug("?...");
    attr = el.attribute("sessionIdleTimeout");
    if (attr != null) {
        String sessionIdleTimeout = attr.getValue();
        if (StringUtils.isNotBlank(sessionIdleTimeout)) {
            try {
                config.builder.getConfiguration().setSessionIdleTimeout(Long.parseLong(sessionIdleTimeout));
                logger.debug("?" + sessionIdleTimeout);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    //?
    logger.debug("?...");
    attr = el.attribute("statisticsServer");
    if (attr != null) {
        String statisticsServer = attr.getValue();
        if (StringUtils.isNotBlank(statisticsServer)) {
            try {
                config.builder.getConfiguration().setStatisticsServer(Boolean.parseBoolean(statisticsServer));
                logger.debug("?" + statisticsServer);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("?...");
    attr = el.attribute("statisticsInterval");
    if (attr != null) {
        String statisticsInterval = attr.getValue();
        if (StringUtils.isNotBlank(statisticsInterval)) {
            try {
                config.builder.getConfiguration().setStatisticsInterval(Long.parseLong(statisticsInterval));
                logger.debug("?" + statisticsInterval);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("????...");
    attr = el.attribute("optimizeMergeBuffer");
    if (attr != null) {
        String optimizeMergeBufferS = attr.getValue();
        if (StringUtils.isNotBlank(optimizeMergeBufferS)) {
            try {
                config.optimizeMergeBuffer = Boolean.parseBoolean(optimizeMergeBufferS);
                logger.debug("????" + optimizeMergeBufferS);
            } catch (Exception e) {
                logger.error("??????", e);
            }
        } else {
            logger.error("??????");
        }
    } else {
        logger.warn("?????");
    }
    logger.debug("??...");
    attr = el.attribute("mergeFactor");
    if (attr != null) {
        String mergeFactorS = attr.getValue();
        if (StringUtils.isNotBlank(mergeFactorS)) {
            try {
                config.mergeFactor = Integer.parseInt(mergeFactorS);
                logger.debug("?" + mergeFactorS);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("??...");
    attr = el.attribute("commandFactory");
    if (attr != null) {
        String commandFactory = attr.getValue();
        if (StringUtils.isNotBlank(commandFactory)) {
            try {
                config.builder.setCommandFactory((CommandFactory) Class.forName(commandFactory).newInstance());
                logger.debug("??" + commandFactory);
            } catch (Exception e) {
                logger.error("????", e);
            }
        } else {
            logger.error("????");
        }
    } else {
        logger.warn("???");
    }
    config.builder.setCommandFactory(new BinaryCommandFactory());
    logger.debug("...");
    nodeList = doc.selectNodes("/memcached/socketOption/*");
    if (!nodeList.isEmpty()) {
        for (Node n : nodeList) {
            try {
                attr = ((Element) n).attribute("type");
                if (attr == null) {
                    logger.error("type attribute undefined");
                } else {
                    String type = attr.getValue();
                    if (StringUtils.isNotBlank(type)) {
                        String name = n.getName();
                        String value = n.getStringValue();
                        Class valueType = Class.forName(type);
                        Constructor constructor = SocketOption.class.getConstructor(String.class, Class.class);
                        SocketOption socketOption = (SocketOption) constructor.newInstance(name, valueType);
                        constructor = valueType.getConstructor(String.class);
                        config.builder.setSocketOption(socketOption, constructor.newInstance(value));
                        logger.debug("[" + name + "]" + value);
                    }
                }
            } catch (NoSuchMethodException e) {
                logger.error("NoSuchMethodException", e);
            } catch (InvocationTargetException e) {
                logger.error("InvocationTargetException", e);
            } catch (InstantiationException e) {
                logger.error("InstantiationException", e);
            } catch (IllegalAccessException e) {
                logger.error("IllegalAccessException", e);
            } catch (ClassNotFoundException e) {
                logger.error("ClassNotFoundException", e);
            }
        }
    } else {
        logger.warn("?");
    }
    logger.debug("Failure?...");
    attr = el.attribute("failureMode");
    if (attr != null) {
        String failureMode = attr.getValue();
        if (StringUtils.isNotBlank(failureMode)) {
            try {
                config.builder.setFailureMode(Boolean.parseBoolean(failureMode));
                logger.debug("Failure?" + failureMode);
            } catch (Exception e) {
                logger.error("Failure???", e);
            }
        } else {
            logger.error("Failure???");
        }
    } else {
        logger.warn("Failure??");
    }
    return config;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.context.GeoEventDispatcher.java

License:Apache License

static Message buildGeoMessageFromPubSubIQ(final IQ geoIQ) {
    Message geoMessage = null;/*from   ww  w. j ava  2s .  com*/
    if (IQ.Type.set == geoIQ.getType() && geoIQ.getTo().toString().startsWith("pubsub")) {
        // find 'geoloc'
        Element element = geoIQ.getChildElement();
        Element action = element.element("publish");
        if (action != null) {
            String nodeID = action.attributeValue("node");
            if (nodeID != null && nodeID.endsWith(TopicHelper.TOPIC_GEOLOC)) {
                // Entity publishes an item
                geoMessage = new Message();
                geoMessage.setType(Message.Type.chat);
                Map<String, String> geoValues = new HashMap<String, String>();
                JID from = geoIQ.getFrom();
                String appId = JIDUtil.getAppId(from);
                geoValues.put(APP_ID, appId);

                String userId = JIDUtil.getUserId(from);
                geoValues.put(USER_ID, userId);

                String deviceId = JIDUtil.getResource(from.toString());
                geoValues.put(DEVICE_ID, deviceId);

                Iterator<Element> items = action.elementIterator("item");
                if (items != null) {
                    while (items.hasNext()) {
                        Element item = (Element) items.next();
                        Element mmx = item.element(Constants.MMX_ELEMENT);
                        if (mmx == null)
                            continue;
                        Element payload = mmx.element(Constants.MMX_PAYLOAD);
                        if (payload == null)
                            return null; // not a valid MMX payload, ignore it
                        Attribute timestamp = payload.attribute(Constants.MMX_ATTR_STAMP);
                        if (timestamp != null) {
                            geoValues.put(Constants.MMX_ATTR_STAMP, timestamp.getValue());
                        }
                        String geoData = payload.getTextTrim();
                        GeoLoc geoLoc = GsonData.getGson().fromJson(geoData, GeoLoc.class);
                        if (geoLoc.getAccuracy() != null) {
                            geoValues.put(ACCURACY, geoLoc.getAccuracy().toString());
                        }
                        if (geoLoc.getLng() != null && geoLoc.getLat() != null) {
                            geoValues.put(LONG, geoLoc.getLng().toString());
                            geoValues.put(LAT, geoLoc.getLat().toString());
                            // calculate geohash
                            GeoPoint point = new GeoPointDefaultImpl(geoLoc.getLat(), geoLoc.getLng());
                            geoValues.put(GEOHASH, localGeoEndocoder.get().encodePoint(point));
                        }
                        if (geoLoc.getAlt() != null) {
                            geoValues.put(ALTITUDE, Integer.toString((int) geoLoc.getAlt().floatValue()));
                        }
                        Element geoElement = geoMessage.addChildElement(Constants.MMX_ELEMENT,
                                Constants.MMX_NS_CONTEXT);
                        geoElement.addAttribute(Constants.MMX_ATTR_MTYPE, Constants.MMX_MTYPE_GEOLOC);
                        JSONObject geoJson = new JSONObject(geoValues);
                        geoElement.setText(geoJson.toString());
                        geoMessage.setBody(geoJson.toString());

                        return geoMessage;

                    }
                }
            }
        }
    }
    return null;
}

From source file:com.mindquarry.desktop.model.task.TaskListTransformer.java

License:Open Source License

@Path("/tasks")
public void tasks(Node node) {
    log.info("Tasks element found. Trying to evaluate children."); //$NON-NLS-1$
    if (node instanceof Element) {
        Element element = (Element) node;
        baseURL = element.attribute("base").getStringValue(); //$NON-NLS-1$
    }/*from   w  w  w  . j  ava  2 s  .  c  o m*/
    applyTemplates();
}

From source file:com.mindquarry.desktop.model.task.TaskListTransformer.java

License:Open Source License

@Path("task")
public void task(Node node) {
    log.info("Found new task element."); //$NON-NLS-1$
    if (node instanceof Element) {
        Element element = (Element) node;

        log.info("Trying to add task from '" //$NON-NLS-1$
                + element.attribute("href").getStringValue() //$NON-NLS-1$
                + "'."); //$NON-NLS-1$
        taskList.add(baseURL + element.attribute("href").getStringValue(), //$NON-NLS-1$ 
                login, password);/*from w w w  . j  a v  a 2 s  .  co  m*/
    }
}

From source file:com.mindquarry.desktop.model.task.TaskTransformer.java

License:Open Source License

@Path("/task")
public void task(Node node) {
    if (node instanceof Element) {
        Element element = (Element) node;

        log.info("Retrieved new task description from " //$NON-NLS-1$
                + element.attribute("base").getStringValue()); //$NON-NLS-1$
        task.setId(element.attribute("base").getStringValue()); //$NON-NLS-1$
    }//from  w w  w. jav  a  2 s.co m
    applyTemplates(node);
}

From source file:com.mindquarry.desktop.model.team.TeamListTransformer.java

License:Open Source License

@Path("/teamspaces")
public void teamspaces(Node node) {
    log.info("Teamspaces element found. Trying to evaluate children."); //$NON-NLS-1$
    if (node instanceof Element) {
        Element element = (Element) node;

        if (element.attribute("base") != null) { //$NON-NLS-1$
            baseURL = element.attribute("base").getStringValue(); //$NON-NLS-1$
        } else {//from ww w.  j a v  a 2 s .co  m
            // FIXME remove this after new server version is available
            baseURL = url + "/team/"; //$NON-NLS-1$
        }
    }
    applyTemplates();
}

From source file:com.mindquarry.desktop.model.team.TeamListTransformer.java

License:Open Source License

@Path("teamspace")
public void teamspace(Node node) throws NotAuthorizedException, MalformedURLException {
    log.info("Found new teamspace element."); //$NON-NLS-1$
    if (node instanceof Element) {
        Element element = (Element) node;

        log.info("Trying to add teamspace from '" //$NON-NLS-1$
                + element.attribute("href").getStringValue() //$NON-NLS-1$
                + "'."); //$NON-NLS-1$
        teamList.add(baseURL + element.attribute("href").getStringValue()//$NON-NLS-1$ 
                + "/", //$NON-NLS-1$
                login, password);/*from w  ww . j av  a 2  s .c  om*/
    }
}

From source file:com.nokia.helium.diamonds.XMLMerger.java

License:Open Source License

/**
 * Compare two elements name and attributes. Returns true if name and all
 * attributes are matching, false otherwise.
 * /*from w  w  w  .jav a2 s.co m*/
 * @param a
 * @param b
 * @return boolean
 */
@SuppressWarnings("unchecked")
protected boolean areSame(Element a, Element b) {
    log.debug("areSame:" + a + " <=> " + b);
    if (!a.getName().equals(b.getName())) {
        return false;
    }
    log.debug("same attribute list size?");
    if (a.attributes().size() != b.attributes().size()) {
        return false;
    }
    log.debug("same attribute list?");
    for (Iterator<Attribute> at = a.attributes().iterator(); at.hasNext();) {
        Attribute attra = at.next();
        Attribute attrb = b.attribute(attra.getName());
        if (attrb == null || !attra.getValue().equals(attrb.getValue())) {
            return false;
        }
    }
    if (!a.getTextTrim().equals(b.getTextTrim())) {
        return false;
    }
    return true;
}

From source file:com.noterik.bart.fs.fscommand.dynamic.collection.config.wizard.java

License:Open Source License

public String run(String uri, String xml) {
    logger.debug("start dynamic/collection/config/wizard");

    Document doc = XMLHelper.asDocument(xml);

    if (doc == null) {
        return FSXMLBuilder.getErrorMessage("403", "The value you sent is not valid",
                "You have to POST a valid command XML", "http://teamelements.noterik.nl/team");
    }//from w  ww.  j  a va2 s.com

    String user = doc.selectSingleNode("//properties/user") == null ? ""
            : doc.selectSingleNode("//properties/user").getText();
    String ticket = doc.selectSingleNode("//properties/ticket") == null ? ""
            : doc.selectSingleNode("//properties/ticket").getText();

    //TODO: validate user/ticket

    //get collection
    Document collection = FSXMLRequestHandler.instance().getNodeProperties(uri, false);
    logger.debug(collection.asXML());

    //get roles
    Element wizard;
    List<Node> wizards = collection.selectNodes("//config[@id='1']/wizard");
    Node userRole = collection
            .selectSingleNode("//config[@id='roles']/user[@id='" + user + "']/properties/wizard");

    if (userRole == null) {
        logger.debug("user not found --> default wizard");
        // Loop all wizards
        for (Iterator<Node> iter = wizards.iterator(); iter.hasNext();) {
            wizard = (Element) iter.next();

            String wizardId = wizard.attribute("id") == null ? "" : wizard.attribute("id").getText();
            if (!wizardId.equals("1")) {
                logger.debug("detach wizard with id " + wizardId);
                wizard.detach();
            }
        }
    } else {
        logger.debug("user found " + userRole.asXML());
        String roles = userRole.getText();
        String[] results = roles.split(",");

        // Loop all wizards
        for (Iterator<Node> iter = wizards.iterator(); iter.hasNext();) {
            wizard = (Element) iter.next();

            String wizardId = wizard.attribute("id") == null ? "" : wizard.attribute("id").getText();
            if (!inArray(results, wizardId)) {
                logger.debug("detach wizard with id " + wizardId);
                wizard.detach();
            }
        }
    }

    //detach config roles
    Node configRoles = collection.selectSingleNode("//config[@id='roles']");
    configRoles.detach();

    return collection.asXML();
}

From source file:com.plugin.UI.Windows.Settings.ThemeSelection.java

/**
 * Parses the theme xml.//from   w  w  w  .  j a v a  2  s  .com
 * 
 * @param xml the xml
 */
private void parseThemeXML(String xml) {
    try {
        Document doc = DocumentHelper.parseText(xml);
        Element elmt = doc.getRootElement();
        List l = elmt.selectNodes("/themes/theme");
        Element e = null;
        theme = new String[l.size()];
        theme_class = new String[l.size()];
        for (int i = 0; i < l.size(); i++) {
            e = (Element) l.get(i);
            theme[i] = e.attribute("name").getValue();
            theme_class[i] = e.attribute("class").getValue();
        }
    } catch (DocumentException e) {
        System.out.println(e);
    }
}