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.action.CreateRawsAction.java

License:Open Source License

@Override
public String run() {
    logger.debug("Starting to create all required raws");
    String uri = event.getUri();/*from   ww  w .  ja va 2 s  . c  o  m*/
    String requestBody = event.getRequestData();
    logger.debug("\n\nRequest Data: " + requestBody);
    String mount = null, original = null;
    Document originalDoc = null;

    try {
        if (requestBody != null) {
            originalDoc = DocumentHelper.parseText(requestBody);
            Node mountNode = originalDoc.selectSingleNode("//properties/mount");
            Node originalNode = originalDoc.selectSingleNode("//properties/original");
            Node transcoderNode = originalDoc.selectSingleNode("//properties/transcoder");
            if (transcoderNode != null && transcoderNode.getText() != null
                    && transcoderNode.getText().equals("apu")) {
                logger.debug("The video was already transcoded by apu, skipping raw2 creation");
                return null;
            }

            if (mountNode != null && mountNode.getText() != null) {
                mount = mountNode.getText();
                logger.debug("\n\n\n\n\nFOUND MOUNT: " + mount);
            } else {
                logger.debug("\n\n\n\n\nNO MOUNT FOUND IN PROPERTIES");
            }

            if (originalNode != null && originalNode.getText() != null) {
                original = originalNode.getText();
                logger.debug("\n\n\n\n\nFOUND ORIGINAL: " + original);
            } else {
                logger.debug(
                        "\n\n\n\n\nNO ORIGINAL FOUND IN PROPERTIES, WHICH MEANS APU UPLOAD, CREATE ORIGINAL ANYWAYS ;)");
                createOriginalTagAfterApuUpload();
                return null;
            }
        }
    } catch (DocumentException e) {
        logger.error("", e);
    }

    //get config and number of raws to work on
    Map<String, EncodingProfile> profiles = getConfigs(uri);
    String[] ids = new String[profiles.size()];
    logger.debug("number of profiles = " + profiles.size());
    int h = 0;

    for (Iterator i = profiles.keySet().iterator(); i.hasNext();) {
        ids[h] = (String) i.next();
        h++;
    }
    //sorting using natural order!! so 11 < 2
    Arrays.sort(ids);

    for (int j = 0; j < ids.length; j++) {
        String id = ids[j];
        logger.debug("found video config " + id);
        String rawUri = uri.substring(0, uri.lastIndexOf("/")) + "/" + id + "/properties";

        if (FSXMLRequestHandler.instance().getPropertyValue(rawUri + "/reencode") == null && mount != null) {
            //if (!FSXMLRequestHandler.instance().hasProperties(rawUri) && mount != null) {         
            createRawProperties(rawUri, mount, profiles.get(id));
        }
    }
    return null;
}

From source file:com.noterik.bart.fs.action.FlandersAction.java

License:Open Source License

@Override
public String run() {
    // parse request
    String requestBody = event.getRequestData();
    String requestUri = event.getUri();

    try {/* ww  w  .j a  va2  s.  c  o  m*/
        Document doc = DocumentHelper.parseText(requestBody);
        Node statusNode = doc.selectSingleNode("//properties/status");
        Node flandersNode = doc.selectSingleNode("//properties/metadata_file"); // created by flanders

        // check if status was done
        if (statusNode == null || !statusNode.getText().toLowerCase().equals(DONE) || flandersNode != null) {
            logger.debug("Not sending to flanders");
            return null;
        } else {
            logger.debug("Sending request to flanders (" + requestUri + ")");
            String newProperties = processRaw(requestUri, requestBody);
            FSXMLRequestHandler.instance().saveFsXml(requestUri, newProperties, "PUT", true);
        }
    } catch (Exception e) {
        logger.error("Could not parse request data");
    }

    return null;
}

From source file:com.noterik.bart.fs.action.FlandersAction.java

License:Open Source License

private String processRaw(String uri, String xml) {
    // parse document
    Document doc = null;/*from ww  w.  jav a 2 s .c  o m*/
    try {
        doc = DocumentHelper.parseText(xml);
    } catch (DocumentException e) {
        logger.error("Could not parse xml", e);
        return null;
    }

    // check mount property
    Node mountNode = doc.selectSingleNode("//mount");
    if (mountNode == null) {
        logger.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")) {
        logger.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) {
        logger.debug("DRM stream");
        Node filenameNode = doc.selectSingleNode("//filename");
        if (filenameNode != null) {
            String filename = filenameNode.getText();
            flandersXml = getXmlFromFlandersBgDrm(filename, mount);
        }
    } else {
        logger.debug("Local stream");
        Node extNode = doc.selectSingleNode("//extension");
        Node filenameNode = doc.selectSingleNode("//filename");
        if (filenameNode != null) {
            String filename = filenameNode.getText();
            flandersXml = getXmlFromFlandersLocal(filename, mount);
        } else if (extNode != null) {
            String extension = extNode.getText();
            String filename = uri + "/raw." + extension;
            flandersXml = getXmlFromFlandersLocal(filename, mount);
        } else {
            logger.error("Extension property was not set");
            return null;
        }
    }

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

From source file:com.noterik.bart.fs.action.FlandersAction.java

License:Open Source License

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

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

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

    try {
        origdoc = DocumentHelper.parseText(original);
        flandoc = DocumentHelper.parseText(flanders);
    } catch (DocumentException e) {
        logger.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);
    }

    logger.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();

        //For marin there metadata is leading
        if (values.containsKey("mount") && values.get("mount").toLowerCase().equals("marin")) {
            if (!values.containsKey(name)) {
                values.put(name, value);
            }
        } else {
            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.action.GenericIndexQueueAction.java

License:Open Source License

private void putInQueue(String eventUri, int priority, String objectUri) {
    logger.debug("putInQueue eventuri = " + eventUri + " priority = " + priority + " objecturi = " + objectUri);

    /* Loop all collections where object is refered and add to queues */
    for (int i = 0; i < referedObjects.size(); i++) {
        String collectionObject = referedObjects.get(i) == null ? null : referedObjects.get(i);
        if (collectionObject != null) {
            //get domain from collection object and add to queue in domain
        }/*from   w w  w. j a  va  2  s .  c  o  m*/

        if (referedObjects.get(i) != null) {
            domainid = URIParser.getDomainIdFromUri(eventUri);
            queueUri = QUEUE_URI.replace("{domainid}", domainid);

            boolean jobExists = false;

            /* check if job is already in proper queue */
            for (int j = 0; j < referedJobs.size(); j++) {
                String refer = referedJobs.get(j);
                if (refer != null) {
                    String[] parts = refer.substring(1).split("/");
                    if (parts[1].equals(domainid)) {
                        //select this job to check priority
                        jobExists = true;
                        String job = getSubUri(refer, 8);
                        String jobContent = null;

                        try {
                            jobContent = FSXMLRequestHandler.instance().handleGET(job, null).getText();
                        } catch (IOException e) {
                        }
                        Document jobProperties = null;
                        try {
                            jobProperties = DocumentHelper.parseText(jobContent);
                        } catch (Exception e) {
                        }

                        String jobId = job.substring(job.lastIndexOf("/") + 1);
                        int currentPriority = jobProperties.selectSingleNode("//priority") == null ? 2
                                : Integer.parseInt(jobProperties.selectSingleNode("//priority").getText());
                        logger.debug("current prio " + currentPriority);
                        if (currentPriority > priority) {
                            updatePriority(priority, jobId);
                        }
                    }
                }
            }
            /* Otherwise add as a new job */
            if (!jobExists) {
                logger.debug("call add job");
                addJob(objectUri, eventUri);
            }

            /* check for double entries */
            getRefers(eventUri);
            int jobCounter = 0;
            for (int k = 0; k < referedJobs.size(); k++) {
                String refer = referedJobs.get(k);
                if (refer != null) {
                    String[] parts = refer.substring(1).split("/");
                    if (parts[1].equals(domainid) && parts[5].equals("genericindex")
                            && parts[6].equals("job")) {
                        jobCounter++;
                        if (jobCounter > 1) {
                            logger.debug("remove double entry from queue");
                            FSXMLRequestHandler.instance().deleteNodeProperties(refer, false);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.noterik.bart.fs.action.MenuIndexAction.java

License:Open Source License

private void rebuildMenuIndex(String uri) {
    totalKeywords = new ArrayList<String>();
    totalLocations = new ArrayList<String>();
    totalDates = new ArrayList<String>();
    totalWitnesses = new ArrayList<String>();
    totalPersons = new ArrayList<String>();
    totalSpeakers = new ArrayList<String>();

    /* Delete current index */
    FSXMLRequestHandler.instance().deleteNodeProperties(indexUri, true);

    String collectionUri = getSubUri(uri, 4) + "/collection";

    /* Determine total number of collections */
    Document collections = FSXMLRequestHandler.instance().getNodePropertiesByType(collectionUri, 0, 0, 1);
    Node resultsAvailable = collections.selectSingleNode("//properties/totalResultsAvailable");
    int numCollections = Integer.parseInt(resultsAvailable.getText());

    logger.debug("num collections = " + numCollections);
    FSXMLRequestHandler.instance().handlePUT(indexUri + "/properties", "<fsxml><properties><lastupdate>"
            + String.valueOf(new Date().getTime()) + "</lastupdate></properties></fsxml>");

    /* Loop over all collections */
    for (int i = 0; i < numCollections; i++) {
        logger.debug("collection " + i);
        Document collection = FSXMLRequestHandler.instance().getNodePropertiesByType(collectionUri, 10, i, 1);

        String cId = collection.selectSingleNode("//collection/@id").getText();
        List<Node> presentations = collection.selectNodes("//presentation");

        if (domainid.equals("webtv")) {
            Document coll = FSXMLRequestHandler.instance().getNodeProperties(collectionUri + "/" + cId, 1,
                    false);/*from w ww. j ava2s.  c  o  m*/
            collectionstatus = coll.selectSingleNode("//properties/publicationstatus") == null ? ""
                    : coll.selectSingleNode("//properties/publicationstatus").getText();
            logger.debug("collectionstatus = " + collectionstatus);
        }

        /* loop over all presentations from collection */
        for (Iterator<Node> iter = presentations.iterator(); iter.hasNext();) {
            Element pres = (Element) iter.next();
            String collectionPresentationUri = collectionUri + "/" + cId + "/presentation/"
                    + pres.attributeValue("id");
            String presentationUri = pres.attributeValue("referid");
            logger.debug("presentation uri = " + presentationUri);

            /* since getnodeproperties is not possible on an id node do a get and convert that to a document */
            //Document presentation = FSXMLRequestHandler.instance().getNodePropertiesByType(presentationUri, 10, 0, 1);
            String pr = null;
            try {
                //FSXMLRequestHandler.instance().getNodeProperties(presentationUri, false);
                pr = FSXMLRequestHandler.instance().handleGET(presentationUri, null).getText();
            } catch (Exception e) {
            }

            Document presentation = null;
            try {
                presentation = DocumentHelper.parseText(pr);
            } catch (Exception e) {
            }

            String pId = presentation.selectSingleNode("//presentation/@id") == null ? ""
                    : presentation.selectSingleNode("//presentation/@id").getText();

            /*JHM hack */
            if (domainid.equals("jhm")) {
                String lockmode = presentation.selectSingleNode("//presentation/properties/lockmode") == null
                        ? ""
                        : presentation.selectSingleNode("//presentation/properties/lockmode").getText();
                if (lockmode.equals("Finished / Approved")) {
                    presentationResults(presentation);
                }
            } else if (!domainid.equals("lhwebtv") || pId.indexOf("p") > -1 || Integer.parseInt(pId) > 240) {
                presentationResults(presentation);
            }
        }
    }
    //add jhm keywords from config
    if (domainid.equals("jhm")) {
        Document document = DocumentHelper.createDocument();
        Element fsxml = document.addElement("fsxml");

        Document db = FSXMLRequestHandler.instance().getNodePropertiesByType(
                "/domain/jhm/config/presentation/filesystem/1/layer/4/database/1/keyword", 10, 0, 1000);

        logger.debug(db.asXML());

        List<Node> keywords = db.selectNodes("//keyword/properties/name");

        logger.debug("nr of keywords = " + keywords.size());

        /* loop over all keywords */
        for (Iterator<Node> iter = keywords.iterator(); iter.hasNext();) {
            Element e = (Element) iter.next();

            String keyword = e.getText();

            logger.debug("add " + keyword);

            Element menuitem = fsxml.addElement("item");
            Element properties = menuitem.addElement("properties");
            properties.addElement("keyword").addText(keyword);

            /* Unique chapter id */
            long timestamp = new Date().getTime();
            menuitem.addAttribute("id", String.valueOf(timestamp));
            try {
                Thread.sleep(1);
            } catch (InterruptedException exc) {
                logger.error("", exc);
            }
        }
        logger.debug("going to save " + document.asXML());
        FSXMLRequestHandler.instance().saveFsXml(indexUri, document.asXML(), "PUT", true);
    }
}

From source file:com.noterik.bart.fs.action.MomarQueueAction.java

License:Open Source License

@Override
public String run() {
    //if (1==1) return null; // not in use anymore in new momar 

    // parse request
    String requestBody = event.getRequestData();
    try {// w w  w . ja  v  a  2 s  .c  o  m
        Document doc = DocumentHelper.parseText(requestBody);
        Node node = doc.selectSingleNode("//properties/reencode");
        if (node == null || !node.getText().toLowerCase().equals(DO_REENCODE)) {
            logger.debug("Not reencoding");
            return null;
        } else {
            putInQueue();
        }
    } catch (Exception e) {
        logger.error(e);
    }

    return null;
}

From source file:com.noterik.bart.fs.action.MomarQueueAction.java

License:Open Source License

/**
 * Put the job in the queue/*from   www  .jav a2s  . c o  m*/
 */
private void putInQueue() {
    // TODO: get queueid
    // get domainid
    String uri = event.getUri();
    domainid = URIParser.getDomainFromUri(uri);

    // build jobs uri
    String queueUri = QUEUE_URI.replace("{domainid}", domainid).replace("{queueid}", queueid);

    // add raw 2 to high priority queue if available
    if (uri.substring(uri.lastIndexOf("/") + 1).equals("2")) {
        logger.debug("raw 2");
        String queueOverviewUri = QUEUE_OVERVIEW_URI.replace("{domainid}", domainid);

        Document queue = FSXMLRequestHandler.instance().getNodePropertiesByType(queueOverviewUri);
        if (queue.selectSingleNode("//queue[@id='" + HIGH + "']") != null) {
            logger.debug("high priority queue available");
            queueUri = QUEUE_URI.replace("{domainid}", domainid).replace("{queueid}", HIGH);
        }
    }

    // build xml
    StringBuffer xml = new StringBuffer("<fsxml>");
    xml.append("<properties/>");
    xml.append("<rawvideo id='1' referid='" + uri + "'/>");
    xml.append("</fsxml>");

    logger.debug("queue uri: " + queueUri);
    logger.debug("xml: " + xml.toString());

    // do request (internally)
    String response = FSXMLRequestHandler.instance().handlePOST(queueUri, xml.toString());
    logger.debug(response);

    // put job id into rawvideo
    try {
        Document doc = DocumentHelper.parseText(response);
        String jobUri = doc.valueOf("//properties/uri");
        FSXMLRequestHandler.instance().updateProperty(uri + "/properties/job", "job", jobUri, "PUT", false);
    } catch (DocumentException e) {
        logger.error("", e);
    }
}

From source file:com.noterik.bart.fs.action.NelsonQueueAction.java

License:Open Source License

@Override
public String run() {

    logger.debug("starting action");

    // parse request
    String requestBody = event.getRequestData();
    try {//from  w w w.  j  a v a 2 s .  c  o  m
        Document doc = DocumentHelper.parseText(requestBody);
        Node node = doc.selectSingleNode("//properties/redo");
        if (node == null || !node.getText().toLowerCase().equals(REDO)) {
            logger.debug("Not redoing the job");
            return null;
        } else {
            logger.debug("about to put in queue");
            putInQueue();
        }
    } catch (Exception e) {
        logger.error("", e);
    }

    return null;
}

From source file:com.noterik.bart.fs.action.PresentationIndexQueueAction.java

License:Open Source License

public String run() {
    String eventUri = event.getUri();
    domainid = URIParser.getDomainIdFromUri(eventUri);
    String presentationUri = getSubUri(eventUri, 6);

    logger.debug("event uri = " + eventUri + " domain = " + domainid + " presentation = " + presentationUri);

    /* Top prio are properties of a presentation itself */
    topPriority[0] = "/domain/" + domainid + "/user/[^/]+/presentation/[^/]+$";
    topPriority[1] = "/domain/" + domainid + "/user/[^/]+/collection/[^/]+$";
    topPriority[2] = "/domain/" + domainid + "/user/[^/]+/collection/[^/]+/presentation/*";

    int priority = getPriority(eventUri);

    //presentation added/updated/deleted
    if (type.equals("presentation")) {
        getRefers(presentationUri);// w w w .  j a  v  a 2 s.  c  o m
        putInQueue(eventUri, priority, presentationUri);
    } //collection presentation 
    else if (type.equals("collection") && collectiontype.equals("presentation")) {
        //presentation deleted from collection
        if (event.getMethod().equals("DELETE")) {
            //get refers to this collectionpresentation
            List<String> collectionRefers = FSXMLRequestHandler.instance()
                    .getReferParents(getSubUri(eventUri, 8));
            Iterator<String> referIterator = collectionRefers.iterator();
            String presentation = "";

            referedPresentations = new ArrayList<String>(collectionRefers.size());
            referedJobs = new ArrayList<String>(0);

            while (presentation.equals("") && referIterator.hasNext()) {
                String refer = referIterator.next();
                String[] parts = refer.substring(1).split("/");

                if (parts[4].equals("index")) {
                    String indexPresentation = getSubUri(refer, 8) + "/presentation/1";
                    Document indexPresentationContent = FSXMLRequestHandler.instance()
                            .getNodeProperties(indexPresentation, false);

                    presentation = indexPresentationContent.selectSingleNode("//presentation/@referid") == null
                            ? null
                            : indexPresentationContent.selectSingleNode("//presentation/@referid").getText();
                    logger.debug("presentation removed from collection = " + presentation);
                }
            }

            if (!presentation.equals("")) {
                List<String> jobRefers = FSXMLRequestHandler.instance().getReferParents(presentation);

                boolean jobExists = false;

                for (int i = 0; i < jobRefers.size(); i++) {
                    String jobRefer = jobRefers.get(i);
                    if (jobRefer != null) {
                        logger.debug("job refer = " + jobRefer);
                        domainid = URIParser.getDomainIdFromUri(eventUri);

                        String[] parts = jobRefer.substring(1).split("/");
                        if (parts[1].equals(domainid) && parts[5].equals("presentationindex")
                                && parts[6].equals("job")) {
                            String job = getSubUri(jobRefer, 8);
                            String jobContent = null;

                            logger.debug("job uri = " + job);

                            try {
                                jobContent = FSXMLRequestHandler.instance().handleGET(job, null).getText();
                            } catch (IOException e) {
                            }
                            Document jobProperties = null;
                            try {
                                jobProperties = DocumentHelper.parseText(jobContent);
                            } catch (Exception e) {
                            }

                            String jobId = job.substring(job.lastIndexOf("/") + 1);
                            int currentPriority = jobProperties.selectSingleNode("//priority") == null ? 2
                                    : Integer.parseInt(jobProperties.selectSingleNode("//priority").getText());
                            logger.debug("current prio " + currentPriority);
                            if (currentPriority > priority) {
                                updatePriority(priority, jobId);
                            }
                        }
                    }
                }
                if (!jobExists && !presentation.equals("")) {
                    String d = URIParser.getDomainIdFromUri(presentation);
                    queueUri = QUEUE_URI.replace("{domainid}", d);
                    addJob(presentation, eventUri);
                }
            }
        } else {
            //presentation added/updated to collection
            String collectionPresentationUri = getSubUri(eventUri, 8);
            Document refercontent = FSXMLRequestHandler.instance().getNodeProperties(collectionPresentationUri,
                    false);
            //logger.debug("refercontent = "+refercontent.asXML());
            String refer = refercontent.selectSingleNode("//presentation/@referid") == null ? "null"
                    : refercontent.selectSingleNode("//presentation/@referid").getText();

            //get only jobs referring to this presentation (refer), but discard other presentation refers
            if (refer != "null") {
                getRefers(refer);
                referedPresentations = new ArrayList<String>(1);
                referedPresentations.add(collectionPresentationUri);
                putInQueue(eventUri, priority, refer);
            }
        }
    } //collection properties added/updated/deleted, all presentations will be updated 
    else if (type.equals("collection")) {
        String collectionUri = presentationUri;
        getPresentations(collectionUri, priority);
    } //video screenshots 
    else if (type.equals("video")) {
        //get all refer that are presentation to this video
        List<String> videoRefers = FSXMLRequestHandler.instance().getReferParents(presentationUri);

        for (Iterator<String> referIterator = videoRefers.iterator(); referIterator.hasNext();) {
            String refer = referIterator.next();

            String[] parts = refer.substring(1).split("/");
            if (parts[4].equals("presentation") && parts[6].equals("videoplaylist")) {
                getRefers(getSubUri(refer, 6));
                putInQueue(getSubUri(refer, 6), 1, getSubUri(refer, 6));
            }
        }
    } else {
        //exception
    }
    return null;
}