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:com.novell.ldapchai.impl.edir.value.DirXML_EntitlementRef.java

License:Open Source License

private static Document convertStrToDoc(final String str) {
    final Reader xmlreader = new StringReader(str);
    final SAXBuilder builder = new SAXBuilder();
    Document doc = null;/*w  w  w. j  a  v  a 2  s. c  o m*/
    try {
        doc = builder.build(xmlreader);
    } catch (JDOMException e) {
        e.printStackTrace(); //@todo
    } catch (IOException e) {
        e.printStackTrace(); //@todo
    }
    return doc;
}

From source file:com.novell.ldapchai.impl.edir.value.nspmComplexityRules.java

License:Open Source License

private static List<Policy> readComplexityPoliciesFromXML(final String input) {
    final List<Policy> returnList = new ArrayList<Policy>();
    try {/*from   w  w  w  . ja v  a2  s.c  o m*/
        final SAXBuilder builder = new SAXBuilder();
        final Document doc = builder.build(new StringReader(input));
        final Element rootElement = doc.getRootElement();

        final List policyElements = rootElement.getChildren("Policy");
        for (final Object policyNode : policyElements) {
            final Element policyElement = (Element) policyNode;
            final List<RuleSet> returnRuleSets = new ArrayList<RuleSet>();
            for (final Object ruleSetObjects : policyElement.getChildren("RuleSet")) {
                final Element loopRuleSet = (Element) ruleSetObjects;
                final Map<Rule, String> returnRules = new HashMap<Rule, String>();
                int violationsAllowed = 0;

                final org.jdom2.Attribute violationsAttribute = loopRuleSet.getAttribute("ViolationsAllowed");
                if (violationsAttribute != null && violationsAttribute.getValue().length() > 0) {
                    violationsAllowed = Integer.parseInt(violationsAttribute.getValue());
                }

                for (final Object ruleObject : loopRuleSet.getChildren("Rule")) {
                    final Element loopRuleElement = (Element) ruleObject;

                    final List ruleAttributes = loopRuleElement.getAttributes();
                    for (final Object attributeObject : ruleAttributes) {
                        final org.jdom2.Attribute loopAttribute = (org.jdom2.Attribute) attributeObject;

                        final Rule rule = Rule.valueOf(loopAttribute.getName());
                        final String value = loopAttribute.getValue();
                        returnRules.put(rule, value);
                    }
                }
                returnRuleSets.add(new RuleSet(violationsAllowed, returnRules));
            }
            returnList.add(new Policy(returnRuleSets));
        }
    } catch (JDOMException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    } catch (IOException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    } catch (NullPointerException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    } catch (IllegalArgumentException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    }
    return returnList;
}

From source file:com.oagsoft.grafo.util.XMLLoader.java

public static Grafo leerArchivoXml(String nomArch) {
    SAXBuilder builder = new SAXBuilder();
    Grafo g = null;//from   www  . j a v a2  s. c om
    try {
        Document document = (Document) builder.build(new File(nomArch));
        Element rootNode = document.getRootElement();
        Element nV = rootNode.getChild("numero-vertices");
        int numVertices = Integer.parseInt(nV.getValue().trim());
        g = new Grafo(numVertices);
        Element ad = rootNode.getChild("adyacencias");
        List<Element> hijos = ad.getChildren("adyacencia");
        for (Element hijo : hijos) {
            int nIni = Integer.parseInt(hijo.getChildTextTrim("nodo-inicial"));
            int nFin = Integer.parseInt(hijo.getChildTextTrim("nodo-final"));
            g.adyacencia(nIni, nFin);
        }
        //            System.out.println("Hay " + hijos.size()+ " adyacencias");
    } catch (JDOMException | IOException ex) {
        System.out.println(ex.getMessage());
    }
    return g;
}

From source file:com.oagsoft.wazgle.tools.XMLLoader.java

public static Grafo<GraphicNode> leerArchivoXml(String nomArch) {
    SAXBuilder builder = new SAXBuilder();
    Grafo<GraphicNode> g = null;// w w  w  . ja v  a  2s .co  m
    try {
        Document document = (Document) builder.build(new File(nomArch));
        Element rootNode = document.getRootElement();
        Element nV = rootNode.getChild("numero-vertices");
        int numVertices = Integer.parseInt(nV.getValue().trim());
        g = new Grafo(numVertices);
        Element nodos = rootNode.getChild("nodos");
        List<Element> hijos = nodos.getChildren("nodo");
        for (Element hijo : hijos) {
            int id = Integer.parseInt(hijo.getChildTextTrim("id"));
            int coorX = Integer.parseInt(hijo.getChildTextTrim("coor-x"));
            int coorY = Integer.parseInt(hijo.getChildTextTrim("coor-y"));
            GraphicNode nodo = new GraphicNode(id, new Point(coorX, coorY), 10, false);
            g.adVertice(nodo);
        }
        Element adyacencias = rootNode.getChild("adyacencias");
        List<Element> ad = adyacencias.getChildren("adyacencia");
        for (Element hijo : ad) {
            int vIni = Integer.parseInt(hijo.getChildTextTrim("nodo-inicial"));
            int vFinal = Integer.parseInt(hijo.getChildTextTrim("nodo-final"));
            GraphicNode nIni = g.getVertice(vIni);
            GraphicNode nFin = g.getVertice(vFinal);

            g.adyacencia(nIni, nFin);

        }
        //            System.out.println("Hay " + hijos.size()+ " adyacencias");
    } catch (JDOMException | IOException ex) {
        System.out.println(ex.getMessage());
    }
    return g;
}

From source file:com.ohnosequences.xml.model.go.GoTermXML.java

License:Open Source License

public static GoTermXML getGoTerm(String id) throws Exception {

    GoTermXML temp = new GoTermXML();

    URL url = new URL(GO_TERM_SERVICE_URL + id);
    // Connect//from  ww  w . j a  v  a 2 s.c  o  m
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    // Get data
    SAXBuilder saxBuilder = new SAXBuilder();
    Document doc = saxBuilder.build(urlConnection.getInputStream());

    Element termElement = doc.getRootElement().getChild("term");

    String idString, nameString, defString;
    idString = termElement.getChildText("id");
    nameString = termElement.getChildText("name");
    defString = termElement.getChild("def").getChildText("defstr");

    if (!id.equals(idString)) {
        throw new Exception(
                "El id proporcionado y el encontrado en el xml proporcionado por el servicio no son el mismo");
    } else {
        temp.setId(idString);
        temp.setGoName(nameString);
        temp.setDefinition(defString);
    }

    return temp;
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static Document loadDocument(URL configs, boolean validate) {
    Document properties;/*from  ww  w .  jav a  2  s.  c o  m*/
    SAXBuilder docBuilder;
    if (validate) {
        docBuilder = new SAXBuilder(XMLReaders.XSDVALIDATING);
    } else {
        docBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    }
    try {
        properties = docBuilder.build(configs);
    } catch (NullPointerException ex) {
        LoggerFactory.getLogger(LouieProperties.class)
                .error("Failed to load properties file. Defaults will be used.\n{}", ex.toString());
        System.out.println(ex.getMessage());
        List<Server> empty = Collections.emptyList();
        Server.processServers(empty);
        return null;
    } catch (IOException | JDOMException ex) {
        Matcher match = missingElem.matcher(ex.getMessage());
        if (match.matches()) {
            LoggerFactory.getLogger(LouieProperties.class).info("No schema located: no validation performed.");
            return loadDocument(configs, false);
        } else {
            String error = "Properties file error! All services shutdown";
            ServiceManager.recordError(error, ex);
            LoggerFactory.getLogger(LouieProperties.class).error("{}\n{}", error, ex.toString());
            List<Server> empty = Collections.emptyList();
            ServiceProperties.globalDisable(); //brute disable
            Server.processServers(empty);
            return null;
        }
    }
    document = new XMLOutputter().outputString(properties);
    return properties;
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void loadInternals() throws JDOMException, IOException {
    Document internals;/*from ww  w  .j  av  a  2s  .  c om*/
    SAXBuilder docBuilder = new SAXBuilder();

    URL xmlURL = LouieProperties.class.getResource("/config/louie-internal.xml");
    internals = docBuilder.build(xmlURL);

    Element louie = internals.getRootElement();

    //Load internal defaults into Server
    Element serverDef = louie.getChild("server_defaults");

    Server.setDefaultHost(serverDef.getChildText(HOST));
    Server.setDefaultGateway(serverDef.getChildText(GATEWAY));
    Server.setDefaultDisplay(serverDef.getChildText(DISPLAY));
    Server.setDefaultIP(serverDef.getChildText(IP));
    Server.setDefaultTimezone(serverDef.getChildText(TIMEZONE));
    Server.setDefaultLocation(serverDef.getChildText(LOCATION));
    Server.setDefaultPort(Integer.parseInt(serverDef.getChildText(PORT)));
    Server.setDefaultSecure(Boolean.parseBoolean(serverDef.getChildText(SECURE)));

    //Load internal defaults into ServiceProperties
    Element serviceDef = louie.getChild("service_defaults");

    ServiceProperties.setDefaultCaching(Boolean.parseBoolean(serviceDef.getChildText(CACHING)));
    ServiceProperties.setDefaultEnable(Boolean.parseBoolean(serviceDef.getChildText(ENABLE)));
    ServiceProperties.setDefaultReadOnly(Boolean.parseBoolean(serviceDef.getChildText(READ_ONLY)));

    //Load internal services into ServiceProperties
    Element coreServices = louie.getChild("core_services");
    processServices(coreServices, true);

    Element schedDef = louie.getChild("scheduler_defaults");
    TaskSchedulerProperties.setThreadPoolSize(Integer.parseInt(schedDef.getChildText(POOL_SIZE)));

    Element accessDef = louie.getChild("group_defaults");
    AccessManager.loadGroups(accessDef);

}

From source file:com.rometools.propono.atom.client.ClientAtomService.java

License:Open Source License

private Document getAtomServiceDocument() throws ProponoException {
    GetMethod method = null;/*from ww  w.  j  a  v a 2  s . c o m*/
    final int code = -1;
    try {
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        // TODO: make connection timeout configurable
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

        method = new GetMethod(uri);
        authStrategy.addAuthentication(httpClient, method);
        httpClient.executeMethod(method);

        final SAXBuilder builder = new SAXBuilder();
        final String doc = method.getResponseBodyAsString();
        LOG.debug(doc);
        return builder.build(method.getResponseBodyAsStream());

    } catch (final Throwable t) {
        final String msg = "ERROR retrieving Atom Service Document, code: " + code;
        LOG.debug(msg, t);
        throw new ProponoException(msg, t);
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.rometools.propono.atom.client.ClientCategories.java

License:Apache License

public void fetchContents() throws ProponoException {
    final GetMethod method = new GetMethod(getHrefResolved());
    clientCollection.addAuthentication(method);
    try {// w w w  .j  av a  2s .c o m
        clientCollection.getHttpClient().executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status code=" + method.getStatusCode());
        }
        final SAXBuilder builder = new SAXBuilder();
        final Document catsDoc = builder.build(new InputStreamReader(method.getResponseBodyAsStream()));
        parseCategoriesElement(catsDoc.getRootElement());

    } catch (final IOException ioe) {
        throw new ProponoException("ERROR: reading out-of-line categories", ioe);
    } catch (final JDOMException jde) {
        throw new ProponoException("ERROR: parsing out-of-line categories", jde);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.rometools.propono.atom.client.EntryIterator.java

License:Open Source License

private void getNextEntries() throws ProponoException {
    if (nextURI == null) {
        return;/*from  ww w . java2 s  . c  o m*/
    }
    final GetMethod colGet = new GetMethod(collection.getHrefResolved(nextURI));
    collection.addAuthentication(colGet);
    try {
        collection.getHttpClient().executeMethod(colGet);
        final SAXBuilder builder = new SAXBuilder();
        final Document doc = builder.build(colGet.getResponseBodyAsStream());
        final WireFeedInput feedInput = new WireFeedInput();
        col = (Feed) feedInput.build(doc);
    } catch (final Exception e) {
        throw new ProponoException("ERROR: fetching or parsing next entries, HTTP code: "
                + (colGet != null ? colGet.getStatusCode() : -1), e);
    } finally {
        colGet.releaseConnection();
    }
    members = col.getEntries().iterator();
    col.getEntries().size();

    nextURI = null;
    final List<Link> altLinks = col.getOtherLinks();
    if (altLinks != null) {
        for (final Link link : altLinks) {
            if ("next".equals(link.getRel())) {
                nextURI = link.getHref();
            }
        }
    }
}