Example usage for org.jdom2.input SAXBuilder SAXBuilder

List of usage examples for org.jdom2.input SAXBuilder SAXBuilder

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder SAXBuilder.

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:app.simulation.Importer.java

License:MIT License

public void importXML() throws Exception {
    File file = new File(path);

    if (!file.exists())
        throw new FileNotFoundException(path);

    if (!getFileFormat().equalsIgnoreCase("xml"))
        throw new InvalidFileFormatException(getFileFormat());

    SAXBuilder saxBuilder = new SAXBuilder();
    Document document = saxBuilder.build(file);
    Element root = document.getRootElement();
    Element configuration = root.getChild("configuration");

    if (configuration != null) {

        String delayText = configuration.getChildText("delay");
        if (delayText == null)
            throw new AttributeNotFoundException("delay");

        String iterationsText = configuration.getChildText("iterations");
        if (iterationsText == null)
            throw new AttributeNotFoundException("iterations");

        String agentsText = configuration.getChildText("agents");
        if (agentsText == null)
            throw new AttributeNotFoundException("agents");

        String latticeSizeText = configuration.getChildText("latticeSize");
        if (latticeSizeText == null)
            throw new AttributeNotFoundException("latticeSize");

        String descriptionText = configuration.getChildText("description");
        if (descriptionText == null)
            throw new AttributeNotFoundException("description");

        int delay = Integer.parseInt(delayText);
        int iterations = Integer.parseInt(iterationsText);
        int agents = Integer.parseInt(agentsText);
        int latticeSize = Integer.parseInt(latticeSizeText);
        this.configuration = new Configuration(delay, iterations, agents, latticeSize, descriptionText);
    }//w ww .  jav a 2 s  .c o  m

    Element initialCell = root.getChild("initialCell");

    if (initialCell != null) {

        String xText = initialCell.getChildText("x");
        if (xText == null)
            throw new AttributeNotFoundException("x");

        String yText = initialCell.getChildText("y");
        if (yText == null)
            throw new AttributeNotFoundException("y");

        String zText = initialCell.getChildText("z");
        if (zText == null)
            throw new AttributeNotFoundException("z");

        String stateText = initialCell.getChildText("state");
        if (stateText == null)
            throw new AttributeNotFoundException("state");

        Element colorElement = initialCell.getChild("color");
        if (colorElement == null)
            throw new AttributeNotFoundException("color");

        String rText = colorElement.getChildText("r");
        if (rText == null)
            throw new AttributeNotFoundException("r");

        String gText = colorElement.getChildText("g");
        if (gText == null)
            throw new AttributeNotFoundException("g");

        String bText = colorElement.getChildText("b");
        if (bText == null)
            throw new AttributeNotFoundException("b");

        int x = Integer.parseInt(xText);
        int y = Integer.parseInt(yText);
        int z = Integer.parseInt(zText);
        int state = Integer.parseInt(stateText);
        Color color = new Color(Integer.parseInt(rText), Integer.parseInt(gText), Integer.parseInt(bText));
        this.initialCell = new Cell(x, y, z, state, color);
    }

    Element tagRules = root.getChild("rules");
    rules = new ArrayList<Rule>();

    if (tagRules != null) {
        List<Element> elementRules = tagRules.getChildren("rule");

        if (elementRules.size() > 0) {

            for (Element rule : elementRules) {

                String neighbourhood = rule.getChildText("neighbourhood");
                if (neighbourhood == null)
                    throw new AttributeNotFoundException("neighbourhood");

                String stateText = rule.getChildText("state");
                if (stateText == null)
                    throw new AttributeNotFoundException("state");

                Element colorElement = rule.getChild("color");
                if (colorElement == null)
                    throw new AttributeNotFoundException("color");

                String rText = colorElement.getChildText("r");
                if (rText == null)
                    throw new AttributeNotFoundException("r");

                String gText = colorElement.getChildText("g");
                if (gText == null)
                    throw new AttributeNotFoundException("g");

                String bText = colorElement.getChildText("b");
                if (bText == null)
                    throw new AttributeNotFoundException("b");

                int state = Integer.parseInt(stateText);
                Color color = new Color(Integer.parseInt(rText), Integer.parseInt(gText),
                        Integer.parseInt(bText));

                rules.add(new Rule(neighbourhood, state, color));
            }
        }
    }

}

From source file:appmain.AppMain.java

public AppMain() {

    fields = new ArrayList<>();

    jdomBuilder = new SAXBuilder();

}

From source file:arquivo.Arquivo.java

public List busca(String nome, boolean completa, String nomeE) {
    SAXBuilder builder = new SAXBuilder();
    List<Element> retorna = new LinkedList<>();
    try {/*from w  w w . j a  va2  s  .c om*/
        Document doc = builder.build(arquivo);
        Element root = (Element) doc.getRootElement();

        retorna = this.buscaInterna(root, completa, nome, nomeE);
    } catch (Exception e) {

    }
    return retorna;
}

From source file:arquivo.Arquivo.java

public String remove(String nome, String nomeE) {
    SAXBuilder builder = new SAXBuilder();
    List<Element> retorna = new LinkedList<>();

    try {/*  www  . j a  v a2 s.  c o m*/
        Document doc = builder.build(arquivo);
        Element root = (Element) doc.getRootElement();

        List<Element> removido = buscaInterna(root, true, nome, nomeE);
        root.removeContent(removido.get(0));

        XMLOutputter xout = new XMLOutputter();
        OutputStream out = new FileOutputStream(arquivo);
        xout.output(doc, out);
        return "Documento alterado com sucesso!";
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "n foi possivel remover " + nome;
}

From source file:arquivo.ArquivoF.java

public String add(String confSenha, String loing, String senha, String nome) {
    if (confSenha.equals(senha) && !loing.equals("") && !nome.equals("") && !senha.equals("")) {
        if (0 != busca(nome, true, "funcinario").size() && buscaLong(loing).size() != 0)
            return "erro nome ja cadastrado";
        SAXBuilder builder = new SAXBuilder();
        try {//from  w  ww .j av a 2s .  c  o  m
            Document doc = builder.build(arquivo);
            Element root = (Element) doc.getRootElement();

            Element funcionario = new Element("funcinario");

            Attribute nomeE = new Attribute("nome", nome);
            funcionario.setAttribute(nomeE);

            Element loingE = new Element("loing");
            loingE.setText(loing);
            funcionario.addContent(loingE);

            Element senhaE = new Element("senha");
            senhaE.setText(senha);
            funcionario.addContent(senhaE);

            root.addContent(funcionario);

            XMLOutputter xout = new XMLOutputter();
            OutputStream out = new FileOutputStream(arquivo);
            xout.output(doc, out);
            System.out.println("Documento alterado com sucesso!");

            return "cadastrado com suceso";
        } catch (Exception e) {

        }
    } else
        return "preemcha os compos corretamente";
    return "erro cadastral";
}

From source file:arquivo.ArquivoF.java

public String edita(String confSenha, String loing, String senha, String nome) {
    if (confSenha.equals(senha) && !loing.equals("") && !senha.equals("")) {
        if (0 == busca(nome, true, "funcinario").size() && buscaLong(loing).size() != 0)
            return "erro nome nao cadastrado";
        SAXBuilder builder = new SAXBuilder();
        try {//from   ww  w  . j  ava  2s.  c  om
            Document doc = builder.build(arquivo);
            Element root = (Element) doc.getRootElement();

            List<Element> funcinario = super.buscaInterna(root, true, nome, "funcinario");
            if (!loing.equals(""))
                funcinario.get(0).getChild("loing").setText(loing);
            if (!senha.equals(""))
                funcinario.get(0).getChild("senha").setText(senha);

            XMLOutputter xout = new XMLOutputter();
            OutputStream out = new FileOutputStream(arquivo);
            xout.output(doc, out);
            System.out.println("Documento alterado com sucesso!");

            return "cadastrado com suceso";
        } catch (Exception e) {

        }
    } else
        return "preemcha os compos corretamente";
    return "erro cadastral";
}

From source file:arquivo.ArquivoF.java

public List buscaLong(String loing) {

    List<Element> loings = new LinkedList<>();

    SAXBuilder builder = new SAXBuilder();
    try {//from  ww  w.j  av  a2  s  . co  m
        Document doc = builder.build(arquivo);
        Element root = (Element) doc.getRootElement();

        List<Element> funcinario = super.buscaInterna(root, false, "", "funcinario");
        for (int i = 0; i < funcinario.size(); i++) {
            if (funcinario.get(i).getChildText("loing").equals(loing))
                loings.add(funcinario.get(i));
        }
    } catch (Exception e) {

    }
    return loings;
}

From source file:arquivo.ArquivoFilme.java

public String add(String nome, String faixa, String tipo) {
    if (0 != busca(nome, true, "filme").size())
        return "erro nome ja cadastrado";
    SAXBuilder builder = new SAXBuilder();
    try {/* ww w  .java  2s.c  o  m*/
        Document doc = builder.build(arquivo);
        Element root = (Element) doc.getRootElement();

        Element filmeE = new Element("filme");

        Attribute nomeE = new Attribute("nome", nome);
        filmeE.setAttribute(nomeE);

        Attribute tipoE = new Attribute("tipo", tipo);
        filmeE.setAttribute(tipoE);

        Attribute faixaE = new Attribute("faixa", faixa);
        filmeE.setAttribute(faixaE);

        root.addContent(filmeE);

        XMLOutputter xout = new XMLOutputter();
        OutputStream out = new FileOutputStream(arquivo);
        xout.output(doc, out);
        System.out.println("Documento alterado com sucesso!");

        return "cadastrado com suceso";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "erro cadastral";
}

From source file:arquivo.ArquivoFilme.java

public String edita(String nome, String novoNome, String faixa, String tipo) {
    String retorno = "erro";
    if (!nome.equals("")) {
        if (0 == busca(nome, true, "funcinario").size())
            return "erro nao nome ja cadastrado";
        SAXBuilder builder = new SAXBuilder();
        try {/*from  w  w w.  j av  a 2  s  .c  om*/
            Document doc = builder.build(arquivo);
            Element root = (Element) doc.getRootElement();

            List<Element> filme = super.buscaInterna(root, true, nome, "filme");
            if (!novoNome.equals(""))
                filme.get(0).setAttribute("nome", novoNome);
            if (!faixa.equals(""))
                filme.get(0).setAttribute("faixa", faixa);
            if (!tipo.equals(""))
                filme.get(0).setAttribute("tipo", tipo);

            XMLOutputter xout = new XMLOutputter();
            OutputStream out = new FileOutputStream(arquivo);
            xout.output(doc, out);
            System.out.println("Documento alterado com sucesso!");

            retorno = "cadastrado com suceso";
        } catch (Exception e) {

        }
    } else
        retorno = "preemcha os compos corretamente";
    return retorno;
}

From source file:arquivo.ArquivoFilme.java

public String addSesao(String nome, int numeroSala, int horaI, int mimI, int horaF, int mimF) {
    String retorno = "erro";
    if (!nome.equals("")) {
        if (this.confirnaSesao(numeroSala, horaI, mimI, horaF, mimF)) {
            SAXBuilder builder = new SAXBuilder();
            try {
                Document doc = builder.build(arquivo);
                Element root = (Element) doc.getRootElement();

                List<Element> filme = super.buscaInterna(root, true, nome, "filme");
                if (filme.size() == 0) {
                    return "erro filme n registrado";
                }//from  ww  w. ja  v a 2  s .co m
                List<Element> salas = filme.get(0).getChildren("sala");

                Element secao = new Element("secao");
                secao.setAttribute(new Attribute("horaI", "" + horaI));
                secao.setAttribute(new Attribute("mimI", "" + mimI));
                secao.setAttribute(new Attribute("horaF", "" + horaF));
                secao.setAttribute(new Attribute("mimF", "" + mimF));

                boolean confirna = false;
                for (int i = 0; i < salas.size(); i++) {
                    if (salas.get(i).getAttributeValue("numeroSala").equals("" + numeroSala)) {
                        confirna = true;
                        salas.get(i).addContent(secao);
                        break;
                    }
                }
                if (!confirna) {
                    Element sala = new Element("sala");
                    sala.addContent(secao);
                    filme.get(0).addContent(sala);
                }

                XMLOutputter xout = new XMLOutputter();
                OutputStream out = new FileOutputStream(arquivo);
                xout.output(doc, out);
                System.out.println("Documento alterado com sucesso!");

                retorno = "cadastrado com suceso";
            } catch (Exception e) {

            }
        } else
            retorno = "nesse horario a sala estara ocupada";
    }
    return retorno;
}