Example usage for org.apache.commons.jxpath JXPathContext getPointer

List of usage examples for org.apache.commons.jxpath JXPathContext getPointer

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext getPointer.

Prototype

public abstract Pointer getPointer(String xpath);

Source Link

Document

Traverses the xpath and returns a Pointer.

Usage

From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java

private void addAttributeSet(Document xForm, Element modelSection, Element formSection,
        XSComplexTypeDefinition controlType, XSElementDeclaration owner, String pathToRoot,
        boolean checkIfExtension) {
    XSObjectList attrUses = controlType.getAttributeUses();

    if (attrUses != null) {
        int nbAttr = attrUses.getLength();
        for (int i = 0; i < nbAttr; i++) {
            XSAttributeUse currentAttributeUse = (XSAttributeUse) attrUses.item(i);
            XSAttributeDeclaration currentAttribute = currentAttributeUse.getAttrDeclaration();

            //test if extended !
            if (checkIfExtension && this.doesAttributeComeFromExtension(currentAttributeUse, controlType)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(//w w w  .j  a  v  a  2s  .c o m
                            "This attribute comes from an extension: recopy form controls. \n Model section: ");
                    DOMUtil.prettyPrintDOM(modelSection);
                }

                String attributeName = currentAttributeUse.getName();
                if (attributeName == null || attributeName.equals(""))
                    attributeName = currentAttributeUse.getAttrDeclaration().getName();

                //find the existing bind Id
                //(modelSection is the enclosing bind of the element)
                NodeList binds = modelSection.getElementsByTagNameNS(XFORMS_NS, "bind");
                int j = 0;
                int nb = binds.getLength();
                String bindId = null;
                while (j < nb && bindId == null) {
                    Element bind = (Element) binds.item(j);
                    String nodeset = bind.getAttributeNS(XFORMS_NS, "nodeset");
                    if (nodeset != null) {
                        String name = nodeset.substring(1); //remove "@" in nodeset
                        if (name.equals(attributeName))
                            bindId = bind.getAttributeNS(XFORMS_NS, "id");
                    }
                    j++;
                }

                //find the control
                Element control = null;
                if (bindId != null) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("bindId found: " + bindId);

                    JXPathContext context = JXPathContext.newContext(formSection.getOwnerDocument());
                    Pointer pointer = context
                            .getPointer("//*[@" + this.getXFormsNSPrefix() + "bind='" + bindId + "']");
                    if (pointer != null)
                        control = (Element) pointer.getNode();
                }

                //copy it
                if (control == null) {
                    LOGGER.warn("Corresponding control not found");
                } else {
                    Element newControl = (Element) control.cloneNode(true);
                    //set new Ids to XForm elements
                    this.resetXFormIds(newControl);

                    formSection.appendChild(newControl);
                }

            } else {
                String newPathToRoot;

                if ((pathToRoot == null) || pathToRoot.equals("")) {
                    newPathToRoot = "@" + currentAttribute.getName();
                } else if (pathToRoot.endsWith("/")) {
                    newPathToRoot = pathToRoot + "@" + currentAttribute.getName();
                } else {
                    newPathToRoot = pathToRoot + "/@" + currentAttribute.getName();
                }

                XSSimpleTypeDefinition simpleType = currentAttribute.getTypeDefinition();
                //TODO SRA: UrType ?
                /*if(simpleType==null){
                simpleType=new UrType();
                }*/

                addSimpleType(xForm, modelSection, formSection, simpleType, currentAttributeUse, newPathToRoot);
            }
        }
    }
}

From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java

/**
 * checkIfExtension: if false, addElement is called wether it is an extension or not
 * if true, if it is an extension, element is recopied (and no additional bind)
 *///from w  w  w  . j  av  a 2 s . c o  m
private void addGroup(Document xForm, Element modelSection, Element formSection, XSModelGroup group,
        XSComplexTypeDefinition controlType, XSElementDeclaration owner, String pathToRoot, int minOccurs,
        int maxOccurs, boolean checkIfExtension) {
    if (group != null) {

        Element repeatSection = addRepeatIfNecessary(xForm, modelSection, formSection,
                owner.getTypeDefinition(), minOccurs, maxOccurs, pathToRoot);
        Element repeatContentWrapper = repeatSection;

        if (repeatSection != formSection) {
            //selector -> no more needed?
            //this.addSelector(xForm, repeatSection);
            //group wrapper
            repeatContentWrapper = _wrapper.createGroupContentWrapper(repeatSection);
        }

        if (LOGGER.isDebugEnabled())
            LOGGER.debug(
                    "addGroup from owner=" + owner.getName() + " and controlType=" + controlType.getName());

        XSObjectList particles = group.getParticles();
        for (int counter = 0; counter < particles.getLength(); counter++) {
            XSParticle currentNode = (XSParticle) particles.item(counter);
            XSTerm term = currentNode.getTerm();

            if (LOGGER.isDebugEnabled())
                LOGGER.debug("   : next term = " + term.getName());

            int childMaxOccurs = currentNode.getMaxOccurs();
            if (currentNode.getMaxOccursUnbounded())
                childMaxOccurs = -1;
            int childMinOccurs = currentNode.getMinOccurs();

            if (term instanceof XSModelGroup) {

                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("   term is a group");

                addGroup(xForm, modelSection, repeatContentWrapper, ((XSModelGroup) term), controlType, owner,
                        pathToRoot, childMinOccurs, childMaxOccurs, checkIfExtension);
            } else if (term instanceof XSElementDeclaration) {
                XSElementDeclaration element = (XSElementDeclaration) term;

                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("   term is an element declaration: " + term.getName());

                //special case for types already added because used in an extension
                //do not add it when it comes from an extension !!!
                //-> make a copy from the existing form control
                if (checkIfExtension && this.doesElementComeFromExtension(element, controlType)) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(
                                "This element comes from an extension: recopy form controls.\n Model Section=");
                        DOMUtil.prettyPrintDOM(modelSection);
                    }

                    //find the existing bind Id
                    //(modelSection is the enclosing bind of the element)
                    NodeList binds = modelSection.getElementsByTagNameNS(XFORMS_NS, "bind");
                    int i = 0;
                    int nb = binds.getLength();
                    String bindId = null;
                    while (i < nb && bindId == null) {
                        Element bind = (Element) binds.item(i);
                        String nodeset = bind.getAttributeNS(XFORMS_NS, "nodeset");
                        if (nodeset != null && nodeset.equals(element.getName()))
                            bindId = bind.getAttributeNS(XFORMS_NS, "id");
                        i++;
                    }

                    //find the control
                    Element control = null;
                    if (bindId != null) {
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("bindId found: " + bindId);

                        JXPathContext context = JXPathContext.newContext(formSection.getOwnerDocument());
                        Pointer pointer = context
                                .getPointer("//*[@" + this.getXFormsNSPrefix() + "bind='" + bindId + "']");
                        if (pointer != null)
                            control = (Element) pointer.getNode();
                    }

                    //copy it
                    if (control == null) {
                        LOGGER.warn("Corresponding control not found");
                    } else {
                        Element newControl = (Element) control.cloneNode(true);
                        //set new Ids to XForm elements
                        this.resetXFormIds(newControl);

                        repeatContentWrapper.appendChild(newControl);
                    }

                } else { //add it normally
                    String elementName = getElementName(element, xForm);

                    String path = pathToRoot + "/" + elementName;

                    if (pathToRoot.equals("")) { //relative
                        path = elementName;
                    }

                    addElement(xForm, modelSection, repeatContentWrapper, element, element.getTypeDefinition(),
                            path);
                }
            } else { //XSWildcard -> ignore ?
                //LOGGER.warn("XSWildcard found in group from "+owner.getName()+" for pathToRoot="+pathToRoot);
            }
        }

        if (LOGGER.isDebugEnabled())
            LOGGER.debug("--- end of addGroup from owner=" + owner.getName());
    }
}

From source file:org.chiba.xml.base.XMLBaseResolverTest.java

/**
 * __UNDOCUMENTED__//from  w  w w. j  av  a 2 s. c o m
 *
 * @throws Exception __UNDOCUMENTED__
 */
public void testResolveXMLBase() throws Exception {
    Document doc = getXmlResource("XMLBaseResolverTest1.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.base.XMLBaseResolverTest.java

/**
 * __UNDOCUMENTED__// w ww.j av a 2 s.c o m
 *
 * @throws Exception __UNDOCUMENTED__
 */
public void testResolveXMLBaseLess() throws Exception {
    Document doc = getXmlResource("XMLBaseResolverTest2.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.util.test.XMLBaseResolverTest.java

/**
 * __UNDOCUMENTED__/*from  w  ww .  ja  v a2  s. c  om*/
 *
 * @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 .ja 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.action.DeleteAction.java

/**
 * Performs the <code>delete</code> action.
 *
 * @return <code>true</code> if delete executed successfully, otherwise
 * <code>false</code>.//from   w w w .ja  v  a 2  s  . c  o  m
 * @throws org.chiba.xml.xforms.exception.XFormsException if an error
 * occurred during <code>delete</code> processing.
 */
public boolean perform() throws XFormsException {
    // get instance
    String instanceId = getInstanceId(getLocationPath());
    Instance instance = this.model.getInstance(instanceId);

    // get pathes
    String locationPath = stripInstanceFunction(getLocationPath());
    String instancePath = locationPath + "[round(" + this.atAttribute + ")]";

    // check for existing path
    if (instance.existsNode(instancePath)) {
        // canonicalize existing path
        JXPathContext context = instance.getInstanceContext();
        instancePath = context.getPointer(instancePath).asPath();

        // strip down to delete path and position
        String deletePath = PathUtil.stripLastPredicate(instancePath);
        String lastStep = PathUtil.lastStep(instancePath);
        int deletePosition = PathUtil.stepIndex(lastStep);

        // delete specified node and dispatch notification event
        instance.deleteNode(deletePath, deletePosition);
        this.container.dispatch(instance.getTarget(), EventFactory.DELETE,
                deletePath + "[" + deletePosition + "]");

        // update behaviour
        setDeferredRebuild(this.model.getId(), true);
        setDeferredRecalculate(this.model.getId(), true);
        setDeferredRevalidate(this.model.getId(), true);
        setDeferredRefresh(this.model.getId(), true);
    } else {
        getLogger().warn(this + " perform: node " + instancePath + " does not exist");
    }

    // always indicate success
    return true;
}

From source file:org.chiba.xml.xforms.action.InsertAction.java

/**
 * Performs the <code>insert</code> action.
 *
 * @return always <code>true</code>.
 * @throws org.chiba.xml.xforms.exception.XFormsException if an error
 * occurred during <code>insert</code> processing.
 *///from  w  w  w .  j  a v  a 2  s  .  c  o m
public boolean perform() throws XFormsException {
    // get instance data
    String instanceId = getInstanceId(getLocationPath());
    Instance instance = this.model.getInstance(instanceId);

    // get path information
    String locationPath = stripInstanceFunction(getLocationPath());
    int contextSize = instance.countNodeset(locationPath);

    // set defaults
    String insertPath = locationPath;
    int insertPosition = 1;

    // check for existing path
    if (contextSize > 0) {
        // canonicalize path
        JXPathContext context = instance.getInstanceContext();
        context.setLenient(true);
        String instancePath = context.getPointer(locationPath + "[round(" + this.atAttribute + ")]").asPath();
        context.setLenient(false);

        if (instancePath.equals("null()")) {
            // caused by NaN
            insertPath = PathUtil.stripLastPredicate(context.getPointer(locationPath).asPath());
            insertPosition = contextSize + 1;
        } else {
            // strip down to insert path and position
            insertPath = PathUtil.stripLastPredicate(instancePath);
            contextSize = instance.countNodeset(insertPath);
            String lastStep = PathUtil.lastStep(instancePath);
            insertPosition = PathUtil.stepIndex(lastStep);

            // sanity checks (see 9.3.5)
            if (insertPosition < 1) {
                insertPosition = 1;
            }
            if (insertPosition > contextSize) {
                insertPosition = contextSize;
            }

            // check position specifier
            if (this.positionAttribute.equals("after")) {
                insertPosition++;
            }
        }
    }

    // insert specified node and dispatch notification event
    instance.insertNode(insertPath, insertPosition);
    this.container.dispatch(instance.getTarget(), EventFactory.INSERT, insertPath + "[" + insertPosition + "]");

    // update behaviour
    setDeferredRebuild(this.model.getId(), true);
    setDeferredRecalculate(this.model.getId(), true);
    setDeferredRevalidate(this.model.getId(), true);
    setDeferredRefresh(this.model.getId(), true);

    // always indicate success
    return true;
}

From source file:org.chiba.xml.xforms.action.SetIndexAction.java

/**
 * Performs the <code>setindex</code> action.
 *
 * @return always <code>true</code>.
 * @throws XFormsException if an error occurred during <code>setindex</code>
 * processing./* ww  w  .  j  a v  a2s.  c  o  m*/
 */
public boolean perform() throws XFormsException {
    // lookup repeat element
    Repeat repeat = (Repeat) this.container.lookup(this.repeatAttribute);

    // todo: check wether this interpretation is correct/useful and write a test for it
    String locationPath;
    if (isRepeated()) {
        // use the enclosing repeat as context
        RepeatItem repeatItem = (RepeatItem) getContainerObject().lookup(getRepeatItemId());
        locationPath = repeatItem.getLocationPath() + "[" + this.indexAttribute + "]";
    } else {
        // use the target repeat as context
        locationPath = repeat.getLocationPath() + "[" + this.indexAttribute + "]";
    }

    // get instance
    Instance instance = repeat.getModel().getInstance(repeat.getInstanceId());
    JXPathContext context = instance.getInstanceContext();

    int index;
    if (instance.existsNode(locationPath)) {
        // compute index from position
        String instancePath = context.getPointer(locationPath).asPath();
        index = PathUtil.stepIndex(PathUtil.lastStep(instancePath));
    } else {
        // compute index w/o context
        index = ((Integer) context.getValue(this.indexAttribute, java.lang.Integer.class)).intValue();
    }

    // check boundaries
    if (index < 1) {
        repeat.setIndex(1);
        this.container.dispatch(repeat.getTarget(), EventFactory.SCROLL_FIRST, null);
    } else if (index > repeat.getContextSize()) {
        index = repeat.getContextSize();
        repeat.setIndex(index);
        this.container.dispatch(repeat.getTarget(), EventFactory.SCROLL_LAST, null);
    } else {
        // set repeat index
        repeat.setIndex(index);
    }

    // update behaviour (defined by chiba ;-)
    setDeferredRefresh(repeat.getModel().getId(), true);

    // always indicate success
    return true;
}

From source file:org.chiba.xml.xforms.action.SetValueAction.java

/**
 * Performs the <code>setvalue</code> action.
 *
 * @return <code>true</code> if setvalue executed successfully, otherwise
 * <code>false</code>.//from   w w  w  . java 2 s.c o m
 * @throws XFormsException if an error occurred during <code>setvalue</code>
 * processing.
 */
public boolean perform() throws XFormsException {
    // get location path and instance id
    String locationPath = getLocationPath();
    String instanceId = getInstanceId(locationPath);

    // get instance
    Instance instance = getModel().getInstance(instanceId);

    if (instance.existsNode(locationPath)) {
        if (this.valueAttribute != null) {
            // evaluate value expression
            JXPathContext context = instance.getInstanceContext();
            Pointer instancePointer = context.getPointer(locationPath);
            JXPathContext valueContext = context.getRelativeContext(instancePointer);
            valueContext.setFunctions(context.getFunctions());
            Object value = valueContext.getValue(this.valueAttribute);
            String newValue = value != null ? value.toString() : "null";

            if (getLogger().isDebugEnabled()) {
                getLogger().debug(this + " perform: setting evaluated value '" + newValue + "'");
            }

            // set node value
            instance.setNodeValue(locationPath, newValue);
        } else {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug(this + " perform: setting literal value '" + this.nodeValue + "'");
            }

            // set node value
            instance.setNodeValue(locationPath, this.nodeValue);
        }

        // update behaviour
        setDeferredRecalculate(this.model.getId(), true);
        setDeferredRevalidate(this.model.getId(), true);
        setDeferredRefresh(this.model.getId(), true);

        // indicate success
        return true;
    }
    // todo: error handling ?
    getLogger().warn(this + " perform: nodeset " + getLocationPath() + " does not exist");

    // indicate failure
    return false;
}