protected static void RegisterFactory(IUxmlFactory factory)
 {
     if (factories.TryGetValue(factory.uxmlQualifiedName, out var factoryList))
     {
         foreach (var f in factoryList)
         {
             if (f.GetType() == factory.GetType())
             {
                 throw new ArgumentException($"A factory for the type {factory.GetType().FullName} was already registered");
             }
         }
         factoryList.Add(factory);
     }
     else
     {
         factoryList = new List <IUxmlFactory>();
         factoryList.Add(factory);
         factories.Add(factory.uxmlQualifiedName, factoryList);
         Type uxmlType = factory.uxmlType;
         var  attr     = uxmlType?.GetCustomAttribute <MovedFromAttribute>(false);
         if (attr != null && typeof(VisualElement).IsAssignableFrom(uxmlType))
         {
             string movedTypeName = GetMovedUIControlTypeName(uxmlType, attr);
             if (string.IsNullOrEmpty(movedTypeName) == false)
             {
                 s_MovedTypesFactories.Add(movedTypeName, factoryList);
             }
         }
     }
 }
        static bool ProcessFactory(IUxmlFactory factory, Dictionary <string, SchemaInfo> schemas, FactoryProcessingHelper processingData)
        {
            if (!string.IsNullOrEmpty(factory.substituteForTypeName))
            {
                if (!processingData.IsKnownElementType(factory.substituteForTypeName, factory.substituteForTypeNamespace))
                {
                    // substituteForTypeName is not yet known. Defer processing to later.
                    return(false);
                }
            }

            string     uxmlNamespace = factory.uxmlNamespace;
            SchemaInfo schemaInfo;

            if (!schemas.TryGetValue(uxmlNamespace, out schemaInfo))
            {
                schemaInfo             = new SchemaInfo(uxmlNamespace);
                schemas[uxmlNamespace] = schemaInfo;
            }

            XmlSchemaType type = AddElementTypeToXmlSchema(factory, schemaInfo, processingData);

            AddElementToXmlSchema(factory, schemaInfo, type);

            processingData.RegisterElementType(factory.uxmlName, factory.uxmlNamespace);

            return(true);
        }
        static XmlSchemaType AddElementTypeToXmlSchema(IUxmlFactory factory, SchemaInfo schemaInfo, FactoryProcessingHelper processingData)
        {
            // We always have complex types with complex content.
            XmlSchemaComplexType elementType = new XmlSchemaComplexType();

            elementType.Name = factory.uxmlName + k_TypeSuffix;

            XmlSchemaComplexContent content = new XmlSchemaComplexContent();

            elementType.ContentModel = content;

            // We only support restrictions of base types.
            XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();

            content.Content = restriction;

            if (factory.substituteForTypeName == String.Empty)
            {
                restriction.BaseTypeName = new XmlQualifiedName("anyType", k_XmlSchemaNamespace);
            }
            else
            {
                restriction.BaseTypeName = new XmlQualifiedName(factory.substituteForTypeName + k_TypeSuffix, factory.substituteForTypeNamespace);
                schemaInfo.importNamespaces.Add(factory.substituteForTypeNamespace);
            }

            if (factory.canHaveAnyAttribute)
            {
                XmlSchemaAnyAttribute anyAttribute = new XmlSchemaAnyAttribute();
                anyAttribute.ProcessContents = XmlSchemaContentProcessing.Lax;
                restriction.AnyAttribute     = anyAttribute;
            }

            foreach (UxmlAttributeDescription attrDesc in factory.uxmlAttributesDescription)
            {
                XmlQualifiedName typeName = AddAttributeTypeToXmlSchema(schemaInfo, attrDesc, factory, processingData);
                if (typeName != null)
                {
                    AddAttributeToXmlSchema(restriction, attrDesc, typeName);
                    schemaInfo.importNamespaces.Add(attrDesc.typeNamespace);
                }
            }

            bool hasChildElements = false;

            foreach (UxmlChildElementDescription childDesc in factory.uxmlChildElementsDescription)
            {
                hasChildElements = true;
                schemaInfo.importNamespaces.Add(childDesc.elementNamespace);
            }

            if (hasChildElements)
            {
                restriction.Particle = MakeChoiceSequence(factory.uxmlChildElementsDescription);
            }

            schemaInfo.schema.Items.Add(elementType);
            return(elementType);
        }
        public VisualElement Create(CreationContext ctx)
        {
            List <IUxmlFactory> factoryList;

            if (!VisualElementFactoryRegistry.TryGetValue(fullTypeName, out factoryList))
            {
                Debug.LogErrorFormat("Element '{0}' has no registered factory method.", fullTypeName);
                return(new Label(string.Format("Unknown type: '{0}'", fullTypeName)));
            }

            IUxmlFactory factory = null;

            foreach (IUxmlFactory f in factoryList)
            {
                if (f.AcceptsAttributeBag(this, ctx))
                {
                    factory = f;
                    break;
                }
            }

            if (factory == null)
            {
                Debug.LogErrorFormat("Element '{0}' has a no factory that accept the set of XML attributes specified.", fullTypeName);
                return(new Label(string.Format("Type with no factory: '{0}'", fullTypeName)));
            }

            if (factory is UxmlRootElementFactory)
            {
                return(null);
            }

            VisualElement res = factory.Create(this, ctx);

            if (res == null)
            {
                Debug.LogErrorFormat("The factory of Visual Element Type '{0}' has returned a null object", fullTypeName);
                return(new Label(string.Format("The factory of Visual Element Type '{0}' has returned a null object", fullTypeName)));
            }

            if (classes != null)
            {
                for (int i = 0; i < classes.Length; i++)
                {
                    res.AddToClassList(classes[i]);
                }
            }

            if (stylesheets != null)
            {
                for (int i = 0; i < stylesheets.Count; i++)
                {
                    res.AddStyleSheetPath(stylesheets[i]);
                }
            }

            return(res);
        }
Ejemplo n.º 5
0
        internal static VisualElement Create(VisualElementAsset asset, CreationContext ctx)
        {
            List <IUxmlFactory> factoryList;

            if (!VisualElementFactoryRegistry.TryGetValue(asset.fullTypeName, out factoryList))
            {
                if (asset.fullTypeName.StartsWith("UnityEngine.Experimental.UIElements.") || asset.fullTypeName.StartsWith("UnityEditor.Experimental.UIElements."))
                {
                    string experimentalTypeName = asset.fullTypeName.Replace(".Experimental.UIElements", ".UIElements");
                    if (!VisualElementFactoryRegistry.TryGetValue(experimentalTypeName, out factoryList))
                    {
                        Debug.LogErrorFormat("Element '{0}' has no registered factory method.", asset.fullTypeName);
                        return(new Label(string.Format("Unknown type: '{0}'", asset.fullTypeName)));
                    }
                }
                else if (asset.fullTypeName == UxmlRootElementFactory.k_ElementName)
                {
                    // Support UXML without namespace for backward compatibility.
                    VisualElementFactoryRegistry.TryGetValue(typeof(UxmlRootElementFactory).Namespace + "." + asset.fullTypeName, out factoryList);
                }
                else
                {
                    Debug.LogErrorFormat("Element '{0}' has no registered factory method.", asset.fullTypeName);
                    return(new Label(string.Format("Unknown type: '{0}'", asset.fullTypeName)));
                }
            }

            IUxmlFactory factory = null;

            foreach (IUxmlFactory f in factoryList)
            {
                if (f.AcceptsAttributeBag(asset, ctx))
                {
                    factory = f;
                    break;
                }
            }

            if (factory == null)
            {
                Debug.LogErrorFormat("Element '{0}' has a no factory that accept the set of XML attributes specified.", asset.fullTypeName);
                return(new Label(string.Format("Type with no factory: '{0}'", asset.fullTypeName)));
            }

            VisualElement res = factory.Create(asset, ctx);

            if (res != null)
            {
                AssignClassListFromAssetToElement(asset, res);
                AssignStyleSheetFromAssetToElement(asset, res);
            }

            return(res);
        }
Ejemplo n.º 6
0
        public static UxmlTraits GetTraits(this IUxmlFactory factory)
        {
            var traitsField = factory.GetType()
                              .GetField("m_Traits", BindingFlags.Instance | BindingFlags.NonPublic);

            if (traitsField == null)
            {
                Debug.LogError(s_TraitsNotFoundMessage);
                return(null);
            }

            return(traitsField.GetValue(factory) as UxmlTraits);
        }
Ejemplo n.º 7
0
        bool ProcessFactory(IUxmlFactory factory, FactoryProcessingHelper processingData)
        {
            if (!string.IsNullOrEmpty(factory.substituteForTypeName))
            {
                if (!processingData.IsKnownElementType(factory.substituteForTypeName, factory.substituteForTypeNamespace))
                {
                    // substituteForTypeName is not yet known. Defer processing to later.
                    return(false);
                }
            }

            processingData.RegisterElementType(factory);

            return(true);
        }
        static void AddElementToXmlSchema(IUxmlFactory factory, SchemaInfo schemaInfo, XmlSchemaType type)
        {
            XmlSchemaElement element = new XmlSchemaElement();

            element.Name = factory.uxmlName;

            if (type != null)
            {
                element.SchemaTypeName = new XmlQualifiedName(type.Name, factory.uxmlNamespace);
            }

            if (factory.substituteForTypeName != String.Empty)
            {
                element.SubstitutionGroup = new XmlQualifiedName(factory.substituteForTypeName, factory.substituteForTypeNamespace);
            }

            schemaInfo.schema.Items.Add(element);
        }
Ejemplo n.º 9
0
 protected static void RegisterFactory(IUxmlFactory factory)
 {
     if (factories.TryGetValue(factory.uxmlQualifiedName, out var factoryList))
     {
         foreach (var f in factoryList)
         {
             if (f.GetType() == factory.GetType())
             {
                 throw new ArgumentException($"A factory for the type {factory.GetType().FullName} was already registered");
             }
         }
         factoryList.Add(factory);
     }
     else
     {
         factoryList = new List <IUxmlFactory>();
         factoryList.Add(factory);
         factories.Add(factory.uxmlQualifiedName, factoryList);
     }
 }
Ejemplo n.º 10
0
 private static void DiscoverFactories()
 {
     if (Factories.s_Factories == null)
     {
         Factories.s_Factories = new Dictionary <string, Func <IUxmlAttributes, CreationContext, VisualElement> >();
         CoreFactories.RegisterAll();
         AppDomain        currentDomain = AppDomain.CurrentDomain;
         HashSet <string> hashSet       = new HashSet <string>(ScriptingRuntime.GetAllUserAssemblies());
         Assembly[]       assemblies    = currentDomain.GetAssemblies();
         for (int i = 0; i < assemblies.Length; i++)
         {
             Assembly assembly = assemblies[i];
             if (hashSet.Contains(assembly.GetName().Name + ".dll"))
             {
                 try
                 {
                     Type[] types = assembly.GetTypes();
                     for (int j = 0; j < types.Length; j++)
                     {
                         Type type = types[j];
                         if (typeof(IUxmlFactory).IsAssignableFrom(type))
                         {
                             IUxmlFactory uxmlFactory = (IUxmlFactory)Activator.CreateInstance(type);
                             Factories.RegisterFactory(uxmlFactory.CreatesType.FullName, new Func <IUxmlAttributes, CreationContext, VisualElement>(uxmlFactory.Create));
                         }
                     }
                 }
                 catch (TypeLoadException ex)
                 {
                     Debug.LogWarningFormat("Error while loading types from assembly {0}: {1}", new object[]
                     {
                         assembly.FullName,
                         ex
                     });
                 }
             }
         }
     }
 }
Ejemplo n.º 11
0
        internal static void RegisterFactory(IUxmlFactory factory)
        {
            List <IUxmlFactory> factoryList;

            if (factories.TryGetValue(factory.uxmlQualifiedName, out factoryList))
            {
                foreach (IUxmlFactory f in factoryList)
                {
                    if (f.GetType() == factory.GetType())
                    {
                        throw new ArgumentException("A factory of this type was already registered");
                    }
                }
                factoryList.Add(factory);
            }
            else
            {
                factoryList = new List <IUxmlFactory>();
                factoryList.Add(factory);
                factories.Add(factory.uxmlQualifiedName, factoryList);
            }
        }
Ejemplo n.º 12
0
 private static void RegisterEngineFactories()
 {
     IUxmlFactory[] array = new IUxmlFactory[]
     {
         new UxmlRootElementFactory(),
         new UxmlTemplateFactory(),
         new UxmlStyleFactory(),
         new UxmlAttributeOverridesFactory(),
         new Button.UxmlFactory(),
         new VisualElement.UxmlFactory(),
         new IMGUIContainer.UxmlFactory(),
         new Image.UxmlFactory(),
         new Label.UxmlFactory(),
         new RepeatButton.UxmlFactory(),
         new ScrollView.UxmlFactory(),
         new Scroller.UxmlFactory(),
         new Slider.UxmlFactory(),
         new SliderInt.UxmlFactory(),
         new MinMaxSlider.UxmlFactory(),
         new Toggle.UxmlFactory(),
         new TextField.UxmlFactory(),
         new TemplateContainer.UxmlFactory(),
         new Box.UxmlFactory(),
         new HelpBox.UxmlFactory(),
         new PopupWindow.UxmlFactory(),
         new ListView.UxmlFactory(),
         new TwoPaneSplitView.UxmlFactory(),
         new TreeView.UxmlFactory(),
         new Foldout.UxmlFactory(),
         new BindableElement.UxmlFactory()
     };
     IUxmlFactory[] array2 = array;
     for (int i = 0; i < array2.Length; i++)
     {
         IUxmlFactory factory = array2[i];
         VisualElementFactoryRegistry.RegisterFactory(factory);
     }
 }
Ejemplo n.º 13
0
        internal static void RegisterFactory(IUxmlFactory factory)
        {
            List <IUxmlFactory> list;
            bool flag = VisualElementFactoryRegistry.factories.TryGetValue(factory.uxmlQualifiedName, out list);

            if (flag)
            {
                foreach (IUxmlFactory current in list)
                {
                    bool flag2 = current.GetType() == factory.GetType();
                    if (flag2)
                    {
                        throw new ArgumentException("A factory for the type " + factory.GetType().FullName + " was already registered");
                    }
                }
                list.Add(factory);
            }
            else
            {
                list = new List <IUxmlFactory>();
                list.Add(factory);
                VisualElementFactoryRegistry.factories.Add(factory.uxmlQualifiedName, list);
            }
        }
Ejemplo n.º 14
0
 public void RegisterElementType(IUxmlFactory factory)
 {
     knownTypes.Add(XmlQualifiedName.ToString(factory.uxmlName, factory.uxmlNamespace), factory);
 }
        static XmlQualifiedName AddAttributeTypeToXmlSchema(SchemaInfo schemaInfo, UxmlAttributeDescription description, IUxmlFactory factory, FactoryProcessingHelper processingData)
        {
            if (description.name == null)
            {
                return(null);
            }

            string attrTypeName = factory.uxmlQualifiedName + "_" + description.name + "_" + k_TypeSuffix;
            string attrTypeNameInBaseElement = factory.substituteForTypeQualifiedName + "_" + description.name + "_" + k_TypeSuffix;

            FactoryProcessingHelper.AttributeRecord attrRecord;
            if (processingData.attributeTypeNames.TryGetValue(attrTypeNameInBaseElement, out attrRecord))
            {
                // If restriction != baseElement.restriction, we need to declare a new type.
                // Note: we do not support attributes having a less restrictive restriction than its base type.
                if ((description.restriction == null && attrRecord.desc.restriction == null) ||
                    (description.restriction != null && description.restriction.Equals(attrRecord.desc.restriction)))
                {
                    // Register attrTypeName -> attrRecord for potential future derived elements.
                    processingData.attributeTypeNames.Add(attrTypeName, attrRecord);
                    return(attrRecord.name);
                }
            }

            XmlQualifiedName xqn;

            FactoryProcessingHelper.AttributeRecord attributeRecord;

            if (description.restriction == null)
            {
                // Type is a built-in type.
                xqn             = new XmlQualifiedName(description.type, description.typeNamespace);
                attributeRecord = new FactoryProcessingHelper.AttributeRecord {
                    name = xqn, desc = description
                };
                processingData.attributeTypeNames.Add(attrTypeName, attributeRecord);
                return(xqn);
            }

            string attrTypeNameForSchema = factory.uxmlName + "_" + description.name + "_" + k_TypeSuffix;

            xqn = new XmlQualifiedName(attrTypeNameForSchema, schemaInfo.schema.TargetNamespace);

            XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType();

            simpleType.Name = attrTypeNameForSchema;

            UxmlEnumeration enumRestriction = description.restriction as UxmlEnumeration;

            if (enumRestriction != null)
            {
                XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
                simpleType.Content       = restriction;
                restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace);

                foreach (var v in enumRestriction.values)
                {
                    XmlSchemaEnumerationFacet enumValue = new XmlSchemaEnumerationFacet();
                    enumValue.Value = v;
                    restriction.Facets.Add(enumValue);
                }
            }
            else
            {
                UxmlValueMatches regexRestriction = description.restriction as UxmlValueMatches;
                if (regexRestriction != null)
                {
                    XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
                    simpleType.Content       = restriction;
                    restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace);

                    XmlSchemaPatternFacet pattern = new XmlSchemaPatternFacet();
                    pattern.Value = regexRestriction.regex;
                    restriction.Facets.Add(pattern);
                }
                else
                {
                    UxmlValueBounds bounds = description.restriction as UxmlValueBounds;
                    if (bounds != null)
                    {
                        XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
                        simpleType.Content       = restriction;
                        restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace);

                        XmlSchemaFacet facet;
                        if (bounds.excludeMin)
                        {
                            facet = new XmlSchemaMinExclusiveFacet();
                        }
                        else
                        {
                            facet = new XmlSchemaMinInclusiveFacet();
                        }
                        facet.Value = bounds.min;
                        restriction.Facets.Add(facet);

                        if (bounds.excludeMax)
                        {
                            facet = new XmlSchemaMaxExclusiveFacet();
                        }
                        else
                        {
                            facet = new XmlSchemaMaxInclusiveFacet();
                        }
                        facet.Value = bounds.max;
                        restriction.Facets.Add(facet);
                    }
                    else
                    {
                        Debug.Log("Unsupported restriction type.");
                    }
                }
            }

            schemaInfo.schema.Items.Add(simpleType);
            attributeRecord = new FactoryProcessingHelper.AttributeRecord {
                name = xqn, desc = description
            };
            processingData.attributeTypeNames.Add(attrTypeName, attributeRecord);
            return(xqn);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Register customElement factory
 /// </summary>
 public static void RegisterFactory(IUxmlFactory factory)
 {
     VisualElementFactoryRegistry.RegisterFactory(factory);
 }
Ejemplo n.º 17
0
        internal static VisualElement Create(VisualElementAsset asset, CreationContext ctx)
        {
            List <IUxmlFactory> factoryList;

            if (!VisualElementFactoryRegistry.TryGetValue(asset.fullTypeName, out factoryList))
            {
                if (asset.fullTypeName.StartsWith("UnityEngine.Experimental.UIElements.") || asset.fullTypeName.StartsWith("UnityEditor.Experimental.UIElements."))
                {
                    string experimentalTypeName = asset.fullTypeName.Replace(".Experimental.UIElements", ".UIElements");
                    if (!VisualElementFactoryRegistry.TryGetValue(experimentalTypeName, out factoryList))
                    {
                        Debug.LogErrorFormat("Element '{0}' has no registered factory method.", asset.fullTypeName);
                        return(new Label(string.Format("Unknown type: '{0}'", asset.fullTypeName)));
                    }
                }
                else
                {
                    Debug.LogErrorFormat("Element '{0}' has no registered factory method.", asset.fullTypeName);
                    return(new Label(string.Format("Unknown type: '{0}'", asset.fullTypeName)));
                }
            }

            IUxmlFactory factory = null;

            foreach (IUxmlFactory f in factoryList)
            {
                if (f.AcceptsAttributeBag(asset, ctx))
                {
                    factory = f;
                    break;
                }
            }

            if (factory == null)
            {
                Debug.LogErrorFormat("Element '{0}' has a no factory that accept the set of XML attributes specified.", asset.fullTypeName);
                return(new Label(string.Format("Type with no factory: '{0}'", asset.fullTypeName)));
            }

            if (factory is UxmlRootElementFactory)
            {
                return(null);
            }

            VisualElement res = factory.Create(asset, ctx);

            if (res == null)
            {
                Debug.LogErrorFormat("The factory of Visual Element Type '{0}' has returned a null object", asset.fullTypeName);
                return(new Label(string.Format("The factory of Visual Element Type '{0}' has returned a null object", asset.fullTypeName)));
            }

            if (asset.classes != null)
            {
                for (int i = 0; i < asset.classes.Length; i++)
                {
                    res.AddToClassList(asset.classes[i]);
                }
            }

            if (asset.stylesheetPaths != null)
            {
                for (int i = 0; i < asset.stylesheetPaths.Count; i++)
                {
                    res.AddStyleSheetPath(asset.stylesheetPaths[i]);
                }
            }

            if (asset.stylesheets != null)
            {
                for (int i = 0; i < asset.stylesheets.Count; ++i)
                {
                    res.styleSheets.Add(asset.stylesheets[i]);
                }
            }

            return(res);
        }
        internal static IEnumerable <string> GenerateSchemaFiles(string baseDir = null)
        {
            Dictionary <string, SchemaInfo> schemas           = new Dictionary <string, SchemaInfo>();
            List <IUxmlFactory>             deferredFactories = new List <IUxmlFactory>();
            FactoryProcessingHelper         processingData    = new FactoryProcessingHelper();

            if (baseDir == null)
            {
                baseDir = Application.temporaryCachePath + "/";
            }

            // Convert the factories into schemas info.
            foreach (var factories in VisualElementFactoryRegistry.factories)
            {
                if (factories.Value.Count == 0)
                {
                    continue;
                }

                // Only process the first factory, as the other factories define the same element.
                IUxmlFactory factory = factories.Value[0];
                if (!ProcessFactory(factory, schemas, processingData))
                {
                    // Could not process the factory now, because it depends on a yet unprocessed factory.
                    // Defer its processing.
                    deferredFactories.Add(factory);
                }
            }

            List <IUxmlFactory> deferredFactoriesCopy;

            do
            {
                deferredFactoriesCopy = new List <IUxmlFactory>(deferredFactories);
                foreach (var factory in deferredFactoriesCopy)
                {
                    deferredFactories.Remove(factory);
                    if (!ProcessFactory(factory, schemas, processingData))
                    {
                        // Could not process the factory now, because it depends on a yet unprocessed factory.
                        // Defer its processing again.
                        deferredFactories.Add(factory);
                    }
                }
            }while (deferredFactoriesCopy.Count > deferredFactories.Count);

            if (deferredFactories.Count > 0)
            {
                Debug.Log("Some factories could not be processed because their base type is missing.");
            }

            // Compile schemas.
            XmlSchemaSet schemaSet    = new XmlSchemaSet();
            XmlSchema    masterSchema = new XmlSchema();

            masterSchema.ElementFormDefault = XmlSchemaForm.Qualified;

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

            nsmgr.AddNamespace("xs", k_XmlSchemaNamespace);

            File.Delete(baseDir + k_MainSchemaFileName);

            foreach (var schema in schemas)
            {
                if (schema.Value.schema.TargetNamespace != null)
                {
                    nsmgr.AddNamespace(schema.Value.namepacePrefix, schema.Value.schema.TargetNamespace);

                    // Import schema into the master schema.
                    XmlSchemaImport import = new XmlSchemaImport();
                    import.Namespace = schema.Value.schema.TargetNamespace;
                    string schemaLocation = GetFileNameForNamespace(schema.Value.schema.TargetNamespace);
                    File.Delete(baseDir + schemaLocation);
                    import.SchemaLocation = schemaLocation;
                    masterSchema.Includes.Add(import);
                }
                else
                {
                    XmlSchemaInclude include        = new XmlSchemaInclude();
                    string           schemaLocation = GetFileNameForNamespace(null);
                    File.Delete(baseDir + schemaLocation);
                    include.SchemaLocation = schemaLocation;
                    masterSchema.Includes.Add(include);
                }

                // Import referenced schemas into this XSD
                foreach (string ns in schema.Value.importNamespaces)
                {
                    if (ns != schema.Value.schema.TargetNamespace && ns != k_XmlSchemaNamespace)
                    {
                        XmlSchemaImport import = new XmlSchemaImport();
                        import.Namespace      = ns;
                        import.SchemaLocation = GetFileNameForNamespace(ns);
                        schema.Value.schema.Includes.Add(import);
                    }
                }

                schemaSet.Add(schema.Value.schema);
            }
            schemaSet.Add(masterSchema);
            schemaSet.Compile();

            // Now generate the schema textual data.
            foreach (XmlSchema compiledSchema in schemaSet.Schemas())
            {
                string schemaName = compiledSchema.TargetNamespace;

                // Three possible cases:
                // TargetNamespace == null and Items.Count == 0: the main schema, that include/import all other schema files
                // TargetNamespace == null and Items.Count != 0: the schema file for the global namespace
                // TargetNamespace != null: the schema file for TargetNamespace
                if (schemaName == null && compiledSchema.Items.Count == 0)
                {
                    schemaName = k_MainSchemaFileName;
                }
                else
                {
                    schemaName = GetFileNameForNamespace(compiledSchema.TargetNamespace);
                }

                yield return(baseDir + schemaName);

                StringWriter strWriter = new UTF8StringWriter();
                compiledSchema.Write(strWriter, nsmgr);
                yield return(strWriter.ToString());
            }
        }