Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

In this page you can find the example usage for org.dom4j Node selectSingleNode.

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:server.data.ObjectSystemData.java

License:Open Source License

/**
 * Set the Metadata on this object. Tries to parse the submitted string
 * and throws an exception if it is not valid XML.<br/>
 * Note: this parses the meta-xml into metasets and stores them as such.
 * This method will unlink existing metasets if they are missing from the metadata,
 * that is, you cannot submit partial metadata to setMetadata. You must set <em>all</em>
 * metadata if you use this method. (see addMetaset to add an individual metaset)
 *
 * @param metadata    the custom metadata
 * @param writePolicy the write policy - what to do if other items already reference a metaset.
 *//*ww w  .java2s.  c  om*/
public void setMetadata(String metadata, WritePolicy writePolicy) {
    try {
        MetasetService metasetService = new MetasetService();

        EntityManager em = HibernateSession.getLocalEntityManager();
        if (metadata == null || metadata.trim().length() == 0) {
            this.metadata = "<meta/>";
            for (OsdMetaset om : getOsdMetasets()) {
                em.remove(om);
            }
        } else {
            Document doc = ParamParser.parseXmlToDocument(metadata, "error.param.metadata");
            List<Node> sets = doc.selectNodes("//metaset");
            //            log.debug("found "+sets.size()+" metaset nodes in:\n"+metadata);
            if (sets.size() == 0) {
                this.metadata = metadata;
                if (osdMetasets.size() > 0) {
                    // delete obsolete metasets:
                    for (Metaset m : fetchMetasets()) {
                        new MetasetService().unlinkMetaset(this, m);
                    }
                }
                return;
            }

            Set<MetasetType> currentMetasetMap = new HashSet<MetasetType>();
            for (Metaset metaset : fetchMetasets()) {
                // create a set of the currently existing metasets.
                currentMetasetMap.add(metaset.getType());
            }

            for (Node metasetNode : sets) {
                String content = metasetNode.detach().asXML();
                String metasetTypeName = metasetNode.selectSingleNode("@type").getText();
                log.debug("metasetType: " + metasetTypeName);
                MetasetTypeDAO mtDao = daoFactory.getMetasetTypeDAO(em);
                MetasetType metasetType = mtDao.findByName(metasetTypeName);
                if (metasetType == null) {
                    throw new CinnamonException("error.unknown.metasetType", metasetTypeName);
                }
                metasetService.createOrUpdateMetaset(this, metasetType, content, writePolicy);
                currentMetasetMap.remove(metasetType);
            }
            for (MetasetType metasetType : currentMetasetMap) {
                // any metaset that was not found in the metadata parameter will be deleted.                    
                metasetService.unlinkMetaset(this, this.fetchMetaset(metasetType.getName())); // somewhat convoluted.
            }
        }
    } catch (Exception e) {
        log.debug("failed to add metadata:", e);
        throw new RuntimeException(e);
    }
    // remove legacy metadata:
    this.metadata = "<meta />";
}

From source file:server.Folder.java

License:Open Source License

/**
 * Set the Metadata on this item. Tries to parse the submitted string
 * and throws an exception if it is not valid XML.<br/>
 * Note: this parses the meta-xml into metasets and stores them as such.
 * This method will unlink existing metasets if they are missing from the metadata,
 * that is, you cannot submit partial metadata to setMetadata. You must set <em>all</em>
 * metadata if you use this method. (see addMetaset to add an individual metaset) 
 * @param metadata the custom metadata/*w w  w  . ja  v  a 2s .c o  m*/
 * @param writePolicy the WritePolicy - how to treat existing metasets.
 */
public void setMetadata(String metadata, WritePolicy writePolicy) {
    EntityManager em = HibernateSession.getLocalEntityManager();
    if (metadata == null || metadata.trim().length() == 0) {
        this.metadata = "<meta/>";
        for (FolderMetaset fm : getFolderMetasets()) {
            em.remove(fm);
        }
    } else {
        MetasetService metasetService = new MetasetService();
        Document doc = ParamParser.parseXmlToDocument(metadata, "error.param.metadata");
        List<Node> sets = doc.selectNodes("//metaset");
        if (sets.size() == 0) {
            this.metadata = metadata;
            if (folderMetasets.size() > 0) {
                // delete obsolete metasets:
                for (Metaset m : fetchMetasets()) {
                    new MetasetService().unlinkMetaset(this, m);
                }
            }
            return;
        }

        Set<MetasetType> currentMetasetMap = new HashSet<MetasetType>();
        for (Metaset metaset : fetchMetasets()) {
            // create a set of the currently existing metasets.
            currentMetasetMap.add(metaset.getType());
        }

        for (Node metasetNode : sets) {
            String content = metasetNode.detach().asXML();
            String metasetTypeName = metasetNode.selectSingleNode("@type").getText();
            log.debug("metasetType: " + metasetTypeName);
            MetasetTypeDAO mtDao = daoFactory.getMetasetTypeDAO(em);
            MetasetType metasetType = mtDao.findByName(metasetTypeName);
            if (metasetType == null) {
                throw new CinnamonException("error.unknown.metasetType", metasetTypeName);
            }
            metasetService.createOrUpdateMetaset(this, metasetType, content, writePolicy);
            currentMetasetMap.remove(metasetType);
        }
        for (MetasetType metasetType : currentMetasetMap) {
            // any metaset that was not found in the metadata parameter will be deleted.                    
            metasetService.unlinkMetaset(this, this.fetchMetaset(metasetType.getName())); // somewhat convoluted.
        }
    }
    // remove legacy metadata
    this.metadata = "<meta />";
}

From source file:service.SerTimerHandler.java

License:Open Source License

public static void deleteOldTimer() {
    Element root = getTimerDocument().getRootElement();
    List nodes = SerXPathHandling.getNodes("/timerList/localTimer/stopTime", getTimerDocument());
    boolean edited = false;

    for (int i = 0; i < nodes.size(); i++) {
        Node node = (Node) nodes.get(i);
        long localTimerStop = Long.parseLong(node.getText());
        long now = new GregorianCalendar().getTimeInMillis();

        if (now > localTimerStop) {
            Node localTimer = node.getParent();
            Element repeatId = (Element) localTimer.selectSingleNode("mainTimer/eventRepeatId");
            if (repeatId != null && repeatId.getText().equals("0")) {
                root.remove(localTimer);
                edited = true;//  w  w  w  .  ja  v  a  2 s.c  om
            }
        }
    }
    if (edited) {
        try {
            SerXMLHandling.saveXMLFile(new File(timerFile), getTimerDocument());
        } catch (IOException e) {
            Logger.getLogger("SerTimerHandler").error(e.getMessage());
        }
    }
}

From source file:service.SerTimerHandler.java

License:Open Source License

private static void editOldTimer(BOLocalTimer timer) {
    Node timerNode = timer.getTimerNode();
    timerNode.selectSingleNode("ac3ReplaceStereo").setText(Boolean.toString(timer.isAc3ReplaceStereo()));
    timerNode.selectSingleNode("description").setText(timer.getDescription());
    timerNode.selectSingleNode("dirPattern").setText(timer.getDirPattern());
    timerNode.selectSingleNode("filePattern").setText(timer.getFilePattern());
    timerNode.selectSingleNode("jgrabberStreamType").setText(timer.getJgrabberStreamType());
    timerNode.selectSingleNode("vlcStreamType").setText(timer.getVlcStreamType());
    timerNode.selectSingleNode("recordAllPids").setText(Boolean.toString(timer.isRecordAllPids()));
    timerNode.selectSingleNode("recordVtxt").setText(Boolean.toString(timer.isRecordVtxt()));
    timerNode.selectSingleNode("savePath").setText(timer.getSavePath());
    timerNode.selectSingleNode("shutdownAfterRecord").setText(Boolean.toString(timer.isShutdownAfterRecord()));
    timerNode.selectSingleNode("startPX")
            .setText(Boolean.toString(timer.getAfterRecordOptions().isUseProjectX()));
    timerNode.selectSingleNode("startMplex")
            .setText(Boolean.toString(timer.getAfterRecordOptions().isUseMplex()));
    timerNode.selectSingleNode("mplexOption")
            .setText(Integer.toString(timer.getAfterRecordOptions().getMplexOption()));
    timerNode.selectSingleNode("startTime").setText(timer.getMainTimer().getLongStartTime());
    timerNode.selectSingleNode("stopTime").setText(timer.getMainTimer().getLongStopTime());
    timerNode.selectSingleNode("stereoReplaceAc3").setText(Boolean.toString(timer.isStereoReplaceAc3()));
    timerNode.selectSingleNode("stopPlaybackAtRecord")
            .setText(Boolean.toString(timer.isStopPlaybackAtRecord()));
    timerNode.selectSingleNode("storeEpg").setText(Boolean.toString(timer.isStoreEPG()));
    timerNode.selectSingleNode("storeLogAfterRecord").setText(Boolean.toString(timer.isStoreLogAfterRecord()));
    timerNode.selectSingleNode("streamingEngine").setText(Integer.toString(timer.getStreamingEngine()));
    timerNode.selectSingleNode("udrecOptions").setText(timer.getUdrecOptions().toString());
    timerNode.selectSingleNode("udrecStreamType").setText(timer.getUdrecStreamType());
    timerNode.selectSingleNode("local").setText(Boolean.toString(timer.isLocal()));

    if (timer.isLocal()) {
        //          BOTimer
        Element mainTimer = (Element) timerNode.selectSingleNode("mainTimer");
        mainTimer.selectSingleNode("startMainTimer").setText(timer.getMainTimer().getLongStartTime());
        mainTimer.selectSingleNode("stopMainTimer").setText(timer.getMainTimer().getLongStopTime());
        mainTimer.selectSingleNode("announceMainTimer").setText(timer.getMainTimer().getAnnounceTime());
        mainTimer.selectSingleNode("channelId").setText(timer.getMainTimer().getChannelId());
        mainTimer.selectSingleNode("eventTypeId").setText(timer.getMainTimer().getEventTypeId());
        mainTimer.selectSingleNode("eventRepeatId").setText(timer.getMainTimer().getEventRepeatId());
        mainTimer.selectSingleNode("repeatCount").setText(timer.getMainTimer().getRepeatCount());
        mainTimer.selectSingleNode("senderName").setText(timer.getMainTimer().getSenderName());
    }//from  w w w .  j a  v a2 s .c  om
    try {
        SerXMLHandling.saveXMLFile(new File(timerFile), getTimerDocument());
    } catch (IOException e) {
        Logger.getLogger("SerTimerHandler").error(e.getMessage());
    }
}

From source file:service.SerTimerHandler.java

License:Open Source License

private static BOTimer buildMainTimer(Node mainTimerNode) {
    BOTimer botimer = new BOTimer();

    botimer.eventTypeId = mainTimerNode.selectSingleNode("eventTypeId").getText();
    botimer.eventRepeatId = mainTimerNode.selectSingleNode("eventRepeatId").getText();
    botimer.repeatCount = mainTimerNode.selectSingleNode("repeatCount").getText();
    botimer.channelId = mainTimerNode.selectSingleNode("channelId").getText();
    botimer.setSenderName(mainTimerNode.selectSingleNode("senderName").getText());
    botimer.announceTime = mainTimerNode.selectSingleNode("announceMainTimer").getText();

    long startMillis = Long.parseLong(mainTimerNode.selectSingleNode("startMainTimer").getText());
    GregorianCalendar startTime = new GregorianCalendar();
    startTime.setTimeInMillis(startMillis);

    long stopMillis = Long.parseLong(mainTimerNode.selectSingleNode("stopMainTimer").getText());
    GregorianCalendar stopTime = new GregorianCalendar();
    stopTime.setTimeInMillis(stopMillis);

    botimer.unformattedStartTime = startTime;
    botimer.unformattedStopTime = stopTime;

    return botimer;
}

From source file:service.SerTimerHandler.java

License:Open Source License

public static BOLocalTimer buildLocalTimer(Node timerNode, BOLocalTimer localTimer) {
    BOAfterRecordOptions afterRecordOptions = new BOAfterRecordOptions();
    afterRecordOptions.setUseProjectX(timerNode.selectSingleNode("startPX").getText().equals("true"));
    afterRecordOptions.setUseMplex(timerNode.selectSingleNode("startMplex").getText().equals("true"));
    afterRecordOptions.setMplexOption(Integer.parseInt(timerNode.selectSingleNode("mplexOption").getText()));
    localTimer.setAfterRecordOptions(afterRecordOptions);

    localTimer.setAc3ReplaceStereo(timerNode.selectSingleNode("ac3ReplaceStereo").getText().equals("true"));
    localTimer.setDescription(timerNode.selectSingleNode("description").getText());
    localTimer.setDirPattern(timerNode.selectSingleNode("dirPattern").getText());
    localTimer.setFilePattern(timerNode.selectSingleNode("filePattern").getText());
    localTimer.setJgrabberStreamType(timerNode.selectSingleNode("jgrabberStreamType").getText());
    localTimer.setVlcStreamType(timerNode.selectSingleNode("vlcStreamType").getText());
    localTimer.setRecordAllPids(timerNode.selectSingleNode("recordAllPids").getText().equals("true"));
    localTimer.setRecordVtxt(timerNode.selectSingleNode("recordVtxt").getText().equals("true"));
    localTimer.setSavePath(timerNode.selectSingleNode("savePath").getText());
    localTimer//w w w. j  av a  2s  .com
            .setShutdownAfterRecord(timerNode.selectSingleNode("shutdownAfterRecord").getText().equals("true"));
    localTimer.setStartTime(Long.parseLong(timerNode.selectSingleNode("startTime").getText()));
    localTimer.setStopTime(Long.parseLong(timerNode.selectSingleNode("stopTime").getText()));
    localTimer.setStereoReplaceAc3(timerNode.selectSingleNode("stereoReplaceAc3").getText().equals("true"));
    localTimer.setStopPlaybackAtRecord(
            timerNode.selectSingleNode("stopPlaybackAtRecord").getText().equals("true"));
    localTimer.setStoreEPG(timerNode.selectSingleNode("storeEpg").getText().equals("true"));
    localTimer
            .setStoreLogAfterRecord(timerNode.selectSingleNode("storeLogAfterRecord").getText().equals("true"));
    localTimer.setStreamingEngine(Integer.parseInt(timerNode.selectSingleNode("streamingEngine").getText()));
    localTimer.setUdrecOptions(
            new BOUdrecOptions(timerNode.selectSingleNode("udrecOptions").getText().split(" ")));
    localTimer.setUdrecStreamType(timerNode.selectSingleNode("udrecStreamType").getText());
    localTimer.setLocal(timerNode.selectSingleNode("local").getText().equals("true"));
    localTimer.setTimerNode(timerNode);
    return localTimer;
}

From source file:service.XML.SerXPathHandling.java

License:Open Source License

public static Node getFirstDescendentNode(Node node) {
    return node.selectSingleNode("descendant::node()[1]");
}

From source file:service.XML.SerXPathHandling.java

License:Open Source License

public static Node getLastDescendentNode(Node node) {
    return node.selectSingleNode("descendant::node()[last]");
}

From source file:service.XML.SerXPathHandling.java

License:Open Source License

public static Node getFirstFollowingNode(Node node) {
    return node.selectSingleNode("following-sibling::node()[1]/*");
}

From source file:service.XML.SerXPathHandling.java

License:Open Source License

public static Node getFirstPrecedingNode(Node node) {
    return node.selectSingleNode("preceding-sibling::node()[1]/*");
}