List of usage examples for org.jdom2 Element getName
public String getName()
From source file:ca.nrc.cadc.vosi.Availability.java
License:Open Source License
public AvailabilityStatus fromXmlDocument(Document doc) throws ParseException { Namespace vosi = Namespace.getNamespace("vosi", VOSI.AVAILABILITY_NS_URI); Element availability = doc.getRootElement(); if (!availability.getName().equals("availability")) throw new IllegalArgumentException("missing root element 'availability'"); Element elemAvailable = availability.getChild("available", vosi); if (elemAvailable == null) throw new IllegalArgumentException("missing element 'available'"); boolean available = elemAvailable.getText().equalsIgnoreCase("true"); DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC); Element elemUpSince = availability.getChild("upSince", vosi); Element elemDownAt = availability.getChild("downAt", vosi); Element elemBackAt = availability.getChild("backAt", vosi); Element elemNote = availability.getChild("note", vosi); Date upSince = null;/* www . j a v a 2 s . c o m*/ Date downAt = null; Date backAt = null; String note = null; if (elemUpSince != null) upSince = df.parse(elemUpSince.getText()); if (elemDownAt != null) downAt = df.parse(elemDownAt.getText()); if (elemBackAt != null) backAt = df.parse(elemBackAt.getText()); if (elemNote != null) note = elemNote.getText(); return new AvailabilityStatus(available, upSince, downAt, backAt, note); }
From source file:ca.nrc.cadc.vosi.TableSetReader.java
License:Open Source License
private TapSchema toTapSchema(Document doc) { TapSchema ret = new TapSchema(); Element root = doc.getRootElement(); Namespace xsi = root.getNamespace("xsi"); if ("tableset".equals(root.getName())) { // content is element-form unqualified List<Element> sels = root.getChildren("schema"); for (Element se : sels) { String sn = se.getChildTextTrim("name"); SchemaDesc sd = new SchemaDesc(sn); List<Element> tabs = se.getChildren("table"); for (Element te : tabs) { TableDesc td = TableReader.toTable(sn, te, xsi); String tn = td.getTableName(); sd.getTableDescs().add(td); }/*from www .j a v a 2 s . c om*/ ret.getSchemaDescs().add(sd); } } return ret; }
From source file:ca.nrc.cadc.xml.JsonOutputter.java
License:Open Source License
private boolean writeAttributes(Element e, PrintWriter w, int i) throws IOException { boolean ret = writeSchemaAttributes(e, w, i); Iterator<Attribute> iter = e.getAttributes().iterator(); if (ret && iter.hasNext()) w.print(","); while (iter.hasNext()) { ret = true;/* ww w. j av a2 s . c o m*/ Attribute a = iter.next(); indent(w, i); w.print(QUOTE); w.print("@"); if (StringUtil.hasText(a.getNamespacePrefix())) { w.print(a.getNamespacePrefix()); w.print(":"); } w.print(a.getName()); w.print(QUOTE); w.print(" : "); if (isBoolean(e.getName(), a.getValue()) || isNumeric(e.getName(), a.getValue())) { w.print(a.getValue()); } else { w.print(QUOTE); w.print(a.getValue()); w.print(QUOTE); } if (iter.hasNext()) w.print(","); } return ret; }
From source file:ca.nrc.cadc.xml.JsonOutputter.java
License:Open Source License
private void writeElement(Element e, PrintWriter w, int i, boolean listItem) throws IOException { boolean childListItem = listElementNames.contains(e.getName()); indent(w, i);/*from w w w .j av a 2s .c om*/ if (!listItem) { // write key w.print(QUOTE); if (StringUtil.hasText(e.getNamespacePrefix())) { w.print(e.getNamespacePrefix()); w.print(":"); } w.print(e.getName()); w.print(QUOTE); w.print(" : "); } // write value w.print("{"); boolean multiLine = true; boolean children = false; boolean attrs = writeAttributes(e, w, i + 1); if (childListItem) { // in badgerfish, this would be the name of child elements but prefer $ since [] // is the value of e and e is a list of children; $ is also consistent with how // we (and badgerfish) handle leaf values inside elements: { "$" : value } if (attrs) { w.print(","); } indent(w, i + 1); w.print(QUOTE); w.print("$"); w.print(QUOTE); w.print(" : "); w.print("["); children = writeChildElements(e, w, i + 2, childListItem, attrs); indent(w, i + 1); w.print("]"); } else children = writeChildElements(e, w, i + 1, childListItem, attrs); if (!children && !childListItem) // plain value { if (attrs) { w.print(","); indent(w, i + 1); } else multiLine = false; String sval = e.getTextNormalize(); w.print(QUOTE); w.print("$"); w.print(QUOTE); w.print(" : "); if (isBoolean(e.getName(), sval) || isNumeric(e.getName(), sval)) w.print(sval); else { w.print(QUOTE); w.print(sval); w.print(QUOTE); } } if (multiLine) indent(w, i); w.print("}"); }
From source file:cager.parser.test.SimpleTest2.java
License:Open Source License
private void XMLtoJavaParser() { // Creamos el builder basado en SAX SAXBuilder builder = new SAXBuilder(); try {/*from w w w . j av a2 s . co m*/ String nombreMetodo = null; List<String> atributos = new ArrayList<String>(); String tipoRetorno = null; String nombreArchivo = null; String exportValue = ""; String codigoFuente = ""; HashMap<String, String> tipos = new HashMap<String, String>(); // Construimos el arbol DOM a partir del fichero xml Document doc = builder.build(new FileInputStream(ruta + "VBParser\\ejemplosKDM\\archivo.kdm")); //Document doc = builder.build(new FileInputStream("E:\\WorkspaceParser\\VBParser\\ejemplosKDM\\archivo.kdm")); Namespace xmi = Namespace.getNamespace("xmi", "http://www.omg.org/XMI"); XPathExpression<Element> xpath = XPathFactory.instance().compile("//codeElement", Filters.element()); List<Element> elements = xpath.evaluate(doc); for (Element emt : elements) { if (emt.getAttribute("type", xmi).getValue().compareTo("code:CompilationUnit") == 0) { nombreArchivo = emt.getAttributeValue("name").substring(0, emt.getAttributeValue("name").indexOf('.')); } if (emt.getAttribute("type", xmi).getValue().compareTo("code:LanguageUnit") == 0) { List<Element> hijos = emt.getChildren(); for (Element hijo : hijos) { tipos.put(hijo.getAttributeValue("id", xmi), hijo.getAttributeValue("name")); } } } FileOutputStream fout; fout = new FileOutputStream(ruta + "VBParser\\src\\cager\\parser\\test\\" + nombreArchivo + ".java"); //fout = new FileOutputStream("E:\\WorkspaceParser\\VBParser\\src\\cager\\parser\\test\\"+nombreArchivo+".java"); // get the content in bytes byte[] contentInBytes = null; contentInBytes = ("package cager.parser.test;\n\n").getBytes(); fout.write(contentInBytes); fout.flush(); contentInBytes = ("public class " + nombreArchivo + "{\n\n").getBytes(); fout.write(contentInBytes); fout.flush(); for (Element emt : elements) { // System.out.println("XPath has result: " + emt.getName()+" "+emt.getAttribute("type",xmi)); if (emt.getAttribute("type", xmi).getValue().compareTo("code:MethodUnit") == 0) { nombreMetodo = emt.getAttribute("name").getValue(); if (emt.getAttribute("export") != null) exportValue = emt.getAttribute("export").getValue(); atributos = new ArrayList<String>(); List<Element> hijos = emt.getChildren(); for (Element hijo : hijos) { if (hijo.getAttribute("type", xmi) != null) { if (hijo.getAttribute("type", xmi).getValue().compareTo("code:Signature") == 0) { List<Element> parametros = hijo.getChildren(); for (Element parametro : parametros) { if (parametro.getAttribute("kind") == null || parametro.getAttribute("kind").getValue().compareTo("return") != 0) { atributos.add(tipos.get(parametro.getAttribute("type").getValue()) + " " + parametro.getAttributeValue("name")); } else { tipoRetorno = tipos.get(parametro.getAttribute("type").getValue()); } } } } else if (hijo.getAttribute("snippet") != null) { codigoFuente = hijo.getAttribute("snippet").getValue(); } } //System.out.println("MethodUnit!! " + emt.getName()+" "+emt.getAttribute("type",xmi)+" "+emt.getAttribute("name").getValue()); //System.out.println(emt.getAttribute("name").getValue()); if (tipoRetorno.compareTo("Void") == 0) { tipoRetorno = "void"; } contentInBytes = ("\t" + exportValue + " " + tipoRetorno + " " + nombreMetodo + " (") .getBytes(); fout.write(contentInBytes); fout.flush(); int n = 0; for (String parametro : atributos) { if (atributos.size() > 0 && n < atributos.size() - 1) { contentInBytes = (" " + parametro + ",").getBytes(); } else { contentInBytes = (" " + parametro).getBytes(); } fout.write(contentInBytes); fout.flush(); n++; } contentInBytes = (" ) {\n").getBytes(); fout.write(contentInBytes); fout.flush(); contentInBytes = ("\n/* \n " + codigoFuente + " \n */\n").getBytes(); fout.write(contentInBytes); fout.flush(); contentInBytes = ("\t}\n\n").getBytes(); fout.write(contentInBytes); fout.flush(); System.out.print("\t" + exportValue + " " + tipoRetorno + " " + nombreMetodo + " ("); n = 0; for (String parametro : atributos) { if (atributos.size() > 0 && n < atributos.size() - 1) { System.out.print(" " + parametro + ", "); } else { System.out.print(" " + parametro); } n++; } System.out.println(" ) {"); System.out.println("/* \n " + codigoFuente + " \n */"); System.out.println("\t}\n"); } } contentInBytes = ("}\n").getBytes(); fout.write(contentInBytes); fout.flush(); fout.close(); XPathExpression<Attribute> xp = XPathFactory.instance().compile("//@*", Filters.attribute(xmi)); for (Attribute a : xp.evaluate(doc)) { a.setName(a.getName().toLowerCase()); } xpath = XPathFactory.instance().compile("//codeElement/@name='testvb.cls'", Filters.element()); Element emt = xpath.evaluateFirst(doc); if (emt != null) { System.out.println("XPath has result: " + emt.getName()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:ch.rotscher.maven.plugins.InstallWithVersionOverrideMojo.java
License:Apache License
private Element findVersionElement(Document doc) { for (Element element : doc.getRootElement().getChildren()) { if (element.getName().equals("version")) { return element; }/*from ww w.j a va 2 s.c o m*/ } for (Element element : doc.getRootElement().getChildren()) { if (element.getName().equals("parent")) { for (Element childElem : element.getChildren()) { if (childElem.getName().equals("version")) { return childElem; } } } } return null; }
From source file:ch.unifr.diuf.diva.did.commands.AbstractGradientManipulation.java
License:Open Source License
@Override public float execute(Element task) throws IOException, JDOMException, InterruptedException { // Read the noise level float boost = 1; String imgName = script.preprocess(task.getAttributeValue("ref")); if (imgName == null) { throw new IllegalArgumentException("\n" + commandName + ": requires a ref"); }// ww w .j a v a 2 s . c om if (!script.getImages().containsKey(imgName)) { throw new IllegalArgumentException("\n" + commandName + ": cannot find image " + imgName); } Image image = script.getImages().get(imgName); // Read additional parameters int nbSteps = 500; float density = 1; final int SINGLE_CORE = 0; final int MULTI_CORES = 1; final int GPU = 2; int algoType = MULTI_CORES; int currentGPU = 0; for (Element child : task.getChildren()) { if (child.getName().equals("iterations")) { nbSteps = Integer.parseInt(script.preprocess(child.getText())); continue; } if (child.getName().equals("multi-core")) { algoType = MULTI_CORES; continue; } if (child.getName().equals("single-core")) { algoType = SINGLE_CORE; continue; } if (child.getName().equalsIgnoreCase("gpu")) { algoType = GPU; if (child.getText() != null && !child.getText().equals("")) { currentGPU = Integer.parseInt(child.getText()); } continue; } } GradientMap[] grad = { new GradientMap(image, 0), new GradientMap(image, 1), new GradientMap(image, 2) }; modifyGradient(task, grad, image); System.out.println("Starting reconstruction"); for (int lvl = 0; lvl < 3; lvl++) { switch (algoType) { case SINGLE_CORE: grad[lvl].CPUReconstruct(nbSteps); break; case MULTI_CORES: grad[lvl].MultiCPUReconstruct(nbSteps); break; case GPU: grad[lvl].GPUReconstruct(currentGPU, nbSteps); break; } } for (int lvl = 0; lvl < 3; lvl++) { grad[lvl].pasteValues(image, lvl); grad[lvl] = null; System.gc(); Thread.yield(); } return 0; }
From source file:ch.unifr.diuf.diva.did.commands.Calc.java
License:Open Source License
@Override public float execute(Element task) throws IOException, JDOMException, InterruptedException { float val = getAttributeFloat(task, "start"); for (Element child : task.getChildren()) { if (child.getName().equals("add")) { val += getFloat(child); continue; }/* ww w . ja v a2 s .c o m*/ if (child.getName().equals("sub")) { val -= getFloat(child); continue; } if (child.getName().equals("multiply-by")) { val *= getFloat(child); continue; } if (child.getName().equals("divide-by")) { val /= getFloat(child); continue; } if (child.getName().equals("sqrt")) { val = (float) Math.sqrt(val); continue; } if (child.getName().equals("log")) { float base = (float) Math.exp(1); if (child.getAttributeValue("base") != null) { base = Float.parseFloat(child.getAttributeValue("base")); } val = (float) (Math.log(val) / Math.log(base)); continue; } if (child.getName().equals("exp")) { val = (float) Math.exp(val); continue; } if (child.getName().equals("pow")) { float pow = getFloat(child); val = (float) Math.pow(val, pow); continue; } } return val; }
From source file:ch.unifr.diuf.diva.did.commands.ImageCreator.java
License:Open Source License
@Override public float execute(Element task) throws IOException, JDOMException { String id = getAttribute(task, "id"); if (id == null) { throw new IllegalArgumentException("\n" + commandName + ": requires an id"); }/* w w w . j a v a2 s.c om*/ boolean alreadyInitialized = false; Image img = null; for (Element param : task.getChildren()) { if (param.getName().equals("load")) { if (alreadyInitialized) { throw new IllegalArgumentException("\n" + commandName + ": cannot be created twice"); } String fname = param.getAttributeValue("file"); img = loadImage(fname); alreadyInitialized = true; continue; } if (param.getName().equals("copy")) { if (alreadyInitialized) { throw new IllegalArgumentException("\n" + commandName + ", <image> cannot be created twice"); } String otherName = param.getAttributeValue("ref"); img = copyImage(otherName); alreadyInitialized = true; continue; } if (param.getName().equals("diff")) { if (alreadyInitialized) { throw new IllegalArgumentException("\n" + commandName + ", <image> cannot be created twice"); } String a = getChildString(param, "a"); String b = getChildString(param, "b"); Image ia = script.getImages().get(a); Image ib = script.getImages().get(b); img = new Image(ia.getWidth(), ia.getHeight()); for (int x = 0; x < ia.getWidth(); x++) { for (int y = 0; y < ia.getHeight(); y++) { img.set(0, x, y, ia.get(0, x, y) - ib.get(0, x, y)); img.set(1, x, y, ia.get(1, x, y) - ib.get(1, x, y)); img.set(2, x, y, ia.get(2, x, y) - ib.get(2, x, y)); } } alreadyInitialized = true; continue; } } script.getImages().put(id, img); return 0; }
From source file:ch.unifr.diuf.diva.did.Script.java
License:Open Source License
public void run() throws IOException, InterruptedException, JDOMException { for (Element task : root.getChildren()) { String name = task.getName(); if (!commands.containsKey(name)) { throw new IllegalArgumentException("Tag <" + name + "> not understood"); }//from w ww .j a v a 2 s . c o m output = commands.get(name).execute(task); } }