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:amara.applications.ingame.entitysystem.templates.CustomSerializer_Ingame.java
public static void registerClasses() { BitstreamClassManager.getInstance().register(ArrayList.class, //physics/HitboxComponent Circle.class, Rectangle.class, RegularCyclic.class, Shape.class, ConvexShape.class, SimpleConvexPolygon.class, PointShape.class, Transform2D.class, Vector2D.class, PolygonShape.class, BoundRectangle.class, Polygon.class, SetPolygon.class, HolePolygon.class, SimplePolygon.class, //units/DamageHistoryComponent DamageHistoryComponent.DamageHistoryEntry.class); ComponentsRegistrator.registerComponents(); try {// w w w. j av a2s . c o m ComponentSerializer.registerFieldSerializer( new Field[] { Vector2f.class.getDeclaredField("x"), Vector2f.class.getDeclaredField("y") }, new FieldSerializer_Float(20, 8)); ComponentSerializer.registerFieldSerializer(new Field[] { Vector2D.class.getDeclaredField("x"), Vector2D.class.getDeclaredField("y"), Transform2D.class.getDeclaredField("scalecos"), Transform2D.class.getDeclaredField("scalesin"), Transform2D.class.getDeclaredField("x"), Transform2D.class.getDeclaredField("y"), Circle.class.getDeclaredField("localRadius"), }, new FieldSerializer_DoubleAsFloat(20, 8)); } catch (NoSuchFieldException ex) { ex.printStackTrace(); } XMLTemplateManager xmlTemplateManager = XMLTemplateManager.getInstance(); //effects/physics xmlTemplateManager .registerComponent(new XMLComponentConstructor<AddCollisionGroupsComponent>("addCollisionGroups") { @Override public AddCollisionGroupsComponent construct(EntityWorld entityWorld, Element element) { long targetOf = getCollisionGroupBitMask(element.getAttributeValue("targetOf")); long targets = getCollisionGroupBitMask(element.getAttributeValue("targets")); return new AddCollisionGroupsComponent(targetOf, targets); } }); xmlTemplateManager.registerComponent( new XMLComponentConstructor<RemoveCollisionGroupsComponent>("removeCollisionGroups") { @Override public RemoveCollisionGroupsComponent construct(EntityWorld entityWorld, Element element) { long targetOf = getCollisionGroupBitMask(element.getAttributeValue("targetOf")); long targets = getCollisionGroupBitMask(element.getAttributeValue("targets")); return new RemoveCollisionGroupsComponent(targetOf, targets); } }); //physics xmlTemplateManager.registerComponent(new XMLComponentConstructor<HitboxComponent>("hitbox") { @Override public HitboxComponent construct(EntityWorld entityWorld, Element element) { Shape shape = null; Element childElement = (Element) element.getChildren().get(0); String shapeType = childElement.getName(); double x = 0; String xText = childElement.getAttributeValue("x"); if (xText != null) { x = Double.parseDouble(xText); } double y = 0; String yText = childElement.getAttributeValue("y"); if (yText != null) { y = Double.parseDouble(yText); } if (shapeType.equals("regularCyclic")) { int edges = Integer.parseInt(childElement.getAttributeValue("edges")); double radius = Double.parseDouble(childElement.getAttributeValue("radius")); shape = new RegularCyclic(edges, radius); } else if (shapeType.equals("circle")) { double radius = Double.parseDouble(childElement.getAttributeValue("radius")); shape = new Circle(x, y, radius); } else if (shapeType.equals("rectangle")) { double width = Double.parseDouble(childElement.getAttributeValue("width")); double height = Double.parseDouble(childElement.getAttributeValue("height")); shape = new Rectangle(x, y, width, height); } else if (shapeType.equals("point")) { Vector2D localPoint = new Vector2D(); String[] positionCoordinates = element.getText().split(","); if (positionCoordinates.length > 1) { double localPointX = Double .parseDouble(xmlTemplateManager.parseValue(entityWorld, positionCoordinates[0])); double localPointY = Double .parseDouble(xmlTemplateManager.parseValue(entityWorld, positionCoordinates[1])); localPoint = new Vector2D(localPointX, localPointY); } shape = new PointShape(localPoint); } if (shape == null) { throw new UnsupportedOperationException("Unsupported shape type '" + shapeType + "'."); } return new HitboxComponent(shape); } }); //spawns xmlTemplateManager.registerComponent(new XMLComponentConstructor<SpawnTemplateComponent>("spawnTemplate") { @Override public SpawnTemplateComponent construct(EntityWorld entityWorld, Element element) { String[] templates = element.getText().split("\\|"); for (int i = 0; i < templates.length; i++) { templates[i] = xmlTemplateManager.parseTemplate(entityWorld, templates[i]); } return new SpawnTemplateComponent(templates); } }); //spells xmlTemplateManager.registerComponent(new XMLComponentConstructor<CastTypeComponent>("castType") { @Override public CastTypeComponent construct(EntityWorld entityWorld, Element element) { return new CastTypeComponent(CastTypeComponent.CastType.valueOf(element.getText().toUpperCase())); } }); //units xmlTemplateManager .registerComponent(new XMLComponentConstructor<CollisionGroupComponent>("collisionGroup") { @Override public CollisionGroupComponent construct(EntityWorld entityWorld, Element element) { long targetOf = getCollisionGroupBitMask(element.getAttributeValue("targetOf")); long targets = getCollisionGroupBitMask(element.getAttributeValue("targets")); return new CollisionGroupComponent(targetOf, targets); } }); xmlTemplateManager .registerComponent(new XMLComponentConstructor<HealthBarStyleComponent>("healthBarStyle") { @Override public HealthBarStyleComponent construct(EntityWorld entityWorld, Element element) { return new HealthBarStyleComponent( HealthBarStyleComponent.HealthBarStyle.valueOf(element.getText().toUpperCase())); } }); }
From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.DOMFormulaConverter.java
License:Open Source License
@Override public Formula parse(int id, String latexFormula) { Formula formula = super.parseToMathML(id, latexFormula); // Generate output Element div = new Element("div"); Element html = new Element("div"); html.setAttribute("class", "math"); if (formula.isInvalid() == false) { SAXBuilder builder = new SAXBuilder(); try {/* w ww. j a v a 2s . c o m*/ Document mathml = builder.build(new StringReader(formula.getMathMl())); Element root = mathml.getRootElement(); if (root.getChildren().isEmpty()) { return null; } Iterator<Element> it = root.getChildren().iterator(); while (it.hasNext()) { Element cur = it.next(); FormulaElement formulaElement = renderElement(cur); if (formulaElement != null) { Element resultHtml = formulaElement.render(null, null); if (resultHtml != null) { html.addContent(resultHtml); } else { logger.debug("HTML is NULL: " + cur.getName()); } } } } catch (JDOMException e) { logger.error("Error parsing generated MathML:"); logger.error(formula.getMathMl()); logger.error(e.getMessage(), e); } catch (IOException e) { logger.error("Error reading generated MathML:"); logger.error(formula.getMathMl()); logger.error(e.getMessage(), e); } } else { html.addContent(renderInvalidFormulaSource(formula)); } div.addContent(html); formula.setHtml(div); return formula; }
From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.DOMFormulaConverter.java
License:Open Source License
/** * Recursive function to render all FormulaElements * Converts MathML to HTML// ww w . j a v a 2 s . co m * * @param cur the current processed MathML JDOM2 Element (a node or leaf inside MathML's DOM tree) * @return an object which implements the FormulaElement interface, so it can be rendered to HTML */ private FormulaElement renderElement(Element cur) { String name = cur.getName(); String mathvariant = null; FormulaElement output; // Based on the MathML tag a corresponding class will be chosen and output will be rendered switch (name.toLowerCase()) { // Superscripts case "msup": Msup msup = new Msup(); msup.setBase(renderElement(cur.getChildren().get(0))); msup.setSuperscript(renderElement(cur.getChildren().get(1))); output = msup; break; // Subscripts case "msub": Msub msub = new Msub(); msub.setBase(renderElement(cur.getChildren().get(0))); msub.setSubscript(renderElement(cur.getChildren().get(1))); output = msub; break; // Subscript-superscript Pairs case "msubsup": Msubsup msubsup = new Msubsup(); msubsup.setBase(renderElement(cur.getChildren().get(0))); msubsup.setSubscript(renderElement(cur.getChildren().get(1))); msubsup.setSuperscript(renderElement(cur.getChildren().get(2))); output = msubsup; break; // Rows case "mrow": Mrow mrow = new Mrow(); Iterator<Element> iterator = cur.getChildren().iterator(); while (iterator.hasNext()) { Element element = iterator.next(); FormulaElement rowElement = renderElement(element); mrow.addElement(rowElement); } output = mrow; break; // operators case "mo": Mo mo; String operator = cur.getText(); // find operator in dictionary mo = MathmlCharacterDictionary.findOperator(operator, cur.getAttributeValue("form", "infix")); if (mo == null) { mo = new Mo(); mo.setValue(operator); // Parse attributes mo.setAccent(Boolean.parseBoolean(cur.getAttributeValue("accent", "false"))); mo.setSeparator(Boolean.parseBoolean(cur.getAttributeValue("separator", "false"))); mo.setFence(Boolean.parseBoolean(cur.getAttributeValue("fence", "false"))); mo.setMovablelimits(Boolean.parseBoolean(cur.getAttributeValue("movablelimits", "false"))); mo.setLargeop(Boolean.parseBoolean(cur.getAttributeValue("largeop", "false"))); mo.setStretchy(Boolean.parseBoolean(cur.getAttributeValue("stretchy", "false"))); mo.setLspace(Unit.parse(cur.getAttributeValue("lspace", "thickmathspace"))); mo.setRspace(Unit.parse(cur.getAttributeValue("rspace", "thickmathspace"))); mo.setMinsize(Unit.parse(cur.getAttributeValue("minsize"))); mo.setMaxsize(Unit.parse(cur.getAttributeValue("maxsize"))); } output = mo; break; // numbers case "mn": Mn mn = new Mn(); mn.setValue(cur.getText()); output = mn; break; // identifiers case "mi": Mi mi = new Mi(); mi.setValue(cur.getText()); mathvariant = cur.getAttributeValue("mathvariant"); if (mathvariant != null && mathvariant.isEmpty() == false) { mi.setMathvariant(mathvariant); mathvariant = null; } output = mi; break; // fractions case "mfrac": Mfrac mfrac = new Mfrac(); mfrac.setNumerator(renderElement(cur.getChildren().get(0))); mfrac.setDenominator(renderElement(cur.getChildren().get(1))); String linethickness = cur.getAttributeValue("linethickness"); if (linethickness != null) { mfrac.setLinethickness(linethickness); } output = mfrac; break; // Expression Inside Pair of Fences case "mfenced": Mfenced mfenced = new Mfenced(); mfenced.setOpened(cur.getAttributeValue("open")); mfenced.setClosed(cur.getAttributeValue("close")); mfenced.setSeparators(cur.getAttributeValue("separators")); List<Element> children = cur.getChildren(); if (children.isEmpty() == false) { List<FormulaElement> renderedChildren = new ArrayList<>(); for (Element child : children) { FormulaElement renderedChild = renderElement(child); renderedChildren.add(renderedChild); } mfenced.setContent(renderedChildren); } output = mfenced; break; // Space case "mspace": Mspace mspace = new Mspace(); // Parse attributes String widthAttribute = cur.getAttributeValue("width"); if (widthAttribute != null) { mspace.setWidth(Unit.parse(widthAttribute)); } String heightAttribute = cur.getAttributeValue("height"); if (heightAttribute != null) { mspace.setHeight(Unit.parse(heightAttribute)); } // linebreaks will be ignored for now output = mspace; break; // Square root case "msqrt": Mroot msqrt = new Mroot(); msqrt.setBase(renderElement(cur.getChildren().get(0))); // no index output = msqrt; break; // Root case "mroot": Mroot mroot = new Mroot(); mroot.setBase(renderElement(cur.getChildren().get(0))); mroot.setDegree(renderElement(cur.getChildren().get(1))); output = mroot; break; // String literal case "ms": Ms ms = new Ms(); ms.setValue(cur.getText()); output = ms; break; // Text case "mtext": Mtext mtext = new Mtext(); mtext.setValue(cur.getText()); output = mtext; break; // Style change case "mstyle": Mstyle mstyle = new Mstyle(); mathvariant = cur.getAttributeValue("mathvariant"); if (mathvariant != null && mathvariant.isEmpty() == false) { mstyle.setStyle(mathvariant); mathvariant = null; } Iterator<Element> mstyleIterator = cur.getChildren().iterator(); while (mstyleIterator.hasNext()) { mstyle.addBaseElement(renderElement(mstyleIterator.next())); } output = mstyle; break; // Overscript case "mover": Mover mover = new Mover(); mover.setBase(renderElement(cur.getChildren().get(0))); mover.setOverscript(renderElement(cur.getChildren().get(1))); output = mover; break; // Underscript case "munder": Munder munder = new Munder(); munder.setBase(renderElement(cur.getChildren().get(0))); munder.setUnderscript(renderElement(cur.getChildren().get(1))); output = munder; break; // Matrices & tables case "mtable": Mtable mtable = new Mtable(); Iterator<Element> mRowIterator = cur.getChildren().iterator(); while (mRowIterator.hasNext()) { Mtr mtr = (Mtr) renderElement(mRowIterator.next()); mtable.getRows().add(mtr); } output = mtable; break; // Table rows case "mtr": Mtr mtr = new Mtr(); Iterator<Element> mCellIterator = cur.getChildren().iterator(); while (mCellIterator.hasNext()) { Mtd mtd = (Mtd) renderElement(mCellIterator.next()); mtr.getTds().add(mtd); } output = mtr; break; // Table cells case "mtd": Mtd mtd = new Mtd(); Mrow tdContent = new Mrow(); Iterator<Element> mContentIterator = cur.getChildren().iterator(); while (mContentIterator.hasNext()) { tdContent.addElement(renderElement(mContentIterator.next())); } mtd.setContent(tdContent); output = mtd; break; default: logger.info("MathML conversion of element <" + cur.getName() + "> NOT YET IMPLEMENTED"); output = null; break; } return output; }
From source file:ataraxis.passwordmanager.XMLHandler.java
License:Open Source License
/** * Check if the ElementId has Child-Elements. * //w w w. j a va 2 s. c o m * @param ElementID * @return true if Child's exist, false otherwise */ public boolean hasChilds(String ElementID) { boolean hasChilds = false; if (ElementID == null || ElementID.equals("")) { hasChilds = false; } else { XPathExpression<Element> xpath = XPathFactory.instance().compile("//group[@id='" + ElementID + "']", Filters.element()); Element tempElement = xpath.evaluateFirst(s_doc); if (tempElement == null) { hasChilds = false; } else { List<Element> childList = tempElement.getChildren(); hasChilds = (childList.size() > 0); } } return hasChilds; }
From source file:ataraxis.passwordmanager.XMLHandler.java
License:Open Source License
/** * Sort Child-Elements of the parent with the Comparator comp * @param parent the parent Element// w w w . j ava 2s . co m * @param comp the Comparator */ @SuppressWarnings({ "unchecked", "rawtypes" }) // Compiler-Errors with Generics are suppressed private void sortElements(Element parent, Comparator comp) { logger.debug("sortElements(Element, Comparator) - start"); // Create a new static list of child elements // and sort it. List<Element> children = new ArrayList<Element>(parent.getChildren()); Collections.sort(children, comp); // remove from every Element the Parent-Node-Link ListIterator<Element> childrenIter = children.listIterator(); while (childrenIter.hasNext()) { Object obj = childrenIter.next(); if (obj instanceof Element) ((Element) obj).detach(); } // set the new Child structure to the parent parent.setContent(children); logger.debug("sortElements(Element, Comparator) - end"); }
From source file:ataraxis.passwordmanager.XMLHandler.java
License:Open Source License
private List<PasswordEntry> getElementSubTree(Element parent) { List<PasswordEntry> allChildEntries = new ArrayList<PasswordEntry>(); Iterator<Element> topElements = parent.getChildren().iterator(); while (topElements.hasNext()) { Element currentElement = topElements.next(); PasswordEntry entry;/* w w w . j a v a 2s. c om*/ try { entry = getEntry(currentElement.getAttributeValue("id")); if (entry != null) { allChildEntries.add(entry); } String elementType = currentElement.getName(); if (elementType.equals("group")) { List<PasswordEntry> childs = getElementSubTree(currentElement); if (childs != null && childs.size() > 0) { allChildEntries.addAll(childs); } } } catch (EntryDoesNotExistException e) { logger.fatal(e.getMessage()); } } return allChildEntries; }
From source file:br.com.gsoftwares.util.ImportClient.java
private void lerarq(String caminho) throws Exception { //Aqui voc informa o nome do arquivo XML. File f = new File(caminho); File[] arquivos = f.listFiles(); //Criamos uma classe SAXBuilder que vai processar o XML for (File fileTmp : arquivos) { SAXBuilder sb = new SAXBuilder(); //Este documento agora possui toda a estrutura do arquivo. Document d;/*from ww w . j a v a 2s .c o m*/ try { d = sb.build(fileTmp); //Recuperamos o elemento root Element nfe = d.getRootElement(); //Recuperamos os atributos filhos (Attributes) List atributes = nfe.getAttributes(); Iterator i_atr = atributes.iterator(); //Iteramos com os atributos filhos while (i_atr.hasNext()) { Attribute atrib = (Attribute) i_atr.next(); //System.out.println("\nattribute de (" + nfe.getName() + "):" + atrib.getName() + " - valor: " + atrib.getValue()); } //Recuperamos os elementos filhos (children) List elements = nfe.getChildren(); Iterator i = elements.iterator(); //Iteramos com os elementos filhos, e filhos do dos filhos while (i.hasNext()) { Element element = (Element) i.next(); //System.out.println("element:" + element.getName()); trataElement(element); } Salvar(); } catch (JDOMException | IOException ex) { Logger.getLogger(LerArqXML.class.getName()).log(Level.SEVERE, null, ex); } } JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso!", "", JOptionPane.INFORMATION_MESSAGE); }
From source file:br.com.gsoftwares.util.ImportClient.java
private void trataElement(Element element) throws Exception { //Recuperamos os atributos filhos (Attributes) List atributes = element.getAttributes(); Iterator i_atr = atributes.iterator(); //Iteramos com os atributos filhos while (i_atr.hasNext()) { Attribute atrib = (Attribute) i_atr.next(); //System.out.println("\nattribute de (" + element.getName() + "):" + atrib.getName() + " - valor: " + atrib.getValue()); }// ww w . j av a2 s. c o m //Recuperamos os elementos filhos (children) List elements = element.getChildren(); Iterator it = elements.iterator(); //Iteramos com os elementos filhos, e filhos do dos filhos while (it.hasNext()) { Element el = (Element) it.next(); //System.out.println(el.getName() + " - " + el.getText()); switch (el.getName()) { case "CPF": CPF = el.getText(); Tipo = "F"; break; case "CNPJ": CPF = el.getText(); Tipo = "J"; break; case "xNome": Nome = el.getText(); Apelido = ""; break; case "xLgr": Rua = el.getText(); break; case "nro": numero = el.getText(); break; case "xCpl": Comp = el.getText(); break; case "xBairro": Bairro = el.getText(); break; case "cMun": Cod_Mun = el.getText(); break; case "xMun": Cidade = el.getText(); break; case "UF": Estado = el.getText(); break; case "CEP": Cep = el.getText(); break; case "fone": Telefone = el.getText(); break; case "IE": if (el.getText().equals("ISENTO")) { Tipo_ins = "2"; Ins = "ISENTO"; } else { if (el.getText().contains("^[0-9]*$")) { Tipo_ins = "1"; Ins = el.getText(); } else { Tipo_ins = "9"; Ins = ""; } break; } case "email": email = el.getText(); break; } trataElement(el); } }
From source file:br.com.gsoftwares.util.ImportProduto.java
private void lerarq(String caminho) throws Exception { //Aqui voc informa o nome do arquivo XML. File f = new File(caminho); File[] arquivos = f.listFiles(); //Criamos uma classe SAXBuilder que vai processar o XML for (File fileTmp : arquivos) { SAXBuilder sb = new SAXBuilder(); //Este documento agora possui toda a estrutura do arquivo. Document d;// ww w. java 2 s.c o m try { d = sb.build(fileTmp); //Recuperamos o elemento root Element nfe = d.getRootElement(); //Recuperamos os atributos filhos (Attributes) List atributes = nfe.getAttributes(); Iterator i_atr = atributes.iterator(); //Iteramos com os atributos filhos while (i_atr.hasNext()) { Attribute atrib = (Attribute) i_atr.next(); //System.out.println("\nattribute de (" + nfe.getName() + "):" + atrib.getName() + " - valor: " + atrib.getValue()); } //Recuperamos os elementos filhos (children) List elements = nfe.getChildren(); Iterator i = elements.iterator(); //Iteramos com os elementos filhos, e filhos do dos filhos while (i.hasNext()) { Element element = (Element) i.next(); //System.out.println("element:" + element.getName()); trataElement(element); } //Salvar(); } catch (JDOMException | IOException ex) { Logger.getLogger(LerArqXML.class.getName()).log(Level.SEVERE, null, ex); } } JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso!", "", JOptionPane.INFORMATION_MESSAGE); }
From source file:br.com.gsoftwares.util.ImportProduto.java
private void trataElement(Element element) throws Exception { //Recuperamos os atributos filhos (Attributes) List atributes = element.getAttributes(); Iterator i_atr = atributes.iterator(); //Iteramos com os atributos filhos while (i_atr.hasNext()) { Attribute atrib = (Attribute) i_atr.next(); //System.out.println("\nattribute de (" + element.getName() + "):" + atrib.getName() + " - valor: " + atrib.getValue()); }//from ww w . j a v a 2s .c o m //Recuperamos os elementos filhos (children) List elements = element.getChildren(); Iterator it = elements.iterator(); //Iteramos com os elementos filhos, e filhos do dos filhos while (it.hasNext()) { Element el = (Element) it.next(); System.out.println(el.getName() + " - " + el.getText()); switch (el.getName()) { case "cProd": CPF = el.getText(); Tipo = "F"; break; case "xProd": CPF = el.getText(); Tipo = "J"; break; case "NCM": Nome = el.getText(); Apelido = ""; break; case "uCom": Rua = el.getText(); break; case "orig": numero = el.getText(); break; } trataElement(el); } }