Ejemplo n.º 1
0
        public void copyItemsetNode(TreeElement copyNode, TreeReference destRef, FormDef f)
        {
            TreeElement templateNode = getTemplate(destRef);
            TreeElement newNode      = this.copyNode(templateNode, destRef);

            newNode.populateTemplate(copyNode, f);
        }
Ejemplo n.º 2
0
 //very similar to parent(), but assumes contextRef refers to a singular, existing node in the model
 //this means we can do '/a/b/c + ../../d/e/f = /a/d/e/f', which we couldn't do in parent()
 //return null if context ref is not absolute, or we parent up past the root node
 //NOTE: this function still works even when contextRef contains INDEX_UNBOUND multiplicites... conditions depend on this behavior,
 //  even though it's slightly icky
 public TreeReference anchor(TreeReference contextRef)
 {
     if (isAbsolute())
     {
         return(this.clone());
     }
     else if (!contextRef.isAbsolute())
     {
         return(null);
     }
     else
     {
         TreeReference newRef      = contextRef.clone();
         int           contextSize = contextRef.size();
         if (refLevel > contextSize)
         {
             return(null); //tried to do '/..'
         }
         else
         {
             for (int i = 0; i < refLevel; i++)
             {
                 newRef.removeLastLevel();
             }
             for (int i = 0; i < size(); i++)
             {
                 newRef.add(this.getName(i), this.getMultiplicity(i));
             }
             return(newRef);
         }
     }
 }
Ejemplo n.º 3
0
        //return a new reference that is this reference anchored to a passed-in parent reference
        //if this reference is absolute, return self
        //if this ref has 'parent' steps (..), it can only be anchored if the parent ref is a relative ref consisting only of other 'parent' steps
        //return null in these invalid situations
        public TreeReference parent(TreeReference parentRef)
        {
            if (isAbsolute())
            {
                return(this);
            }
            else
            {
                TreeReference newRef = parentRef.clone();

                if (refLevel > 0)
                {
                    if (!parentRef.isAbsolute() && parentRef.size() == 0)
                    {
                        parentRef.refLevel += refLevel;
                    }
                    else
                    {
                        return(null);
                    }
                }

                for (int i = 0; i < names.Count; i++)
                {
                    newRef.add(this.getName(i), this.getMultiplicity(i));
                }

                return(newRef);
            }
        }
Ejemplo n.º 4
0
        public TreeElement getTemplatePath(TreeReference ref_)
        {
            if (!ref_.isAbsolute())
            {
                return(null);
            }

            TreeElement node = root;

            for (int i = 0; i < ref_.size(); i++)
            {
                String name = ref_.getName(i);

                if (ref_.getMultiplicity(i) == TreeReference.INDEX_ATTRIBUTE)
                {
                    node = node.getAttribute(null, name);
                }
                else
                {
                    TreeElement newNode = node.getChild(name, TreeReference.INDEX_TEMPLATE);
                    if (newNode == null)
                    {
                        newNode = node.getChild(name, 0);
                    }
                    if (newNode == null)
                    {
                        return(null);
                    }
                    node = newNode;
                }
            }

            return(node);
        }
Ejemplo n.º 5
0
        //TODO: merge anchor() and parent()

        public TreeReference contextualize(TreeReference contextRef)
        {
            if (!contextRef.isAbsolute())
            {
                return(null);
            }

            TreeReference newRef = anchor(contextRef);

            for (int i = 0; i < contextRef.size() && i < newRef.size(); i++)
            {
                //If the the contextRef can provide a definition for a wildcard, do so
                if (TreeReference.NAME_WILDCARD.Equals(newRef.getName(i)) && !TreeReference.NAME_WILDCARD.Equals(contextRef.getName(i)))
                {
                    newRef.names[i] = contextRef.getName(i);
                }

                if (contextRef.getName(i).Equals(newRef.getName(i)))
                {
                    newRef.setMultiplicity(i, contextRef.getMultiplicity(i));
                }
                else
                {
                    break;
                }
            }

            return(newRef);
        }
Ejemplo n.º 6
0
        //returns true if 'this' is parent of 'child'
        //return true if 'this' equals 'child' only if properParent is false
        public Boolean isParentOf(TreeReference child, Boolean properParent)
        {
            if (refLevel != child.refLevel)
            {
                return(false);
            }
            if (child.size() < size() + (properParent ? 1 : 0))
            {
                return(false);
            }

            for (int i = 0; i < size(); i++)
            {
                if (!this.getName(i).Equals(child.getName(i)))
                {
                    return(false);
                }

                int parMult   = this.getMultiplicity(i);
                int childMult = child.getMultiplicity(i);
                if (parMult != INDEX_UNBOUND && parMult != childMult && !(i == 0 && parMult == 0 && childMult == INDEX_UNBOUND))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
        private ArrayList multiplicity; //ArrayList<Integer>

        public static TreeReference rootRef()
        {
            TreeReference root = new TreeReference();

            root.refLevel = REF_ABSOLUTE;
            return(root);
        }
Ejemplo n.º 8
0
        //return a new reference that is this reference anchored to a passed-in parent reference
        //if this reference is absolute, return self
        //if this ref has 'parent' steps (..), it can only be anchored if the parent ref is a relative ref consisting only of other 'parent' steps
        //return null in these invalid situations
        public virtual TreeReference parent(TreeReference parentRef)
        {
            if (Absolute)
            {
                return(this);
            }
            else
            {
                TreeReference newRef = parentRef.Clone();

                if (refLevel > 0)
                {
                    if (!parentRef.Absolute && parentRef.size() == 0)
                    {
                        parentRef.refLevel += refLevel;
                    }
                    else
                    {
                        return(null);
                    }
                }


                for (TreeReferenceLevel l: data)
                {
                    newRef.add(l.shallowCopy());
                }

                return(newRef);
            }
        }
Ejemplo n.º 9
0
        public static TreeReference selfRef()
        {
            TreeReference self = new TreeReference();

            self.refLevel = 0;
            return(self);
        }
Ejemplo n.º 10
0
        public static TreeReference selfRef()
        {
            TreeReference self = new TreeReference();

            self.refLevel    = 0;
            self.contextType = CONTEXT_INHERITED;
            return(self);
        }
Ejemplo n.º 11
0
        public static TreeReference rootRef()
        {
            TreeReference root = new TreeReference();

            root.refLevel    = REF_ABSOLUTE;
            root.contextType = CONTEXT_ABSOLUTE;
            return(root);
        }
Ejemplo n.º 12
0
        /**
         * clone and extend a reference by one level
         * @param ref
         * @param name
         * @param mult
         * @return
         */
        public TreeReference extendRef(String name, int mult)
        {
            //TODO: Shouldn't work for this if this is an attribute ref;
            TreeReference childRef = this.clone();

            childRef.add(name, mult);
            return(childRef);
        }
Ejemplo n.º 13
0
        /// <summary>Intersect this tree reference with another, returning a new tree reference
        /// which contains all of the common elements, starting with the root element.
        ///
        /// Note that relative references by their nature can't share steps, so intersecting
        /// any (or by any) relative ref will result in the root ref. Additionally, if the
        /// two references don't share any steps, the intersection will consist of the root
        /// reference.
        ///
        /// </summary>
        /// <param name="b">The tree reference to intersect
        /// </param>
        /// <returns> The tree reference containing the common basis of this ref and b
        /// </returns>
        public virtual TreeReference intersect(TreeReference b)
        {
            if (!this.Absolute || !b.Absolute)
            {
                return(TreeReference.rootRef());
            }
            if (this.Equals(b))
            {
                return(this);
            }


            TreeReference a;

            //A should always be bigger if one ref is larger than the other
            if (this.size() < b.size())
            {
                a = b.Clone(); b = this.Clone();
            }
            else
            {
                a = this.Clone(); b = b.Clone();
            }

            //Now, trim the refs to the same length.
            int diff = a.size() - b.size();

            for (int i = 0; i < diff; ++i)
            {
                a.removeLastLevel();
            }

            int aSize = a.size();

            //easy, but requires a lot of re-evaluation.
            for (int i = 0; i <= aSize; ++i)
            {
                if (a.Equals(b))
                {
                    return(a);
                }
                else if (a.size() == 0)
                {
                    return(TreeReference.rootRef());
                }
                else
                {
                    if (!a.removeLastLevel() || !b.removeLastLevel())
                    {
                        //I don't think it should be possible for us to get here, so flip if we do
                        throw new System.SystemException("Dug too deply into TreeReference during intersection");
                    }
                }
            }

            //The only way to get here is if a's size is -1
            throw new System.SystemException("Impossible state");
        }
Ejemplo n.º 14
0
        // same as resolveReference but return a vector containing all interstitial
        // nodes: top-level instance data node first, and target node last
        // returns null in all the same situations as resolveReference EXCEPT ref
        // '/' will instead return empty vector
        public ArrayList explodeReference(TreeReference ref_)
        {
            if (!ref_.isAbsolute())
            {
                return(null);
            }

            ArrayList   nodes = new ArrayList();
            TreeElement cur   = root;

            for (int i = 0; i < ref_.size(); i++)
            {
                String name = ref_.getName(i);
                int    mult = ref_.getMultiplicity(i);

                //If the next node down the line is an attribute
                if (mult == TreeReference.INDEX_ATTRIBUTE)
                {
                    //This is not the attribute we're testing
                    if (cur != root)
                    {
                        //Add the current node
                        nodes.Add(cur);
                    }
                    cur = cur.getAttribute(null, name);
                }

                //Otherwise, it's another child element
                else
                {
                    if (mult == TreeReference.INDEX_UNBOUND)
                    {
                        if (cur.getChildMultiplicity(name) == 1)
                        {
                            mult = 0;
                        }
                        else
                        {
                            // reference is not unambiguous
                            return(null);
                        }
                    }

                    if (cur != root)
                    {
                        nodes.Add(cur);
                    }

                    cur = cur.getChild(name, mult);
                    if (cur == null)
                    {
                        return(null);
                    }
                }
            }
            return(nodes);
        }
Ejemplo n.º 15
0
        //turn unambiguous ref into a generic ref
        public TreeReference genericize()
        {
            TreeReference genericRef = clone();

            for (int i = 0; i < genericRef.size(); i++)
            {
                genericRef.setMultiplicity(i, INDEX_UNBOUND);
            }
            return(genericRef);
        }
Ejemplo n.º 16
0
 public void templateData(FormInstance dm, TreeReference parentRef)
 {
     RestoreUtils.applyDataType(dm, "name", parentRef, typeof(String));
     RestoreUtils.applyDataType(dm, "form-id", parentRef, typeof(int));
     RestoreUtils.applyDataType(dm, "saved-on", parentRef,
                                Constants.DATATYPE_DATE_TIME);
     RestoreUtils.applyDataType(dm, "schema", parentRef, typeof(String));
     RestoreUtils.applyDataType(dm, "sent", parentRef, typeof(Boolean));
     // don't touch data for now
 }
Ejemplo n.º 17
0
        public virtual TreeReference removePredicates()
        {
            TreeReference predicateless = Clone();

            for (int i = 0; i < predicateless.data.size(); ++i)
            {
                predicateless.data.setElementAt(predicateless.data.elementAt(i).setPredicates(null), i);
            }
            return(predicateless);
        }
Ejemplo n.º 18
0
        //return a copy of the ref
        public TreeReference clone()
        {
            TreeReference newRef = new TreeReference();

            newRef.setRefLevel(this.refLevel);
            for (int i = 0; i < this.size(); i++)
            {
                newRef.add(this.getName(i), this.getMultiplicity(i));
            }
            return(newRef);
        }
Ejemplo n.º 19
0
        // take in a potentially-ambiguous ref, and return a vector of refs for all nodes that match the passed-in ref
        // meaning, search out all repeated nodes that match the pattern of the passed-in ref
        // every ref in the returned vector will be unambiguous (no index will ever be INDEX_UNBOUND)
        // does not return template nodes when matching INDEX_UNBOUND, but will match templates when INDEX_TEMPLATE is explicitly set
        // return null if ref is relative, otherwise return vector of refs (but vector will be empty is no refs match)
        // '/' returns {'/'}
        // can handle sub-repetitions (e.g., {/a[1]/b[1], /a[1]/b[2], /a[2]/b[1]})
        public List <TreeReference> expandReference(TreeReference ref_, Boolean includeTemplates)
        {
            if (!ref_.isAbsolute())
            {
                return(null);
            }

            List <TreeReference> v = new List <TreeReference>();

            expandReference(ref_, root, v, includeTemplates);
            return(v);
        }
Ejemplo n.º 20
0
        //turn unambiguous ref into a generic ref
        public virtual TreeReference genericize()
        {
            TreeReference genericRef = Clone();

            for (int i = 0; i < genericRef.size(); i++)
            {
                //TODO: It's not super clear whether template refs should get
                //genericized or not
                genericRef.setMultiplicity(i, INDEX_UNBOUND);
            }
            return(genericRef);
        }
Ejemplo n.º 21
0
        public TreeReference addNode(TreeReference ambigRef)
        {
            TreeReference ref_ = ambigRef.clone();

            if (createNode(ref_) != null)
            {
                return(ref_);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 22
0
        public TreeReference getParentRef()
        {
            //TODO: level
            TreeReference ref_ = this.clone();

            if (ref_.removeLastLevel())
            {
                return(ref_);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 23
0
 public TreeReference relativize(TreeReference parent)
 {
     if (parent.isParentOf(this, false))
     {
         TreeReference relRef = selfRef();
         for (int i = parent.size(); i < this.size(); i++)
         {
             relRef.add(this.getName(i), INDEX_UNBOUND);
         }
         return(relRef);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 24
0
        public TreeReference copyNode(TreeReference from, TreeReference to)
        {
            if (!from.isAbsolute())
            {
                throw new InvalidReferenceException("Source reference must be absolute for copying", from);
            }

            TreeElement src = resolveReference(from);

            if (src == null)
            {
                throw new InvalidReferenceException("Null Source reference while attempting to copy node", from);
            }

            return(copyNode(src, to).getRef());
        }
Ejemplo n.º 25
0
        public Boolean Equals(Object o)
        {
            if (this == o)
            {
                return(true);
            }
            else if (o is TreeReference)
            {
                TreeReference ref_ = (TreeReference)o;

                if (this.refLevel == ref_.refLevel && this.size() == ref_.size())
                {
                    for (int i = 0; i < this.size(); i++)
                    {
                        String nameA = this.getName(i);
                        String nameB = ref_.getName(i);
                        int    multA = this.getMultiplicity(i);
                        int    multB = ref_.getMultiplicity(i);

                        if (!nameA.Equals(nameB))
                        {
                            return(false);
                        }
                        else if (multA != multB)
                        {
                            if (i == 0 && (multA == 0 || multA == INDEX_UNBOUND) && (multB == 0 || multB == INDEX_UNBOUND))
                            {
                                // /data and /data[0] are functionally the same
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 26
0
        //return a copy of the ref
        //UPGRADE_ISSUE: The equivalent in .NET for method 'java.lang.Object.clone' returns a different type. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1224'"
        public virtual System.Object Clone()
        {
            TreeReference newRef = new TreeReference();

            newRef.RefLevel = this.refLevel;


            for (TreeReferenceLevel l: data)
            {
                newRef.add(l.shallowCopy());
            }

            //copy instances
            newRef.InstanceName = instanceName;
            newRef.Context      = this.contextType;
            return(newRef);
        }
Ejemplo n.º 27
0
        //TODO: merge anchor() and parent()

        public virtual TreeReference contextualize(TreeReference contextRef)
        {
            //TODO: Technically we should possibly be modifying context stuff here
            //instead of in the xpath stuff;
            if (!contextRef.Absolute)
            {
                return(null);
            }

            // I think contextualizing of absolute nodes still needs to be done.
            // They may contain predicates that need to be contextualized.

            TreeReference newRef = anchor(contextRef);

            // unclear...
            newRef.Context = contextRef.Context;

            //apply multiplicites and fill in wildcards as necessary based on the context ref
            for (int i = 0; i < contextRef.size() && i < newRef.size(); i++)
            {
                //If the the contextRef can provide a definition for a wildcard, do so
                if (TreeReference.NAME_WILDCARD.Equals(newRef.getName(i)) && !TreeReference.NAME_WILDCARD.Equals(contextRef.getName(i)))
                {
                    newRef.data.setElementAt(newRef.data.elementAt(i).setName(contextRef.getName(i)), i);
                }

                if (contextRef.getName(i).Equals(newRef.getName(i)))
                {
                    //We can't actually merge nodes if the newRef has predicates or filters
                    //on this expression, since those reset any existing resolutions which
                    //may have been done.
                    if (newRef.getPredicate(i) == null)
                    {
                        newRef.setMultiplicity(i, contextRef.getMultiplicity(i));
                    }
                }
                else
                {
                    break;
                }
            }

            return(newRef);
        }
Ejemplo n.º 28
0
        // take a ref that unambiguously refers to a single node and return that node
        // return null if ref is ambiguous, node does not exist, ref is relative, or ref is '/'
        // can be used to retrieve template nodes
        public TreeElement resolveReference(TreeReference ref_)
        {
            if (!ref_.isAbsolute())
            {
                return(null);
            }

            TreeElement node = root;

            for (int i = 0; i < ref_.size(); i++)
            {
                String name = ref_.getName(i);
                int    mult = ref_.getMultiplicity(i);

                if (mult == TreeReference.INDEX_ATTRIBUTE)
                {
                    //Should we possibly just return here?
                    //I guess technically we could step back...
                    node = node.getAttribute(null, name);
                    continue;
                }
                if (mult == TreeReference.INDEX_UNBOUND)
                {
                    if (node.getChildMultiplicity(name) == 1)
                    {
                        mult = 0;
                    }
                    else
                    {
                        // reference is not unambiguous
                        node = null;
                        break;
                    }
                }

                node = node.getChild(name, mult);
                if (node == null)
                {
                    break;
                }
            }

            return(node == root ? null : node);  // never return a reference to '/'
        }
Ejemplo n.º 29
0
        public TreeReference addNode(TreeReference ambigRef, IAnswerData data, int dataType)
        {
            TreeReference ref_ = ambigRef.clone();
            TreeElement   node = createNode(ref_);

            if (node != null)
            {
                if (dataType >= 0)
                {
                    node.dataType = dataType;
                }

                node.setValue(data);
                return(ref_);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 30
0
        /// <summary> Returns the subreference of this reference up to the level specified.
        ///
        /// Used to identify the reference context for a predicate at the same level
        ///
        /// Must be an absolute reference, otherwise will throw IllegalArgumentException
        ///
        /// </summary>
        /// <param name="level">
        /// </param>
        /// <returns>
        /// </returns>
        public virtual TreeReference getSubReference(int level)
        {
            if (!this.Absolute)
            {
                throw new System.ArgumentException("Cannot subreference a non-absolute ref");
            }

            //Copy construct
            TreeReference ret = new TreeReference();

            ret.refLevel     = this.refLevel;
            ret.contextType  = this.contextType;
            ret.instanceName = this.instanceName;

            ret.data = new List <TreeReferenceLevel>();
            for (int i = 0; i <= level; ++i)
            {
                ret.data.addElement(this.data.elementAt(i));
            }
            return(ret);
        }