List of usage examples for org.dom4j Element elementIterator
Iterator<Element> elementIterator(QName qName);
From source file:cn.ict.zyq.bestConf.util.ParseXMLToYaml.java
License:Open Source License
public static HashMap parseXMLToHashmap(String filePath) { HashMap result = new HashMap(); try {// www . j av a 2s. com File f = new File(filePath); SAXReader reader = new SAXReader(); Document doc = reader.read(f); Element root = doc.getRootElement(); Element foo; for (Iterator i = root.elementIterator("property"); i.hasNext();) { foo = (Element) i.next(); String value; if (foo.elementText("value") != null) { value = foo.elementText("value"); Pattern pattern = Pattern.compile("^-?[0-9]\\d*$"); Matcher matcher = pattern.matcher(value); if (matcher.find()) { result.put(foo.elementText("name"), value); } else if (Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$").matcher(value) .find()) { result.put(foo.elementText("name"), value); } else { } } } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:com.adspore.splat.xep0060.PubSubEngine.java
License:Open Source License
private static IQ publishItemsToNode(PubSubService service, IQ iq, Element publishElement) { String nodeID = publishElement.attributeValue("node"); if (nodeID == null) { // No node was specified. Return bad_request error Element pubsubError = DocumentHelper .createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); }// w w w . j a v a 2 s . c o m // Look for the specified node Node node = service.getNode(nodeID); if (node == null) { // Node does not exist. Return item-not-found error return createErrorPacket(iq, PacketError.Condition.item_not_found, null); } JID from = iq.getFrom(); // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet JID owner = new JID(from.getNode(), from.getDomain(), null, true); if (!node.getPublisherModel().canPublish(node, owner) && !service.isServiceAdmin(owner)) { // Entity does not have sufficient privileges to publish to node return createErrorPacket(iq, PacketError.Condition.forbidden, null); } if (node.isCollectionNode()) { // Node is a collection node. Return feature-not-implemented error Element pubsubError = DocumentHelper .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors")); pubsubError.addAttribute("feature", "publish"); return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError); } LeafNode leafNode = (LeafNode) node; Iterator itemElements = publishElement.elementIterator("item"); // Check that an item was included if node persist items or includes payload if (!itemElements.hasNext() && leafNode.isItemRequired()) { Element pubsubError = DocumentHelper .createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); } // Check that no item was included if node doesn't persist items and doesn't // includes payload if (itemElements.hasNext() && !leafNode.isItemRequired()) { Element pubsubError = DocumentHelper .createElement(QName.get("item-forbidden", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); } List<Element> items = new ArrayList<Element>(); List entries; Element payload; while (itemElements.hasNext()) { Element item = (Element) itemElements.next(); entries = item.elements(); payload = entries.isEmpty() ? null : (Element) entries.get(0); // Check that a payload was included if node is configured to include payload // in notifications if (payload == null && leafNode.isPayloadDelivered()) { Element pubsubError = DocumentHelper .createElement(QName.get("payload-required", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); } // Check that the payload (if any) contains only one child element if (entries.size() > 1) { Element pubsubError = DocumentHelper .createElement(QName.get("invalid-payload", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); } items.add(item); } // Publish item and send event notifications to subscribers leafNode.publishItems(from, items); // Return success operation return createSuccessPacket(iq); }
From source file:com.adspore.splat.xep0060.PubSubEngine.java
License:Open Source License
private static IQ deleteItems(PubSubService service, IQ iq, Element retractElement) { String nodeID = retractElement.attributeValue("node"); if (nodeID == null) { // No node was specified. Return bad_request error Element pubsubError = DocumentHelper .createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); }/*from w ww .ja va 2 s. co m*/ Node node; // Look for the specified node node = service.getNode(nodeID); if (node == null) { // Node does not exist. Return item-not-found error return createErrorPacket(iq, PacketError.Condition.item_not_found, null); } // Get the items to delete Iterator itemElements = retractElement.elementIterator("item"); if (!itemElements.hasNext()) { Element pubsubError = DocumentHelper .createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); } if (node.isCollectionNode()) { // Cannot delete items from a collection node. Return an error. Element pubsubError = DocumentHelper .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors")); pubsubError.addAttribute("feature", "persistent-items"); return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError); } LeafNode leafNode = (LeafNode) node; if (!leafNode.isItemRequired()) { // Cannot delete items from a leaf node that doesn't handle itemIDs. Return an error. Element pubsubError = DocumentHelper .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors")); pubsubError.addAttribute("feature", "persistent-items"); return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError); } List<PublishedItem> items = new ArrayList<PublishedItem>(); while (itemElements.hasNext()) { Element itemElement = (Element) itemElements.next(); String itemID = itemElement.attributeValue("id"); if (itemID != null) { PublishedItem item = node.getPublishedItem(itemID); if (item == null) { // ItemID does not exist. Return item-not-found error return createErrorPacket(iq, PacketError.Condition.item_not_found, null); } else { if (item.canDelete(iq.getFrom())) { items.add(item); } else { // Publisher does not have sufficient privileges to delete this item createErrorPacket(iq, PacketError.Condition.forbidden, null); } } } else { // No item ID was specified so return a bad_request error Element pubsubError = DocumentHelper .createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors")); return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError); } } // Send reply with success //router.route(IQ.createResultIQ(iq)); // Delete items and send subscribers a notification leafNode.deleteItems(items); return createSuccessPacket(iq); }
From source file:com.adspore.splat.xep0060.PubSubEngine.java
License:Open Source License
private static IQ modifyNodeSubscriptions(PubSubService service, IQ iq, Element entitiesElement) { String nodeID = entitiesElement.attributeValue("node"); if (nodeID == null) { // NodeID was not provided. Return bad-request error. return createErrorPacket(iq, PacketError.Condition.bad_request, null); }//from w ww. j av a 2 s. c o m Node node = service.getNode(nodeID); if (node == null) { // Node does not exist. Return item-not-found error. return createErrorPacket(iq, PacketError.Condition.item_not_found, null); } if (!node.isAdmin(iq.getFrom())) { // Requesting entity is prohibited from getting affiliates list. Return forbidden error. return createErrorPacket(iq, PacketError.Condition.forbidden, null); } IQ reply = IQ.createResultIQ(iq); // Process modifications or creations of affiliations and subscriptions. for (Iterator it = entitiesElement.elementIterator("subscription"); it.hasNext();) { Element entity = (Element) it.next(); JID subscriber = new JID(entity.attributeValue("jid")); // TODO Assumed that the owner of the subscription is the bare JID of the subscription JID. Waiting StPeter answer for explicit field. JID owner = new JID(subscriber.getNode(), subscriber.getDomain(), null, true); String subStatus = entity.attributeValue("subscription"); String subID = entity.attributeValue("subid"); // Process subscriptions changes // Get current subscription (if any) NodeSubscription subscription = null; if (node.isMultipleSubscriptionsEnabled()) { if (subID != null) { subscription = node.getSubscription(subID); } } else { subscription = node.getSubscription(subscriber); } if ("none".equals(subStatus) && subscription != null) { // Owner is cancelling an existing subscription node.cancelSubscription(subscription); } else if ("subscribed".equals(subStatus)) { if (subscription != null) { // Owner is approving a subscription (i.e. making active) node.approveSubscription(subscription, true); } else { // Owner is creating a subscription for an entity to the node node.createSubscription(null, owner, subscriber, false, null); } } } return reply; }
From source file:com.adspore.splat.xep0060.PubSubEngine.java
License:Open Source License
private static IQ modifyNodeAffiliations(PubSubService service, IQ iq, Element entitiesElement) { String nodeID = entitiesElement.attributeValue("node"); if (nodeID == null) { // NodeID was not provided. Return bad-request error. return createErrorPacket(iq, PacketError.Condition.bad_request, null); }//from www. jav a 2 s .c o m Node node = service.getNode(nodeID); if (node == null) { // Node does not exist. Return item-not-found error. return createErrorPacket(iq, PacketError.Condition.item_not_found, null); } if (!node.isAdmin(iq.getFrom())) { // Requesting entity is prohibited from getting affiliates list. Return forbidden error. return createErrorPacket(iq, PacketError.Condition.forbidden, null); } IQ reply = IQ.createResultIQ(iq); Collection<JID> invalidAffiliates = new ArrayList<JID>(); // Process modifications or creations of affiliations for (Iterator it = entitiesElement.elementIterator("affiliation"); it.hasNext();) { Element affiliation = (Element) it.next(); JID owner = new JID(affiliation.attributeValue("jid")); String newAffiliation = affiliation.attributeValue("affiliation"); // Get current affiliation of this user (if any) NodeAffiliate affiliate = node.getAffiliate(owner); // Check that we are not removing the only owner of the node if (affiliate != null && !affiliate.getAffiliation().name().equals(newAffiliation)) { // Trying to modify an existing affiliation if (affiliate.getAffiliation() == NodeAffiliate.Affiliation.owner && node.getOwners().size() == 1) { // Trying to remove the unique owner of the node. Include in error answer. invalidAffiliates.add(owner); continue; } } // Owner is setting affiliations for new entities or modifying // existing affiliations if ("owner".equals(newAffiliation)) { node.addOwner(owner); } else if ("publisher".equals(newAffiliation)) { node.addPublisher(owner); } else if ("none".equals(newAffiliation)) { node.addNoneAffiliation(owner); } else { node.addOutcast(owner); } } // Process invalid entities that tried to remove node owners. Send original affiliation // of the invalid entities. if (!invalidAffiliates.isEmpty()) { reply.setError(PacketError.Condition.not_acceptable); Element child = reply.setChildElement("pubsub", "http://jabber.org/protocol/pubsub#owner"); Element entities = child.addElement("affiliations"); if (!node.isRootCollectionNode()) { entities.addAttribute("node", node.getNodeID()); } for (JID affiliateJID : invalidAffiliates) { NodeAffiliate affiliate = node.getAffiliate(affiliateJID); Element entity = entities.addElement("affiliation"); entity.addAttribute("jid", affiliate.getJID().toString()); entity.addAttribute("affiliation", affiliate.getAffiliation().name()); } } return reply; }
From source file:com.ah.be.ls.stat.StatManager.java
private static List<StatConfig> readStatConfig() { if (stats != null && !stats.isEmpty()) { return stats; }/*www . java 2s . c om*/ stats = new ArrayList<StatConfig>(); // read from stat-config.xml SAXReader reader = new SAXReader(); File ff = new File(System.getenv("HM_ROOT") + "/resources/stat-config.xml"); if (!ff.exists()) { // for test ff = new File("stat-config.xml"); } try { Document doc = reader.read(ff); Element roota = doc.getRootElement(); log.info("StatManager", "roota..nodcount=" + roota.nodeCount()); Iterator<?> iters = roota.elementIterator("feature"); while (iters.hasNext()) { StatConfig stat = new StatConfig(); Element foo = (Element) iters.next(); if (foo.attribute("ignore") != null) { continue; } stat.setFeatureId(Integer.valueOf(foo.attributeValue("id"))); stat.setFeatureName(foo.attributeValue("name")); Element e2 = foo.element("bo-class"); Element e4 = foo.element("search-rule"); stat.setBoClassName(e2.attributeValue("name")); stat.setSearchRule(e4.attributeValue("value")); stat.setSearchType(e4.attributeValue("type")); stats.add(stat); } return stats; } catch (ClassNotFoundException e) { log.error("StatManager", "readStatConfig: ClassNotFoundException", e); } catch (Exception e) { log.error("StatManager", "readStatConfig: Exception", e); } return null; }
From source file:com.alibaba.citrus.springext.impl.SchemaImpl.java
License:Open Source License
/** * ?schema??/* www. j av a2s . c om*/ * <ol> * <li>targetNamespace</li> * <li>include name</li> * </ol> */ @Override protected void doAnalyze() { Document doc = getDocument(); // ??null org.dom4j.Element root = doc.getRootElement(); // return if not a schema file if (!W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) || !"schema".equals(root.getName())) { return; } // parse targetNamespace if (parsingTargetNamespace) { Attribute attr = root.attribute("targetNamespace"); if (attr != null) { targetNamespace = trimToNull(attr.getStringValue()); } } // parse include Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI); QName includeName = DocumentHelper.createQName("include", xsd); List<String> includeNames = createLinkedList(); // for each <xsd:include> for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) { org.dom4j.Element includeElement = (org.dom4j.Element) i.next(); String schemaLocation = trimToNull(includeElement.attributeValue("schemaLocation")); if (schemaLocation != null) { includeNames.add(schemaLocation); } } includes = includeNames.toArray(new String[includeNames.size()]); // parse xsd:element QName elementName = DocumentHelper.createQName("element", xsd); // for each <xsd:element> for (Iterator<?> i = root.elementIterator(elementName); i.hasNext();) { Element element = new ElementImpl((org.dom4j.Element) i.next()); if (element.getName() != null) { this.elements.put(element.getName(), element); } } }
From source file:com.alibaba.citrus.springext.support.SchemaUtil.java
License:Open Source License
/** schemaincludes */ public static Transformer getTransformerWhoRemovesIncludes() { return new Transformer() { public void transform(Document document, String systemId) { Element root = document.getRootElement(); // <xsd:schema> if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) { // for each <xsd:include> for (Iterator<?> i = root.elementIterator(XSD_INCLUDE); i.hasNext();) { i.next();//from w w w .j a va 2 s .co m i.remove(); } } } }; }
From source file:com.alibaba.citrus.springext.support.SchemaUtil.java
License:Open Source License
/** schema?includes */ public static Transformer getTransformerWhoAddsIndirectIncludes(final Map<String, Schema> includes) { return new Transformer() { public void transform(Document document, String systemId) { Element root = document.getRootElement(); root.addNamespace("xsd", W3C_XML_SCHEMA_NS_URI); // <xsd:schema> if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) { Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI); QName includeName = DocumentHelper.createQName("include", xsd); // for each <xsd:include> for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) { i.next();/* w ww. j a va 2 s. c o m*/ i.remove(); } // includes @SuppressWarnings("unchecked") List<Node> nodes = root.elements(); int i = 0; for (Schema includedSchema : includes.values()) { Element includeElement = DocumentHelper.createElement(includeName); nodes.add(i++, includeElement); includeElement.addAttribute("schemaLocation", includedSchema.getName()); } } } }; }
From source file:com.alibaba.citrus.springext.support.SchemaUtil.java
License:Open Source License
/** ?URI? */ public static Transformer getAddPrefixTransformer(final SchemaSet schemas, String prefix) { if (prefix != null) { if (!prefix.endsWith("/")) { prefix += "/"; }/* www. j a v a 2 s . co m*/ } final String normalizedPrefix = prefix; return new Transformer() { public void transform(Document document, String systemId) { if (normalizedPrefix != null) { Element root = document.getRootElement(); // <xsd:schema> if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) { Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI); QName includeName = DocumentHelper.createQName("include", xsd); QName importName = DocumentHelper.createQName("import", xsd); // for each <xsd:include> for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) { Element includeElement = (Element) i.next(); String schemaLocation = trimToNull(includeElement.attributeValue("schemaLocation")); if (schemaLocation != null) { schemaLocation = getNewSchemaLocation(schemaLocation, null, systemId); if (schemaLocation != null) { includeElement.addAttribute("schemaLocation", schemaLocation); } } } // for each <xsd:import> for (Iterator<?> i = root.elementIterator(importName); i.hasNext();) { Element importElement = (Element) i.next(); String schemaLocation = importElement.attributeValue("schemaLocation"); String namespace = trimToNull(importElement.attributeValue("namespace")); if (schemaLocation != null || namespace != null) { schemaLocation = getNewSchemaLocation(schemaLocation, namespace, systemId); if (schemaLocation != null) { importElement.addAttribute("schemaLocation", schemaLocation); } } } } } } private String getNewSchemaLocation(String schemaLocation, String namespace, String systemId) { // ?schemaLocation if (schemaLocation != null) { Schema schema = schemas.findSchema(schemaLocation); if (schema != null) { return normalizedPrefix + schema.getName(); } else { return schemaLocation; // location?? } } // ??namespace if (namespace != null) { Set<Schema> nsSchemas = schemas.getNamespaceMappings().get(namespace); if (nsSchemas != null && !nsSchemas.isEmpty()) { // ?nsschema?schema String versionedExtension = getVersionedExtension(systemId); if (versionedExtension != null) { for (Schema schema : nsSchemas) { if (schema.getName().endsWith(versionedExtension)) { return normalizedPrefix + schema.getName(); } } } // schema?beans.xsd?beans-2.5.xsd?beans-2.0.xsd return normalizedPrefix + nsSchemas.iterator().next().getName(); } } return null; } /** spring-aop-2.5.xsd?-2.5.xsd */ private String getVersionedExtension(String systemId) { if (systemId != null) { int dashIndex = systemId.lastIndexOf("-"); int slashIndex = systemId.lastIndexOf("/"); if (dashIndex > slashIndex) { return systemId.substring(dashIndex); } } return null; } }; }