Example usage for org.jdom2.input SAXBuilder build

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

Introduction

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

Prototype

@Override
public Document build(final String systemId) throws JDOMException, IOException 

Source Link

Document

This builds a document from the supplied URI.

Usage

From source file:displayclient.DownloadManager.java

License:Open Source License

@Override
public void run() {
    int updateInterval = Integer.parseInt(DisplayClient.prop.getProperty("CollectInterval"));
    log.log(Level.INFO, "Run. Collection Interval = {0}", updateInterval);

    while (running) {
        // Update thread limits and updateInterval incase they changed on
        // instruction from the CMS
        this.numThreadsLimit = Integer.parseInt(DisplayClient.prop.getProperty("MaxConcurrentDownloads"));
        updateInterval = Integer.parseInt(DisplayClient.prop.getProperty("CollectInterval"));

        String response = "";
        try {//from w  w w.ja v a 2 s. c o m
            response = this.DC.XMDS.RequiredFiles(DisplayClient.prop.getProperty("ServerKey"),
                    DisplayClient.prop.getProperty("HardwareKey"));
            log.log(Level.FINE, "DownloadManager: xmds.requiredFiles => {0}", response);
        } catch (Exception ex) {
            log.log(Level.FINEST, "DownloadManager: xmds.requiredFiles => {0}", response);
            log.log(Level.WARNING, "DownloadManager: Could not connect to XMDS at {0}",
                    DisplayClient.prop.getProperty("ServerUri"));
            log.log(Level.WARNING, "DownloadManager: xmds.requiredFiles => {0}", ex);
            continue;
        }

        Document doc;
        Iterator iter;

        if (!response.equals("")) {
            // Code here to parse the returned XML
            SAXBuilder sb = new SAXBuilder();

            try {
                doc = sb.build(new StringReader(response));

                // Build an iterator over the files section of the response
                XPathFactory xFactory = XPathFactory.instance();

                XPathExpression<Element> expr = xFactory.compile("//file", Filters.element());
                List<Element> files = expr.evaluate(doc);
                for (Element n : files) {

                    FileFetcher f = null;

                    if (n.getAttributeValue("download").equalsIgnoreCase("http")) {
                        f = new HttpFileFetcher(this, n);
                    } else {
                        f = new XmdsFileFetcher(this, n);
                    }

                    Boolean skip = false;

                    // Check to see if there is already a download
                    // running for this file ID. If there is, skip it.
                    for (FileFetcher tmp : runningDownloads) {
                        try {
                            if (f.getID() == tmp.getID()) {
                                skip = true;
                                log.log(Level.FINEST,
                                        "DownloadManager: FileFetcher {0} is still running from previous collection. Skipping.",
                                        f.getID());
                            }
                        } catch (NullPointerException e) {
                            // The download completed while we were testing for it.
                            // Ignore it.
                            skip = true;
                        }
                    }

                    if (!skip) {
                        f.start();
                        this.runningDownloads.add(f);
                        this.numThreads++;
                    }

                    while (this.numThreads >= (this.numThreadsLimit - 1)) {
                        // Thread throttling
                        // Sleep until there is a spare space for a new thread.
                        try {
                            log.log(Level.FINE, "DownloadManager: {0} downloads in progress. Sleeping.",
                                    this.numThreadsLimit);
                            sleep(5000);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            } catch (JDOMException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Make sure updateInterval is sane.
        if (updateInterval < 30) {
            updateInterval = 30;
        }

        // Sleep for "updateInterval" seconds before trying again.
        try {
            sleep(updateInterval * 1000);
        } catch (InterruptedException e) {
        }
    }
}

From source file:diuf.diva.dia.ms.script.XMLScript.java

License:Open Source License

/**
 * Constructs an XML script./*from ww w  .j ava 2  s.  co m*/
 * @param fname file name from which to read the XML file
 * @throws IOException if the file cannot be read
 * @throws JDOMException if the XML is not valid
 */
public XMLScript(String fname) throws IOException, JDOMException {
    SAXBuilder builder = new SAXBuilder();
    Document xml = builder.build(new File(fname));
    root = xml.getRootElement();
    readColorspace();
    prepareCommands();

    // Select the appropriate rows for your launch. Please comment/De-coment the logging too

    //System.out.println("\n\n[WARNING] The network randomness is being seeded in XMLScript\n\n");
    // random = new Random(123456789l);

    random = new Random();
}

From source file:Domain.DataAccess.DatabaseHandler.java

public boolean bookAvailable(String pBookId) {

    boolean result = false;

    org.jdom2.Element book;/*w  w  w  .j  av  a  2  s .  c  o  m*/

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Books.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {

            book = (org.jdom2.Element) children.get(i);

            if (book.getChildText("bookId").equals(pBookId)) {

                String childText = book.getChildText("available");

                if (childText.equals("1")) {
                    result = true;
                }
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Books.xml")));

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    System.out.println(result);
    return result;

}

From source file:Domain.DataAccess.DatabaseHandler.java

public void disableBook(String pBookId) {

    org.jdom2.Element book;/*  w  w w  .j  av a  2s .c o m*/

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Books.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {

            book = (org.jdom2.Element) children.get(i);

            if (book.getChildText("bookId").equals(pBookId)) {

                book.getChild("available").setText("0");
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Books.xml")));

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Domain.DataAccess.DatabaseHandler.java

public void enableBook(String pBookId) {

    org.jdom2.Element book;/*  w ww  . ja  v a 2  s  . c om*/

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Books.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {

            book = (org.jdom2.Element) children.get(i);

            if (book.getChildText("bookId").equals(pBookId)) {

                book.getChild("available").setText("1");
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Books.xml")));

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Domain.DataAccess.DatabaseHandler.java

public boolean insertBookLoan(String pBookId, String pStudentId) {

    boolean result = false;

    try {/*from  www.j  a  v  a 2  s .c  om*/

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document document = documentBuilder.parse("./src/Loans.xml");
        Element root = document.getDocumentElement();

        Element newServer = document.createElement("loan");
        root.appendChild(newServer);

        Element name = document.createElement("studentId");
        name.appendChild(document.createTextNode(pStudentId));
        newServer.appendChild(name);

        Element port = document.createElement("bookId");
        port.appendChild(document.createTextNode(pBookId));
        newServer.appendChild(port);

        root.appendChild(newServer);

        DOMSource source = new DOMSource(document);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult res = new StreamResult("./src/Loans.xml");
        transformer.transform(source, res);

        SAXBuilder builder = new SAXBuilder();
        org.jdom2.Document readDoc = null;
        readDoc = builder.build(new File("./src/Loans.xml"));

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());

        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Loans.xml")));

        disableBook(pBookId);

        result = true;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Domain.DataAccess.DatabaseHandler.java

public boolean removeBookLoan(String pBookId) {

    boolean result = false;

    org.jdom2.Element loan;//from  w w w .  ja v a2s  . c o m

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Loans.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {
            loan = (org.jdom2.Element) children.get(i);

            if (loan.getChildText("bookId").equals(pBookId)) {

                System.out.println("bookId: " + loan.getChildText("bookId") + "--- param: " + pBookId);
                children.remove(i);
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Loans.xml")));

        enableBook(pBookId);

        result = true;

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return result;
}

From source file:Domain.Model.Student.java

public boolean studentExists(String studentId) {

    boolean result = false;
    SAXBuilder builder = new SAXBuilder();
    File xmlFile = new File("./src/Students.xml");

    try {/*w w w  .  jav  a 2 s  . c  o m*/

        Document document = (Document) builder.build(xmlFile);
        Element rootNode = document.getRootElement();
        List list = rootNode.getChildren("student");

        for (int i = 0; i < list.size(); i++) {

            Element node = (Element) list.get(i);

            String id = node.getChildText("studentId");

            if (id.equals(studentId)) {
                result = true;
                break;
            }

        }

    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    System.out.println(result);
    return result;
}

From source file:edd.practica1.Principal.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (seleccionado.showDialog(null, "Abrir Archivo") == JFileChooser.APPROVE_OPTION) {
            archivos = seleccionado.getSelectedFile();

            SAXBuilder builder = new SAXBuilder();

            try {
                Document document = (Document) builder.build(archivos);
                Element rootNode = document.getRootElement();

                Element Raiz = document.getRootElement();

                List listpri = Raiz.getChildren("dimension");

                for (int i = 0; i < listpri.size(); i++) {
                    Element camp = (Element) Raiz.getChildren("dimension").get(i);
                    String Dimensiones = camp.getTextTrim();
                    Dimension = Integer.parseInt(Dimensiones);
                    System.out.println("Dimension :" + Dimensiones);

                }//from  w w w .j av  a 2 s.c  o m

                List list = rootNode.getChildren("dobles");

                for (int i = 0; i < list.size(); i++) {
                    Element scrabble = (Element) list.get(i);
                    List lista_dobles = scrabble.getChildren();
                    System.out.println("\tdobles");
                    for (int j = 0; j < lista_dobles.size(); j++) {
                        Element dobles = (Element) lista_dobles.get(j);
                        String x = dobles.getChildTextTrim("x");
                        String y = dobles.getChildTextTrim("y");
                        System.out.println("\t" + x + "\t\t" + y + "\t");
                    }
                }

                List listtri = rootNode.getChildren("triples");

                for (int i = 0; i < listtri.size(); i++) {
                    Element scrabble = (Element) listtri.get(i);
                    List lista_triples = scrabble.getChildren();
                    System.out.println("\ttriples");
                    for (int j = 0; j < lista_triples.size(); j++) {
                        Element triples = (Element) lista_triples.get(j);
                        String x = triples.getChildTextTrim("x");
                        String y = triples.getChildTextTrim("y");
                        System.out.println("\t" + x + "\t\t" + y + "\t");
                    }
                }

                for (int i = 0; i < rootNode.getChildren("diccionario").size(); i++) {
                    Element listd = (Element) rootNode.getChildren("diccionario").get(i);
                    for (int j = 0; j < listd.getChildren().size(); j++) {
                        Element diccionario = (Element) listd.getChildren("palabra").get(j);
                        String palabra = (diccionario.getTextTrim());
                        System.out.println("\t" + palabra + "\t");
                        ListasSimplesDiccionario.IngresarNodo(palabra);
                        //ListaSimpleFichasJugador.VerLista();
                    }
                }
                GenerarFichasTotales();

            } catch (IOException | JDOMException io) {
                System.out.println(io.getMessage());
            }

        }

    }

From source file:edd.practica1_201404218.LeerXML.java

public void cargarXML(String ruta) {

    SAXBuilder builder = new SAXBuilder();
    File archivo = new File(ruta);
    try {//from   ww w .ja v a2 s  . co m

        Document documento = (Document) builder.build(archivo);
        Element raiz = documento.getRootElement();

        for (int index = 0; index < raiz.getChildren("dimension").size(); index++) {
            Element campo = raiz.getChildren("dimension").get(index);
            String dimension = campo.getTextTrim();
            System.out.println(dimension);
        }

        for (int index = 0; index < raiz.getChildren("dobles").size(); index++) {
            Element doble = (Element) raiz.getChildren("dobles").get(index);

            for (int index2 = 0; index2 < doble.getChildren().size(); index2++) {
                Element casilla = (Element) doble.getChildren("casilla").get(index2);

                Posicion pos = new Posicion();
                pos.setTipo("doble");
                pos.setX(Integer.parseInt(casilla.getChildren("x").get(0).getTextTrim()));
                pos.setY(Integer.parseInt(casilla.getChildren("y").get(0).getTextTrim()));

                listaPosiciones.add(pos);

            }
        }

        for (int index = 0; index < raiz.getChildren("triples").size(); index++) {
            Element triple = (Element) raiz.getChildren("triples").get(index);

            for (int index2 = 0; index2 < triple.getChildren().size(); index2++) {
                Element casilla = (Element) triple.getChildren("casilla").get(index2);

                Posicion pos = new Posicion();
                pos.setTipo("triple");
                pos.setX(Integer.parseInt(casilla.getChildren("x").get(0).getTextTrim()));
                pos.setY(Integer.parseInt(casilla.getChildren("y").get(0).getTextTrim()));

                listaPosiciones.add(pos);

            }
        }

        for (int index = 0; index < raiz.getChildren("diccionario").size(); index++) {
            Element diccionario = (Element) raiz.getChildren("diccionario").get(index);

            for (int index2 = 0; index2 < diccionario.getChildren().size(); index2++) {
                Element palabra = (Element) diccionario.getChildren("palabra").get(index2);

                String valor = palabra.getTextTrim();
                listaPalabras.add(valor);
            }
        }

    } catch (Exception e) {

    }

}