Beispiel #1
0
        internal NamedContentModelEntity GetNamedEntity(XName name)
        {
            NamedContentModelEntity namedEntity = null;

            elementPositions.TryGetValue(name, out namedEntity);
            return(namedEntity);
        }
        private void CheckChoiceBranches(XName currentBranch, XElement parentElement)
        {
            var elementsToRemove = new List <XElement>();
            NamedContentModelEntity otherBranch = null;

            foreach (XElement instanceElement in parentElement.Elements())
            {
                if (instanceElement.Name == currentBranch)
                {
                    //This is the element we set just now
                    continue;
                }

                otherBranch = GetNamedEntity(instanceElement.Name);
                if (otherBranch != null)
                {
                    //It is a branch of choice
                    Debug.Assert(otherBranch.ParentContentModel ==
                                 this); //Currently this should be invoked only for flat choices
                    elementsToRemove.Add(instanceElement);
                }
            }

            foreach (XElement elementToRemove in elementsToRemove)
            {
                elementToRemove.Remove();
            }
        }
Beispiel #3
0
        internal XElement FindElementPosition(NamedContentModelEntity namedEntity, XElement parentElement, bool addToExisting, out EditAction editAction)
        {
            XElement xElement;

            Debug.Assert(namedEntity != null);
            editAction = EditAction.None;
            int      newElementPos = namedEntity.ElementPosition;
            XElement lastElement   = this.GetLastElement(parentElement);

            if (lastElement != null)
            {
                int lastElementPos = this.GetNamedEntity(lastElement.Name).ElementPosition;
                if (newElementPos == lastElementPos)
                {
                    if (!addToExisting)
                    {
                        editAction = EditAction.Update;
                    }
                    else
                    {
                        editAction = EditAction.Append;
                    }
                    xElement = lastElement;
                    return(xElement);
                }
                else if (newElementPos > lastElementPos)
                {
                    editAction = EditAction.Append;
                    xElement   = lastElement;
                    return(xElement);
                }
            }
            int      instanceElementPos       = -1;
            XElement instanceElem             = null;
            IEnumerator <XElement> enumerator = parentElement.Elements().GetEnumerator();

            while (enumerator.MoveNext())
            {
                instanceElem       = enumerator.Current;
                instanceElementPos = this.GetElementPosition(instanceElem.Name);
                if (instanceElementPos == newElementPos)
                {
                    if (!addToExisting)
                    {
                        editAction = EditAction.Update;
                        xElement   = instanceElem;
                        return(xElement);
                    }
                }
                else if (instanceElementPos > newElementPos)
                {
                    editAction = EditAction.AddBefore;
                    xElement   = instanceElem;
                    return(xElement);
                }
            }
            editAction = EditAction.Append;
            xElement   = instanceElem;
            return(xElement);
        }
Beispiel #4
0
        internal void AddElementInPosition(XName name, XElement parentElement, bool addToExisting, XTypedElement xObj)
        {
            NamedContentModelEntity namedEntity = GetNamedEntity(name);

            if (namedEntity == null)
            {
                // See http://linqtoxsd.codeplex.com/WorkItem/View.aspx?WorkItemId=3542
                throw new LinqToXsdException(
                          "Name does not belong in content model. Cannot set value for child " +
                          name.LocalName);
            }
            EditAction editAction    = EditAction.None;
            XElement   elementMarker = FindElementPosition(namedEntity, parentElement, addToExisting, out editAction);

            XElement newElement = XTypedServices.GetXElement(xObj, name);

            Debug.Assert(xObj != null);

            switch (editAction)
            {
            case EditAction.Append:
                parentElement.Add(newElement);
                break;

            case EditAction.Update:
                elementMarker.AddBeforeSelf(newElement);
                elementMarker.Remove();
                break;

            case EditAction.AddBefore:
                elementMarker.AddBeforeSelf(newElement);
                break;
            }
        }
Beispiel #5
0
        internal void AddValueInPosition(XName name, XElement parentElement, bool addToExisting, object value, XmlSchemaDatatype datatype)
        {
            NamedContentModelEntity namedEntity = GetNamedEntity(name);

            if (namedEntity == null)
            {
                throw new LinqToXsdException("Name does not belong in content model. Cannot set value for child " + namedEntity.Name);
            }
            EditAction editAction    = EditAction.None;
            XElement   elementMarker = FindElementPosition(namedEntity, parentElement, addToExisting, out editAction);

            Debug.Assert(datatype != null); //Simple typed value add or set

            switch (editAction)
            {
            case EditAction.Append:
                parentElement.Add(new XElement(name, XTypedServices.GetXmlString(value, datatype, parentElement)));
                break;

            case EditAction.Update:
                Debug.Assert(elementMarker != null);
                elementMarker.Value = XTypedServices.GetXmlString(value, datatype, elementMarker);
                break;

            case EditAction.AddBefore:
                Debug.Assert(elementMarker != null);
                elementMarker.AddBeforeSelf(new XElement(name, XTypedServices.GetXmlString(value, datatype, elementMarker)));
                break;

            default:
                throw new InvalidOperationException();
            }
        }
Beispiel #6
0
        protected SchemaAwareContentModelEntity(params ContentModelEntity[] items)
        {
            elementPositions = new Dictionary <XName, NamedContentModelEntity>();

            foreach (ContentModelEntity cmEntity in items)
            {
                NamedContentModelEntity named = cmEntity as NamedContentModelEntity;
                if (named != null)
                {
                    if (!elementPositions.ContainsKey(named.Name))   //Pick the first position for a repeating name
                    {
                        named.ElementPosition    = last++;
                        named.ParentContentModel = this;
                        elementPositions.Add(named.Name, named);
                        //Add subst members to the same position as head if this a substitution head
                        CheckSubstitutionGroup(named);
                    }
                }
                else   //cmEntity is choice or sequence
                {
                    SchemaAwareContentModelEntity scmEntity = cmEntity as SchemaAwareContentModelEntity;
                    Debug.Assert(scmEntity != null);
                    foreach (NamedContentModelEntity childEntity in scmEntity.ElementPositions.Values)
                    {
                        if (!elementPositions.ContainsKey(childEntity.Name))
                        {
                            childEntity.ElementPosition = last++; //Update position w.r.t parent
                            elementPositions.Add(childEntity.Name, childEntity);
                        }
                    }
                }
            }
        }
Beispiel #7
0
        internal int GetElementPosition(XName name)
        {
            NamedContentModelEntity named = GetNamedEntity(name);

            if (named != null)
            {
                return(named.ElementPosition);
            }
            return(-1);
        }
Beispiel #8
0
        internal XElement FindElementPosition(NamedContentModelEntity namedEntity, XElement parentElement, bool addToExisting, out EditAction editAction)
        {
            Debug.Assert(namedEntity != null);
            editAction = EditAction.None;
            int      newElementPos = namedEntity.ElementPosition;
            XElement lastElement   = GetLastElement(parentElement);

            if (lastElement != null)   //Optimization to check last first
            {
                int lastElementPos = GetNamedEntity(lastElement.Name).ElementPosition;
                if (newElementPos == lastElementPos)
                {
                    if (addToExisting)
                    {
                        editAction = EditAction.Append;
                    }
                    else
                    {
                        editAction = EditAction.Update;
                    }
                    return(lastElement);
                }
                if (newElementPos > lastElementPos)   //We need to add the new element at the end
                {
                    editAction = EditAction.Append;
                    return(lastElement);
                }
            }
            int      instanceElementPos       = -1;
            XElement instanceElem             = null;
            IEnumerator <XElement> enumerator = parentElement.Elements().GetEnumerator();

            while (enumerator.MoveNext())
            {
                instanceElem       = enumerator.Current;
                instanceElementPos = GetElementPosition(instanceElem.Name);
                if (instanceElementPos == newElementPos)
                {
                    if (!addToExisting)   //Matching element found for update
                    {
                        editAction = EditAction.Update;
                        return(instanceElem);
                    }
                }
                else if (instanceElementPos > newElementPos)   //Found first element greater than new position
                {
                    editAction = EditAction.AddBefore;
                    return(instanceElem);
                }
            }
            //Either its the first element being added or Scanned the whole list, end of list reached
            editAction = EditAction.Append;
            return(instanceElem);
        }
Beispiel #9
0
        private void CheckSubstitutionGroup(NamedContentModelEntity named)
        {
            SubstitutedContentModelEntity substEntity = named as SubstitutedContentModelEntity;

            if (substEntity != null)
            {
                foreach (XName name in substEntity.Members)  //Add Subst members to the lookup table
                {
                    if (!elementPositions.ContainsKey(name))
                    {
                        elementPositions.Add(name, named);
                    }
                }
            }
        }
Beispiel #10
0
        private void CheckSubstitutionGroup(NamedContentModelEntity named)
        {
            SubstitutedContentModelEntity substEntity = named as SubstitutedContentModelEntity;

            if (substEntity != null)
            {
                XName[] members = substEntity.Members;
                for (int i = 0; i < (int)members.Length; i++)
                {
                    XName name = members[i];
                    if (!this.elementPositions.ContainsKey(name))
                    {
                        this.elementPositions.Add(name, named);
                    }
                }
            }
        }
Beispiel #11
0
        internal void AddElementInPosition(XName name, XElement parentElement, bool addToExisting, XTypedElement xObj)
        {
            NamedContentModelEntity namedEntity = this.GetNamedEntity(name);

            if (namedEntity == null)
            {
                throw new LinqToXsdException(string.Concat("Name does not belong in content model. Cannot set value for child ", name.LocalName));
            }
            EditAction editAction    = EditAction.None;
            XElement   elementMarker = this.FindElementPosition(namedEntity, parentElement, addToExisting, out editAction);
            XElement   newElement    = XTypedServices.GetXElement(xObj, name);

            Debug.Assert(xObj != null);
            switch (editAction)
            {
            case EditAction.AddBefore:
            {
                elementMarker.AddBeforeSelf(newElement);
                return;
            }

            case EditAction.AddAfter:
            {
                return;
            }

            case EditAction.Append:
            {
                parentElement.Add(newElement);
                return;
            }

            case EditAction.Update:
            {
                elementMarker.AddBeforeSelf(newElement);
                elementMarker.Remove();
                return;
            }

            default:
            {
                return;
            }
            }
        }
Beispiel #12
0
        protected SchemaAwareContentModelEntity(params ContentModelEntity[] items)
        {
            int num;

            this.elementPositions = new Dictionary <XName, NamedContentModelEntity>();
            ContentModelEntity[] contentModelEntityArray = items;
            for (int i = 0; i < (int)contentModelEntityArray.Length; i++)
            {
                ContentModelEntity      cmEntity = contentModelEntityArray[i];
                NamedContentModelEntity named    = cmEntity as NamedContentModelEntity;
                if (named == null)
                {
                    SchemaAwareContentModelEntity scmEntity = cmEntity as SchemaAwareContentModelEntity;
                    Debug.Assert(scmEntity != null);
                    foreach (NamedContentModelEntity childEntity in scmEntity.ElementPositions.Values)
                    {
                        if (!this.elementPositions.ContainsKey(childEntity.Name))
                        {
                            SchemaAwareContentModelEntity schemaAwareContentModelEntity = this;
                            int num1 = schemaAwareContentModelEntity.last;
                            num = num1;
                            schemaAwareContentModelEntity.last = num1 + 1;
                            childEntity.ElementPosition        = num;
                            this.elementPositions.Add(childEntity.Name, childEntity);
                        }
                    }
                }
                else if (!this.elementPositions.ContainsKey(named.Name))
                {
                    SchemaAwareContentModelEntity schemaAwareContentModelEntity1 = this;
                    int num2 = schemaAwareContentModelEntity1.last;
                    num = num2;
                    schemaAwareContentModelEntity1.last = num2 + 1;
                    named.ElementPosition    = num;
                    named.ParentContentModel = this;
                    this.elementPositions.Add(named.Name, named);
                    this.CheckSubstitutionGroup(named);
                }
            }
        }
        private void CheckChoiceBranches(XName currentBranch, XElement parentElement)
        {
            List <XElement>         elementsToRemove = new List <XElement>();
            NamedContentModelEntity otherBranch      = null;

            foreach (XElement instanceElement in parentElement.Elements())
            {
                if (!(instanceElement.Name == currentBranch))
                {
                    otherBranch = base.GetNamedEntity(instanceElement.Name);
                    if (otherBranch != null)
                    {
                        Debug.Assert(otherBranch.ParentContentModel == this);
                        elementsToRemove.Add(instanceElement);
                    }
                }
            }
            foreach (XElement elementToRemove in elementsToRemove)
            {
                elementToRemove.Remove();
            }
        }
Beispiel #14
0
 internal XElement FindElementPosition(NamedContentModelEntity namedEntity, XElement parentElement, bool addToExisting, out EditAction editAction) {
     Debug.Assert(namedEntity != null);
     editAction = EditAction.None;
     int newElementPos = namedEntity.ElementPosition;
     XElement lastElement = GetLastElement(parentElement);
     if (lastElement != null) { //Optimization to check last first
         int lastElementPos = GetNamedEntity(lastElement.Name).ElementPosition;
         if (newElementPos == lastElementPos) {
             if (addToExisting) {
                 editAction = EditAction.Append;
             }
             else {
                 editAction = EditAction.Update;
             }
             return lastElement;                        
         }
         if (newElementPos > lastElementPos) { //We need to add the new element at the end
             editAction = EditAction.Append;
             return lastElement;
         }
     }
     int instanceElementPos = -1;
     XElement instanceElem = null;
     IEnumerator<XElement> enumerator = parentElement.Elements().GetEnumerator();
     
     while(enumerator.MoveNext()) {
         instanceElem = enumerator.Current;
         instanceElementPos = GetElementPosition(instanceElem.Name);
         if (instanceElementPos == newElementPos) { 
             if (!addToExisting) { //Matching element found for update
                 editAction = EditAction.Update;
                 return instanceElem;
             }
         }
         else if (instanceElementPos > newElementPos) { //Found first element greater than new position
             editAction = EditAction.AddBefore;
             return instanceElem;
         }
     }
     //Either its the first element being added or Scanned the whole list, end of list reached         
     editAction = EditAction.Append;
     return instanceElem;
 }
Beispiel #15
0
 private void CheckSubstitutionGroup(NamedContentModelEntity named) {
     SubstitutedContentModelEntity substEntity = named as SubstitutedContentModelEntity;
     if (substEntity != null) {
         foreach(XName name in substEntity.Members) { //Add Subst members to the lookup table
             if (!elementPositions.ContainsKey(name)) {
                 elementPositions.Add(name, named);
             }
         }
     }
 }
Beispiel #16
0
        internal int GetElementPosition(XName name)
        {
            NamedContentModelEntity named = this.GetNamedEntity(name);

            return(named == null ? -1 : named.ElementPosition);
        }