/// <summary>
        /// Clone IPluglet
        /// </summary>
        /// <param name="pluglet"></param>
        /// <param name="withChild">True if childs also need to be cloned</param>
        /// <returns></returns>
        public static Pluglet Clone(this IPluglet pluglet, bool withChild)
        {
            Pluglet newPluglet = new Pluglet(pluglet.Name, pluglet.Definition, pluglet.PlugletType, null,
                                             pluglet.RepetitionInfo.MinOccurs, pluglet.RepetitionInfo.MaxOccurs);

            newPluglet.DataType             = pluglet.DataType;
            newPluglet.IsRecursiveMandatory = pluglet.IsRecursiveMandatory;

            if (pluglet.Attributes != null)
            {
                foreach (IPluglet attr in pluglet.Attributes)
                {
                    newPluglet.Attributes.Add(attr.Clone(false));
                }
            }

            if (withChild && pluglet.Children != null)
            {
                foreach (Pluglet child in pluglet.Children)
                {
                    newPluglet.Children.Add(child.Clone(withChild));
                }
            }

            return(newPluglet);
        }
        /// <summary>
        /// Remove loops from pluglet
        /// </summary>
        /// <param name="pluglet"></param>
        /// <param name="canonicalPluglet"></param>
        /// <returns></returns>
        public static Pluglet RemoveLoops(this IPluglet pluglet, IPluglet canonicalPluglet)
        {
            if (pluglet == null)
            {
                throw new ArgumentNullException("pluglet");
            }

            Pluglet resultPluglet = pluglet.Clone(false);

            if (pluglet.Children != null && pluglet.Children.Count > 0)
            {
                int      childIndex;
                IPluglet prevChild, mergedChild, canonicalChildPluglet = null;
                prevChild = mergedChild = canonicalChildPluglet = null;

                for (childIndex = 0; childIndex < pluglet.Children.Count; childIndex++)
                {
                    canonicalChildPluglet        = canonicalPluglet.Children.First(p => p.Name == pluglet.Children[childIndex].Name);
                    pluglet.Children[childIndex] = pluglet.Children[childIndex].RemoveLoops(canonicalChildPluglet);

                    if (prevChild != null)
                    {
                        if (string.Equals(prevChild.Name, pluglet.Children[childIndex].Name, StringComparison.OrdinalIgnoreCase))
                        {
                            mergedChild = mergedChild.Merge(pluglet.Children[childIndex], canonicalChildPluglet);
                        }
                        else
                        {
                            resultPluglet.Children.Add(mergedChild);
                            mergedChild = null;
                        }
                    }

                    prevChild = pluglet.Children[childIndex];
                    if (mergedChild == null)
                    {
                        mergedChild = pluglet.Children[childIndex];
                    }
                }

                if (mergedChild == null)
                {
                    mergedChild = pluglet.Children[childIndex - 1];
                }

                resultPluglet.Children.Add(mergedChild);
            }

            return(resultPluglet);
        }
Пример #3
0
        /// <summary>
        /// Create plug based on the fatpipeDocument (instance tree).
        /// Generated plug will only have pluglets referred in instance tree.
        /// Generated plug will have repeated pluglets in case of loops.
        /// </summary>
        /// <param name="documentFragment"></param>
        /// <returns></returns>
        public static Pluglet GeneratePluglet(this IDocumentFragment documentFragment)
        {
            Pluglet pluglet = documentFragment.Pluglet.Clone(false);

            // Check if instance tree has any childrens, if not then copy all childrens from existingPluglet
            if (documentFragment.Children != null)
            {
                foreach (IDocumentFragment child in documentFragment.Children)
                {
                    pluglet.Children.Add(child.GeneratePluglet());
                }
            }

            return(pluglet);
        }
Пример #4
0
        /*
         * This method is used to parse an element. An element declaration
         * looks like one of the following
         *
         * <xs:element name="name" min="0" max="1" type="ns:type">
         *
         * <xs:element ref="name" min="0" max="1">
         *
         * <xs:element name="name" min="0" max="1">
         *  <xs:complexType>
         *    <xs:sequence>
         *       <xs:element name = ...>
         *       <xs:element name = ...>
         *       <xs:sequence min=0 max=1>
         *          <xs:element ...>
         *   </xs:sequence>
         *  </xs:complexType>
         * </xs:element>
         */
        private static IPluglet ParseElement(IPluglet parent, XmlSchemaElement elem, XmlSchema schema)
        {
            IPluglet         ret     = null;
            XmlSchemaElement refElem = elem;

            if (elem.Name == null)
            {
                refElem = (XmlSchemaElement)schema.Elements[elem.RefName];
            }

            //Console.WriteLine("ParseElement.. " + refElem.Name);
            Dictionary <string, string> pairs;
            string description = ReadDocumentationFromElement(elem, !(elem.ElementType is XmlSchemaComplexType), out pairs);

            if (string.IsNullOrEmpty(description) && string.IsNullOrEmpty(elem.Name))
            {
                description = ReadDocumentationFromElement(refElem, !(elem.ElementType is XmlSchemaComplexType), out pairs);
            }

            int maxOccurs = elem.MaxOccursString == "unbounded" ? -1 : (int)elem.MaxOccurs;
            //int maxOccurs = elem.MaxOccursString == "unbounded" || string.IsNullOrEmpty(elem.MaxOccursString) ? -1 : (int)elem.MaxOccurs;
            int minOccurs = (int)elem.MinOccurs;

            //Console.WriteLine("Parsing element " + elem.QualifiedName.Name);
            XmlSchemaComplexType elemType = elem.ElementType as XmlSchemaComplexType;

            if (elemType != null)
            {
                string name = elem.QualifiedName.Name;

                if (name.IndexOf("Loop", StringComparison.Ordinal) >= 0)
                {
                    ret = new Pluglet(name, description, PlugletType.Loop, parent, minOccurs, maxOccurs);
                }

                else if (name != "UNA" && name != "UNB" && name != "UNG")
                {
                    PlugletType type = PlugletType.Segment;
                    if (parent != null && parent.PlugletType == PlugletType.Segment && parent.Parent != null)
                    {
                        type = PlugletType.CompositeData;
                    }

                    //the only exception is for EDI documents where we have component fields, they begin with C0*
                    if (name.StartsWith("C0"))
                    {
                        type = PlugletType.Data;
                    }

                    ret = new Pluglet(name, description, type, parent, minOccurs, maxOccurs);
                }
                else // In case of UNA, UNB and UNG elements just return (ignore those elements)
                {
                    return(null);
                }

                XmlSchemaComplexType ct = (XmlSchemaComplexType)refElem.ElementType;
                ParseComplexType(ret, ct, schema);
            }

            /*
             * If elementType is simple, create a SimpleField and proceed
             */
            else if (elem.ElementType is XmlSchemaSimpleType || elem.ElementType is XmlSchemaDatatype)
            {
                string name = elem.QualifiedName.Name;
                ret = new Pluglet(name, description, PlugletType.Data, parent, minOccurs, maxOccurs);
            }

            if (ret.PlugletType == PlugletType.Data)
            {
                //This is old behavior for BTS schemas. Raj@5/25/2013
                //ret.DataType = DataTypeFactory.CreateDataTypeFromXmlSchema(elem, false);

                //Now we need to use WPC logic
                string dataTypeName, minL, maxL;
                dataTypeName = RetrieveDataTypeAndStampStandarDENumber(ret, elem, out minL, out maxL);
                if (string.IsNullOrWhiteSpace(dataTypeName))
                {
                    ret.DataType = DataTypeFactory.CreateDataTypeFromXmlSchema2(elem, false);
                }
                else
                {
                    ret.DataType = DataTypeFactory.CreateDataTypeFromXmlSchema(elem, false, dataTypeName, minL, maxL);
                }
            }

            return(ret);
        }