Example usage for org.w3c.dom Node setUserData

List of usage examples for org.w3c.dom Node setUserData

Introduction

In this page you can find the example usage for org.w3c.dom Node setUserData.

Prototype

public Object setUserData(String key, Object data, UserDataHandler handler);

Source Link

Document

Associate an object to a key on this node.

Usage

From source file:org.apache.ode.bpel.elang.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Evaluate expression and return opaque type
 *
 * @param cexp cexp//from  w ww .j a  v  a 2s  . com
 * @param ctx ctx
 * @param type type
 *
 * @return type
 *
 * @throws FaultException FaultException
 * @throws EvaluationException EvaluationException
 */
private Object evaluate(OExpression cexp, EvaluationContext ctx, QName type)
        throws FaultException, EvaluationException {
    try {
        OXQuery10ExpressionBPEL20 oxquery10 = ((OXQuery10ExpressionBPEL20) cexp);

        XQDataSource xqds = new SaxonXQDataSource();
        XQConnection xqconn = xqds.getConnection();

        Configuration configuration = ((SaxonXQConnection) xqconn).getConfiguration();
        configuration.setAllNodesUntyped(true);
        configuration.setHostLanguage(Configuration.XQUERY);

        XQStaticContext staticEnv = xqconn.getStaticContext();

        NSContext nsContext = oxquery10.namespaceCtx;
        Set<String> prefixes = nsContext.getPrefixes();
        for (String prefix : prefixes) {
            String uri = nsContext.getNamespaceURI(prefix);
            staticEnv.declareNamespace(prefix, uri);
        }

        configuration.setSchemaValidationMode(Validation.SKIP);
        xqconn.setStaticContext(staticEnv);

        // Prepare expression, for starters
        String xquery = oxquery10.xquery.replaceFirst(Constants.XQUERY_FUNCTION_HANDLER_COMPILER,
                Constants.XQUERY_FUNCTION_HANDLER_RUNTIME);
        XQPreparedExpression exp = xqconn.prepareExpression(xquery);

        JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(ctx, oxquery10);
        JaxpVariableResolver variableResolver = new JaxpVariableResolver(ctx, oxquery10, configuration);
        // Bind external variables to runtime values
        for (QName variable : exp.getAllUnboundExternalVariables()) {
            // Evaluate referenced variable
            Object value = variableResolver.resolveVariable(variable);

            if (value instanceof Value) {
                SaxonXQConnection saxonConn = (SaxonXQConnection) xqconn;
                try {
                    Item item = ((Value) value).asItem();
                    if (item == null) {
                        exp.bindSequence(variable, xqconn.createSequence(Collections.EMPTY_LIST.iterator()));
                    } else {
                        XQItem item2 = new SaxonXQItem(item, saxonConn);
                        exp.bindItem(variable, item2);
                    }
                } catch (XPathException e) {
                    __log.warn("", e);
                }
            } else {

                if (value instanceof Date) {
                    Date d = (Date) value;
                    value = org.apache.ode.utils.ISO8601DateParser.format(d);
                }

                // Figure out type of variable
                XQSequenceType xqType = getItemType(xqconn, value);

                // Saxon doesn't like binding sequences to variables
                if (value instanceof Node) {
                    // a node is a node-list, but the inverse isn't true.
                    // so, if the value is truly a node, leave it alone.
                } else if (value instanceof NodeList) {
                    // So extract the first item from the node list
                    NodeList nodeList = (NodeList) value;
                    ArrayList nodeArray = new ArrayList();
                    for (int i = 0; i < nodeList.getLength(); i++) {
                        nodeArray.add(nodeList.item(i));
                    }
                    value = xqconn.createSequence(nodeArray.iterator());
                }

                // Bind value with external variable
                if (value != null && xqType != null) {
                    if (value instanceof XQSequence) {
                        exp.bindSequence(variable, (XQSequence) value);
                    } else {
                        if (xqType instanceof XQItemType) {
                            exp.bindObject(variable, value, (XQItemType) xqType);
                        }
                    }
                }
            }
        }

        // Set context node
        Node contextNode = (ctx.getRootNode() == null) ? DOMUtils.newDocument() : ctx.getRootNode();
        contextNode.setUserData(XQuery10BpelFunctions.USER_DATA_KEY_FUNCTION_RESOLVER, funcResolver, null);
        exp.bindItem(XQConstants.CONTEXT_ITEM, xqconn.createItemFromNode(contextNode, xqconn.createNodeType()));

        // Execute query
        XQResultSequence result = exp.executeQuery();

        // Cast Saxon result to Java result
        Object evalResult = getResultValue(type, result);

        if ((evalResult != null) && __log.isDebugEnabled()) {
            __log.debug("Expression " + cexp.toString() + " generated result " + evalResult + " - type="
                    + evalResult.getClass().getName());

            if (ctx.getRootNode() != null) {
                __log.debug("Was using context node " + DOMUtils.domToString(ctx.getRootNode()));
            }
        }

        return evalResult;
    } catch (XQException xqe) {
        // Extracting the real cause from all this wrapping isn't a simple task
        Throwable cause = (xqe.getCause() != null) ? xqe.getCause() : xqe;

        if (cause instanceof DynamicError) {
            Throwable th = ((DynamicError) cause).getException();

            if (th != null) {
                cause = th;

                if (cause.getCause() != null) {
                    cause = cause.getCause();
                }
            }
        }

        throw new EvaluationException("Error while executing an XQuery expression: " + cause.toString(), cause);
    } catch (WrappedResolverException wre) {
        __log.debug("Could not evaluate expression because of ", wre);
        throw (FaultException) wre.getCause();
    }
}

From source file:org.apache.ode.bpel.rtrep.v2.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Evaluate expression and return opaque type
 *
 * @param cexp cexp /*from  www. ja  v a  2 s  .  com*/
 * @param ctx ctx 
 * @param type type 
 *
 * @return type
 *
 * @throws FaultException FaultException 
 */
private Object evaluate(OExpression cexp, EvaluationContext ctx, QName type) throws FaultException {
    try {
        OXQuery10ExpressionBPEL20 oxquery10 = ((OXQuery10ExpressionBPEL20) cexp);

        XQDataSource xqds = new SaxonXQDataSource();
        XQConnection xqconn = xqds.getConnection();

        Configuration configuration = ((SaxonXQConnection) xqconn).getConfiguration();
        configuration.setAllNodesUntyped(true);
        configuration.setHostLanguage(Configuration.XQUERY);

        XQStaticContext staticEnv = xqconn.getStaticContext();

        NSContext nsContext = oxquery10.namespaceCtx;
        Set<String> prefixes = nsContext.getPrefixes();
        for (String prefix : prefixes) {
            String uri = nsContext.getNamespaceURI(prefix);
            staticEnv.declareNamespace(prefix, uri);
        }

        configuration.setSchemaValidationMode(Validation.SKIP);
        xqconn.setStaticContext(staticEnv);

        // Prepare expression, for starters
        String xquery = oxquery10.xquery.replaceFirst(Constants.XQUERY_FUNCTION_HANDLER_COMPILER,
                Constants.XQUERY_FUNCTION_HANDLER_RUNTIME);
        XQPreparedExpression exp = xqconn.prepareExpression(xquery);

        JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(ctx, oxquery10);
        JaxpVariableResolver variableResolver = new JaxpVariableResolver(ctx, oxquery10, configuration);
        // Bind external variables to runtime values
        for (QName variable : exp.getAllUnboundExternalVariables()) {
            // Evaluate referenced variable
            Object value = variableResolver.resolveVariable(variable);

            // Figure out type of variable
            XQSequenceType xqType = getItemType(xqconn, value);

            // Saxon doesn't like binding sequences to variables
            if (value instanceof Node) {
                // a node is a node-list, but the inverse isn't true.
                // so, if the value is truly a node, leave it alone.
            } else if (value instanceof NodeList) {
                // So extract the first item from the node list
                NodeList nodeList = (NodeList) value;
                ArrayList nodeArray = new ArrayList();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    nodeArray.add(nodeList.item(i));
                }
                value = xqconn.createSequence(nodeArray.iterator());
            }

            // Bind value with external variable
            if (value != null && xqType != null) {
                if (value instanceof XQSequence) {
                    exp.bindSequence(variable, (XQSequence) value);
                } else {
                    if (xqType instanceof XQItemType) {
                        exp.bindObject(variable, value, (XQItemType) xqType);
                    }
                }
            }
        }

        // Set context node
        Node contextNode = (ctx.getRootNode() == null) ? DOMUtils.newDocument() : ctx.getRootNode();
        contextNode.setUserData(XQuery10BpelFunctions.USER_DATA_KEY_FUNCTION_RESOLVER, funcResolver, null);
        exp.bindItem(XQConstants.CONTEXT_ITEM, xqconn.createItemFromNode(contextNode, xqconn.createNodeType()));

        // Execute query
        XQResultSequence result = exp.executeQuery();

        // Cast Saxon result to Java result
        Object evalResult = getResultValue(type, result);

        if ((evalResult != null) && __log.isDebugEnabled()) {
            __log.debug("Expression " + cexp.toString() + " generated result " + evalResult + " - type="
                    + evalResult.getClass().getName());

            if (ctx.getRootNode() != null) {
                __log.debug("Was using context node " + DOMUtils.domToString(ctx.getRootNode()));
            }
        }

        return evalResult;
    } catch (XQException xqe) {
        // Extracting the real cause from all this wrapping isn't a simple task
        Throwable cause = (xqe.getCause() != null) ? xqe.getCause() : xqe;

        if (cause instanceof DynamicError) {
            Throwable th = ((DynamicError) cause).getException();

            if (th != null) {
                cause = th;

                if (cause.getCause() != null) {
                    cause = cause.getCause();
                }
            }
        }
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, cause.getMessage(),
                cause);
    } catch (Throwable t) {
        __log.debug("Could not evaluate expression because of ", t);
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, t.getMessage(), t);
    }
}

From source file:org.jboss.windup.util.xml.LocationAwareContentHandler.java

private void storeLineInformation(Node e) {
    e.setUserData(LINE_NUMBER_KEY_NAME, this.locator.getLineNumber(), null);
    e.setUserData(COLUMN_NUMBER_KEY_NAME, this.locator.getColumnNumber(), null);
}

From source file:org.nuxeo.theme.themes.ThemeParser.java

public static void parseFormats(final ThemeElement theme, org.w3c.dom.Element doc,
        Map<Style, Map<String, Properties>> commonStyles, Map<Integer, String> inheritanceMap, Node node)
        throws ThemeIOException, ThemeException {
    Node baseNode = getBaseNode(doc);
    String themeName = theme.getName();

    String resourceBankName = null;
    ThemeDescriptor themeDescriptor = ThemeManager.getThemeDescriptorByThemeName(themeName);
    if (themeDescriptor != null) {
        resourceBankName = themeDescriptor.getResourceBankName();
    }/* ww w .java  2s. c  o m*/

    ThemeManager themeManager = Manager.getThemeManager();

    for (Node n : getChildElements(node)) {
        String nodeName = n.getNodeName();
        NamedNodeMap attributes = n.getAttributes();
        Node elementItem = attributes.getNamedItem("element");
        String elementXPath = null;
        if (elementItem != null) {
            elementXPath = elementItem.getNodeValue();
        }

        Format format;
        try {
            format = FormatFactory.create(nodeName);
        } catch (ThemeException e) {
            throw new ThemeIOException(e);
        }
        format.setProperties(getPropertiesFromNode(n));

        String description = getCommentAssociatedTo(n);
        if (description != null) {
            format.setDescription(description);
        }

        if ("widget".equals(nodeName)) {
            List<Node> viewNodes = getChildElementsByTagName(n, "view");
            if (!viewNodes.isEmpty()) {
                format.setName(viewNodes.get(0).getTextContent());
            }

        } else if ("layout".equals(nodeName)) {
            // TODO: validate layout properties

        } else if ("style".equals(nodeName)) {
            Node nameAttr = attributes.getNamedItem("name");
            Style style = (Style) format;

            // register the style name
            String styleName = null;
            if (nameAttr != null) {
                styleName = nameAttr.getNodeValue();
                // the style may have been registered already
                Style registeredStyle = (Style) themeManager.getNamedObject(themeName, "style", styleName);
                if (registeredStyle == null) {
                    style.setName(styleName);
                    themeManager.setNamedObject(themeName, "style", style);
                } else {
                    style = registeredStyle;
                }
            }

            Node inheritedAttr = attributes.getNamedItem("inherit");
            if (inheritedAttr != null) {
                String inheritedName = inheritedAttr.getNodeValue();
                if ("".equals(inheritedName)) {
                    continue;
                }
                inheritanceMap.put(style.getUid(), inheritedName);
            }

            Node remoteAttr = attributes.getNamedItem("remote");
            if (remoteAttr != null) {
                Boolean remote = Boolean.valueOf(remoteAttr.getNodeValue());
                if (style.isNamed()) {
                    style.setRemote(remote);
                } else {
                    log.warn("Only named styles can be remote, ignoring remote attribute on" + style.getUid());
                }
            }

            if (styleName != null && elementXPath != null) {
                log.warn("Style parser: named style '" + styleName + "' cannot have an 'element' attribute: '"
                        + elementXPath + "'.");
                continue;
            }

            List<Node> selectorNodes = getChildElementsByTagName(n, "selector");

            if (style.isRemote() && resourceBankName != null) {
                if (!selectorNodes.isEmpty()) {
                    style.setCustomized(true);
                }
            }

            // Use style properties from the theme
            for (Node selectorNode : selectorNodes) {
                NamedNodeMap attrs = selectorNode.getAttributes();
                Node pathAttr = attrs.getNamedItem("path");
                if (pathAttr == null) {
                    log.warn(String.format(
                            "Style parser: named style '%s' has a selector with no path: ignored", styleName));
                    continue;
                }
                String path = pathAttr.getNodeValue();

                String viewName = null;
                Node viewAttr = attrs.getNamedItem("view");
                if (viewAttr != null) {
                    viewName = viewAttr.getNodeValue();
                }

                String selectorDescription = getCommentAssociatedTo(selectorNode);
                if (selectorDescription != null) {
                    style.setSelectorDescription(path, viewName, selectorDescription);
                }

                // BBB: remove in a later release
                if (elementXPath != null && (viewName == null || viewName.equals("*"))) {
                    log.warn("Style parser: trying to guess the view name for: " + elementXPath);
                    viewName = guessViewNameFor(doc, elementXPath);
                    if (viewName == null) {
                        if (!commonStyles.containsKey(style)) {
                            commonStyles.put(style, new LinkedHashMap<String, Properties>());
                        }
                        commonStyles.get(style).put(path, getPropertiesFromNode(selectorNode));
                    }
                }

                if (styleName != null) {
                    if (viewName != null) {
                        log.info("Style parser: ignoring view name '" + viewName + "' in named style '"
                                + styleName + "'.");
                    }
                    viewName = "*";
                }

                if (viewName != null) {
                    style.setPropertiesFor(viewName, path, getPropertiesFromNode(selectorNode));
                }
            }
        }

        themeManager.registerFormat(format);
        if (elementXPath != null) {
            if ("".equals(elementXPath)) {
                baseNode.setUserData(nodeName, format, null);
            } else {
                for (Node element : getNodesByXPath(baseNode, elementXPath)) {
                    element.setUserData(nodeName, format, null);
                }
            }
        }
    }

}

From source file:org.nuxeo.theme.themes.ThemeParser.java

public static void parseProperties(org.w3c.dom.Element doc, Node node) throws ThemeIOException {
    NamedNodeMap attributes = node.getAttributes();
    Node elementAttr = attributes.getNamedItem("element");
    if (elementAttr == null) {
        throw new ThemeIOException("<properties> node has no 'element' attribute.");
    }//w  ww. j  a v a  2 s .c o m
    String elementXPath = elementAttr.getNodeValue();

    Node baseNode = getBaseNode(doc);
    Node element = null;
    try {
        element = (Node) xpath.evaluate(elementXPath, baseNode, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        throw new ThemeIOException(e);
    }
    if (element == null) {
        throw new ThemeIOException("Could not find the element associated to: " + elementXPath);
    }
    Properties properties = getPropertiesFromNode(node);
    if (properties != null) {
        element.setUserData("properties", properties, null);
    }
}