Example usage for org.apache.commons.jxpath Pointer getNode

List of usage examples for org.apache.commons.jxpath Pointer getNode

Introduction

In this page you can find the example usage for org.apache.commons.jxpath Pointer getNode.

Prototype

Object getNode();

Source Link

Document

Returns the raw value of the object, property or collection element this pointer represents.

Usage

From source file:org.chiba.xml.util.test.XMLBaseResolverTest.java

/**
 * __UNDOCUMENTED__/*from w  w  w  .j a  v a  2s.  c  o  m*/
 *
 * @throws Exception __UNDOCUMENTED__
 */
public void testResolveXMLBase() throws Exception {
    Document doc = getXmlResource("xmlbase1.xml");

    JXPathContext context = JXPathContext.newContext(doc);
    Pointer p = context.getPointer("descendant::*[@xlink:href][1]");
    Node n = (Node) p.getNode();
    String s = XMLBaseResolver.resolveXMLBase(n, "new.xml");
    assertTrue(s.equals("http://example.org/today/new.xml"));

    p = context.getPointer("descendant::*[@xlink:href][2]");
    n = (Node) p.getNode();
    s = XMLBaseResolver.resolveXMLBase(n, "pick1.xml");
    assertTrue(s.equals("http://example.org/hotpicks/pick1.xml"));

    p = context.getPointer("descendant::*[@xlink:href][3]");
    n = (Node) p.getNode();
    s = XMLBaseResolver.resolveXMLBase(n, "pick2.xml");
    assertTrue(s.equals("http://example.org/hotpicks/pick2.xml"));

    p = context.getPointer("descendant::*[@xlink:href][4]");
    n = (Node) p.getNode();
    s = XMLBaseResolver.resolveXMLBase(n, "pick3.xml");
    assertTrue(s.equals("http://example.org/hotpicks/pick3.xml"));
}

From source file:org.chiba.xml.util.test.XMLBaseResolverTest.java

/**
 * __UNDOCUMENTED__/*from   w ww . j  a  v a  2 s  . c  o  m*/
 *
 * @throws Exception __UNDOCUMENTED__
 */
public void testResolveXMLBaseLess() throws Exception {
    Document doc = getXmlResource("xmlbase2.xml");

    JXPathContext context = JXPathContext.newContext(doc);
    Pointer p = context.getPointer("descendant::*[@xlink:href][1]");
    Node n = (Node) p.getNode();
    String s = XMLBaseResolver.resolveXMLBase(n, "new.xml");
    assertTrue(s.equals("new.xml"));

    p = context.getPointer("descendant::*[@xlink:href][2]");
    n = (Node) p.getNode();
    s = XMLBaseResolver.resolveXMLBase(n, "pick1.xml");
    assertTrue(s.equals("pick1.xml"));

    p = context.getPointer("descendant::*[@xlink:href][3]");
    n = (Node) p.getNode();
    s = XMLBaseResolver.resolveXMLBase(n, "pick2.xml");
    assertTrue(s.equals("pick2.xml"));

    p = context.getPointer("descendant::*[@xlink:href][4]");
    n = (Node) p.getNode();
    s = XMLBaseResolver.resolveXMLBase(n, "pick3.xml");
    assertTrue(s.equals("pick3.xml"));
}

From source file:org.chiba.xml.xforms.config.Config.java

/**
 * Read custom instance serializer that are used by AbstractConnector.
 *
 * @param configContext  the JXPath context holding the configuration.
 * @param sectionPath    the context relative path to the section.
 * @param scheme         the name of the attribute holding the scheme.
 * @param method         the name of the attribute holding the method.
 * @param mediatype      the name of the attribute holding the mediatype.
 * @param serializerClass the name of the attribute holding the InstanceSerializer implementation.
 * @return the specified configuration section in a hash map.
 * @throws Exception if any error occured during configuration loading.
 *//*from  w  ww . j  a va  2 s.  com*/
private InstanceSerializerMap loadSerializer(JXPathContext configContext, String sectionPath, String scheme,
        String method, String mediatype, String serializerClass) throws Exception {
    InstanceSerializerMap map = new InstanceSerializerMap();
    Iterator iterator = configContext.iteratePointers(sectionPath);

    while (iterator.hasNext()) {
        Pointer pointer = (Pointer) iterator.next();
        Element element = (Element) pointer.getNode();

        try {

            String schemeVal = element.getAttribute(scheme);
            schemeVal = ("".equals(schemeVal)) ? "*" : schemeVal;

            String methodVal = element.getAttribute(method);
            methodVal = ("".equals(methodVal)) ? "*" : methodVal;

            String mediatypeVal = element.getAttribute(mediatype);
            mediatypeVal = ("".equals(mediatypeVal)) ? "*" : mediatypeVal;

            String classVal = element.getAttribute(serializerClass);
            if (classVal == null) {
                continue;
            }

            InstanceSerializer serializer = (InstanceSerializer) Class.forName(classVal).newInstance();

            map.registerSerializer(schemeVal, methodVal, mediatypeVal, serializer);

        } catch (Exception e) {
            // silently ignore invalid references ...
            LOGGER.error("registerSerializer(\"" + scheme + "\",\"" + method + "\"," + mediatype + "\",\""
                    + serializerClass + "\") failed: " + e.getMessage(), e);
        }
    }

    return map;
}

From source file:org.chiba.xml.xforms.config.Config.java

private HashMap loadExtensionFunctions(JXPathContext configContext, String sectionPath) {
    HashMap map = new HashMap();
    Iterator iterator = configContext.iteratePointers(sectionPath);

    while (iterator.hasNext()) {
        Pointer pointer = (Pointer) iterator.next();
        Element element = (Element) pointer.getNode();

        String namespace = element.getAttribute("namespace");
        namespace = ("".equals(namespace)) ? null : namespace;

        //String prefix = element.getAttribute("prefix");
        //prefix = ("".equals(prefix)) ? null : prefix;

        String function = element.getAttribute("name");
        function = ("".equals(function)) ? null : function;

        String functionClass = element.getAttribute("class");
        functionClass = ("".equals(functionClass)) ? null : functionClass;

        String key = (namespace == null) ? function : namespace + ((function == null) ? "" : " " + function);
        //String prefixKey = (prefix == null) ? function : prefix + ((function == null) ? "" : " " + function);

        if ((function != null) && (functionClass != null)) {
            String javaFunction = element.getAttribute("java-name");
            javaFunction = ((javaFunction == null) || "".equalsIgnoreCase(javaFunction)) ? function
                    : javaFunction;//from w  w  w  .ja  v  a 2s.co  m
            String[] classFunction = new String[] { functionClass, javaFunction };

            if (key != null) {
                map.put(key, classFunction);
            }
            //if (prefixKey != null) {
            //    map.put(prefixKey, classFunction);
            //}
        }
    }

    return map;
}

From source file:org.chiba.xml.xforms.config.Config.java

/**
 * Returns the specified configuration value in a String.
 *
 * @param configContext the JXPath context holding the configuration.
 * @param path          the context relative path to the section.
 * @param nameAttribute the name of the attribute to load from
 * @return the specified configuration value in a String
 * @throws Exception if any error occured during configuration loading.
 *///from  ww w  . ja va2  s .  c  om
private String load(JXPathContext configContext, String path, String nameAttribute) throws Exception {
    String value;

    Pointer pointer = configContext.getPointer(path);
    Element element = (Element) pointer.getNode();
    value = element.getAttribute(nameAttribute);

    return value;
}

From source file:org.chiba.xml.xforms.config.DefaultConfig.java

private HashMap loadExtensionFunctions(JXPathContext configContext, String sectionPath) {
    HashMap map = new HashMap();
    Iterator iterator = configContext.iteratePointers(sectionPath);

    while (iterator.hasNext()) {
        Pointer pointer = (Pointer) iterator.next();
        Element element = (Element) pointer.getNode();

        String namespace = element.getAttribute("namespace");
        namespace = ("".equals(namespace)) ? null : namespace;

        //String prefix = element.getXFormsAttribute("prefix");
        //prefix = ("".equals(prefix)) ? null : prefix;

        String function = element.getAttribute("name");
        function = ("".equals(function)) ? null : function;

        String functionClass = element.getAttribute("class");
        functionClass = ("".equals(functionClass)) ? null : functionClass;

        String key = (namespace == null) ? function : namespace + ((function == null) ? "" : " " + function);
        //String prefixKey = (prefix == null) ? function : prefix +
        // ((function == null) ? "" : " " + function);

        if ((function != null) && (functionClass != null)) {
            String javaFunction = element.getAttribute("java-name");
            javaFunction = ((javaFunction == null) || "".equalsIgnoreCase(javaFunction)) ? function
                    : javaFunction;/*from  w w w .j a v a 2 s .  c om*/
            String[] classFunction = new String[] { functionClass, javaFunction };

            if (key != null) {
                map.put(key, classFunction);
            }
            //if (prefixKey != null) {
            //    map.put(prefixKey, classFunction);
            //}
        }
    }

    return map;
}

From source file:org.chiba.xml.xforms.config.DefaultConfig.java

private HashMap loadCustomElements(JXPathContext configContext, String sectionPath) {
    HashMap map = new HashMap();
    Iterator iterator = configContext.iteratePointers(sectionPath);

    while (iterator.hasNext()) {
        Pointer pointer = (Pointer) iterator.next();
        Element element = (Element) pointer.getNode();

        String namespace = element.getAttribute("namespace");
        namespace = ("".equals(namespace)) ? null : namespace;

        String elementName = element.getAttribute("name");
        elementName = ("".equals(elementName)) ? null : elementName;

        String elementClass = element.getAttribute("class");
        elementClass = ("".equals(elementClass)) ? null : elementClass;

        String key = (namespace == null) ? elementName
                : namespace + ((elementName == null) ? "" : ":" + elementName);

        if ((elementName != null) && (elementClass != null)) {
            if (key != null) {
                map.put(key, elementClass);
            }/*w w  w. j  av  a2s. co  m*/
        }
    }

    return map;
}

From source file:org.chiba.xml.xforms.connector.context.ContextResolver.java

/**
 * Performs a lookup in Chiba's context map and returns the object denoted
 * by the scheme specific part of the resolver's URI.
 *
 * @return the object denoted by the scheme specific part of the
 * resolver's URI./*from ww  w .  j  a va  2s  .c  o m*/
 * @throws XFormsException if any error occurred during link traversal.
 */
public Object resolve() throws XFormsException {
    try {
        String xpath = new URI(getURI()).getSchemeSpecificPart();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("resolve: " + xpath);
        }

        JXPathContext contextContext = JXPathContext.newContext(getContext());
        Pointer pointer = contextContext.getPointer(xpath);
        if (pointer.getValue() == null) {
            throw new JXPathException("No pointer for xpath: " + xpath);
        }

        return pointer.getNode();
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}

From source file:org.chiba.xml.xforms.constraints.Validator.java

/**
 * Validates the specified instance data node and its descendants.
 *
 * @param instance the instance to be validated.
 * @param path an xpath denoting an arbitrary subtre of the instance.
 * @return <code>true</code> if all relevant instance data nodes are valid
 *         regarding in terms of their <code>constraint</code> and
 *         <code>required</code> properties, otherwise <code>false</code>.
 *///from ww w . j  a v a2 s  .  c o  m
public boolean validate(Instance instance, String path) {
    // initialize
    boolean result = true;
    String expressionPath = path;

    if (!path.endsWith("/")) {
        expressionPath = expressionPath + "/";
    }

    // set expression path to contain the specified path and its subtree
    expressionPath = expressionPath + "descendant-or-self::*";

    // evaluate expression path
    JXPathContext context = instance.getInstanceContext();
    Iterator iterator = context.iteratePointers(expressionPath);
    Pointer locationPointer;
    String locationPath;

    while (iterator.hasNext()) {
        locationPointer = (Pointer) iterator.next();
        locationPath = locationPointer.asPath();
        Element element = (Element) locationPointer.getNode();

        // validate element node against type
        String type = element.getAttributeNS(NamespaceCtx.XMLSCHEMA_INSTANCE_NS, "type");
        result &= validateNode(instance, locationPath, type);

        // handle attributes explicitely since JXPath has
        // seriuos problems with namespaced attributes
        NamedNodeMap attributes = element.getAttributes();

        for (int index = 0; index < attributes.getLength(); index++) {
            Attr attr = (Attr) attributes.item(index);

            if (isInstanceAttribute(attr)) {
                // validate attribute node
                result &= validateNode(instance, locationPath + "/@" + attr.getNodeName());
            }
        }
    }

    return result;
}

From source file:org.chiba.xml.xforms.Container.java

/**
 * adds a DOM EventListener to a target Node.
 *
 * @param targetId      the target Node for this Event
 * @param eventType     the type of Event
 * @param eventListener the EventListener
 * @param useCapture    true, if capture should be used for this Eventtype
 * @throws XFormsException throws XFormsException if target Node cannot be found
 *//*from www  . ja  v  a2  s.c  o m*/
public void addEventListener(String targetId, String eventType, EventListener eventListener, boolean useCapture)
        throws XFormsException {
    if (this.rootContext != null) {
        Pointer pointer = this.rootContext.getPointer("//*[@id='" + targetId + "']");

        if (pointer != null) {
            EventTarget eventTarget = (EventTarget) pointer.getNode();
            eventTarget.addEventListener(eventType, eventListener, useCapture);

            return;
        }
    }

    throw new XFormsException("event target '" + targetId + "' not found");
}