Esempio n. 1
0
        private CultureDictionary GetAnnotationsForElement(XElement currentElement, string currentId)
        {
            var elements = new CultureDictionary();

            if (currentElement.Element(XDocName.Annotation) != null)
            {
                var annotationElement     = currentElement.Element(XDocName.Annotation);
                var documentationElements = annotationElement.Elements(XDocName.Documentation).ToList();
                if (documentationElements != null)
                {
                    foreach (var documentationElement in documentationElements)
                    {
                        var textElement = documentationElement.Element(XDocName.Tekst);
                        if (textElement != null)
                        {
                            var language = textElement.AttributeValue(XDocName.Lang);
                            var textType = textElement.AttributeValue(XDocName.TextType);
                            var text     = textElement.Value;

                            var key = currentId + ".TODO";

                            if (textType == "LEDE")
                            {
                                key = currentId + "." + TextCategoryType.Label;
                            }
                            else
                            {
                                if ((textType == "HJELP") || (textType == "DEF"))
                                {
                                    key = currentId + "." + TextCategoryType.Help;
                                }
                                else
                                {
                                    if (textType == "FEIL")
                                    {
                                        key = currentId + "." + TextCategoryType.Error;
                                    }
                                    else
                                    {
                                        if (textType == "HINT")
                                        {
                                            key = currentId + "." + TextCategoryType.PlaceHolder;
                                        }
                                    }
                                }
                            }

                            CultureString cultureString;
                            if (!elements.ContainsKey(key))
                            {
                                cultureString = new CultureString();
                                elements.Add(key, cultureString);
                            }
                            else
                            {
                                cultureString = elements[key];
                            }

                            if (language == "NOB")
                            {
                                cultureString.Add("nb-NO", text);
                            }
                            else
                            {
                                if (language == "NON")
                                {
                                    cultureString.Add("nn-NO", text);
                                }
                                else
                                {
                                    if (language == "EN")
                                    {
                                        cultureString.Add("en", text);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(elements);
        }
Esempio n. 2
0
        private void ProcessChildElement(
            XElement currentComplexType,
            XElement childElement,
            Dictionary <string, ElementMetadata> allElements,
            string parentTrail,
            CultureDictionary allTexts,
            string parentName = null)
        {
            var elementMetadata = new ElementMetadata();

            var currentElement   = childElement;
            var actualElement    = currentElement;
            var currentIsComplex = false;
            var skipRecursive    = false;
            var typeName         = string.Empty;

            if (!string.IsNullOrEmpty(childElement.AttributeValue("ref")))
            {
                // Load the referenced element
                var reference = childElement.AttributeValue("ref");
                if (reference.Split(':').Count() == 2)
                {
                    var name = reference.Split(':')[0];
                    var type = reference.Split(':')[1];

                    typeName       = type;
                    currentElement = GetXElementByNameAttribute(type, secondaryXsdsByNamespace[name]);
                    actualElement  = currentElement;
                }
                else
                {
                    typeName       = reference;
                    currentElement = GetXElementByNameAttribute(reference);
                    actualElement  = currentElement;
                }
            }

            if (!string.IsNullOrEmpty(currentElement.AttributeValue("type")) &&
                !currentElement.AttributeValue("type").Contains(":"))
            {
                // Load the type definition
                actualElement = GetComplexTypeByNameAttribute(currentElement.AttributeValue("type"));
                if (actualElement == null)
                {
                    actualElement = GetSimpleTypeByNameAttribute(currentElement.AttributeValue("type"));
                }
                else
                {
                    currentIsComplex = true;
                }

                typeName = currentElement.AttributeValue("type");
            }
            else
            {
                if (currentElement.Element(XDocName.SimpleType) != null)
                {
                    // Get the direct child simple type
                    typeName         = currentElement.AttributeValue("name");
                    actualElement    = currentElement.Element(XDocName.SimpleType);
                    currentIsComplex = false;
                }
                else
                {
                    if (currentElement.Element(XDocName.ComplexType) != null)
                    {
                        // Get the direct child complex type
                        typeName      = currentElement.AttributeValue("name");
                        actualElement = currentElement.Element(XDocName.ComplexType);

                        if (actualElement.Element(XDocName.SimpleContent) != null)
                        {
                            var    simpleContent = actualElement.Element(XDocName.SimpleContent);
                            string xtraTypeName  = SanitizeName(typeName);

                            ProcessSimpleContent(
                                actualElement,
                                simpleContent,
                                allElements,
                                $"{parentTrail}/{xtraTypeName}",
                                typeName.Split('.')[0]);

                            AddAttributeElements(currentElement, allElements, $"{parentTrail}/{xtraTypeName}");
                            currentIsComplex = true;
                            skipRecursive    = true;
                        }
                        else
                        {
                            currentIsComplex = true;
                        }
                    }
                }
            }

            if (childElement.Name.Equals(XDocName.Any))
            {
                typeName = "Any";
            }

            elementMetadata.XName = typeName;
            var    classShortRefName = SanitizeName(typeName);
            string newTrail          = $"{parentTrail}/{typeName}";

            var elementName = classShortRefName;

            if (!string.IsNullOrEmpty(currentElement.AttributeValue("name")))
            {
                elementName           = SanitizeName(currentElement.AttributeValue("name"));
                elementMetadata.XName = currentElement.AttributeValue("name");

                newTrail = $"{parentTrail}/{elementName}";
            }

            elementMetadata.Name            = elementName;
            elementMetadata.TypeName        = classShortRefName;
            elementMetadata.XPath           = newTrail;
            elementMetadata.ID              = newTrail.Replace("/", ".").Substring(1);
            elementMetadata.ParentElement   = parentTrail.Replace("/", ".").Substring(1);
            elementMetadata.DataBindingName = GetDataBindingName(elementMetadata.ID);

            var currentElementAnnotations = GetAnnotationsForElement(currentElement, elementMetadata.ID);
            var childElementAnnotations   = GetAnnotationsForElement(childElement, elementMetadata.ID);
            var actualElementAnnotations  = GetAnnotationsForElement(actualElement, elementMetadata.ID);

            foreach (var resource in childElementAnnotations)
            {
                if (!currentElementAnnotations.ContainsKey(resource.Key))
                {
                    currentElementAnnotations.Add(resource.Key, resource.Value);
                }
            }

            foreach (var resource in actualElementAnnotations)
            {
                if (!currentElementAnnotations.ContainsKey(resource.Key))
                {
                    currentElementAnnotations.Add(resource.Key, resource.Value);
                }
            }

            if (allElements.ContainsKey(elementMetadata.ID + ".Value"))
            {
                var newElementAnnotations = new CultureDictionary();
                foreach (var resourceText in currentElementAnnotations)
                {
                    var oldIdParts = resourceText.Key.Split('.').ToList();
                    oldIdParts.Insert(oldIdParts.Count - 1, "Value");
                    var newKey = string.Join(".", oldIdParts.ToArray());

                    newElementAnnotations.Add(newKey, resourceText.Value);
                }

                currentElementAnnotations = newElementAnnotations;
            }

            string orid = GetOrid(elementMetadata.XName);

            foreach (var cultureString in currentElementAnnotations)
            {
                var newKey = ShortenKeyID(cultureString, orid);

                if (!allTexts.ContainsKey(cultureString.Key))
                {
                    allTexts.Add(newKey, cultureString.Value);
                }

                if (cultureString.Key.Split('.').Last().EndsWith(TextCategoryType.Label.ToString()))
                {
                    elementMetadata.Texts.Add(TextCategoryType.Label.ToString(), newKey);
                }
                else
                {
                    if (cultureString.Key.Split('.').Last().EndsWith(TextCategoryType.Help.ToString()))
                    {
                        elementMetadata.Texts.Add(TextCategoryType.Help.ToString(), newKey);
                    }
                    else
                    {
                        if (cultureString.Key.Split('.').Last().EndsWith(TextCategoryType.Error.ToString()))
                        {
                            elementMetadata.Texts.Add(TextCategoryType.Error.ToString(), newKey);
                        }
                        else
                        {
                            if (cultureString.Key.Split('.').Last().EndsWith(TextCategoryType.PlaceHolder.ToString()))
                            {
                                elementMetadata.Texts.Add(TextCategoryType.PlaceHolder.ToString(), newKey);
                            }
                        }
                    }
                }
            }

            if (allElements.ContainsKey(elementMetadata.ID + ".Value"))
            {
                allElements[elementMetadata.ID + ".Value"].Texts = elementMetadata.Texts;
                elementMetadata.Texts = new Dictionary <string, string>();
            }

            WriteRestrictions(elementMetadata, actualElement, childElement);

            string errorTextKey = null;

            if (currentElementAnnotations.Count(a => a.Key.Split('.').Last() == TextCategoryType.Error.ToString()) > 0)
            {
                errorTextKey =
                    currentElementAnnotations.FirstOrDefault(
                        a => a.Key.Split('.').Last() == TextCategoryType.Error.ToString()).Key;
            }

            if (errorTextKey != null)
            {
                foreach (var restriction in elementMetadata.Restrictions.Values)
                {
                    restriction.ErrortText = errorTextKey;
                }
            }

            if (!currentIsComplex)
            {
                elementMetadata.Type = ElementType.Field;
            }
            else
            {
                elementMetadata.Type = ElementType.Group;

                elementMetadata.DataBindingName = null;

                if (!skipRecursive)
                {
                    BuildJsonRecursive(actualElement, allElements, newTrail, allTexts);
                }
            }

            if (string.IsNullOrEmpty(elementMetadata.TypeName))
            {
                elementMetadata.TypeName = null;
            }

            if (allElements.ContainsKey(elementMetadata.ID))
            {
                elementMetadata.ID += _randomGen.Next();
            }

            allElements.Add(elementMetadata.ID, elementMetadata);
            AddSchemaReferenceInformation(currentComplexType, elementMetadata);
        }