List of usage examples for org.jdom2 Element getChildren
public List<Element> getChildren()
List
of all the child elements nested directly (one level deep) within this element, as Element
objects. From source file:com.init.octo.schema.XSDSchema.java
License:Open Source License
/** wow * /*w w w . j a v a2 s . co m*/ * @param parent * @param childName * @return */ private Element findElement(Element parent, String childName) { List<Element> children = parent.getChildren(); ListIterator<?> it = children.listIterator(); /** We need to go backwards through the list... **/ while (it.hasNext()) { it.next(); } while (it.hasPrevious()) { Element child = (Element) it.previous(); if (child.getName().equals(childName)) { return (child); } } return (null); }
From source file:com.init.octo.schema.XSDSequence.java
License:Open Source License
/** * This method builds a sequence element * * @param root - the sequence element that defines this element * @param cache - a list of pre-defined XML types *//*from w ww .j ava2 s .co m*/ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of a sequence"); id = root.getAttributeValue(XSDSchema.ID_ATT); maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT); minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT); group = new LinkedList<XMLType>(); Element element; String elementName; for (Iterator<?> i = (root.getChildren()).iterator(); i.hasNext();) { element = (Element) i.next(); elementName = element.getName(); log.debug("" + indent + ": " + "Child element <" + elementName + "> found"); /** process the child elements that define this element... **/ if (elementName.equals(XSDSchema.ANNOTATION)) { annotation = element.getTextTrim(); } else if (elementName.equals(XSDSchema.ELEMENT)) { log.debug("" + indent + ": " + "Adding element to the list of child elements"); XSDElement e = new XSDElement(indent + 1); if (e.build(element, cache, parentName) != true) { log.debug("Error building the element"); return (false); } group.add(e); } else if (elementName.equals(XSDSchema.GROUP)) { log.debug("" + indent + ": " + "Adding group to the list of child elements"); XSDGroupType g = new XSDElementGroup(indent); // child elements will be at the same level if (g.build(element, cache, parentName) != true) { log.error("Error building a group"); return (false); } group.add(g); } else if (elementName.equals(XSDSchema.CHOICE)) { log.debug("" + indent + ": " + "Adding choice to the list of child elements"); XSDChoice c = new XSDChoice(indent); // child elements will be at the same level if (c.build(element, cache, parentName) != true) { log.error("Error building a choice"); return (false); } group.add(c); } else if (elementName.equals(XSDSchema.SEQUENCE)) { log.debug("" + indent + ": " + "Adding sequence to the list of child elements"); XSDSequence s = new XSDSequence(indent); // child elements will be at the same level if (s.build(element, cache, parentName) != true) { log.error("Error building a sequence"); return (false); } group.add(s); } else { log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <sequence> tag log.debug("" + indent + ": " + "Sequence built"); return (true); }
From source file:com.izforge.izpack.util.xmlmerge.action.DtdInsertAction.java
License:Open Source License
@Override public void perform(Element originalElement, Element patchElement, Element outputParentElement) throws AbstractXmlMergeException { Element element;/* w w w .ja va 2 s .c o m*/ if (originalElement != null) { element = (Element) originalElement.clone(); } else { element = (Element) patchElement.clone(); } DTD dtd = getDTD(outputParentElement); List<DTDElement> dtdElements = dtd.getItemsByType(DTDElement.class); // Find the corresponding element DTDElement parentDtdElement = null; for (DTDElement dtdElement : dtdElements) { if (dtdElement.getName().equals(outputParentElement.getName())) { parentDtdElement = dtdElement; } } if (parentDtdElement == null) { throw new ElementException(element, "Element " + outputParentElement.getName() + " not defined in DTD"); } else { DTDItem item = parentDtdElement.getContent(); if (item instanceof DTDAny) { // the parent element accepts anything in any order outputParentElement.addContent(element); } else if (item instanceof DTDContainer) { // List existing elements in output parent element List<Element> existingChildren = outputParentElement.getChildren(); if (existingChildren.size() == 0) { // This is the first child outputParentElement.addContent(element); } else { List<String> orderedDtdElements = getOrderedDtdElements((DTDContainer) item); int indexOfNewElementInDtd = orderedDtdElements.indexOf(element.getName()); logger.fine("index of element " + element.getName() + ": " + indexOfNewElementInDtd); int pos = existingChildren.size(); // Calculate the position in the parent where we insert the // element for (int i = 0; i < existingChildren.size(); i++) { String elementName = (existingChildren.get(i)).getName(); logger.fine( "index of child " + elementName + ": " + orderedDtdElements.indexOf(elementName)); if (orderedDtdElements.indexOf(elementName) > indexOfNewElementInDtd) { pos = i; break; } } logger.fine("adding element " + element.getName() + " add in pos " + pos); outputParentElement.addContent(pos, element); } } } }
From source file:com.izforge.izpack.util.xmlmerge.merge.DefaultXmlMerge.java
License:Open Source License
/** * Performs the actual merge./* w w w. j a v a 2s.c o m*/ * * @param docs The documents to merge. The first doc is assumed to be the original one to apply patches against. * @return The merged result document * @throws AbstractXmlMergeException If an error occurred during the merge */ private Document doMerge(Document[] docs) throws AbstractXmlMergeException { Document originalDoc = docs[0]; Element origRootElement = originalDoc.getRootElement(); for (int i = 1; i < docs.length; i++) { Element comparedRootElement = docs[i].getRootElement(); Document output = new Document(); if (originalDoc.getDocType() != null) { output.setDocType((DocType) originalDoc.getDocType().clone()); } output.setRootElement(new Element("root")); Element outputRootElement = output.getRootElement(); m_rootMergeAction.perform(origRootElement, comparedRootElement, outputRootElement); Element root = (Element) outputRootElement.getChildren().get(0); root.detach(); sortRootChildrenRecursive(root); originalDoc.setRootElement(root); } return originalDoc; }
From source file:com.izforge.izpack.util.xmlmerge.merge.DefaultXmlMerge.java
License:Open Source License
private static void sortRootChildrenRecursive(Element root, Comparator<Element> comparator) { for (Element element : root.getChildren()) { sortRootChildrenRecursive(element, comparator); }//from www .j av a 2s . c o m root.sortChildren(comparator); }
From source file:com.kixeye.scout.eureka.EurekaServiceAmazonDataCenterInfo.java
License:Apache License
/** * Creates a descriptor from a parent and a raw element. * /*www .j a v a 2s . c om*/ * @param parent * @param instanceElement */ protected EurekaServiceAmazonDataCenterInfo(EurekaServiceInstanceDescriptor parent, Element instanceElement) { this.parent = parent; this.name = instanceElement.getChildText("name"); Element metadata = instanceElement.getChild("metadata"); if (metadata != null) { for (Element element : metadata.getChildren()) { this.metadata.put(element.getName(), element.getText()); } } }
From source file:com.kixeye.scout.eureka.EurekaServiceInstanceDescriptor.java
License:Apache License
/** * Creates a descriptor from a parent and a raw element. * //from w w w. j av a 2 s .c o m * @param parent * @param instanceElement */ protected EurekaServiceInstanceDescriptor(EurekaApplication parent, Element instanceElement) { this.parent = parent; this.app = instanceElement.getChildText("app"); this.ipAddress = instanceElement.getChildText("ipAddr"); this.hostname = instanceElement.getChildText("hostName"); this.vipAddress = instanceElement.getChildText("vipAddress"); int lastUpdatedTimestampRaw; try { lastUpdatedTimestampRaw = Integer.parseInt(instanceElement.getChildText("lastUpdatedTimestamp")); } catch (Exception e) { lastUpdatedTimestampRaw = -11; } this.lastUpdatedTimestamp = lastUpdatedTimestampRaw; int lastDirtyTimestampRaw; try { lastDirtyTimestampRaw = Integer.parseInt(instanceElement.getChildText("lastDirtyTimestamp")); } catch (Exception e) { lastDirtyTimestampRaw = -11; } this.lastDirtyTimestamp = lastDirtyTimestampRaw; Element port = instanceElement.getChild("port"); if (port != null) { this.isPortEnabled = Boolean.valueOf(port.getAttributeValue("enabled", "true")); this.port = Integer.valueOf(port.getTextTrim()); } else { this.isPortEnabled = false; this.port = -1; } Element securePort = instanceElement.getChild("securePort"); if (securePort != null) { this.isSecurePortEnabled = Boolean.valueOf(securePort.getAttributeValue("enabled", "true")); this.securePort = Integer.valueOf(securePort.getTextTrim()); } else { this.isSecurePortEnabled = false; this.securePort = -1; } Element statusElement = instanceElement.getChild("status"); ServiceStatus status = null; if (statusElement != null) { switch (statusElement.getTextTrim()) { case "UP": status = ServiceStatus.UP; break; case "DOWN": status = ServiceStatus.DOWN; break; default: status = ServiceStatus.UNKNOWN; } } this.status = status; Element overridenStatusElement = instanceElement.getChild("overriddenstatus"); ServiceStatus overridenStatus = null; if (overridenStatusElement != null) { switch (overridenStatusElement.getTextTrim()) { case "UP": overridenStatus = ServiceStatus.UP; break; case "DOWN": overridenStatus = ServiceStatus.DOWN; break; default: overridenStatus = ServiceStatus.UNKNOWN; } } this.overridenStatus = overridenStatus; Element metadata = instanceElement.getChild("metadata"); if (metadata != null) { for (Element element : metadata.getChildren()) { this.metadata.put(element.getName(), element.getText()); } } Element dataCenterInfo = instanceElement.getChild("dataCenterInfo"); if (dataCenterInfo != null) { Attribute dataCenterInfoClass = instanceElement.getAttribute("class"); if (dataCenterInfoClass != null && dataCenterInfoClass.getValue() != null) { switch (dataCenterInfoClass.getValue()) { case EurekaServiceAmazonDataCenterInfo.DATA_CENTER_INFO_CLASS: this.dataCenterInfo = new EurekaServiceAmazonDataCenterInfo(this, dataCenterInfo); break; default: this.dataCenterInfo = null; } } else { this.dataCenterInfo = null; } } else { this.dataCenterInfo = null; } }
From source file:com.locutus.outils.graphes.ConvertisseurXGMLGraphe.java
License:Open Source License
/** * @param fl//w w w . j ava 2s. c om * @throws GraphConversionException */ private DiGraph<String> produceDiGraphByGXML(File fl) throws GraphConversionException { DiGraph<String> dg = new DiGraph<String>(); // Racine du document Element racine = document.getRootElement(); // Itrateur des lments du premier niveau Iterator<Element> it = racine.getChildren().iterator(); // Element correspondant au graphe Element graph = null; while (it.hasNext() && graph == null) { Element local = it.next(); // On rcupre la section Graph if (local.getAttribute("name") != null && local.getAttribute("name").getValue().equals("graph")) { // On a trouv une section graphe graph = local; } } if (graph == null) { // Erreur car le fichier ne possde pas de section Graph au premier // niveaus throw new GraphConversionException("Le fichier transmis n'est pas un graphe utilisable."); } else { this.traduireGraphe(dg, graph); if (idNode.size() > 0) { makeEdgesAndAdd(dg); } else { throw new GraphConversionException("Le fichier ne contient aucun noeud."); } } return dg; }
From source file:com.locutus.outils.graphes.ConvertisseurXGMLGraphe.java
License:Open Source License
/** * //ww w . ja v a 2 s . c o m * @param graph * @throws GraphConversionException */ private void traduireGraphe(DiGraph<String> dgc, Element graph) throws GraphConversionException { Iterator<Element> elemGraphIt = graph.getChildren().iterator(); while (elemGraphIt.hasNext()) { // On va chercher dans cette catgorie les Nodes et les Edges Element local2 = elemGraphIt.next(); if (local2.getAttribute("name") != null && local2.getAttribute("name").getValue().equals("node")) { createNodeAndAdd(dgc, local2); } else if (local2.getAttribute("name") != null && local2.getAttribute("name").getValue().equals("edge")) { createEdge(dgc, local2); } } }
From source file:com.locutus.outils.graphes.ConvertisseurXGMLGraphe.java
License:Open Source License
/** * /*from w w w . ja va2s . co m*/ * @param dgc * @param el * @throws GraphConversionException */ private void createNodeAndAdd(DiGraph<String> dgc, Element el) throws GraphConversionException { // On cre un itrateur sur les informations du Node qu'on a // pralablement dtect. Iterator<Element> it3 = el.getChildren().iterator(); // On prpare la rcupration de l'id du node et du nomFichier du // Concept. int[] id = new int[1]; String nomFichierConcept = null; while (it3.hasNext()) { Element local3 = it3.next(); // On rcupre l'id if (local3.getAttribute("key") != null && local3.getAttribute("key").getValue().equals("id")) id[0] = Integer.parseInt(local3.getText()); // On rcupre le nom else if (local3.getAttribute("key") != null && local3.getAttribute("key").getValue().equals("label")) nomFichierConcept = new String(local3.getText()); } // Si le nom n'est pas vide if (nomFichierConcept != null) { idNode.put(id, new Node<String>(nomFichierConcept)); } else { throw new GraphConversionException( "Il manque le nom fichier du concept pour le node en cours de traitement"); } }