Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        // determine if nodes are homogeneous, meaning their descendant structure is 'identical' for repeat purposes
        // identical means all children match, and the children's children match, and so on
        // repeatable children are ignored; as they do not have to exist in the same quantity for nodes to be homogeneous
        // however, the child repeatable nodes MUST be verified amongst themselves for homogeneity later
        // this function ignores the names of the two nodes
        public static Boolean isHomogeneous(TreeElement a, TreeElement b)
        {
            if (a.isLeaf() && b.isLeaf())
            {
                return(true);
            }
            else if (a.isChildable() && b.isChildable())
            {
                // verify that every (non-repeatable) node in a exists in b and vice
                // versa
                for (int k = 0; k < 2; k++)
                {
                    TreeElement n1 = (k == 0 ? a : b);
                    TreeElement n2 = (k == 0 ? b : a);

                    for (int i = 0; i < n1.getNumChildren(); i++)
                    {
                        TreeElement child1 = n1.getChildAt(i);
                        if (child1.repeatable)
                        {
                            continue;
                        }
                        TreeElement child2 = n2.getChild(child1.getName(), 0);
                        if (child2 == null)
                        {
                            return(false);
                        }
                        if (child2.repeatable)
                        {
                            throw new SystemException("shouldn't happen");
                        }
                    }
                }

                // compare children
                for (int i = 0; i < a.getNumChildren(); i++)
                {
                    TreeElement childA = a.getChildAt(i);
                    if (childA.repeatable)
                    {
                        continue;
                    }
                    TreeElement childB = b.getChild(childA.getName(), 0);
                    if (!isHomogeneous(childA, childB))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
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 '/'
        }
Exemplo n.º 5
0
        // for making new repeat instances; 'from' and 'to' must be unambiguous
        // references EXCEPT 'to' may be ambiguous at its final step
        // return true is successfully copied, false otherwise
        public TreeElement copyNode(TreeElement src, TreeReference to)
        {
            if (!to.isAbsolute())
            {
                throw new InvalidReferenceException("Destination reference must be absolute for copying", to);
            }

            // strip out dest node info and get dest parent
            String        dstName  = to.getNameLast();
            int           dstMult  = to.getMultLast();
            TreeReference toParent = to.getParentRef();

            TreeElement parent = resolveReference(toParent);

            if (parent == null)
            {
                throw new InvalidReferenceException("Null parent reference whle attempting to copy", toParent);
            }
            if (!parent.isChildable())
            {
                throw new InvalidReferenceException("Invalid Parent Node: cannot accept children.", toParent);
            }

            if (dstMult == TreeReference.INDEX_UNBOUND)
            {
                dstMult = parent.getChildMultiplicity(dstName);
            }
            else if (parent.getChild(dstName, dstMult) != null)
            {
                throw new InvalidReferenceException("Destination already exists!", to);
            }

            TreeElement dest = src.deepCopy(false);

            dest.setName(dstName);
            dest.multiplicity = dstMult;
            parent.addChild(dest);
            return(dest);
        }
Exemplo n.º 6
0
        /*
         * create the specified node in the tree, creating all intermediary nodes at
         * each step, if necessary. if specified node already exists, return null
         *
         * creating a duplicate node is only allowed at the final step. it will be
         * done if the multiplicity of the last step is ALL or equal to the count of
         * nodes already there
         *
         * at intermediate steps, the specified existing node is used; if
         * multiplicity is ALL: if no nodes exist, a new one is created; if one node
         * exists, it is used; if multiple nodes exist, it's an error
         *
         * return the newly-created node; modify ref so that it's an unambiguous ref
         * to the node
         */
        private TreeElement createNode(TreeReference ref_)
        {
            TreeElement node = root;

            for (int k = 0; k < ref_.size(); k++)
            {
                String name  = ref_.getName(k);
                int    count = node.getChildMultiplicity(name);
                int    mult  = ref_.getMultiplicity(k);

                TreeElement child;
                if (k < ref_.size() - 1)
                {
                    if (mult == TreeReference.INDEX_UNBOUND)
                    {
                        if (count > 1)
                        {
                            return(null); // don't know which node to use
                        }
                        else
                        {
                            // will use existing (if one and only one) or create new
                            mult = 0;
                            ref_.setMultiplicity(k, 0);
                        }
                    }

                    // fetch
                    child = node.getChild(name, mult);
                    if (child == null)
                    {
                        if (mult == 0)
                        {
                            // create
                            child = new TreeElement(name, count);
                            node.addChild(child);
                            ref_.setMultiplicity(k, count);
                        }
                        else
                        {
                            return(null); // intermediate node does not exist
                        }
                    }
                }
                else
                {
                    if (mult == TreeReference.INDEX_UNBOUND || mult == count)
                    {
                        if (k == 0 && root.getNumChildren() != 0)
                        {
                            return(null); // can only be one top-level node, and it
                            // already exists
                        }

                        if (!node.isChildable())
                        {
                            return(null); // current node can't have children
                        }

                        // create new
                        child = new TreeElement(name, count);
                        node.addChild(child);
                        ref_.setMultiplicity(k, count);
                    }
                    else
                    {
                        return(null); // final node must be a newly-created node
                    }
                }

                node = child;
            }

            return(node);
        }
Exemplo n.º 7
0
        // recursive helper function for expandReference
        // sourceRef: original path we're matching against
        // node: current node that has matched the sourceRef thus far
        // templateRef: explicit path that refers to the current node
        // refs: Vector to collect matching paths; if 'node' is a target node that
        // matches sourceRef, templateRef is added to refs
        private void expandReference(TreeReference sourceRef, TreeElement node, List <TreeReference> refs, Boolean includeTemplates)
        {
            int depth = node.getDepth();

            if (depth == sourceRef.size())
            {
                refs.Add(node.getRef());
            }
            else
            {
                String             name = sourceRef.getName(depth);
                int                mult = sourceRef.getMultiplicity(depth);
                List <TreeElement> set  = new List <TreeElement>();

                if (node.getNumChildren() > 0)
                {
                    if (mult == TreeReference.INDEX_UNBOUND)
                    {
                        int count = node.getChildMultiplicity(name);
                        for (int i = 0; i < count; i++)
                        {
                            TreeElement child = node.getChild(name, i);
                            if (child != null)
                            {
                                set.Add(child);
                            }
                            else
                            {
                                throw new InvalidOperationException(); // missing/non-sequential
                                // nodes
                            }
                        }
                        if (includeTemplates)
                        {
                            TreeElement template = node.getChild(name, TreeReference.INDEX_TEMPLATE);
                            if (template != null)
                            {
                                set.Add(template);
                            }
                        }
                    }
                    else if (mult != TreeReference.INDEX_ATTRIBUTE)
                    {
                        //TODO: Make this test mult >= 0?
                        //If the multiplicity is a simple integer, just get
                        //the appropriate child
                        TreeElement child = node.getChild(name, mult);
                        if (child != null)
                        {
                            set.Add(child);
                        }
                    }
                }

                if (mult == TreeReference.INDEX_ATTRIBUTE)
                {
                    TreeElement attribute = node.getAttribute(null, name);
                    set.Add(attribute);
                }

                for (IEnumerator e = set.GetEnumerator(); e.MoveNext();)
                {
                    expandReference(sourceRef, (TreeElement)e.Current, refs, includeTemplates);
                }
            }
        }