Example usage for org.dom4j Node getDocument

List of usage examples for org.dom4j Node getDocument

Introduction

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

Prototype

Document getDocument();

Source Link

Document

getDocument returns the Document that this Node is part of if this node supports the parent relationship.

Usage

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java

/**
 * Update the document for DAMS ARK ID/*from   ww  w .j a  v  a  2s  . c om*/
 * @param doc
 * @param record
 * @param title
 * @throws Exception
 */
private void updateDocument(Document doc, Node record, String field, String title, Map<String, String> props)
        throws Exception {
    // Skip if the record detached
    if (record.getDocument() == null)
        return;

    // Subject, Authority records use mads:authoritativeLabel
    Node aboutAttr = record.selectSingleNode("@rdf:about");
    String srcUri = aboutAttr.getStringValue();
    //String nName = record.getName();
    String xPath = record.getPath();
    String elemName = xPath.substring(xPath.lastIndexOf("/") + 1);

    // MADSScheme model: MadsScheme
    if (elemName.endsWith("MADSScheme"))
        elemName = elemName.replace("MADSScheme", "Scheme");
    String modelName = (elemName.substring(0, 1).toUpperCase() + elemName.substring(1)).replace(":", "");
    String nKey = INFO_MODEL_PREFIX + modelName + "::" + title;
    if (props != null) {
        for (Iterator<String> it = props.keySet().iterator(); it.hasNext();) {
            String iKey = it.next();
            nKey += "_" + iKey + "::" + props.get(iKey);
        }
    }
    String oid = idsMap.get(nKey);
    // Retrieve the record
    if (oid == null) {
        /*Map<String, String> props = null;
        if(nName.endsWith(COPYRIGHT)){
           props = copyrightProperties(record);
        }*/
        oid = lookupRecord(damsClient, field, title, modelName, props);

        if (oid == null) {
            // Create the record
            oid = getNewId();
            aboutAttr.setText(oid);
            idsMap.put(nKey, oid);
            log.info("Found new redord " + srcUri + " (" + oid + ": " + field + " -- " + title);

        } else {
            // Record found. Add linking, remove it.
            toResourceLinking(oid, record);
        }
    } else {
        // Record added. Add linking, remove it.
        toResourceLinking(oid, record);
    }
    updateReference(doc, srcUri, oid);
}

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportTsHandler.java

/**
 * Update the document for DAMS ARK ID/*from  w  ww  .j  av a 2s  .  co  m*/
 * @param doc
 * @param record
 * @param title
 * @throws Exception
 */
private void updateDocument(Document doc, Node record, String field, String title, Map<String, String> props)
        throws Exception {
    // Skip if the record detached
    if (record.getDocument() == null)
        return;

    // Subject, Authority records use mads:authoritativeLabel
    Node aboutAttr = record.selectSingleNode("@rdf:about");
    String srcUri = aboutAttr.getStringValue();
    //String nName = record.getName();
    String xPath = record.getPath();
    String elemName = xPath.substring(xPath.lastIndexOf("/") + 1);

    String modelName = elemName;
    String nKey = INFO_MODEL_PREFIX + modelName + "::" + title;
    if (props != null) {
        for (Iterator<String> it = props.keySet().iterator(); it.hasNext();) {
            String iKey = it.next();
            nKey += "_" + iKey + "::" + props.get(iKey);
        }
    }
    String oid = idsMap.get(nKey);

    if (oid == null) {
        //Lookup records from the triplestore, matching the required properties that are null or empty.
        List<Map<String, String>> oids = lookupRecordsFromTs(field, title, "\"" + modelName + "\"", props);
        if (oids != null && oids.size() > 0) {

            String propName = null;
            String propValue = null;
            Document recDoc = null;
            Node cNode = null;
            if (props != null) {
                List<Map<String, String>> oidsCopy = new ArrayList<Map<String, String>>();
                oidsCopy.addAll(oids);
                for (int i = 0; i < oidsCopy.size(); i++) {

                    Collection<String> propValues = props.values();
                    Map<String, String> resolution = oidsCopy.get(i);
                    String rid = resolution.values().iterator().next();
                    if (rid.startsWith("http")) {
                        if (propValues.contains(null)) {

                            recDoc = damsClient.getRecord(rid);
                            for (Iterator<String> it = props.keySet().iterator(); it.hasNext();) {
                                propName = it.next();
                                propValue = props.get(propName);
                                // Test for the nodes for null properties and remove it from the result
                                if (propValue == null) {
                                    int idx = propName.indexOf("/", 1);
                                    if (idx > 0)
                                        cNode = recDoc.selectSingleNode("//" + modelName + "/" + propName);
                                    else
                                        cNode = recDoc.selectSingleNode("//" + modelName + "/" + propName);

                                    if (cNode != null) {
                                        oids.remove(resolution);
                                        break;
                                    }
                                }
                            }
                        }
                    } else // removed internal BlankNodes from the results
                        oids.remove(resolution);
                }
            }

            if (oids.size() > 0) {
                oid = oids.get(0).values().iterator().next();
                if (oids.size() > 1) {
                    String duids = "";
                    for (Iterator<Map<String, String>> it = oids.iterator(); it.hasNext();)
                        duids += (duids.length() > 0 ? ", " : "") + it.next().values().iterator().next();

                    log.warn("Duplicated records found for " + title + " (" + field + "): " + duids + ".");
                }
            }

        }

        if (oid == null) {
            // Create the record
            oid = getNewId();
            aboutAttr.setText(oid);
        } else {
            // Record found. Add to the map, link and remove it.
            toResourceLinking(oid, record);
        }
        idsMap.put(nKey, oid);
    } else {
        // Record added. Add linking, remove it.
        toResourceLinking(oid, record);
    }
    updateReference(doc, srcUri, oid);
}

From source file:edu.umd.cs.findbugs.PluginLoader.java

License:Open Source License

private void loadPluginComponents() throws PluginException {
    Document pluginDescriptor = getPluginDescriptor();
    List<Document> messageCollectionList = getMessageDocuments();
    List<Node> cloudNodeList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/Cloud");
    for (Node cloudNode : cloudNodeList) {

        String cloudClassname = cloudNode.valueOf("@cloudClass");
        String cloudId = cloudNode.valueOf("@id");
        String usernameClassname = cloudNode.valueOf("@usernameClass");
        boolean onlineStorage = Boolean.valueOf(cloudNode.valueOf("@onlineStorage"));
        String propertiesLocation = cloudNode.valueOf("@properties");
        boolean disabled = Boolean.valueOf(cloudNode.valueOf("@disabled"))
                && !cloudId.equals(CloudFactory.DEFAULT_CLOUD);
        if (disabled) {
            continue;
        }//from  w w w.  j a  v  a  2s .  co m
        boolean hidden = Boolean.valueOf(cloudNode.valueOf("@hidden"))
                && !cloudId.equals(CloudFactory.DEFAULT_CLOUD);

        Class<? extends Cloud> cloudClass = getClass(classLoader, cloudClassname, Cloud.class);

        Class<? extends NameLookup> usernameClass = getClass(classLoader, usernameClassname, NameLookup.class);
        Node cloudMessageNode = findMessageNode(messageCollectionList,
                "/MessageCollection/Cloud[@id='" + cloudId + "']",
                "Missing Cloud description for cloud " + cloudId);
        String description = getChildText(cloudMessageNode, "Description").trim();
        String details = getChildText(cloudMessageNode, "Details").trim();
        PropertyBundle properties = new PropertyBundle();
        if (propertiesLocation != null && propertiesLocation.length() > 0) {
            URL properiesURL = classLoader.getResource(propertiesLocation);
            if (properiesURL == null) {
                continue;
            }
            properties.loadPropertiesFromURL(properiesURL);
        }
        List<Node> propertyNodes = XMLUtil.selectNodes(cloudNode, "Property");
        for (Node node : propertyNodes) {
            String key = node.valueOf("@key");
            String value = node.getText().trim();
            properties.setProperty(key, value);
        }

        CloudPlugin cloudPlugin = new CloudPluginBuilder().setFindbugsPluginId(plugin.getPluginId())
                .setCloudid(cloudId).setClassLoader(classLoader).setCloudClass(cloudClass)
                .setUsernameClass(usernameClass).setHidden(hidden).setProperties(properties)
                .setDescription(description).setDetails(details).setOnlineStorage(onlineStorage)
                .createCloudPlugin();
        plugin.addCloudPlugin(cloudPlugin);
    }

    // Create PluginComponents
    try {
        List<Node> componentNodeList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/PluginComponent");
        for (Node componentNode : componentNodeList) {
            @DottedClassName
            String componentKindname = componentNode.valueOf("@componentKind");
            if (componentKindname == null) {
                throw new PluginException(
                        "Missing @componentKind for " + plugin.getPluginId() + " loaded from " + loadedFrom);
            }
            @DottedClassName
            String componentClassname = componentNode.valueOf("@componentClass");
            if (componentClassname == null) {
                throw new PluginException("Missing @componentClassname for " + plugin.getPluginId()
                        + " loaded from " + loadedFrom);
            }
            String componentId = componentNode.valueOf("@id");
            if (componentId == null) {
                throw new PluginException(
                        "Missing @id for " + plugin.getPluginId() + " loaded from " + loadedFrom);
            }

            try {
                String propertiesLocation = componentNode.valueOf("@properties");
                boolean disabled = Boolean.valueOf(componentNode.valueOf("@disabled"));

                Node filterMessageNode = findMessageNode(messageCollectionList,
                        "/MessageCollection/PluginComponent[@id='" + componentId + "']",
                        "Missing Cloud description for PluginComponent " + componentId);
                String description = getChildText(filterMessageNode, "Description").trim();
                String details = getChildText(filterMessageNode, "Details").trim();
                PropertyBundle properties = new PropertyBundle();
                if (propertiesLocation != null && propertiesLocation.length() > 0) {
                    URL properiesURL = classLoaderForResources.getResource(propertiesLocation);
                    if (properiesURL == null) {
                        AnalysisContext.logError("Could not load properties for " + plugin.getPluginId()
                                + " component " + componentId + " from " + propertiesLocation);
                        continue;
                    }
                    properties.loadPropertiesFromURL(properiesURL);
                }
                List<Node> propertyNodes = XMLUtil.selectNodes(componentNode, "Property");
                for (Node node : propertyNodes) {
                    String key = node.valueOf("@key");
                    String value = node.getText();
                    properties.setProperty(key, value);
                }

                Class<?> componentKind = classLoader.loadClass(componentKindname);
                loadComponentPlugin(plugin, componentKind, componentClassname, componentId, disabled,
                        description, details, properties);
            } catch (RuntimeException e) {
                AnalysisContext.logError("Unable to load ComponentPlugin " + componentId + " : "
                        + componentClassname + " implementing " + componentKindname, e);
            }
        }

        // Create FindBugsMains

        if (!FindBugs.isNoMains()) {
            List<Node> findBugsMainList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/FindBugsMain");
            for (Node main : findBugsMainList) {
                String className = main.valueOf("@class");
                if (className == null) {
                    throw new PluginException("Missing @class for FindBugsMain in plugin" + plugin.getPluginId()
                            + " loaded from " + loadedFrom);
                }
                String cmd = main.valueOf("@cmd");
                if (cmd == null) {
                    throw new PluginException("Missing @cmd for for FindBugsMain in plugin "
                            + plugin.getPluginId() + " loaded from " + loadedFrom);
                }
                String kind = main.valueOf("@kind");
                boolean analysis = Boolean.valueOf(main.valueOf("@analysis"));
                Element mainMessageNode = (Element) findMessageNode(messageCollectionList,
                        "/MessageCollection/FindBugsMain[@cmd='" + cmd
                        // + " and @class='" + className
                                + "']/Description",
                        "Missing FindBugsMain description for cmd " + cmd);
                String description = mainMessageNode.getTextTrim();
                try {
                    Class<?> mainClass = classLoader.loadClass(className);
                    plugin.addFindBugsMain(mainClass, cmd, description, kind, analysis);
                } catch (Exception e) {
                    String msg = "Unable to load FindBugsMain " + cmd + " : " + className + " in plugin "
                            + plugin.getPluginId() + " loaded from " + loadedFrom;
                    PluginException e2 = new PluginException(msg, e);
                    AnalysisContext.logError(msg, e2);
                }
            }
        }

        List<Node> detectorNodeList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/Detector");
        int detectorCount = 0;
        for (Node detectorNode : detectorNodeList) {
            String className = detectorNode.valueOf("@class");
            String speed = detectorNode.valueOf("@speed");
            String disabled = detectorNode.valueOf("@disabled");
            String reports = detectorNode.valueOf("@reports");
            String requireJRE = detectorNode.valueOf("@requirejre");
            String hidden = detectorNode.valueOf("@hidden");
            if (speed == null || speed.length() == 0) {
                speed = "fast";
            }
            // System.out.println("Found detector: class="+className+", disabled="+disabled);

            // Create DetectorFactory for the detector
            Class<?> detectorClass = null;
            if (!FindBugs.isNoAnalysis()) {
                detectorClass = classLoader.loadClass(className);

                if (!Detector.class.isAssignableFrom(detectorClass)
                        && !Detector2.class.isAssignableFrom(detectorClass)) {
                    throw new PluginException(
                            "Class " + className + " does not implement Detector or Detector2");
                }
            }
            DetectorFactory factory = new DetectorFactory(plugin, className, detectorClass,
                    !"true".equals(disabled), speed, reports, requireJRE);
            if (Boolean.valueOf(hidden).booleanValue()) {
                factory.setHidden(true);
            }
            factory.setPositionSpecifiedInPluginDescriptor(detectorCount++);
            plugin.addDetectorFactory(factory);

            // Find Detector node in one of the messages files,
            // to get the detail HTML.
            Node node = findMessageNode(messageCollectionList,
                    "/MessageCollection/Detector[@class='" + className + "']/Details",
                    "Missing Detector description for detector " + className);

            Element details = (Element) node;
            String detailHTML = details.getText();
            StringBuilder buf = new StringBuilder();
            buf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
            buf.append("<HTML><HEAD><TITLE>Detector Description</TITLE></HEAD><BODY>\n");
            buf.append(detailHTML);
            buf.append("</BODY></HTML>\n");
            factory.setDetailHTML(buf.toString());
        }
    } catch (ClassNotFoundException e) {
        throw new PluginException("Could not instantiate detector class: " + e, e);
    }

    // Create ordering constraints
    Node orderingConstraintsNode = pluginDescriptor.selectSingleNode("/FindbugsPlugin/OrderingConstraints");
    if (orderingConstraintsNode != null) {
        // Get inter-pass and intra-pass constraints
        List<Element> elements = XMLUtil.selectNodes(orderingConstraintsNode, "./SplitPass|./WithinPass");
        for (Element constraintElement : elements) {
            // Create the selectors which determine which detectors are
            // involved in the constraint
            DetectorFactorySelector earlierSelector = getConstraintSelector(constraintElement, plugin,
                    "Earlier");
            DetectorFactorySelector laterSelector = getConstraintSelector(constraintElement, plugin, "Later");

            // Create the constraint
            DetectorOrderingConstraint constraint = new DetectorOrderingConstraint(earlierSelector,
                    laterSelector);

            // Keep track of which constraints are single-source
            constraint.setSingleSource(earlierSelector instanceof SingleDetectorFactorySelector);

            // Add the constraint to the plugin
            if ("SplitPass".equals(constraintElement.getName())) {
                plugin.addInterPassOrderingConstraint(constraint);
            } else {
                plugin.addIntraPassOrderingConstraint(constraint);
            }
        }
    }

    // register global Category descriptions

    List<Node> categoryNodeListGlobal = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/BugCategory");
    for (Node categoryNode : categoryNodeListGlobal) {
        String key = categoryNode.valueOf("@category");
        if ("".equals(key)) {
            throw new PluginException("BugCategory element with missing category attribute");
        }
        BugCategory bc = plugin.addOrCreateBugCategory(key);

        boolean hidden = Boolean.valueOf(categoryNode.valueOf("@hidden"));
        if (hidden) {
            bc.setHidden(hidden);
        }
    }

    for (Document messageCollection : messageCollectionList) {
        List<Node> categoryNodeList = XMLUtil.selectNodes(messageCollection, "/MessageCollection/BugCategory");
        if (DEBUG) {
            System.out.println("found " + categoryNodeList.size() + " categories in " + plugin.getPluginId());
        }
        for (Node categoryNode : categoryNodeList) {
            String key = categoryNode.valueOf("@category");
            if ("".equals(key)) {
                throw new PluginException("BugCategory element with missing category attribute");
            }
            BugCategory bc = plugin.addOrCreateBugCategory(key);
            String shortDesc = getChildText(categoryNode, "Description");
            bc.setShortDescription(shortDesc);
            try {
                String abbrev = getChildText(categoryNode, "Abbreviation");
                if (bc.getAbbrev() == null) {
                    bc.setAbbrev(abbrev);
                    if (DEBUG) {
                        System.out.println("category " + key + " abbrev -> " + abbrev);
                    }
                } else if (DEBUG) {
                    System.out.println(
                            "rejected abbrev '" + abbrev + "' for category " + key + ": " + bc.getAbbrev());
                }
            } catch (PluginException pe) {
                if (DEBUG) {
                    System.out.println("missing Abbreviation for category " + key + "/" + shortDesc);
                    // do nothing else -- Abbreviation is required, but handle
                    // its omission gracefully
                }
            }
            try {
                String details = getChildText(categoryNode, "Details");
                if (bc.getDetailText() == null) {
                    bc.setDetailText(details);
                    if (DEBUG) {
                        System.out.println("category " + key + " details -> " + details);
                    }
                } else if (DEBUG) {
                    System.out.println("rejected details [" + details + "] for category " + key + ": ["
                            + bc.getDetailText() + ']');
                }
            } catch (PluginException pe) {
                // do nothing -- LongDescription is optional
            }

        }
    }

    // Create BugPatterns
    List<Node> bugPatternNodeList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/BugPattern");
    for (Node bugPatternNode : bugPatternNodeList) {
        String type = bugPatternNode.valueOf("@type");
        String abbrev = bugPatternNode.valueOf("@abbrev");
        String category = bugPatternNode.valueOf("@category");
        boolean experimental = Boolean.parseBoolean(bugPatternNode.valueOf("@experimental"));

        // Find the matching element in messages.xml (or translations)
        String query = "/MessageCollection/BugPattern[@type='" + type + "']";
        Node messageNode = findMessageNode(messageCollectionList, query,
                "messages.xml missing BugPattern element for type " + type);
        Node bugsUrlNode = messageNode.getDocument()
                .selectSingleNode("/MessageCollection/Plugin/" + (experimental ? "AllBugsUrl" : "BugsUrl"));

        String bugsUrl = bugsUrlNode == null ? null : bugsUrlNode.getText();

        String shortDesc = getChildText(messageNode, "ShortDescription");
        String longDesc = getChildText(messageNode, "LongDescription");
        String detailText = getChildText(messageNode, "Details");
        int cweid = 0;
        try {
            String cweString = bugPatternNode.valueOf("@cweid");
            if (cweString.length() > 0) {
                cweid = Integer.parseInt(cweString);
            }
        } catch (RuntimeException e) {
            assert true; // ignore
        }

        BugPattern bugPattern = new BugPattern(type, abbrev, category, experimental, shortDesc, longDesc,
                detailText, bugsUrl, cweid);

        try {
            String deprecatedStr = bugPatternNode.valueOf("@deprecated");
            boolean deprecated = deprecatedStr.length() > 0 && Boolean.valueOf(deprecatedStr).booleanValue();
            if (deprecated) {
                bugPattern.setDeprecated(deprecated);
            }
        } catch (RuntimeException e) {
            assert true; // ignore
        }

        plugin.addBugPattern(bugPattern);

    }

    // Create BugCodes
    Set<String> definedBugCodes = new HashSet<String>();
    for (Document messageCollection : messageCollectionList) {
        List<Node> bugCodeNodeList = XMLUtil.selectNodes(messageCollection, "/MessageCollection/BugCode");
        for (Node bugCodeNode : bugCodeNodeList) {
            String abbrev = bugCodeNode.valueOf("@abbrev");
            if ("".equals(abbrev)) {
                throw new PluginException("BugCode element with missing abbrev attribute");
            }
            if (definedBugCodes.contains(abbrev)) {
                continue;
            }
            String description = bugCodeNode.getText();

            String query = "/FindbugsPlugin/BugCode[@abbrev='" + abbrev + "']";
            Node fbNode = pluginDescriptor.selectSingleNode(query);
            int cweid = 0;
            if (fbNode != null) {
                try {
                    cweid = Integer.parseInt(fbNode.valueOf("@cweid"));
                } catch (RuntimeException e) {
                    assert true; // ignore
                }
            }
            BugCode bugCode = new BugCode(abbrev, description, cweid);
            plugin.addBugCode(bugCode);
            definedBugCodes.add(abbrev);
        }

    }

    // If an engine registrar is specified, make a note of its classname
    Node node = pluginDescriptor.selectSingleNode("/FindbugsPlugin/EngineRegistrar");
    if (node != null) {
        String engineClassName = node.valueOf("@class");
        if (engineClassName == null) {
            throw new PluginException("EngineRegistrar element with missing class attribute");
        }

        try {
            Class<?> engineRegistrarClass = classLoader.loadClass(engineClassName);
            if (!IAnalysisEngineRegistrar.class.isAssignableFrom(engineRegistrarClass)) {
                throw new PluginException(
                        engineRegistrarClass + " does not implement IAnalysisEngineRegistrar");
            }

            plugin.setEngineRegistrarClass(
                    engineRegistrarClass.<IAnalysisEngineRegistrar>asSubclass(IAnalysisEngineRegistrar.class));
        } catch (ClassNotFoundException e) {
            throw new PluginException("Could not instantiate analysis engine registrar class: " + e, e);
        }

    }
    try {
        URL bugRankURL = getResource(BugRanker.FILENAME);

        if (bugRankURL == null) {
            // see
            // https://sourceforge.net/tracker/?func=detail&aid=2816102&group_id=96405&atid=614693
            // plugin can not have bugrank.txt. In this case, an empty
            // bugranker will be created
            if (DEBUG) {
                System.out.println("No " + BugRanker.FILENAME + " for plugin " + plugin.getPluginId());
            }
        }
        BugRanker ranker = new BugRanker(bugRankURL);
        plugin.setBugRanker(ranker);
    } catch (IOException e) {
        throw new PluginException("Couldn't parse \"" + BugRanker.FILENAME + "\"", e);
    }
}

From source file:org.hudsonci.xpath.impl.Dom2Dom.java

License:Open Source License

/**
 * Map a org.dom4j.Node to a org.w3c.dom.Node, including its ancestors
 * and descendents.// w w  w  . j ava2s .  c  om
 * 
 * Once mapped, originalNode will return the original Node
 * corresponding to any mapped w3c Node.
 * 
 * @see #getOriginalNode(org.w3c.dom.Node)
 * 
 * @param node to convert
 * @param trim true if whitespace is to be trimmed from any consecutive
 * sequence of Text nodes; false if whitespace retained
 * @return W3C DOM Node corresponding to node argument
 * @throws XPathException 
 */
public org.w3c.dom.Node dom2DomX(Node node, boolean trim) throws XPathException {

    trimText = trim;

    // The first time we see a context node we create a w3c Document
    // for XPath processing. The important thing is that equals
    // works correctly.

    Document ddoc = node.getDocument();

    reverseMap = new ReverseMap();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new XPathException(ex);
    }
    wdoc = builder.newDocument();

    createChildren(ddoc, wdoc);

    return findNode(wdoc, node);
}

From source file:org.hudsonci.xpath.impl.Dom2Dom.java

License:Open Source License

public org.w3c.dom.Node dom2Dom(Node node, boolean trim) throws XPathException {

    trimText = trim;//from   w  w  w. ja  v  a2 s .  c o  m

    // The first time we see a context node we create a w3c Document
    // for XPath processing. The important thing is that equals
    // works correctly.

    // This cache is probably bogus because Xerces likes to create Nodes
    // as flyweight objects on the fly. So different operations might
    // retrieve the same actual Node in two different NodeImpl objects
    // that do not compare equals.
    //
    // It did seem to work for some simple tests and when it does work
    // it is fast, but I've worked around it in getOriginalNode.

    Document ddoc = node.getDocument();
    DocMapPair pair = cache.get(ddoc);

    if (pair == null) {

        ReverseMap map = new ReverseMap();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            throw new XPathException(ex);
        }
        org.w3c.dom.Document doc = builder.newDocument();

        map.put(doc, ddoc);
        pair = new DocMapPair(doc, map);

        cache.put(ddoc, pair);
    }

    wdoc = pair.getLeft();
    reverseMap = pair.getRight();

    if (!wdoc.hasChildNodes())
        createChildren(ddoc, wdoc);

    return findNode(wdoc, node);
}

From source file:org.hudsonci.xpath.impl.Rewriter.java

License:Open Source License

/**
 * @param node1/*w ww  .j a  va  2  s . co  m*/
 * @param node2
 * @return the node that is or contains the other
 * @throws Exception 
 */
private Node findDominantNode(Node node1, Node node2) {
    // Fast tests
    if (node1 == node2)
        return node1;
    // Both nodes come from same document
    Document doc = node1.getDocument();
    if (node1 == doc)
        return node1;
    if (node2 == doc)
        return node2;
    // Slow tests
    if (nodeContains(node1, node2))
        return node1;
    if (nodeContains(node2, node1))
        return node2;
    return null;
}

From source file:org.orbeon.oxf.xforms.action.actions.XFormsDeleteAction.java

License:Open Source License

private static DeleteInfo doDeleteOne(IndentedLogger indentedLogger, List collectionToUpdate, int deleteIndex) {
    final NodeInfo nodeInfoToRemove = (NodeInfo) collectionToUpdate.get(deleteIndex - 1);
    final NodeInfo parentNodeInfo = nodeInfoToRemove.getParent();

    final Node nodeToRemove = XFormsUtils.getNodeFromNodeInfo(nodeInfoToRemove, CANNOT_DELETE_READONLY_MESSAGE);

    final List contentToUpdate;
    final int indexInContentToUpdate;
    final Element parentElement = nodeToRemove.getParent();
    if (parentElement != null) {
        // Regular case
        if (nodeToRemove instanceof Attribute) {
            contentToUpdate = parentElement.attributes();
        } else {//  ww  w .  j a  v a 2 s  . c o  m
            contentToUpdate = parentElement.content();
        }
        indexInContentToUpdate = contentToUpdate.indexOf(nodeToRemove);
    } else if (nodeToRemove.getDocument() != null
            && nodeToRemove == nodeToRemove.getDocument().getRootElement()) {
        // Case of root element where parent is Document
        contentToUpdate = nodeToRemove.getDocument().content();
        indexInContentToUpdate = contentToUpdate.indexOf(nodeToRemove);
    } else if (nodeToRemove instanceof Document) {
        // Case where node to remove is Document

        // "except if the node is the root document element of an instance then the delete action
        // is terminated with no effect."

        if (indentedLogger.isDebugEnabled())
            indentedLogger.logDebug("xf:delete", "ignoring attempt to delete document node");

        return null;
    } else {
        // Node to remove doesn't have a parent so we can't delete it
        // This can happen for nodes already detached, or nodes newly created with e.g. xf:element()
        return null;
    }

    // Actually perform the deletion
    // "The node at the delete location in the Node Set Binding node-set is deleted"
    contentToUpdate.remove(indexInContentToUpdate);

    return new DeleteInfo(parentNodeInfo, nodeInfoToRemove, indexInContentToUpdate);
}

From source file:org.orbeon.oxf.xforms.action.actions.XFormsInsertAction.java

License:Open Source License

public static List<NodeInfo> doInsert(XFormsContainingDocument containingDocument,
        IndentedLogger indentedLogger, String positionAttribute, List collectionToBeUpdated,
        NodeInfo insertContextNodeInfo, List<Item> originItems, int insertionIndex, boolean doClone,
        boolean doDispatch) {

    final boolean isEmptyNodesetBinding = collectionToBeUpdated == null || collectionToBeUpdated.size() == 0;

    // "3. The origin node-set is determined."
    // "5. Each node in the origin node-set is cloned in the order it appears in the origin node-set."
    final List<Node> sourceNodes;
    final List<Node> clonedNodes;
    {//from w  w  w  . ja  va2s . c om
        final List<Node> clonedNodesTemp;
        if (originItems == null) {
            // There are no explicitly specified origin objects, use node from Node Set Binding node-set

            // "If the origin attribute is not given and the Node Set Binding node-set is empty, then the origin
            // node-set is the empty node-set. [...] The insert action is terminated with no effect if the
            // origin node-set is the empty node-set."

            if (isEmptyNodesetBinding) {
                if (indentedLogger != null && indentedLogger.isDebugEnabled())
                    indentedLogger.logDebug("xf:insert",
                            "origin node-set from node-set binding is empty, terminating");
                return Collections.EMPTY_LIST;
            }

            // "Otherwise, if the origin attribute is not given, then the origin node-set consists of the last
            // node of the Node Set Binding node-set."
            final Node singleSourceNode = XFormsUtils.getNodeFromNodeInfoConvert(
                    (NodeInfo) collectionToBeUpdated.get(collectionToBeUpdated.size() - 1));
            // TODO: check namespace handling might be incorrect. Should use copyElementCopyParentNamespaces() instead?
            final Node singleClonedNode = Dom4jUtils.createCopy(singleSourceNode);

            sourceNodes = Collections.singletonList(singleSourceNode);
            clonedNodesTemp = Collections.singletonList(singleClonedNode);
        } else {
            // There are explicitly specified origin objects

            // "The insert action is terminated with no effect if the origin node-set is the empty node-set."
            if (originItems.size() == 0) {
                if (indentedLogger != null && indentedLogger.isDebugEnabled())
                    indentedLogger.logDebug("xf:insert", "origin node-set is empty, terminating");
                return Collections.EMPTY_LIST;
            }

            // "Each node in the origin node-set is cloned in the order it appears in the origin node-set."

            sourceNodes = new ArrayList<Node>(originItems.size()); // set to max possible size
            clonedNodesTemp = new ArrayList<Node>(originItems.size());

            for (final Object currentObject : originItems) {
                if (currentObject instanceof NodeInfo) {
                    // This is the regular case covered by XForms 1.1 / XPath 1.0

                    // NOTE: Don't clone nodes if doClone == false
                    final Node sourceNode = XFormsUtils.getNodeFromNodeInfoConvert((NodeInfo) currentObject);
                    final Node clonedNode = doClone
                            ? (sourceNode instanceof Element) ? ((Element) sourceNode).createCopy()
                                    : (Node) sourceNode.clone()
                            : sourceNode;

                    sourceNodes.add(sourceNode);
                    clonedNodesTemp.add(clonedNode);

                } else if (currentObject instanceof AtomicValue) {
                    // This is an extension: support sequences containing atomic values

                    // Convert the result to a text node
                    final String stringValue = ((Item) currentObject).getStringValue();
                    final Text textNode = Dom4jUtils.createText(stringValue);

                    sourceNodes.add(null); // there is no source node for this cloned node, it's a source item
                    clonedNodesTemp.add(textNode);
                } else
                    throw new IllegalStateException();
            }
        }

        // Remove instance data from cloned nodes and perform Document node adjustment
        for (int i = 0; i < clonedNodesTemp.size(); i++) {
            final Node clonedNodeTemp = clonedNodesTemp.get(i);

            if (clonedNodeTemp instanceof Element) {
                // Element node
                InstanceData.remove(clonedNodeTemp);
                clonedNodeTemp.detach();
            } else if (clonedNodeTemp instanceof Attribute) {
                // Attribute node
                InstanceData.remove(clonedNodeTemp);
                clonedNodeTemp.detach();
            } else if (clonedNodeTemp instanceof Document) {
                // Document node
                final Element clonedNodeTempRootElement = clonedNodeTemp.getDocument().getRootElement();

                if (clonedNodeTempRootElement == null) {
                    // Can be null in rare cases of documents without root element
                    clonedNodesTemp.set(i, null); // we support having a null node further below, so set this to null
                } else {
                    InstanceData.remove(clonedNodeTempRootElement);
                    // We can never really insert a document into anything at this point, but we assume that this means the root element
                    clonedNodesTemp.set(i, clonedNodeTempRootElement.detach());
                }
            } else {
                // Other nodes
                clonedNodeTemp.detach();
            }
        }
        clonedNodes = clonedNodesTemp;
    }

    // "6. The target location of each cloned node or nodes is determined"
    // "7. The cloned node or nodes are inserted in the order they were cloned at their target location
    // depending on their node type."

    // Identify the instance that actually changes
    final XFormsInstance modifiedInstance;
    // Find actual insertion point and insert
    final NodeInfo insertLocationNodeInfo;
    final List<Node> insertedNodes;
    final String beforeAfterInto;
    if (isEmptyNodesetBinding) {
        // Insert INTO a node

        // "If the Node Set Binding node-set is not specified or empty, the insert location node is the insert
        // context node."

        // "a. If the Node Set Binding node-set is not specified or empty, the target location depends on the
        // node type of the cloned node. If the cloned node is an attribute, then the target location is before
        // the first attribute of the insert location node. If the cloned node is not an attribute, then the
        // target location is before the first child of the insert location node."

        modifiedInstance = (containingDocument != null)
                ? containingDocument.getInstanceForNode(insertContextNodeInfo)
                : null;
        insertLocationNodeInfo = insertContextNodeInfo;
        final Node insertLocationNode = XFormsUtils.getNodeFromNodeInfo(insertContextNodeInfo,
                CANNOT_INSERT_READONLY_MESSAGE);
        insertedNodes = doInsert(insertLocationNode, clonedNodes, modifiedInstance, doDispatch);
        beforeAfterInto = "into";

        // Normalize text nodes if needed to respect XPath 1.0 constraint
        {
            boolean hasTextNode = false;
            for (Node clonedNode : clonedNodes) {
                hasTextNode |= clonedNode != null && clonedNode.getNodeType() == Node.TEXT_NODE;
            }
            if (hasTextNode)
                Dom4jUtils.normalizeTextNodes(insertLocationNode);
        }
    } else {
        // Insert BEFORE or AFTER a node
        insertLocationNodeInfo = (NodeInfo) collectionToBeUpdated.get(insertionIndex - 1);
        final Node insertLocationNode = XFormsUtils.getNodeFromNodeInfo(insertLocationNodeInfo,
                CANNOT_INSERT_READONLY_MESSAGE);
        modifiedInstance = (containingDocument != null)
                ? containingDocument.getInstanceForNode(insertLocationNodeInfo)
                : null;

        final Document insertLocationNodeDocument = insertLocationNode.getDocument();
        if (insertLocationNodeDocument != null
                && insertLocationNodeDocument.getRootElement() == insertLocationNode) {

            // "c. if insert location node is the root element of an instance, then that instance root element
            // location is the target location. If there is more than one cloned node to insert, only the
            // first node that does not cause a conflict is considered."

            insertedNodes = doInsert(insertLocationNode.getDocument(), clonedNodes, modifiedInstance,
                    doDispatch);
            beforeAfterInto = positionAttribute; // TODO: ideally normalize to "into document node"?

            // NOTE: Don't need to normalize text nodes in this case, as no new text node is inserted
        } else {
            // "d. Otherwise, the target location is immediately before or after the insert location
            // node, based on the position attribute setting or its default."

            if (insertLocationNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                // Special case for "next to an attribute"

                // NOTE: In XML, attributes are unordered. dom4j handles them as a list so has order, but
                // the XForms spec shouldn't rely on attribute order. We could try to keep the order, but it
                // is harder as we have to deal with removing duplicate attributes and find a reasonable
                // insertion strategy.

                // TODO: Don't think we should even do this now in XForms 1.1
                insertedNodes = doInsert(insertLocationNode.getParent(), clonedNodes, modifiedInstance,
                        doDispatch);

            } else {
                // Other node types
                final Element parentNode = insertLocationNode.getParent();
                final List<Node> siblingElements = Dom4jUtils.content(parentNode);
                final int actualIndex = siblingElements.indexOf(insertLocationNode);

                // Prepare insertion of new element
                final int actualInsertionIndex;
                if ("before".equals(positionAttribute)) {
                    actualInsertionIndex = actualIndex;
                } else {
                    // "after"
                    actualInsertionIndex = actualIndex + 1;
                }

                // "7. The cloned node or nodes are inserted in the order they were cloned at their target
                // location depending on their node type."

                boolean hasTextNode = false;
                int addIndex = 0;
                insertedNodes = new ArrayList<Node>(clonedNodes.size());
                for (Node clonedNode : clonedNodes) {

                    if (clonedNode != null) {// NOTE: we allow passing some null nodes so we check on null
                        if (!(clonedNode instanceof Attribute || clonedNode instanceof Namespace)) {
                            // Element, text, comment, processing instruction node
                            siblingElements.add(actualInsertionIndex + addIndex, clonedNode);
                            insertedNodes.add(clonedNode);
                            hasTextNode |= clonedNode.getNodeType() == Node.TEXT_NODE;
                            addIndex++;
                        } else {
                            // We never insert attributes or namespace nodes as siblings
                            if (indentedLogger != null && indentedLogger.isDebugEnabled())
                                indentedLogger.logDebug("xf:insert",
                                        "skipping insertion of node as sibling in element content", "type",
                                        clonedNode.getNodeTypeName(), "node",
                                        clonedNode instanceof Attribute
                                                ? Dom4jUtils.attributeToDebugString((Attribute) clonedNode)
                                                : clonedNode.toString());
                        }
                    }
                }

                // Normalize text nodes if needed to respect XPath 1.0 constraint
                if (hasTextNode)
                    Dom4jUtils.normalizeTextNodes(parentNode);
            }

            beforeAfterInto = positionAttribute;
        }
    }

    // Whether some nodes were inserted
    final boolean didInsertNodes = insertedNodes != null && insertedNodes.size() > 0;

    // Log stuff
    if (indentedLogger != null && indentedLogger.isDebugEnabled()) {
        if (didInsertNodes)
            indentedLogger.logDebug("xf:insert", "inserted nodes", "count",
                    Integer.toString(insertedNodes.size()), "instance",
                    (modifiedInstance != null) ? modifiedInstance.getEffectiveId() : null);
        else
            indentedLogger.logDebug("xf:insert", "no node inserted");
    }

    // "XForms Actions that change the tree structure of instance data result in setting all four flags to true"
    if (didInsertNodes && modifiedInstance != null) {
        // NOTE: Can be null if document into which delete is performed is not in an instance, e.g. in a variable
        modifiedInstance.markModified();
        modifiedInstance.model().markStructuralChange(modifiedInstance);
    }

    // Gather list of modified nodes
    final List<NodeInfo> insertedNodeInfos;
    if (didInsertNodes && modifiedInstance != null) {
        // Can be null if document into which delete is performed is not in an instance, e.g. in a variable
        final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance.documentInfo();
        insertedNodeInfos = new ArrayList<NodeInfo>(insertedNodes.size());
        for (Object insertedNode : insertedNodes)
            insertedNodeInfos.add(documentWrapper.wrap(insertedNode));
    } else {
        insertedNodeInfos = Collections.emptyList();
    }

    // "4. If the insert is successful, the event xforms-insert is dispatched."
    // XFormsInstance handles index and repeat items updates 
    if (doDispatch && didInsertNodes && modifiedInstance != null) {

        // Adjust insert location node and before/after/into in case the root element was replaced
        final NodeInfo adjustedInsertLocationNodeInfo;
        final String adjustedBeforeAfterInto;

        final NodeInfo parent = insertedNodeInfos.get(0).getNodeKind() == org.w3c.dom.Node.ELEMENT_NODE
                ? insertedNodeInfos.get(0).getParent()
                : null;
        if (parent != null && parent.equals(parent.getDocumentRoot())) {
            // Node was inserted under document node
            adjustedInsertLocationNodeInfo = parent.getDocumentRoot();
            adjustedBeforeAfterInto = "into";
        } else {
            adjustedInsertLocationNodeInfo = insertLocationNodeInfo;
            adjustedBeforeAfterInto = beforeAfterInto;
        }

        Dispatch.dispatchEvent(new XFormsInsertEvent(modifiedInstance, insertedNodeInfos, originItems,
                adjustedInsertLocationNodeInfo, adjustedBeforeAfterInto));
    }

    return insertedNodeInfos;
}

From source file:org.orbeon.oxf.xforms.submission.XFormsModelSubmission.java

License:Open Source License

private Document reRootAndPrune(final NodeInfo currentNodeInfo, boolean resolvedRelevant,
        String resolvedAnnotate) {

    final Document documentToSubmit;
    if (currentNodeInfo instanceof VirtualNode) {
        final Node currentNode = (Node) ((VirtualNode) currentNodeInfo).getUnderlyingNode();

        // "A node from the instance data is selected, based on attributes on the submission
        // element. The indicated node and all nodes for which it is an ancestor are considered for
        // the remainder of the submit process. "
        if (currentNode instanceof Element) {
            // Create subset of document
            documentToSubmit = Dom4jUtils.createDocumentCopyParentNamespaces((Element) currentNode);
        } else {/*from  w  w w .  ja va  2s  .c  om*/
            // Use entire instance document
            documentToSubmit = Dom4jUtils.createDocumentCopyElement(currentNode.getDocument().getRootElement());
        }

        if (resolvedRelevant) {
            // "Any node which is considered not relevant as defined in 6.1.4 is removed."
            final Node[] nodeToDetach = new Node[1];
            do {
                // NOTE: This is not very efficient, but at least we avoid NPEs that we would get by
                // detaching elements within accept(). Should implement a more efficient algorithm to
                // prune non-relevant nodes.
                nodeToDetach[0] = null;
                documentToSubmit.accept(new VisitorSupport() {

                    public final void visit(Element element) {
                        checkInstanceData(element);
                    }

                    public final void visit(Attribute attribute) {
                        checkInstanceData(attribute);
                    }

                    private void checkInstanceData(Node node) {
                        if (nodeToDetach[0] == null) {
                            // Check "relevant" MIP and remove non-relevant nodes
                            if (!InstanceData.getInheritedRelevant(node))
                                nodeToDetach[0] = node;
                        }
                    }
                });
                if (nodeToDetach[0] != null)
                    nodeToDetach[0].detach();

            } while (nodeToDetach[0] != null);
        }

        // Annotate with alerts if needed
        if (StringUtils.isNotBlank(resolvedAnnotate))
            annotateWithAlerts(containingDocument, documentToSubmit, resolvedAnnotate);

        // TODO: handle includenamespaceprefixes
    } else {
        // Submitting read-only instance backed by TinyTree (no MIPs to check)
        if (currentNodeInfo.getNodeKind() == org.w3c.dom.Node.ELEMENT_NODE) {
            documentToSubmit = TransformerUtils.tinyTreeToDom4j(currentNodeInfo);
        } else {
            documentToSubmit = TransformerUtils.tinyTreeToDom4j(currentNodeInfo.getRoot());
        }
    }
    return documentToSubmit;
}

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

public static Branch parent(Node node) {
    Branch parent = node.getParent();/*from   www . j  av  a  2  s  .c  om*/
    if (parent == null)
        parent = node.getDocument();
    return parent;
}