List of usage examples for org.jdom2.filter Filters attribute
public static final Filter<Attribute> attribute(Namespace ns)
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 a va 2 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(); } }