Example usage for org.jdom2 Attribute setName

List of usage examples for org.jdom2 Attribute setName

Introduction

In this page you can find the example usage for org.jdom2 Attribute setName.

Prototype

public Attribute setName(final String name) 

Source Link

Document

This sets the local name of the Attribute.

Usage

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 ww .  j  a va 2 s . c  om*/

        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:com.archimatetool.editor.model.compatibility.Archimate2To3Converter.java

License:Open Source License

/**
 * Convert "relationship" attribute name to "archimateRelationship"
 *///from w w  w .  j  a va2 s .  c om
private void convertRelationshipType(Element element) {
    Attribute attRelationship = element.getAttribute("relationship");
    if (attRelationship != null) {
        attRelationship.setName("archimateRelationship");
    }
}

From source file:org.artifactory.mime.version.converter.v1.XmlIndexedConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    Namespace namespace = rootElement.getNamespace();

    List mimetypes = rootElement.getChildren("mimetype", namespace);
    // make sure there are no more 'xml' attributes
    for (Object mimetype : mimetypes) {
        Element mimeTypeElement = (Element) mimetype;
        Attribute xmlAttribute = mimeTypeElement.getAttribute("xml", namespace);
        if (xmlAttribute != null) {
            // rename to index
            xmlAttribute.setName("index");
            // change to false unless maven of ivy
            String type = mimeTypeElement.getAttributeValue("type");
            if (!"application/x-maven-pom+xml".equals(type) && !"application/x-ivy+xml".equals(type)) {
                xmlAttribute.setValue("false");
            }// w w w . j  av a  2  s .c  o m
        }
    }
}