Example usage for org.jdom2 Document Document

List of usage examples for org.jdom2 Document Document

Introduction

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

Prototype

public Document(List<? extends Content> content) 

Source Link

Document

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance.

Usage

From source file:ca.nrc.cadc.uws.JobListWriter.java

License:Open Source License

/**
 * Write to root Element to a writer.//from   ww  w  . j  av a 2s  .  com
 *
 * @param root Root Element to write.
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
protected void writeDocument(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    outputter.output(document, writer);
}

From source file:ca.nrc.cadc.uws.JobListWriter.java

License:Open Source License

/**
 * Write the job list to our streaming xml writer.
 *
 * @param jobs/*ww  w .  j  av  a2s . co  m*/
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
public void write(Iterator<JobRef> jobs, Writer writer) throws IOException {
    Element root = getRootElement(jobs);
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    outputter.output(document, writer);
}

From source file:ca.nrc.cadc.uws.JobReader.java

License:Open Source License

private JobInfo parseJobInfo(Document doc) {
    JobInfo rtn = null;/*from  www  .j  a v a 2 s.  c  o  m*/
    Element root = doc.getRootElement();
    Element e = root.getChild(JobAttribute.JOB_INFO.getAttributeName(), UWS.NS);
    if (e != null) {
        log.debug("found jobInfo element");
        String content = e.getText();
        List children = e.getChildren();
        if (content != null)
            content = content.trim();
        if (content.length() > 0) // it was text content
        {
            rtn = new JobInfo(content, null, null);
        } else if (children != null) {
            if (children.size() == 1) {
                try {
                    Element ce = (Element) children.get(0);
                    Document jiDoc = new Document((Element) ce.detach());
                    XMLOutputter outputter = new XMLOutputter();
                    StringWriter sw = new StringWriter();
                    outputter.output(jiDoc, sw);
                    sw.close();
                    rtn = new JobInfo(sw.toString(), null, null);

                } catch (IOException ex) {
                    throw new RuntimeException("BUG while writing element to string", ex);
                }
            }
        }
    }
    log.debug("parseJobInfo: " + rtn);
    return rtn;
}

From source file:ca.nrc.cadc.uws.JobWriter.java

License:Open Source License

/**
 * Write the job to a writer.//from  w w w  .  ja v a  2 s  .  com
 *
 * @param job
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
public void write(Job job, Writer writer) throws IOException {
    Element root = getRootElement(job);
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    outputter.output(document, writer);
}

From source file:ca.nrc.cadc.vos.JsonNodeWriter.java

License:Open Source License

@Override
protected void write(Element root, Writer writer) throws IOException {
    JsonOutputter outputter = new JsonOutputter();
    outputter.getListElementNames().add("nodes");
    outputter.getListElementNames().add("properties");
    outputter.getListElementNames().add("accepts");
    outputter.getListElementNames().add("provides");

    // WebRT 72612
    // Treat all property values as Strings.
    // jenkinsd 2016.01.20
    outputter.getStringElementNames().add("property");

    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    outputter.output(document, writer);//  www . j a  v a  2  s.c o m
}

From source file:ca.nrc.cadc.vos.TransferWriter.java

License:Open Source License

/**
 * Write to root Element to a writer./*from w  ww  .  j ava  2s . co m*/
 *
 * @param root Root Element to write.
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
@SuppressWarnings("unchecked")
protected void write(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    outputter.output(document, writer);
}

From source file:catalogo.XMLOut.java

public void criaDocument(List<String[]> lista, String[] categorias) {

    Element artigos = new Element("listagem");
    Document doc = new Document(artigos);

    //iterao sobre a lista com os artigos
    for (int i = 0; i < lista.size(); i++) {
        Element artigo1 = new Element("artigo");
        artigo1.setAttribute("id", Integer.toString(i));
        //iterao sobre os valores do artigo
        for (int j = 0; j < lista.get(i).length; j++) {
            if (categorias[j].equals("ano"))
                artigo1.addContent(new Element("ano").setText(lista.get(i)[j]));
            else/* w  w w  .  j  av  a 2  s  .  c  o m*/
                artigo1.addContent(new Element(categorias[j]).setText(lista.get(i)[j]));
        }
        doc.getRootElement().addContent(artigo1);
    }

    criaXML(doc);

}

From source file:codigoFonte.Sistema.java

public boolean addUser(User u) throws IOException {

    boolean success = false, matriculaExists = false;
    File file = new File("Sistema.xml");
    Document newDocument = null;//from   w  w w  .  jav  a 2s  .co  m
    Element root = null;
    Attribute matricula = null, nome = null, tipo = null, senha = null;
    Element user = null;

    if (u.getTipo().isEmpty() || u.getTipo().isEmpty() || u.getPassword().isEmpty()) {
        return success;
    }

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    if (root.getChildren().size() > 0) {
        List<Element> listUsers = root.getChildren();
        for (Element a : listUsers) {
            if (a.getAttributeValue("matrcula").equals(u.getMatricula())) {
                matriculaExists = true;
            }
        }
    }

    if (!matriculaExists) {
        user = new Element("user");

        matricula = new Attribute("matrcula", this.newId());
        tipo = new Attribute("tipo", u.getTipo());
        nome = new Attribute("nome", u.getNome());
        senha = new Attribute("senha", u.getPassword());

        user.setAttribute(matricula);
        user.setAttribute(nome);
        user.setAttribute(tipo);
        user.setAttribute(senha);
        //user.setAttribute(divida);

        root.addContent(user);

        success = true;
    }
    //        

    XMLOutputter out = new XMLOutputter();

    try {
        FileWriter arquivo = new FileWriter(file);
        out.output(newDocument, arquivo);
    } catch (IOException ex) {
        Logger.getLogger(Sistema.class.getName()).log(Level.SEVERE, null, ex);
    }

    return success;
}

From source file:codigoFonte.Sistema.java

public ArrayList<User> listarUser() {
    File file = new File("Sistema.xml");
    Document newDocument = null;/*w w  w . java 2  s.  c  o m*/
    Element root = null;
    ArrayList<User> users = new ArrayList<User>();
    ;
    ArrayList<Livro> livros = new ArrayList<Livro>();

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    List<Element> listusers = root.getChildren();
    for (Element e : listusers) {
        User user = new User(null, null, null, null);
        List<Element> listlivro = e.getChildren("livro");
        List<Element> historico = e.getChildren("histrico");

        if (!listlivro.isEmpty())

            for (Element l : listlivro) {
                Livro livro = new Livro(null, null, null, 0);
                livro.setAutor(l.getAttributeValue("autor"));
                livro.setEditora(l.getAttributeValue("editora"));
                livro.setTitulo(l.getAttributeValue("ttulo"));
                livros.add(livro);
            }

        //List<Element> historico = e.getChildren("histrico");
        ArrayList<Livro> historicoObjeto = new ArrayList<Livro>();
        if (!historico.isEmpty()) {
            for (Element h : historico) {
                Livro livroHistorico = new Livro(null, null, null, 0);
                livroHistorico.setAutor(h.getAttributeValue("autor"));
                livroHistorico.setEditora(h.getAttributeValue("editora"));
                livroHistorico.setTitulo(h.getAttributeValue("ttulo"));
                historicoObjeto.add(livroHistorico);
            }
        }

        user.setMatricula(e.getAttributeValue("matrcula"));
        user.setNome(e.getAttributeValue("nome"));
        user.setTipo(e.getAttributeValue("tipo"));
        user.setLivros(livros);

        users.add(user);
    }
    return users;
}

From source file:codigoFonte.Sistema.java

public User pesquisarUser(String matricula) {
    File file = new File("Sistema.xml");
    Document newDocument = null;/*from  ww  w. ja  v a2  s.c  om*/
    Element root = null;
    User user = null;

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    List<Element> listusers = root.getChildren();
    for (Element e : listusers) {
        if (e.getAttributeValue("matrcula").equals(matricula)) {
            List<Element> listlivros = e.getChildren("livro");
            user = new User(null, null, null, null);
            if (!listlivros.isEmpty()) {
                for (Element l : listlivros) {
                    Livro livro = new Livro(null, null, null, 0);
                    livro.setAutor(l.getAttributeValue("autor"));
                    livro.setEditora(l.getAttributeValue("editora"));
                    livro.setTitulo(l.getAttributeValue("ttulo"));
                    livro.setEntrega(l.getAttributeValue("dataEntrega"));
                    livro.setAluguel(l.getAttributeValue("dataAluguel"));
                    livro.setId(l.getAttributeValue("id"));
                    user.getLivros().add(livro);
                }
            }
            List<Element> historico = e.getChildren("historico");
            if (!historico.isEmpty()) {
                for (Element h : historico) {
                    Livro livroHistorico = new Livro(null, null, null, 0);
                    livroHistorico.setAutor(h.getAttributeValue("autor"));
                    livroHistorico.setEditora(h.getAttributeValue("editora"));
                    livroHistorico.setTitulo(h.getAttributeValue("ttulo"));
                    livroHistorico.setEntrega(h.getAttributeValue("dataEntrega"));
                    livroHistorico.setAluguel(h.getAttributeValue("dataAluguel"));
                    livroHistorico.setId(h.getAttributeValue("id"));
                    user.getHistorico().add(livroHistorico);
                }
            }
            user.setMatricula(matricula);
            user.setTipo(e.getAttributeValue("tipo"));
            user.setNome(e.getAttributeValue("nome"));
            user.setPassword(e.getAttributeValue("senha"));

            return user;
        }
    }
    return user;
}