List of usage examples for org.jdom2 Element setText
public Element setText(final String text)
From source file:org.dvlyyon.net.netconf.marshalling.NetconfUtil.java
License:Open Source License
/** * Given an attribute mapping for a class, convert it to NETCONF XML and add them to the root XML node. * * @param am Attribute mappings defining an attribute of the class. * @param classElem Root XML node representing the object. * @param cm ClassMapping representing the class being transformed. * @param oldObject the old POJO, in case the edit operation specified is an update. We need to pass this in, since * there may be a specific need to "unset" attributes that were previously set. * @param object the POJO that needs to be converted to NETOCNF XML. * @param editOperation The type of operation to be performed. * @throws RuntimeException if an error occurred. *///from w ww .ja va 2 s. c om @SuppressWarnings("unchecked") void dumpAttributeToXml(AttributeMapping am, final Element classElem, final ClassMapping cm, final Object oldObject, final Object object, final EditOperation editOperation) throws RuntimeException { Namespace namespace = Namespace.getNamespace(cm.getXmlNamespace()); Namespace ns = namespace; String attrNs = am.getXmlNamespace(); if (attrNs != null) { ns = Namespace.getNamespace(attrNs); } if (am.isReadOnly()) { // We do not stream read-only values out return; } if (am.getType() == AttributeMapping.Type.Class) { Path childPath = Path.fromString(cm.getXmlPath()); PathSegment ps = new PathSegment(cm.getXmlNamespace(), cm.getXmlTag()); childPath.addSegment(ps); ps = new PathSegment(am.getXmlNamespace(), am.getXmlTag()); childPath.addSegment(ps); ClassMapping childClass = m_model.getClassMappingByPath(childPath); if (childClass == null) { throw new RuntimeException("Class mapping not found for path: " + childPath); } if (childClass.getType() == ClassMapping.Type.Union) { Object childValue = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), object); if (am.isList()) { if (editOperation == EditOperation.Merge && oldObject != null) { List<Object> oldUnions = (List<Object>) BeanUtil.getDirectListValue(am.getJavaMemberName(), oldObject); for (Object oldUnion : oldUnions) { Element unionAttrib = new Element(am.getXmlTag(), ns); unionAttrib.setAttribute("operation", "delete"); // TODO: Optimize later - only dump out the index attributes m_unionChoiceHelper.unionToXml(childClass, oldUnion, unionAttrib); classElem.addContent(unionAttrib); } } // Stick the new ones in List<Object> unionList = (List<Object>) childValue; for (Object unionObject : unionList) { Element attrib = new Element(am.getXmlTag(), ns); m_unionChoiceHelper.unionToXml(childClass, unionObject, attrib); classElem.addContent(attrib); } } else { Element attrib = new Element(am.getXmlTag(), ns); m_unionChoiceHelper.unionToXml(childClass, childValue, attrib); classElem.addContent(attrib); } } else if (childClass.getType() == ClassMapping.Type.Choice) { Object choiceValue = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), object); m_unionChoiceHelper.choiceToXml(cm, childClass, choiceValue, classElem, editOperation); } else if (!am.isList()) { boolean add = false; Object value = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), object); Element childClassElem = new Element(childClass.getXmlTag(), Namespace.getNamespace(childClass.getXmlNamespace())); if (value != null) { childClassElem.setAttribute("operation", "replace"); add = true; dumpAttributesToXml(childClassElem, childClass, null, value, EditOperation.NotApplicable); } else { // Remove the child if there used to be one there originally if (editOperation == EditOperation.Merge && oldObject != null) { Object oldValue = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), oldObject); if (oldValue != null) { childClassElem.setAttribute("operation", "delete"); add = true; } } } if (add) { classElem.addContent(childClassElem); } } else { // Need to delete the old objects first (if there is an old object) if (oldObject != null) { List<Object> oldVos = (List<Object>) BeanUtil.getDirectListValue(am.getJavaMemberName(), oldObject); for (Object vo : oldVos) { Element childClassElem = new Element(childClass.getXmlTag(), Namespace.getNamespace(childClass.getXmlNamespace())); classElem.addContent(childClassElem); childClassElem.setAttribute("operation", "delete"); // TODO: Optimize later - only dump out the index attributes dumpAttributesToXml(childClassElem, childClass, null, vo, EditOperation.Create); } } // Then, add the current ones - aka "create" List<Object> vos = (List<Object>) BeanUtil.getDirectListValue(am.getJavaMemberName(), object); for (Object vo : vos) { Element childClassElem = new Element(childClass.getXmlTag(), Namespace.getNamespace(childClass.getXmlNamespace())); classElem.addContent(childClassElem); childClassElem.setAttribute("operation", "replace"); dumpAttributesToXml(childClassElem, childClass, null, vo, EditOperation.Create); } } } else { // Check for a list here, too (this corresponds to a leaf-list) if (am.isList()) { // Delete all the existing leaf-list attributes if the operation is an edit if (editOperation == EditOperation.Merge && oldObject != null) { List<Object> oldPrimitives = (List<Object>) BeanUtil.getDirectListValue(am.getJavaMemberName(), oldObject); for (Object prim : oldPrimitives) { Element attrib = new Element(am.getXmlTag(), ns); attrib.setAttribute("operation", "delete"); String strVal = am.convertValueToString(prim); if (strVal != null) { attrib.setText(strVal); } classElem.addContent(attrib); } } // Stick the new ones in List<Object> primitives = (List<Object>) BeanUtil.getDirectListValue(am.getJavaMemberName(), object); for (Object prim : primitives) { Element attrib = new Element(am.getXmlTag(), ns); String strVal = am.convertValueToString(prim); if (strVal != null) { attrib.setText(strVal); } classElem.addContent(attrib); } } else { Element targetNode = classElem; boolean add = false; Element attrib = null; ; if (am.isSynthetic() && am.getType() != AttributeMapping.Type.Primitive_Boolean) { // No extra XML node here - just set the content directly (unless it is "empty") attrib = classElem; targetNode = null; } else { attrib = new Element(am.getXmlTag(), ns); } Object value = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), object); if (value != null) { String strVal = am.convertValueToString(value); if (strVal != null) { attrib.setText(strVal); add = true; } } else { // "Unset" the attribute by setting delete = true if (editOperation == EditOperation.Merge && oldObject != null) { Object oldValue = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), oldObject); if (oldValue != null) { attrib.setAttribute("operation", "delete"); add = true; } } } if (add && targetNode != null) { targetNode.addContent(attrib); } } } }
From source file:org.dvlyyon.net.netconf.marshalling.UnionChoiceHelper.java
License:Open Source License
void unionToXml(ClassMapping cm, Object unionObject, Element targetNode) { // Take the union class and check out every attribute in it one by one for (AttributeMapping am : cm.getAttributeMappings()) { // The first one that is a non-null is Xml-ized and returned // TODO: This could be a nested union, so we should probably recurse Object value = BeanUtil.getDirectFieldValue(am.getJavaMemberName(), unionObject); if (value != null) { String strVal = am.convertValueToString(value); if (strVal != null) { targetNode.setText(strVal); break; }/*from ww w . j av a2 s .c o m*/ } } }
From source file:org.dvlyyon.net.netconf.NotificationStream.java
License:Open Source License
/** * Returns the notification stream formatted like it is exchanged over NETCONF as XML. * * @return Element representing the stream as NETCONF XML. *///from www .j a v a 2s. co m public Element toNetconfXml() { Element ret = new Element("stream", s_notificationNs); Element child = null; if (m_name != null) { child = new Element("name", s_notificationNs); child.setText(m_name); ret.addContent(child); } if (m_description != null) { child = new Element("description", s_notificationNs); child.setText(m_description); ret.addContent(child); } child = new Element("supportsReplay", s_notificationNs); child.setText("" + m_supportsReplay); ret.addContent(child); if (m_replayLogCreationTime != null) { child = new Element("replayLogCreationTime", s_notificationNs); RFC3399Timestamp ts = new RFC3399Timestamp(m_replayLogCreationTime); child.setText(ts.toString()); ret.addContent(child); } if (m_replayLogAgedTime != null) { child = new Element("replayLogAgedTime", s_notificationNs); RFC3399Timestamp ts = new RFC3399Timestamp(m_replayLogAgedTime); child.setText(ts.toString()); ret.addContent(child); } return ret; }
From source file:org.dvlyyon.net.netconf.transport.ssh.AsyncSshConnection.java
License:Open Source License
/** * Create the <b>create-subscription</b> message to send to the device. * * @param messageId Message ID to use. * @param stream Stream to create subscription for (NULL to use the default stream). * @param startTime Start time from when messages need to be replayed (NULL if no replay is required). * @return XML representing the create-subscription message. *//*from ww w . j a va 2s . c o m*/ private static Element createSubscriptionMessageXml(final String messageId, final String stream, final Timestamp startTime) { Namespace netconfNs = Namespace.getNamespace(BASE_NAMESPACE); Element rpc = new Element("rpc", netconfNs); rpc.setAttribute("message-id", messageId); Namespace notificationNs = Namespace.getNamespace(NOTIF_NAMESPACE); Element cs = new Element("create-subscription", notificationNs); rpc.addContent(cs); // Add the stream name (if specified) if (stream != null) { Element streamElem = new Element("stream", notificationNs); streamElem.setText(stream); cs.addContent(streamElem); } // Add the start time (if specified) if (startTime != null) { Element fromWhen = new Element("startTime", notificationNs); RFC3399Timestamp ts = new RFC3399Timestamp(startTime); fromWhen.setText(ts.toString()); cs.addContent(fromWhen); } return rpc; }
From source file:org.dvlyyon.net.netconf.transport.ssh.AsyncSshConnection.java
License:Open Source License
private static Element createSubscriptionMessageXml(final String messageId, final String stream, final Element filter, final String startTime, final String stopTime) { Namespace netconfNs = Namespace.getNamespace(BASE_NAMESPACE); Element rpc = new Element("rpc", netconfNs); rpc.setAttribute("message-id", messageId); Namespace notificationNs = Namespace.getNamespace(NOTIF_NAMESPACE); Element cs = new Element("create-subscription", notificationNs); rpc.addContent(cs);//from w w w .ja va2 s . co m // Add the stream name (if specified) if (stream != null) { Element streamElem = new Element("stream", notificationNs); streamElem.setText(stream); cs.addContent(streamElem); } if (filter != null) { cs.addContent(filter); } // Add the start time (if specified) if (startTime != null) { Element fromWhen = new Element("startTime", notificationNs); fromWhen.setText(startTime); cs.addContent(fromWhen); } if (stopTime != null) { Element stopAt = new Element("stopTime", notificationNs); stopAt.setText(stopTime); cs.addContent(stopAt); } return rpc; }
From source file:org.dvlyyon.net.netconf.transport.ssh.SshConnection.java
License:Open Source License
/** * Adds the client's capabilities to the specified XML node. Currently, we support only the base capability. * * @param root XML element to add the capabilities to. * @param ns XML namespace used for the hello message. *///from w w w .j a va2 s . co m private void addCapabilities(final Element root, final Namespace ns) { final Element cap = new Element("capability", ns); cap.setText("urn:ietf:params:netconf:base:1.1"); root.addContent(cap); // TODO: Any other capabilities to add ? Should we give the subclass a chance to add its own? }
From source file:org.fiware.cybercaptor.server.attackgraph.AttackGraph.java
License:Open Source License
/** * @return the dom element corresponding to this attack graph XML file *///from w w w. j ava 2s . c o m public Element toDomElement() { Element root = new Element("attack_graph"); //arcs Element arcsElement = new Element("arcs"); root.addContent(arcsElement); for (Arc arc : arcs) { Element arcElement = new Element("arc"); arcsElement.addContent(arcElement); Element srcElement = new Element("src"); srcElement.setText(arc.destination.id + ""); arcElement.addContent(srcElement); Element dstElement = new Element("dst"); dstElement.setText(arc.source.id + ""); arcElement.addContent(dstElement); } //vertices Element verticesElement = new Element("vertices"); root.addContent(verticesElement); for (int key : vertices.keySet()) { Vertex vertex = vertices.get(key); Element vertexElement = new Element("vertex"); verticesElement.addContent(vertexElement); Element idElement = new Element("id"); idElement.setText(vertex.id + ""); vertexElement.addContent(idElement); Element factElement = new Element("fact"); factElement.setText(vertex.fact.factString); vertexElement.addContent(factElement); Element metricElement = new Element("metric"); metricElement.setText(vertex.mulvalMetric + ""); vertexElement.addContent(metricElement); Element typeElement = new Element("type"); typeElement.setText(vertex.type.toString().toUpperCase()); vertexElement.addContent(typeElement); } return root; }
From source file:org.fiware.cybercaptor.server.attackgraph.AttackPath.java
License:Open Source License
/** * @return the dom element corresponding to this attack path XML file *///w w w . ja v a 2 s. c om public Element toDomXMLElement() { Element root = new Element("attack_path"); Element scoringElement = new Element("scoring"); scoringElement.setText(this.scoring + ""); root.addContent(scoringElement); //arcs Element arcsElement = new Element("arcs"); root.addContent(arcsElement); for (Arc arc : arcs) { Element arcElement = new Element("arc"); arcsElement.addContent(arcElement); Element srcElement = new Element("src"); srcElement.setText(arc.destination.id + ""); arcElement.addContent(srcElement); Element dstElement = new Element("dst"); dstElement.setText(arc.source.id + ""); arcElement.addContent(dstElement); } return root; }
From source file:org.fiware.cybercaptor.server.informationsystem.InformationSystemHost.java
License:Open Source License
/** * @return the dom element corresponding to this host with the format of the tva report file *//*w w w. ja v a2s. c o m*/ public Element toDomXMLElement() { Element root = new Element("machine"); Element nameElement = new Element("name"); nameElement.setText(this.getName()); root.addContent(nameElement); Element cpeElement = new Element("cpe"); cpeElement.setText("cpe:/"); root.addContent(cpeElement); //Interfaces Element interfacesElement = new Element("interfaces"); root.addContent(interfacesElement); for (String key : getInterfaces().keySet()) { Interface intface = getInterfaces().get(key); interfacesElement.addContent(intface.toDomElement()); } //Services Element servicesElement = new Element("services"); root.addContent(servicesElement); for (String key : services.keySet()) { Service service = services.get(key); servicesElement.addContent(service.toDomXMLElement()); } //Routes Element routesElement = new Element("routes"); root.addContent(routesElement); for (int i = 0; i < this.getRoutingTable().getRouteList().size(); i++) { Route route = this.getRoutingTable().getRouteList().get(i); routesElement.addContent(route.toDomXMLElement()); } //Firwall root.addContent(getInputFirewallRulesTable().toDomXMLElement()); root.addContent(getOutputFirewallRulesTable().toDomXMLElement()); return root; }
From source file:org.fiware.cybercaptor.server.informationsystem.Service.java
License:Open Source License
/** * Create the XML DOM element from this service * * @return the dom element corresponding to this service *///from ww w .j a va2 s .c o m public Element toDomXMLElement() { Element root = new Element("service"); Element nameElement = new Element("name"); nameElement.setText(this.getName()); root.addContent(nameElement); if (this.getIpAddress() != null) { Element ipaddressElement = new Element("ipaddress"); ipaddressElement.setText(this.getIpAddress().getAddress()); root.addContent(ipaddressElement); } if (this.getProtocol() != null) { Element protocolElement = new Element("protocol"); protocolElement.setText(this.getProtocol().toString().toUpperCase()); root.addContent(protocolElement); } if (this.getPortNumber() != 0) { Element portElement = new Element("port"); portElement.setText(this.getPortNumber() + ""); root.addContent(portElement); } if (this.getAccount() != null) { Element userElement = new Element("user"); userElement.setText(this.getAccount().getName()); root.addContent(userElement); } if (this.getCPE() != null) { Element cpeElement = new Element("CPE"); cpeElement.setText(this.getCPE()); root.addContent(cpeElement); } if (this.getVulnerabilities().size() > 0) { Element vulnerabilitiesElement = new Element("vulnerabilities"); root.addContent(vulnerabilitiesElement); for (String key : getVulnerabilities().keySet()) { Vulnerability vuln = getVulnerabilities().get(key); if (vuln == null) break; Element vulnerabilitiyElement = new Element("vulnerability"); Element typeElement = new Element("type"); typeElement.addContent(vuln.exploitType); vulnerabilitiyElement.addContent(typeElement); Element goalElement = new Element("goal"); goalElement.addContent(vuln.exploitGoal); vulnerabilitiyElement.addContent(goalElement); Element cveElement = new Element("cve"); cveElement.addContent(vuln.cve); vulnerabilitiyElement.addContent(cveElement); vulnerabilitiesElement.addContent(vulnerabilitiyElement); } } return root; }