Beispiel #1
0
        public SimplifiedSchemaObject(SimplifiedSchemaObject parent, XmlSchemaElement element)
        {
            this.Name   = element.Name;
            this.Type   = Shared.SimplifiedSchemaObjectType.Element;
            this.Parent = parent;

            string minOccurs = element.MinOccurs.ToString();
            string maxOccurs = element.MaxOccurs.ToString();

            this.Cardinality = string.Format("{0}..{1}",
                                             minOccurs != "79228162514264337593543950335" ? minOccurs : "*",
                                             maxOccurs != "79228162514264337593543950335" ? maxOccurs : "*");

            if (this.Cardinality == "0..0")
            {
                this.Conformance = "SHALL NOT";
            }
            else if (this.Cardinality.StartsWith("0.."))
            {
                this.Conformance = "MAY";
            }
            else if (this.Cardinality.StartsWith("1.."))
            {
                this.Conformance = "SHALL";
            }

            if (element.ElementSchemaType != null && !string.IsNullOrEmpty(element.ElementSchemaType.Name))
            {
                this.DataType = Helper.GetDataTypeName(element.ElementSchemaType.Name);
                this.Mixed    = element.ElementSchemaType.IsMixed;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Because of the performance hit involved in recursively checking each parent node to determine if it
        /// is a recursively recurring element, IsRecurring is used to determine if the type in question is
        /// actually a recurring type. When building complex types, the types that recursively occur
        /// are added to a list, and the list is checked first when the actual document's elements are being built.
        /// This reduces the number of times we have to recursively check parent nodes for re-occurance.
        /// </summary>
        public bool IsRecurring(SimplifiedSchemaObject parent, string compareName, string compareDataType)
        {
            if (this.recurringTypes.Contains(compareDataType))
            {
                compareName     = !string.IsNullOrEmpty(compareName) ? compareName : null;
                compareDataType = !string.IsNullOrEmpty(compareDataType) ? compareDataType : null;

                return(CheckRecursivelyRecurring(parent, compareName, compareDataType));
            }

            return(false);
        }
Beispiel #3
0
        private SimplifiedSchemaObject FindOrAddComplexType(XmlQualifiedName complexTypeName)
        {
            string simpleComplexTypeName            = Helper.GetDataTypeName(complexTypeName.Name);
            SimplifiedSchemaObject foundComplexType = this.ComplexTypes.SingleOrDefault(y => y.Name == simpleComplexTypeName);

            if (foundComplexType == null)
            {
                XmlSchemaComplexType schemaComplexType = this.schema.SchemaTypes[complexTypeName] as XmlSchemaComplexType;
                foundComplexType = new SimplifiedSchemaObject(schemaComplexType);
                this.ComplexTypes.Add(foundComplexType);
                InitializeChildren(schemaComplexType, foundComplexType, true);
                return(foundComplexType);
            }

            return(foundComplexType);
        }
Beispiel #4
0
        public SimplifiedSchemaObject GetObjectFromString(string uniqueString)
        {
            string[] contextSplit = uniqueString.Split('/');
            SimplifiedSchemaObject lastFoundObject = null;

            foreach (string cContext in contextSplit)
            {
                if (string.IsNullOrEmpty(cContext))
                {
                    continue;
                }

                string cActualContext  = cContext;
                string cActualDataType = string.Empty;

                if (this.uniqueStringContextRegex.IsMatch(cContext))
                {
                    Match uniqueStringMatch = this.uniqueStringContextRegex.Match(cContext);
                    cActualContext  = uniqueStringMatch.Groups[1].Value;
                    cActualDataType = uniqueStringMatch.Groups[2].Value;
                }

                List <SimplifiedSchemaObject> searchChildren = this.Children;

                if (lastFoundObject != null)
                {
                    searchChildren = lastFoundObject.Children;
                }

                if (cActualContext.StartsWith("@"))
                {
                    lastFoundObject = searchChildren.SingleOrDefault(y => y.Name == cActualContext.Substring(1) && y.IsAttribute);
                }
                else
                {
                    lastFoundObject = searchChildren.SingleOrDefault(y => y.Name == cActualContext);
                }

                if (lastFoundObject == null)
                {
                    return(null);
                }
            }

            return(lastFoundObject);
        }
Beispiel #5
0
        private void InitializeLevel(SimplifiedSchemaObject parent, XmlSchemaElement currentSchemaElement, bool initializingComplexType)
        {
            string currentName     = currentSchemaElement.Name;
            string currentDataType = currentSchemaElement.ElementSchemaType != null?Helper.GetDataTypeName(currentSchemaElement.ElementSchemaType.Name) : string.Empty;

            // If we are initializing a complex type, we should check the complex type to se if it is recursively used. If so, add it to the recurring types.
            // Otherwise, use the IsRecurring method to improve performance by only checking for known recurring types
            if (initializingComplexType)
            {
                if (CheckRecursivelyRecurring(parent, currentName, currentDataType))
                {
                    this.recurringTypes.Add(currentDataType);
                    return;
                }
            }
            else
            {
                if (IsRecurring(parent, currentName, currentDataType))
                {
                    return;
                }
            }

            SimplifiedSchemaObject newElement = new SimplifiedSchemaObject(parent, currentSchemaElement);

            if (currentSchemaElement.SchemaTypeName != null)
            {
                newElement.BaseComplexType = FindOrAddComplexType(currentSchemaElement.SchemaTypeName);
            }

            if (parent == null)
            {
                Children.Add(newElement);
            }
            else
            {
                parent.Children.Add(newElement);
            }

            if (newElement.BaseComplexType == null)
            {
                InitializeChildren(currentSchemaElement.ElementSchemaType as XmlSchemaComplexType, newElement, initializingComplexType);
            }
        }
Beispiel #6
0
        private bool CheckRecursivelyRecurring(SimplifiedSchemaObject parent, string compareName, string compareDataType)
        {
            if (parent == null)
            {
                return(false);
            }

            string currentName     = !string.IsNullOrEmpty(parent.Name) ? parent.Name : null;
            string currentDataType = !string.IsNullOrEmpty(parent.DataType) ? parent.DataType : null;

            if (currentName == compareName && currentDataType == compareDataType)
            {
                return(true);
            }

            if (CheckRecursivelyRecurring(parent.Parent, compareName, compareDataType))
            {
                return(true);
            }

            return(false);
        }
Beispiel #7
0
        public SimplifiedSchema GetSchemaFromContext(ContextTypes contextType, string context)
        {
            SimplifiedSchemaObject foundObject = null;

            if (contextType == ContextTypes.ComplexType)
            {
                foundObject = GetFromComplexType(context);
            }
            else if (contextType == ContextTypes.XPath)
            {
                foundObject = GetFromXpath(context);
            }

            if (foundObject != null)
            {
                SimplifiedSchema schema = new SimplifiedSchema();
                schema.Children.Add(foundObject);
                return(schema);
            }

            return(null);
        }
Beispiel #8
0
        public SimplifiedSchemaObject(SimplifiedSchemaObject parent, XmlSchemaAttribute attribute)
        {
            this.Name       = attribute.Name;
            this.Type       = SimplifiedSchemaObjectType.Attribute;
            this.Parent     = parent;
            this.Value      = attribute.DefaultValue;
            this.FixedValue = attribute.FixedValue;

            if (attribute.Use == XmlSchemaUse.Prohibited)
            {
                this.Conformance = "SHALL NOT";
                this.Cardinality = "0..0";
            }
            else if (attribute.Use == XmlSchemaUse.Required || !string.IsNullOrEmpty(FixedValue))
            {
                this.Conformance = "SHALL";
                this.Cardinality = "1..1";
            }
            else
            {
                this.Conformance = "MAY";
                this.Cardinality = "0..1";
            }

            if (attribute.SchemaTypeName != null && !string.IsNullOrEmpty(attribute.SchemaTypeName.Name))
            {
                if (attribute.SchemaTypeName.Name.Contains("."))
                {
                    this.DataType = attribute.SchemaTypeName.Name.Substring(attribute.SchemaTypeName.Name.LastIndexOf(".") + 1);
                }
                else
                {
                    this.DataType = attribute.SchemaTypeName.Name;
                }
            }
        }
Beispiel #9
0
        private SimplifiedSchemaObject GetFromXpath(string xpath)
        {
            string[] xpathParts            = xpath.Split('/');
            SimplifiedSchemaObject current = null;

            foreach (string cXpathPart in xpathParts)
            {
                if (current == null)
                {
                    current = this.Children.SingleOrDefault(y => y.Name.ToLower() == cXpathPart);
                }
                else
                {
                    current = current.Children.SingleOrDefault(y => y.Name.ToLower() == cXpathPart);
                }

                if (current == null)
                {
                    return(null);
                }
            }

            return(null);
        }
Beispiel #10
0
        private void InitializeChildren(XmlSchemaComplexType complexType, SimplifiedSchemaObject element, bool initializingComplexType)
        {
            if (complexType == null)
            {
                return;
            }

            XmlSchemaComplexContentRestriction restriction = complexType.ContentModel != null ? complexType.ContentModel.Content as XmlSchemaComplexContentRestriction : null;
            XmlSchemaComplexContentExtension   extension   = complexType.ContentModel != null ? complexType.ContentModel.Content as XmlSchemaComplexContentExtension : null;

            XmlSchemaObjectCollection baseAttributes = null;

            if (restriction != null)
            {
                baseAttributes = restriction.Attributes;
            }
            else if (extension != null)
            {
                baseAttributes = extension.Attributes;
            }

            if (baseAttributes != null)
            {
                foreach (XmlSchemaObject cObject in baseAttributes)
                {
                    XmlSchemaAttribute cAttribute = cObject as XmlSchemaAttribute;

                    if (cAttribute != null)
                    {
                        element.Children.Add(
                            new SimplifiedSchemaObject(element, cAttribute));
                    }
                }
            }

            foreach (XmlSchemaObject cObject in complexType.Attributes)
            {
                XmlSchemaAttribute cAttribute = cObject as XmlSchemaAttribute;

                if (cAttribute != null)
                {
                    element.Children.Add(
                        new SimplifiedSchemaObject(element, cAttribute));
                }
            }

            //if (element.ToString() == "ClinicalDocument/recordTarget/patientRole")
            //    Console.WriteLine("Test");

            if (complexType.ContentTypeParticle is XmlSchemaSequence)
            {
                XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;

                foreach (XmlSchemaObject cObject in sequence.Items)
                {
                    // Check here if this element's name is the same as it's parent or grand-parents
                    XmlSchemaElement cElement = cObject as XmlSchemaElement;
                    XmlSchemaChoice  cChoice  = cObject as XmlSchemaChoice;

                    if (cElement != null)
                    {
                        InitializeLevel(element, cElement, initializingComplexType);
                    }
                    else if (cChoice != null)
                    {
                        foreach (XmlSchemaObject cChoiceObject in cChoice.Items)
                        {
                            XmlSchemaElement cChoiceElement = cChoiceObject as XmlSchemaElement;

                            if (cChoiceElement != null)
                            {
                                InitializeLevel(element, cChoiceElement, initializingComplexType);
                            }
                        }
                    }
                }
            }
        }