Esempio n. 1
0
        // Start a snapshot at the root object, using own namespace manager.

        // Creates an XElement representing this object, and appends it as the root
        // element of the Document.
        //
        // The Document must not yet have a root element Additionally, the supplied
        // schemaManager must be populated with any application-level namespaces
        // referenced in the document that the parentElement resides within.
        // (Normally this is achieved simply by using AppendXml passing in a new
        // schemaManager - see ToXml() or XmlSnapshot).

        private Place AppendXml(INakedObjectAdapter nakedObjectAdapter)
        {
            var fullyQualifiedClassName = nakedObjectAdapter.Spec.FullName;

            Schema.SetUri(fullyQualifiedClassName); // derive URI from fully qualified name

            var place = ObjectToElement(nakedObjectAdapter);

            var element          = place.XmlElement;
            var xsElementElement = element.Annotation <XElement>();

            XmlDocument.Add(element);

            XsdElement.Add(xsElementElement);

            Schema.SetTargetNamespace(XsdDocument, fullyQualifiedClassName);

            var schemaLocationFileName = fullyQualifiedClassName + ".xsd";

            Schema.AssignSchema(XmlDocument, fullyQualifiedClassName, schemaLocationFileName);

            XmlElement             = element;
            SchemaLocationFileName = schemaLocationFileName;

            return(place);
        }
Esempio n. 2
0
        public Xsd2XmlGenerator(string currentFileName)
        {
            InitializeComponent();
            _currentFileName = currentFileName;

            try
            {
                SchemaParser           schema = new SchemaParser(currentFileName, false);
                IEnumerable <IXsdNode> nodes  = schema.GetVirtualRoot().GetChildren();
                foreach (IXsdNode xsdNode in nodes)
                {
                    XsdElement ele = xsdNode as XsdElement;
                    if (ele != null)
                    {
                        cboRoot.Items.Add(ele.Name);
                    }
                }
            }
            catch
            {
                //do nothing
            }

            _foldingManager  = FoldingManager.Install(edtResult.TextArea);
            _foldingStrategy = new XmlFoldingStrategy();
        }
Esempio n. 3
0
        // Creates an XElement representing this object, and appends it as the root
        // element of the Document.
        //
        // The Document must not yet have a root element Additionally, the supplied
        // schemaManager must be populated with any application-level namespaces
        // referenced in the document that the parentElement resides within.
        // (Normally this is achieved simply by using AppendXml passing in a new
        // schemaManager - see ToXml() or XmlSnapshot).

        private Place AppendXml(INakedObject nakedObject)
        {
            Log.Debug("appendXml(" + DoLog("obj", nakedObject) + "')");

            string fullyQualifiedClassName = nakedObject.Spec.FullName;

            Schema.SetUri(fullyQualifiedClassName); // derive URI from fully qualified name

            Place place = ObjectToElement(nakedObject);

            XElement element          = place.XmlElement;
            var      xsElementElement = element.Annotation <XElement>();

            Log.Debug("appendXml(NO): add as element to XML doc");
            XmlDocument.Add(element);

            Log.Debug("appendXml(NO): add as xs:element to xs:schema of the XSD document");
            XsdElement.Add(xsElementElement);

            Log.Debug("appendXml(NO): set target name in XSD, derived from FQCN of obj");
            Schema.SetTargetNamespace(XsdDocument, fullyQualifiedClassName);

            Log.Debug("appendXml(NO): set schema location file name to XSD, derived from FQCN of obj");
            string schemaLocationFileName = fullyQualifiedClassName + ".xsd";

            Schema.AssignSchema(XmlDocument, fullyQualifiedClassName, schemaLocationFileName);

            Log.Debug("appendXml(NO): copy into snapshot obj");
            XmlElement             = element;
            SchemaLocationFileName = schemaLocationFileName;

            return(place);
        }
Esempio n. 4
0
        private void AddProperties(Type type, XElement sequenceElement)
        {
            var properties = type.GetProperties();

            foreach (var propertyInfo in properties)
            {
                var typeName        = Common.GetXsdTypeName(propertyInfo.PropertyType);
                var propertyElement = new XElement(
                    _xs + "element",
                    new XAttribute("name", propertyInfo.Name),
                    new XAttribute("type", typeName)
                    );

                var xsdElement = XsdElement.Get(propertyInfo);
                if (xsdElement != null)
                {
                    if (!string.IsNullOrEmpty(xsdElement.MinOccurs))
                    {
                        propertyElement.SetAttributeValue("minOccurs", xsdElement.MinOccurs);
                    }
                    if (!string.IsNullOrEmpty(xsdElement.MaxOccurs))
                    {
                        propertyElement.SetAttributeValue("maxOccurs", xsdElement.MaxOccurs);
                    }

                    if (!string.IsNullOrEmpty(xsdElement.Annotation))
                    {
                        propertyElement.Add(new XElement(
                                                _xs + "annotation",
                                                new XElement(
                                                    _xs + "documentation", xsdElement.Annotation
                                                    )
                                                ));
                    }
                }

                //判断是否自定义类型, 添加Import
                if (!typeName.StartsWith("xs:"))
                {
                    var parentClassFileGroup   = XsdComplexType.Get(type).FileGroup;
                    var propertyClassFileGroup = Common.GetFileGroup(propertyInfo.PropertyType);
                    if (parentClassFileGroup != propertyClassFileGroup)
                    {
                        string importXsd = propertyClassFileGroup + ".xsd";
                        //判断是否已经存在该Import
                        if (_xsdFiles[parentClassFileGroup].Imports.All(item => item.Attribute("schemaLocation").Value != importXsd))
                        {
                            _xsdFiles[parentClassFileGroup].Imports.Add(
                                new XElement(
                                    _xs + "include",
                                    new XAttribute("schemaLocation", importXsd)
                                    )
                                );
                        }
                    }
                }
                sequenceElement.Add(propertyElement);
            }
        }
Esempio n. 5
0
        private void ProcessChildElement(Context context, XsdElement xsdParent, XmlSchemaElement child)
        {
            XsdElement xsdChild;

            // If this is a reference element, set the type to reference
            var refName = child.RefName?.Name;

            if (!string.IsNullOrEmpty(refName))
            {
                xsdChild = xsdParent.FindOrCreateElement(refName);

                xsdChild.DataType = "ref";
                xsdChild.RefName  = refName;
            }
            else
            {
                xsdChild = xsdParent.FindOrCreateElement(child.Name);

                if (child.ElementSchemaType is XmlSchemaComplexType complex)
                {
                    xsdChild.DataType = "ref";

                    if (!string.IsNullOrEmpty(child.SchemaTypeName.Name))
                    {
                        xsdChild.RefName = child.SchemaTypeName.Name;
                    }
                    else
                    {
                        xsdChild.RefName = child.Name;
                    }

                    // Only process if it does not already exist, to avoid stack overflow
                    if (context.Document.FindElement(xsdChild.RefName) == null)
                    {
                        var xsdTarget = context.Document.FindOrCreateElement(xsdChild.RefName);

                        ProcessComplexType(context, xsdTarget, complex);
                    }
                }
                else if (child.ElementSchemaType is XmlSchemaSimpleType simple)
                {
                    ProcessSimpleType(xsdChild, simple);
                }
                else
                {
                    throw new ParserException(child, $"Could not determine data type for child element: {xsdParent.Name}.{child.Name}.");
                }
            }

            xsdChild.MinOccurs = child.MinOccursString;
            xsdChild.MaxOccurs = child.MaxOccursString;
        }
Esempio n. 6
0
        private void ProcessComplexType(Context context, XsdElement xsdElement, XmlSchemaComplexType complexType)
        {
            // Pick out any attributes, and add them to the element
            foreach (XmlSchemaAttribute xmlSchemaAttribute in complexType.Attributes)
            {
                var xsdAttribute = xsdElement.FindOrCreateAttribute(xmlSchemaAttribute.Name);

                if (xmlSchemaAttribute.AttributeSchemaType is XmlSchemaSimpleType simple)
                {
                    ProcessSimpleType(xsdAttribute, simple);
                }
                else
                {
                    xsdAttribute.DataType = xmlSchemaAttribute.AttributeSchemaType.Datatype.ValueType.Name;
                }

                switch (xmlSchemaAttribute.Use)
                {
                case XmlSchemaUse.None:
                case XmlSchemaUse.Optional:
                    xsdAttribute.MinOccurs = "0";
                    xsdAttribute.MaxOccurs = "1";
                    break;

                case XmlSchemaUse.Prohibited:
                    xsdAttribute.MinOccurs = "0";
                    xsdAttribute.MaxOccurs = "0";
                    break;

                case XmlSchemaUse.Required:
                    xsdAttribute.MinOccurs = "1";
                    xsdAttribute.MaxOccurs = "1";
                    break;
                }
            }

            // Parse any children
            if (complexType.Particle == null)
            {
                // No children
                return;
            }
            else if (complexType.Particle is XmlSchemaSequence seq)
            {
                ProcessSequence(context, xsdElement, seq);
            }
            else
            {
                throw new ParserException(complexType, $"Unexpected complexType.Particle: {complexType.Particle}.");
            }
        }
Esempio n. 7
0
 private void ProcessSequence(Context context, XsdElement xsdElement, XmlSchemaSequence sequence)
 {
     foreach (XmlSchemaAnnotated item in sequence.Items)
     {
         if (item is XmlSchemaElement elem)
         {
             ProcessChildElement(context, xsdElement, elem);
         }
         else if (item is XmlSchemaSequence seq)
         {
             ProcessSequence(context, xsdElement, seq);
         }
         else
         {
             throw new ParserException(sequence, $"Unexpected sequence item type: {item.GetType().Name}.");
         }
     }
 }
Esempio n. 8
0
 private XsdAttribute GetVerifiedAttribute(XsdElement element, string attributeName)
 {
     element.Attributes.Should().Contain(x => x.Name == attributeName);
     return(element.Attributes.First(x => x.Name == attributeName));
 }
Esempio n. 9
0
 private XsdElement GetVerifiedElement(XsdElement parent, string elementName)
 {
     parent.Elements.Should().Contain(x => x.Name == elementName);
     return(parent.Elements.First(x => x.Name == elementName));
 }