Example usage for org.dom4j DocumentHelper parseText

List of usage examples for org.dom4j DocumentHelper parseText

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper parseText.

Prototype

public static Document parseText(String text) throws DocumentException 

Source Link

Document

parseText parses the given text as an XML document and returns the newly created Document.

Usage

From source file:org.springfield.lou.application.types.DemolinkedtvApplication.java

License:Open Source License

/**
 * Load the video from the file system//from ww w  .j a  va 2 s.c  o  m
 * 
 * @return true if everything went ok, otherwise false
 */
private boolean loadVideo() {
    ServiceInterface smithers = ServiceManager.getService("smithers");
    if (smithers == null)
        return false;
    //String data = LazyHomer.sendRequestBart("GET", videoUri, null, null);
    String data = smithers.get(videoUri, null, null);
    try {
        Document response = DocumentHelper.parseText(data);
        String imageUri = response.selectSingleNode("//screens[@id='1']/properties/uri") == null ? ""
                : response.selectSingleNode("//screens[@id='1']/properties/uri").getText();
        String mount = response.selectSingleNode("//rawvideo[@id='1']/properties/mount") == null ? ""
                : response.selectSingleNode("//rawvideo[@id='1']/properties/mount").getText();
        mount = mount.substring(0, mount.indexOf(","));

        this.imageUri = imageUri;
        this.mount = mount;
        return true;
    } catch (Exception e) {
        System.out.println("ERROR: loading video failed");
        e.printStackTrace();
        return false;
    }
}

From source file:org.springfield.lou.application.types.DemolinkedtvApplication.java

License:Open Source License

/**
 * Get entities for the given media resource id
 * //from   ww  w .  j ava2 s .c  o  m
 * @param domain - the domain the presentation is in
 * @param mediaResourceId - the media resource id of the video
 * @param path - the path 
 * @param imageBasePath - the image path
 * @param curated - curated version or not
 * @return FsTimeTagNodes
 */
public static FsTimeTagNodes getTagNodes(String domain, String mediaResourceId, String path,
        String imageBasePath, boolean curated) {
    FsTimeTagNodes results = new FsTimeTagNodes();

    String uri = FS_SERVER + "?domain=" + domain + "&id=" + mediaResourceId + "&annotations";
    if (curated) {
        System.out.println("Curated request");
        uri += "&curated&renew";
    }

    String data = HttpHelper.sendRequest("GET", uri, null, null).getResponse();
    System.out.println("data = " + data);
    try {
        Document response = DocumentHelper.parseText(data);
        List<Node> annotations = response.selectNodes("//annotations/*");
        System.out.println("number of childs:" + annotations.size());
        for (Iterator<Node> i = annotations.iterator(); i.hasNext();) {
            Element annotation = (Element) i.next();
            FsNode node = new FsNode();
            node.setName(annotation.getName());
            node.setId(annotation.attribute("id").getText());
            node.setPath(path + "/" + node.getName() + "/" + node.getId());
            node.setImageBaseUri(imageBasePath);
            List<Node> properties = annotation.selectNodes("properties/*");
            for (Iterator<Node> j = properties.iterator(); j.hasNext();) {
                Element property = (Element) j.next();
                node.setProperty(property.getName(), property.getText());
            }
            results.addNode(node);
        }
    } catch (Exception e) {
        System.out.println("Error in parsing nodes from maggie");
    }
    data = HttpHelper.sendRequest("GET",
            FS_SERVER + "?domain=" + domain + "&id=" + mediaResourceId + "&chapters", null, null).getResponse();
    System.out.println("data from chapters = " + data);
    try {
        Document response = DocumentHelper.parseText(data);
        List<Node> chapters = response.selectNodes("//chapters/*");
        System.out.println("number of childs:" + chapters.size());
        for (Iterator<Node> i = chapters.iterator(); i.hasNext();) {
            Element chapter = (Element) i.next();

            FsNode node = new FsNode();
            node.setName(chapter.getName());
            node.setId(chapter.attribute("id").getText());
            node.setPath(path + "/" + node.getName() + "/" + node.getId());
            node.setImageBaseUri(imageBasePath);
            List<Node> properties = chapter.selectNodes("properties/*");
            for (Iterator<Node> j = properties.iterator(); j.hasNext();) {
                Element property = (Element) j.next();
                node.setProperty(property.getName(), property.getText());
            }
            results.addNode(node);
        }
    } catch (Exception e) {
        System.out.println("Error in parsing nodes from maggie");
    }

    System.out.println("current ammount of annotations = " + results.size());
    return results;
}

From source file:org.springfield.lou.application.types.DemolinkedtvApplication.java

License:Open Source License

/**
 * Get list of enrichments for the given entity
 * //from  w w w  .  j  a  v a2s.  c  om
 * @param domain - the current domain
 * @param entityId - the entity id
 * @return List of enrichment elements
 */
public static List<Element> getEnrichment(String domain, String entityId) {
    String uri = FS_SERVER + "?domain=" + domain + "&id=" + entityId + "&enrichments";
    System.out.println("Getting enrichment " + entityId + " (" + uri + ")");
    String data = HttpHelper.sendRequest("GET", uri, null, null).getResponse();
    List<Element> results = null;

    try {
        Document response = DocumentHelper.parseText(data);
        List<Node> enrichments = response.selectNodes("//enrichments/*");
        System.out.println("number of childs:" + enrichments.size());
        results = new ArrayList<Element>(enrichments.size());
        for (Iterator<Node> i = enrichments.iterator(); i.hasNext();) {
            Element enrichment = (Element) i.next();
            results.add(enrichment);
        }
    } catch (Exception e) {
        System.out.println("Error in parsing nodes from maggie");
    }
    return results;
}

From source file:org.springfield.lou.homer.LazyHomer.java

License:Open Source License

private Boolean checkKnown() {
    String xml = "<fsxml><properties><depth>1</depth></properties></fsxml>";
    ServiceInterface smithers = ServiceManager.getService("smithers");
    if (smithers == null)
        return false;
    String nodes = smithers.get("/domain/internal/service/lou/nodes", xml, "text/xml");

    boolean iamok = false;

    try {//from   ww w  . j a va  2 s.  c om
        boolean foundmynode = false;
        Document result = DocumentHelper.parseText(nodes);
        for (Iterator<Node> iter = result.getRootElement().nodeIterator(); iter.hasNext();) {
            Element child = (Element) iter.next();
            if (!child.getName().equals("properties")) {
                String ipnumber = child.attributeValue("id");
                String status = child.selectSingleNode("properties/status").getText();
                String name = child.selectSingleNode("properties/name").getText();

                // lets put all in our lou list
                LouProperties mp = lous.get(ipnumber);
                if (mp == null) {
                    mp = new LouProperties();
                    lous.put(ipnumber, mp);

                }

                mp.setIpNumber(ipnumber);
                mp.setName(name);
                mp.setStatus(status);
                mp.setDefaultLogLevel(child.selectSingleNode("properties/defaultloglevel").getText());
                mp.setPreferedSmithers(child.selectSingleNode("properties/preferedsmithers").getText());
                Node dn = child.selectSingleNode("properties/developermode");
                if (dn != null) {
                    String dm = dn.getText();
                    if (dm != null && dm.equals("true")) {
                        mp.setDeveloperMode(true);
                    }
                }

                //System.out.println("DEVELOPER MODE = "+mp.getDeveloperMode());

                //System.out.println("comparing ip "+ipnumber+" with "+myip);               
                if (ipnumber.equals(myip)) {
                    //System.out.println("ip was equal");
                    foundmynode = true;
                    retryCounter = 0;
                    if (name.equals("unknown")) {
                        LOG.info("This lou is not verified change its name, use smithers todo this for ip "
                                + myip);
                    } else {
                        // so we have a name (verified) return true
                        iamok = true;
                    }
                } else {
                    //System.out.println("ip was not equal!");
                }
            }
        }
        if (!foundmynode) {
            if (retryCounter < 30) {
                //retry 30 times (= 5 min) to handle temp smithers downtime (eg daily restarts)
                retryCounter++;
            } else {
                LOG.info("LazyHomer : Creating my processing node " + LazyHomer.getSmithersUrl()
                        + "/domain/internal/service/lou/properties");
                String os = "unknown"; // we assume windows ?
                try {
                    os = System.getProperty("os.name");
                } catch (Exception e) {
                    LOG.error("LazyHomer : " + e.getMessage());
                }

                String newbody = "<fsxml>";
                newbody += "<nodes id=\"" + myip + "\"><properties>";
                newbody += "<name>unknown</name>";
                newbody += "<status>off</status>";
                newbody += "<lastseen>" + new Date().getTime() + "</lastseen>";
                newbody += "<preferedsmithers>" + myip + "</preferedsmithers>";
                newbody += "<activesmithers>" + selectedsmithers.getIpNumber() + "</activesmithers>";

                // i know this looks weird but left it for future extentions
                if (isWindows()) {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                }
                if (isMac()) {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                }
                if (isUnix()) {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                } else {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                }
                newbody += "</properties></nodes></fsxml>";
                smithers.put("/domain/internal/service/lou/properties", newbody, "text/xml");
            }
        }
    } catch (Exception e) {
        LOG.info("LazyHomer exception doc");
        e.printStackTrace();
    }
    return iamok;
}

From source file:org.springfield.lou.util.NodeObserver.java

License:Open Source License

private void sendInitialState() {
    System.out.println("NodeObserver.sendInitialState()");
    ServiceInterface smithers = ServiceManager.getService("smithers");
    if (smithers == null)
        return;/*from   ww w.j  ava  2s. co m*/
    String responseStr = smithers.get(node, "<fsxml><properties><depth>2</depth></properties></fsxml>",
            "text/xml");

    //   String responseStr = LazyHomer.sendRequestBart("GET", node, "<fsxml><properties><depth>2</depth></properties></fsxml>", "text/xml");
    try {
        Document response = DocumentHelper.parseText(responseStr);
        xml = filterAllowed(response);
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.springfield.lou.util.NodeObserver.java

License:Open Source License

@Override
public void remoteSignal(String from, String method, String url) {
    // TODO Auto-generated method stub
    System.out.println("NodeObserver.remoteSignal(" + from + ", " + method + ", " + url + ")");
    if (method.equals("PUT") || method.equals("DELETE")) {
        String[] urls = url.split(",");
        String updatedUrl = urls[0];

        ServiceInterface smithers = ServiceManager.getService("smithers");
        if (smithers == null)
            return;

        String updatedNode = smithers.get(updatedUrl,
                "<fsxml><properties><depth>4</depth></properties></fsxml>", "text/xml");
        try {/*from w  w  w. j  a v a2s  .  c o  m*/
            Document response = DocumentHelper.parseText(updatedNode);
            List<Node> errors = response.selectNodes("//error");
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:org.springfield.maggie.homer.LazyHomer.java

License:Open Source License

private Boolean checkKnown() {
    //   System.out.println("MYIP="+myip+" SM="+selectedsmithers);

    String xml = "<fsxml><properties><depth>1</depth></properties></fsxml>";
    //String nodes = LazyHomer.sendRequest("GET","/domain/internal/service/maggie/nodes",xml,"text/xml");
    ServiceInterface smithers = ServiceManager.getService("smithers");
    if (smithers == null)
        return false;
    String nodes = smithers.get("/domain/internal/service/maggie/nodes", xml, "text/xml");

    boolean iamok = false;

    try {//from   w w w . j av  a  2 s.  co m
        boolean foundmynode = false;
        //System.out.println("NODES="+nodes);
        Document result = DocumentHelper.parseText(nodes);
        for (Iterator<Node> iter = result.getRootElement().nodeIterator(); iter.hasNext();) {
            Element child = (Element) iter.next();
            if (!child.getName().equals("properties")) {
                String ipnumber = child.attributeValue("id");
                String status = child.selectSingleNode("properties/status").getText();
                String name = child.selectSingleNode("properties/name").getText();

                // lets put all in our maggie list
                MaggieProperties mp = maggies.get(ipnumber);
                if (mp == null) {
                    mp = new MaggieProperties();
                    maggies.put(ipnumber, mp);

                }
                mp.setIpNumber(ipnumber);
                mp.setName(name);
                mp.setStatus(status);
                mp.setDefaultLogLevel(child.selectSingleNode("properties/defaultloglevel").getText());
                mp.setPreferedSmithers(child.selectSingleNode("properties/preferedsmithers").getText());

                //System.out.println("comparing ip "+ipnumber+" with "+myip);               
                if (ipnumber.equals(myip)) {
                    //System.out.println("ip was equal");
                    foundmynode = true;
                    retryCounter = 0;
                    if (name.equals("unknown")) {
                        LOG.info("This maggie is not verified change its name, use smithers todo this for ip "
                                + myip);
                    } else {
                        // so we have a name (verified) return true
                        iamok = true;
                    }
                } else {
                    //System.out.println("ip was not equal!");
                }
            }
        }
        if (!foundmynode) {
            if (retryCounter < 30) {
                //retry 30 times (= 5 min) to handle temp smithers downtime (eg daily restarts)
                retryCounter++;
            } else {
                LOG.info("LazyHomer : Creating my processing node " + LazyHomer.getSmithersUrl()
                        + "/domain/internal/service/maggie/properties");
                String os = "unknown"; // we assume windows ?
                try {
                    os = System.getProperty("os.name");
                } catch (Exception e) {
                    LOG.error("LazyHomer : " + e.getMessage());
                }

                String newbody = "<fsxml>";
                newbody += "<nodes id=\"" + myip + "\"><properties>";
                newbody += "<name>unknown</name>";
                newbody += "<status>off</status>";
                newbody += "<lastseen>" + new Date().getTime() + "</lastseen>";
                newbody += "<preferedsmithers>" + myip + "</preferedsmithers>";
                newbody += "<activesmithers>" + selectedsmithers.getIpNumber() + "</activesmithers>";

                // i know this looks weird but left it for future extentions
                if (isWindows()) {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                }
                if (isMac()) {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                }
                if (isUnix()) {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                } else {
                    newbody += "<defaultloglevel>info</defaultloglevel>";
                }
                newbody += "</properties></nodes></fsxml>";
                smithers.put("/domain/internal/service/maggie/properties", newbody, "text/xml");
                //LazyHomer.sendRequest("PUT","/domain/internal/service/maggie/properties",newbody,"text/xml");
            }
        }
    } catch (Exception e) {
        LOG.info("LazyHomer exception doc");
        e.printStackTrace();
    }
    return iamok;
}

From source file:org.springfield.mojo.linkedtv.Channel.java

License:Open Source License

public List<Episode> getEpisodes() {
    try {/*from  www .  j a  va 2  s .  c  om*/
        Document doc = DocumentHelper.parseText(channelXml);

        List<Node> nodes = doc.selectNodes("//mediaresources/mediaresource");
        ArrayList<Episode> episodes = new ArrayList<Episode>();

        for (Node node : nodes) {
            String id = node.selectSingleNode("id") == null ? null : node.selectSingleNode("id").getText();
            if (id != null) {
                episodes.add(new Episode(id));
            }
        }
        return episodes;
    } catch (DocumentException e) {
        return new ArrayList<Episode>();
    }
}

From source file:org.springfield.mojo.linkedtv.Channel.java

License:Open Source License

public Episode getLatestEpisode() {
    try {//  w w  w .j  av  a 2 s.  c om
        Document doc = DocumentHelper.parseText(channelXml);

        List<Node> nodes = doc.selectNodes("//mediaresources/mediaresource");

        if (nodes.size() > 0) {
            Node node = nodes.get(0);
            String id = node.selectSingleNode("id") == null ? null : node.selectSingleNode("id").getText();
            if (id != null) {
                return new Episode(id);
            }
        }

        return new Episode();
    } catch (DocumentException e) {
        return new Episode();
    }
}