List of usage examples for org.jdom2 Element isRootElement
public boolean isRootElement()
From source file:de.danielluedecke.zettelkasten.DesktopFrame.java
License:Open Source License
/** * This method retrieves all entries on the desktop and adds their number to the * list {@code liste}. This array of entry-numbers is needed in the export-dialog. * * @param e the starting point for the jTree-enumeration, either the root elementor a bullet (if only * a bullet should be exported, see {@link #exportDesktopBullet() exportDesktopBullet()}). * @param liste an array-object that will hold the found entry-nubers *//* w w w. j av a 2 s . c o m*/ private void createExportEntries(Element e, ArrayList<Object> liste) { // if we have no element, return. if (null == e) return; // get a list with all children of the element List<Element> children = e.getChildren(); // create an iterator Iterator<Element> it = children.iterator(); // go through all children while (it.hasNext()) { // get the child e = it.next(); // we have to ignore the comment-tags here. comments are no tags that will // be displayed in the jtree, but that store comments which will be displayed // in the jeditorpane (see "updateDisplay" method for further details) if (!e.getName().equals("comment")) { // if the child is a bullet... if (e.getName().equals("bullet")) { // first, we want to retrieve the header-level int headerlevel = 1; // get bullet's parent Element f = e.getParentElement(); // as long as we have not reached the root, get further parent-elements // and increase counter for header-level while (!f.isRootElement()) { f = f.getParentElement(); headerlevel++; } // add the element's name-attribute. since headers might consist of only numbers, // we add a char here. this is necessary, since the export-methods distinguish // between headers and entry-numbers simply by parsing integer-values. if the parsing // succeeds, we have an entry, if a NumberFormatException is thrown, we have a headline. // to treat headline with numbers only as headlines, we add a char to be sure that every // headline will throw an exception when parsing the array's elements to integer. liste.add("h" + String.valueOf(headerlevel) + e.getAttributeValue("name")); } else { // now we know we have an entry. so get the entry number... int nr = Integer.parseInt(e.getAttributeValue("id")); liste.add(nr); } // when the new element also has children, call this method again, // so we go through the strucuture recursively... if (desktopObj.hasChildren(e)) { createExportEntries(e, liste); } } } }
From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.TypesExtractorImpl.java
License:Apache License
/** * Recursive method that traverses an element to extract all the possible information from it. * It is recursive because it calls itself for each child of the element (obviously, infinite recursion * is not possible as there are not, or there should not be, parent-child loops). * The index of the current document is necessary in order to add well some information to * the statistics./* w w w . j a va 2 s . c o m*/ * @param documentIndex index of current document * @param element the element to traverse (as a JDOM2 {@link Element}) * @param enclosingComplexType the complex type which will contain the current element */ private void traverseElement(int documentIndex, Element element, String enclosingComplexType) { //Elements in the XSI namespace should be ignored if (element.getNamespaceURI().equalsIgnoreCase(XSI_NAMESPACE_URI)) return; List<String> realPathUnfiltered = getRealPathOfElementUnfiltered(element, configuration, false, solvedNamespaceToPrefixMapping); String realPathFiltered = filterAndJoinRealPath(realPathUnfiltered);//Path for the statistics List<String> typePathUnfiltered = getRealPathOfElementUnfiltered(element, configuration, false, solvedNamespaceToPrefixMapping); List<String> suitablePath = getSuitablePath(typePathUnfiltered);//Path for type name inferencing //First, we will register the information of width and depth //The root is in a level whose width is 1, if we did not do the following, that width would be never registered if (element.isRootElement()) { statistics.registerWidth(documentIndex, 1); } statistics.registerDepth(documentIndex, realPathUnfiltered.size()); int width = element.getChildren().size(); if (width > 0) { statistics.registerWidth(documentIndex, width); } TypeNameInferencer typeNameInferencer = configuration.getTypeNameInferencer(); String complexTypeName = typeNameInferencer.inferTypeName(suitablePath, configuration);//Complex type of this element // //Little workaround that ensures that the same complex type is used // //when the elements on its path are the same (same name and namespace) but some of them // //use different namespace prefixes // List<String> realPathUnfilteredKey=getRealPathOfElementUnfiltered(element, configuration, false, solvedNamespaceToPrefixMapping); // List<String> suitablePathKey=getSuitablePath(realPathUnfilteredKey);//Path for type name inferencing // String complexTypeNameKey = typeNameInferencer.inferTypeName(suitablePathKey, configuration);//Complex type of this element String complexTypeNameKey = complexTypeName; //The complex type object of this element. ComplexType complexType = complexTypes.get(complexTypeNameKey); if (complexType == null) { complexType = new ComplexType(complexTypeName, null, null, null); complexTypes.put(complexTypeNameKey, complexType); //New complex type } complexType.addSourceNodeNamespaceAndName(element.getNamespaceURI(), element.getName()); //Comment processing for (Comment comment : element.getDescendants(Filters.comment())) { if (comment.getParentElement().equals(element)) complexType.getComments().add(comment.getText()); } //Key to find the corresponding SchemaElement //This key is: if the SchemaElement has an enclosing complex type (i.e., it is not a valid root), its name will be: //enclosingComplexType+typeNamesSeparator+elementName //If the element is a suitable root, the key is the name of the element. String schemaElementKey = (!enclosingComplexType.equals("")) ? enclosingComplexType + configuration.getTypeNamesAncestorsSeparator() + element.getName() : element.getName(); if (configuration.getTypeNameInferencer() instanceof NameTypeNameInferencer) { schemaElementKey = element.getName(); //If we use a name-based type inferencer, the key is the name and we avoid problems. } SchemaElement schemaElement = elements.get(element.getNamespaceURI(), schemaElementKey); if (schemaElement == null) { schemaElement = new SchemaElement(element.getName(), element.getNamespaceURI(), complexType);//Complex type already not known. elements.put(element.getNamespaceURI(), schemaElementKey, schemaElement); } boolean wasAlreadyValidRoot = schemaElement.isValidRoot(); schemaElement.setValidRoot(wasAlreadyValidRoot || element.isRootElement()); ComplexTypeStatisticsEntry complexTypeStatisticsEntry = statistics.getComplexTypeInfo().get(complexType); if (complexTypeStatisticsEntry == null) { complexTypeStatisticsEntry = new ComplexTypeStatisticsEntry(xmlDocuments.size()); statistics.getComplexTypeInfo().put(complexType, complexTypeStatisticsEntry); } AttributeListInferencer attributeListInferencer = attributeListInferencers.get(complexTypeName); if (attributeListInferencer == null) { attributeListInferencer = inferencersFactory.getAttributeListInferencerInstance(complexTypeName, configuration, solvedNamespaceToPrefixMapping, statistics); attributeListInferencers.put(complexTypeName, attributeListInferencer); } attributeListInferencer.learnAttributeList(element.getAttributes(), documentIndex); SimpleTypeInferencer simpleTypeInferencer = simpleTypeInferencersOfComplexTypes.get(complexTypeName); if (simpleTypeInferencer == null) { simpleTypeInferencer = inferencersFactory.getSimpleTypeInferencerInstance(complexTypeName, configuration); simpleTypeInferencersOfComplexTypes.put(complexTypeName, simpleTypeInferencer); } simpleTypeInferencer.learnValue(element.getText(), element.getNamespaceURI(), element.getName()); // SchemaElement previousChildSchemaElement=null; //We need to store the previous child in order to add the edge between it and the current child. List<SchemaElement> schemaElementChildren = new ArrayList<>(element.getChildren().size()); for (int i = 0; i < element.getChildren().size(); i++) { Element child = element.getChildren().get(i); traverseElement(documentIndex, child, complexTypeName); String childSchemaElementKey = complexTypeName + configuration.getTypeNamesAncestorsSeparator() + child.getName(); if (configuration.getTypeNameInferencer() instanceof NameTypeNameInferencer) { childSchemaElementKey = child.getName(); // If we use the name-based type name inferencer, the name is the key } SchemaElement childSchemaElement = elements.get(child.getNamespaceURI(), childSchemaElementKey);//The SchemaElement object does exist because the method traverseElement is called before this. // if(i==0){ // automaton.addEdge(automaton.getInitialState(), childSchemaElement); // } // else { // automaton.addEdge(previousChildSchemaElement, childSchemaElement); // if(i==(element.getChildren().size()-1)){ // automaton.addEdge(childSchemaElement, automaton.getFinalState()); // } // } complexTypeStatisticsEntry.registerElementCount(childSchemaElement, documentIndex); schemaElementChildren.add(childSchemaElement); // previousChildSchemaElement=childSchemaElement; } ExtendedAutomaton automaton = automatons.get(complexTypeName); if (automaton == null) { automaton = new ExtendedAutomaton(); SchemaElement initialState = new SchemaElement("initial", DEFAULT_PSEUDOELEMENTS_NAMESPACE, null); automaton.setInitialState(initialState); SchemaElement finalState = new SchemaElement("final", DEFAULT_PSEUDOELEMENTS_NAMESPACE, null); automaton.setFinalState(finalState); automatons.put(complexTypeName, automaton); } List<SchemaElement> schemaElementChildrenWithInitialAndFinal = new ArrayList<>(schemaElementChildren); schemaElementChildrenWithInitialAndFinal.add(0, automaton.getInitialState()); schemaElementChildrenWithInitialAndFinal.add(automaton.getFinalState()); automaton.learn(schemaElementChildrenWithInitialAndFinal); complexTypeStatisticsEntry.registerSubpatternsFromList(schemaElementChildren); complexTypeStatisticsEntry.registerValueOfNodeCount(element.getText(), schemaElement, documentIndex); statistics.registerElementAtPathCount(realPathFiltered, documentIndex); statistics.registerValueAtPathCount(realPathFiltered, element.getText(), documentIndex); if (enclosingComplexType.equals("")) { statistics.registerRootElementOccurrence(schemaElement); } }
From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java
License:Open Source License
private Map<String, DocElement> fillEntityDetails(Document templateDoc) { Map<String, DocElement> entityLevels = new HashMap<String, DocElement>(); Map<Element, Namespace> namespaces = removeNamespaces(templateDoc); for (ComponentEntitySetting compEntitySetting : getComponent().getEntitySettings()) { if (compEntitySetting.getName().equals(XML_FORMATTER_XPATH)) { XPathExpression<Element> expression = XPathFactory.instance().compile(compEntitySetting.getValue(), Filters.element());//ww w . j a va2 s .co m List<Element> matches = expression.evaluate(templateDoc.getRootElement()); if (matches.size() == 0) { log(LogLevel.WARN, "XPath expression " + compEntitySetting.getValue() + " did not find any matches"); } else { int level = 1; Element element = matches.get(0); if (!element.isRootElement()) { Element elementToMatch = element.getParentElement(); while (!elementToMatch.getName().equalsIgnoreCase(templateDoc.getRootElement().getName())) { elementToMatch = elementToMatch.getParentElement(); level++; } } entityLevels.put(compEntitySetting.getEntityId(), new DocElement(level, element, null, compEntitySetting.getValue())); } } } restoreNamespaces(templateDoc, namespaces); return entityLevels; }
From source file:se.miun.itm.input.model.param.generator.ValueGenerator.java
License:Open Source License
private void checkConstructorInit() throws InPUTException { Element parent = param.getParentElement(); if (parent.isRootElement()) return;//from ww w . ja va2 s. c om if (((Param<?>) parent).initByConstructor(param.getLocalId())) throw new InPUTException("The parameter \"" + param.getId() + "\" cannot be set via setter injection because it is instantiated by a constructor."); }
From source file:se.miun.itm.input.tuning.converter.SpotDesignInitializer.java
License:Open Source License
private boolean isAppropriateSubParam(Element parent, Param<?> param) { if (parent == null || containsAsChild(parent, param.getLocalId()) != null) return false; if (parent.isRootElement()) return true; String parentValue = parent.getAttributeValue(Q.VALUE_ATTR); String parentLocalId = parent.getAttributeValue(Q.ID_ATTR); if (parentValue == null) return true; // param.paramId second last entry must match "value" attribute of // parent!/*from www .j a va 2 s . c om*/ String paramParentLocalId = ((Param<?>) param.getParentElement()).getLocalId(); if (paramParentLocalId.equals(parentValue) || paramParentLocalId.equals(parentLocalId)) return true; return false; }
From source file:se.miun.itm.input.tuning.converter.SpotDesignInitializer.java
License:Open Source License
private boolean hasComplexGrandParent(Param<?> param) throws InPUTException { Element grandParent = param.getParentElement().getParentElement(); return grandParent != null && !grandParent.isRootElement() && isStructuralArrayType((Param<?>) grandParent); }
From source file:se.miun.itm.input.tuning.sequential.spot.analysis.AnalysisHelper.java
License:Open Source License
private static boolean isParentInDesign(Param<?> p, Map<String, String> sample) throws InPUTException { Element parent = p.getParentElement(); if (parent.isRootElement()) return true; boolean flag = false; if (parent instanceof SParam || parent instanceof NParam) { if (SpotDesignInitializer.isStructuralArrayType(parent) && p instanceof SChoice) { String compl, choiceNumber; for (int j = 1; j <= ((SParam) parent).getDimensions()[0]; j++) { compl = p.getParamId() + "." + j; choiceNumber = sample.get(compl); if (isRelevantComplexChoice((SChoice) p, sample, (SParam) parent, compl, choiceNumber)) { flag = true;/*from w ww.j ava 2 s. c o m*/ break; } } } else flag = isParentInDesign((Param<?>) parent, sample); } else if (parent instanceof SChoice) { flag = checkChoice(sample, parent, flag); } return flag; }
From source file:se.miun.itm.input.util.ParamInitializer.java
License:Open Source License
private static Integer getRootDist(Element elem) { int counter = 0; while (!elem.isRootElement()) { elem = elem.getParentElement();/* w ww . java2 s. c o m*/ counter++; } return counter; }
From source file:se.miun.itm.input.util.ParamUtil.java
License:Open Source License
public static String deriveInputValueId(Element value, String localId) { if (value == null || value.isRootElement()) return localId; return deriveInputValueId(value.getParentElement()) + "." + localId; }
From source file:se.miun.itm.input.util.ParamUtil.java
License:Open Source License
/** * derive parameter id for a given InPUT element. * /*from ww w.jav a2 s .com*/ * @param element * @param name * @return */ private static String deriveParamId(final Element element, String name) { // 1) not null, 2) is Element, 3) not root -> next! if (element.getParent() != null && element.getParent() instanceof Element) { Element parent = (Element) element.getParent(); if (!parent.isRootElement()) name = deriveParamId(parent, parent.getAttributeValue(Q.ID_ATTR) + "." + name); } return name; }