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:com.noterik.bart.fs.fsxml.FSXMLRequestHandler.java

License:Open Source License

/**
 * Get pruning parameters from request body
 * /*from ww  w  .  j ava2s  .c  o  m*/
 * @param value
 * @return
 */
private Map<String, String> getParameters(String value) {
    Map<String, String> params = new HashMap<String, String>();

    // parse request body
    Node start = null, limit = null, depth = null;
    try {
        Document doc = DocumentHelper.parseText(value);
        start = doc.selectSingleNode("//properties/start");
        limit = doc.selectSingleNode("//properties/limit");
        depth = doc.selectSingleNode("//properties/depth");
    } catch (Exception e) { /*
                            * request body empty or request body could not
                            * be parsed
                            */
    }

    // add to params
    if (start != null) {
        params.put("start", start.getText());
    }
    if (limit != null) {
        params.put("limit", limit.getText());
    }
    if (depth != null) {
        params.put("depth", depth.getText());
    }

    return params;
}

From source file:com.noterik.bart.fs.ingest.SimpleIngestHandler.java

License:Open Source License

private String saveVideoProperties(EncodingProfile ep, IngestInputData iid, String domain, String rawIndex) {
    String destUri = null;/* w  w  w  .  j a v  a2s .  c o  m*/
    String vidUri = "/domain/" + domain + "/user/" + iid.getUser() + "/video";
    if (iid.getVideoId() == null) {
        String vidXml = "<fsxml><properties></properties></fsxml>";
        logger.debug("ADD(" + vidUri + ") " + vidXml);
        int id = IdHandler.instance().insert(vidUri);
        vidUri += "/" + id;
        logger.debug("VID URI AFTER: " + vidUri);
        FSXMLRequestHandler.instance().saveFsXml(vidUri, vidXml, "POST", true);
    } else {
        vidUri += "/" + iid.getVideoId();
    }
    destUri = vidUri + "/rawvideo/" + rawIndex;
    logger.debug("DEST URI BECOMES: " + destUri);
    String rawXml = FSXMLBuilder.getVideoPropertiesFromProfile(ep);

    // add original filename
    try {
        String originalFilename = iid.getSource();
        originalFilename = originalFilename.substring(originalFilename.lastIndexOf("/") + 1);
        Document doc = DocumentHelper.parseText(rawXml);
        Element properties = (Element) doc.selectSingleNode("//properties");
        Element oName = properties.addElement("original-filename");
        oName.setText(originalFilename);
        rawXml = doc.asXML();
    } catch (Exception e) {
        logger.error("Caught Exception: " + e.getMessage());
    }

    logger.debug("ADD(" + rawXml + ") " + destUri);
    FSXMLRequestHandler.instance().saveFsXml(destUri + "/properties", rawXml, "PUT", true);
    return destUri;
}

From source file:com.noterik.bart.fs.LazyHomer.java

License:Open Source License

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

    boolean iamok = false;

    try {/*w  ww .j av a 2  s.co  m*/
        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();

                if (ipnumber.equals(myip)) {
                    foundmynode = true;
                    if (name.equals("unknown")) {
                        LOG.info("This smithers is not verified change its name, use smithers todo this for ip "
                                + myip);
                    } else {
                        // so we have a name (verified) return true
                        iamok = true;
                    }
                }
            }
        }
        if (!foundmynode) {
            LOG.info("LazyHomer : Creating my processing node " + LazyHomer.getSmithersUrl()
                    + "/domain/internal/service/smithers/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>master</name>";
            newbody += "<status>on</status>";
            newbody += "<lastseen>" + new Date().getTime() + "</lastseen>";
            newbody += "<preferedsmithers>" + myip + "</preferedsmithers>";
            newbody += "<activesmithers>" + myip + "</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/smithers/properties", newbody, "text/xml");

            //   LazyHomer.sendRequest("PUT","/domain/internal/service/smithers/properties",newbody,"text/xml");
        }
    } catch (Exception e) {
        LOG.info("LazyHomer exception doc");
        e.printStackTrace();
    }
    return iamok;
}

From source file:com.noterik.bart.fs.LazyHomer.java

License:Open Source License

/**
 * Get the address of a active service in the cluster
 * /*from  w  w w  . j av a 2  s.  c  o  m*/
 * @param service - The name of the service requested
 */
public static String getActiveService(String service) {
    String serviceAddress = "";
    long mostRecentServiceTime = 0L;

    service = service.toLowerCase();
    //   String response = LazyHomer.sendRequest("GET","/domain/internal/service/"+service+"/nodes",null,null);
    ServiceInterface smithers = ServiceManager.getService("smithers");
    if (smithers == null)
        return null;
    String response = smithers.get("/domain/internal/service/" + service + "/nodes", null, null);

    //Get all nodes for this service
    try {
        Document result = DocumentHelper.parseText(response);
        for (Iterator<Node> iter = result.getRootElement().nodeIterator(); iter.hasNext();) {
            Element child = (Element) iter.next();
            if (!child.getName().equals("properties")) {
                String name = child.attributeValue("id");
                String status = child.selectSingleNode("properties/status") == null ? ""
                        : child.selectSingleNode("properties/status").getText();
                String lastSeen = child.selectSingleNode("properties/lastseen") == null ? "0"
                        : child.selectSingleNode("properties/lastseen").getText();
                String port = child.selectSingleNode("properties/port") == null
                        ? String.valueOf(getSmithersPort())
                        : child.selectSingleNode("properties/port").getText();
                long serviceTime = Long.parseLong(lastSeen);

                LOG.debug("node " + name + ":" + port + " found with status " + status + " lastseen " + lastSeen
                        + " - " + serviceTime);

                //Check if this service is enabled and recently active
                if (status.equals("on") && serviceTime > mostRecentServiceTime) {
                    mostRecentServiceTime = serviceTime;
                    serviceAddress = name + ":" + port;
                }
            }
        }
    } catch (DocumentException e) {
        LOG.info("LazyHomer: " + e.getMessage());
    }

    //Check if this service was active within the MAX_SERVICE_DELAY
    long currentTime = new Date().getTime();
    if ((mostRecentServiceTime + MAX_SERVICE_DELAY) > currentTime) {
        return serviceAddress;
    }
    return null;
}

From source file:com.noterik.bart.fs.legacy.properties.PropertyHandler.java

License:Open Source License

/**
 * This function will walk trough all children of a given uri and add it's
 * properties to the Document passed as parameter. The counter sets the
 * number of levels it should go into/*  w w  w  .ja  v  a  2s.  c  om*/
 *
 * @param doc
 * @param current
 * @param uri
 * @param count
 * @return
 */
public static Document loopInIt(Document doc, Element current, String uri, int count, List uris) {
    // the depth level it should go into
    if (count < 10) {
        count++;
        String xml = getXMLfromProp(uri);
        if (!xml.equals("")) {
            xml = unwrapXml(xml);
        }
        Element elem = null;
        // if xml is not empty add the node
        if (!xml.equals("")) {
            try {
                elem = (Element) DocumentHelper.parseText(xml).getRootElement().clone();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            current.add(elem);
        } else {
            // if it is empty, just add an empty node
            current.addElement("properties");
        }
        String childUri = "";
        String childId = "";
        String childType = "";
        // get the children for this uri (refer_uris)
        Map<String, Props> childs = getChildrenOfUri(uri);
        Iterator<String> it = childs.keySet().iterator();
        // for each children do the loop
        while (it.hasNext()) {
            childUri = it.next();
            // check if this uri was already processed
            if (!uris.contains(childUri)) {
                uris.add(childUri);
                childId = childs.get(childUri).getId();
                childType = childs.get(childUri).getType();
                Element newElem = current.addElement(childType).addAttribute("id", childId);
                doc = loopInIt(doc, newElem, childUri, count, uris);
            }
        }
    }
    return doc;
}

From source file:com.noterik.bart.fs.legacy.properties.PropertyHandler.java

License:Open Source License

/**
 * This function will get the properties of a uri only, and not of it's
 * childs// w  ww.  j av  a  2  s  . com
 *
 * @param uri
 * @return
 */
public static Document getPropsNoChilds(String uri) {
    String url = uri.substring(0, uri.lastIndexOf("/"));
    logger.debug("url is:" + url);
    Document doc = DocumentHelper.createDocument();
    String id = url.substring(url.lastIndexOf("/") + 1);
    String type = getTypefromProp(url);
    Element root = doc.addElement("fsxml");
    Element current = root.addElement(type).addAttribute("id", id);
    String xml = getXMLfromProp(url);
    Element elem = null;
    // if xml is not empty, unwrapp it and add the node
    if (!xml.equals("")) {
        xml = unwrapXml(xml);
        try {
            elem = (Element) DocumentHelper.parseText(xml).getRootElement().clone();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        current.add(elem);
    } else {
        // if it is empty, just add an empty node
        current.addElement("properties");
    }
    return doc;
}

From source file:com.noterik.bart.fs.legacy.properties.PropertyHandler.java

License:Open Source License

/**
 * This function takes the wrapping <fsxml> tag from the xml properties
 * string//from   w w w .j  a v a 2  s  .  c om
 *
 * @param xml
 * @return
 */
public static String unwrapXml(String xml) {
    logger.debug("\nXML IS: " + xml);
    try {
        Document doc = DocumentHelper.parseText(xml);
        Element props = (Element) doc.selectSingleNode("/fsxml/properties");
        xml = props.asXML();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    return xml;
}

From source file:com.noterik.bart.fs.legacy.tools.FlandersHelper.java

License:Open Source License

public static String processRaw(String uri, String xml) {
    // parse document
    Document doc = null;//from   ww w  . j a  v  a  2s.  co  m
    try {
        doc = DocumentHelper.parseText(xml);
    } catch (DocumentException e) {
        LOG.error("Could not parse xml", e);
        return null;
    }

    // check mount property
    Node mountNode = doc.selectSingleNode("//mount");
    if (mountNode == null) {
        LOG.error("No mount property was set");
        return null;
    }

    // extract single mount
    String mount = null;
    String mounts = mountNode.getText();
    if (mounts != null && !mounts.equals("")) {
        mount = mounts.indexOf(",") != -1 ? mounts.substring(0, mounts.indexOf(",")) : mounts;
    }

    // determine external or local stream
    String flandersXml = null;
    if (mount.toLowerCase().startsWith("rtmp")) {
        LOG.debug("External stream");
        Node filenameNode = doc.selectSingleNode("//filename");
        if (filenameNode != null) {
            String filename = filenameNode.getText();
            flandersXml = getXmlFromFlandersExternal(filename, mount);
        }
    } else if (mount.toLowerCase().indexOf("drm://") != -1) {
        LOG.debug("DRM stream");
        Node filenameNode = doc.selectSingleNode("//filename");
        if (filenameNode != null) {
            String filename = filenameNode.getText();
            flandersXml = getXmlFromFlandersBgDrm(filename, mount);
        }
    } else {
        LOG.debug("Local stream");
        Node extNode = doc.selectSingleNode("//extension");
        if (extNode != null) {
            String extension = extNode.getText();
            String filename = uri + "/raw." + extension;
            flandersXml = getXmlFromFlandersLocal(filename, mount);
        } else {
            LOG.error("Extension property was not set");
            return null;
        }
    }

    LOG.debug("FLANDERS XML: " + flandersXml);
    xml = processXml(xml, flandersXml);
    return xml;
}

From source file:com.noterik.bart.fs.legacy.tools.FlandersHelper.java

License:Open Source License

private static String processXml(String original, String flanders) {
    String xml = "";

    Map<String, String> values = new HashMap<String, String>();

    Document origdoc = null;/*from ww w.j  a  v  a2 s . c o m*/
    Document flandoc = null;

    try {
        origdoc = DocumentHelper.parseText(original);
        flandoc = DocumentHelper.parseText(flanders);
    } catch (DocumentException e) {
        LOG.error("", e);
    }

    Element origProp = (Element) origdoc.selectSingleNode("//properties");
    Iterator i = origProp.elementIterator();

    while (i.hasNext()) {
        Element prop = (Element) i.next();
        String name = prop.getName();
        String value = prop.getText();
        values.put(name, value);
    }

    LOG.debug("\n flandProp = " + flandoc.asXML());

    Element flandProp = (Element) flandoc.selectSingleNode("/meta-data");
    Iterator j = flandProp.elementIterator();

    while (j.hasNext()) {
        Element prop = (Element) j.next();
        String name = prop.getName();
        String value = prop.getText();
        values.put(name, value);
    }

    Element finalEl = DocumentHelper.createElement("fsxml");
    Element propsEl = finalEl.addElement("properties");

    Iterator<String> it = values.keySet().iterator();
    while (it.hasNext()) {
        String name = it.next();
        String value = values.get(name);
        propsEl.addElement(name).addText(value);
    }

    xml = finalEl.asXML();

    return xml;
}

From source file:com.noterik.bart.fs.legacy.tools.XmlHelper.java

License:Open Source License

/**
 * This function sets the value to the properties file in the path passed as
 * parameter/*from  w  w w .  j  a  v  a  2s . c o  m*/
 *
 * @param xml
 *            content of the properties file
 * @param xpath
 *            xpath of the property to set
 * @param value
 *            value to assign to the property
 * @return xml String updated content of the properties file
 */
public static String setValueOfProperty(String xml, String path, String value) {
    String xpath = "/fsxml/" + path;
    Document propdoc = null;
    try {
        propdoc = DocumentHelper.parseText(xml);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    Element elem = (Element) propdoc.selectSingleNode(xpath);
    if (elem != null) {
        elem.setText(value);
    }
    Element props = (Element) propdoc.selectSingleNode("/fsxml");
    xml = props.asXML();
    return xml;
}