/// <summary> /// Creates the culture string from X element. /// </summary> /// <param name="element"> /// The element. /// </param> /// <returns> /// culture string /// </returns> public static CultureString CreateCultureStringFromXElement(this XElement element) { CultureString cs = new CultureString(); foreach (XElement content in element.Elements("content")) { XAttribute xAttribute = content.Attribute("lang"); if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value)) { cs[xAttribute.Value] = content.Value; } } return(cs); }
private static CultureString CreateCultureStringFromXElement(XElement root, string attributeCondition) { CultureString cultureString = new CultureString(); if (root.Name != XDocName.Annotation) { return(cultureString); } attributeCondition = attributeCondition.ToLower(); foreach (XElement element in root.Elements(XDocName.Documentation)) { XElement textElement = element.Element(XDocName.Tekst); if (textElement == null) { continue; } XAttribute type = textElement.Attribute(XDocName.TextType); XAttribute lang = textElement.Attribute(XDocName.Lang); // Skip creating if one of the elements weren't found, or the type does not match to condition if (type == null || lang == null || type.Value.ToLower() != attributeCondition) { continue; } string text = element.Value; if (text.StartsWith("<p>")) { text = text.Remove(0, 2); } if (text.EndsWith("<p>")) { text = text.Remove(text.Length - 3, text.Length); } cultureString.Add(text, lang.Value); } return(cultureString); }
/// <summary> /// Creates an XML representations of a CultureString /// </summary> /// <param name="cultureString"> /// this CultureString /// </param> /// <param name="elementName"> /// Name of the XElement /// </param> /// <param name="wrapInCData"> /// Wrap the contents in CDATA, default = false /// </param> /// <returns> /// XElement with language strings /// </returns> public static XElement CreateCultreStringXElement(this CultureString cultureString, string elementName, bool wrapInCData) { XElement element = new XElement(elementName); foreach (int lang in Enum.GetValues(typeof(Language))) { string str = cultureString.Get(lang); if (string.IsNullOrEmpty(str)) { continue; } XElement content = new XElement("content", wrapInCData ? new XCData(str).ToString() : str); content.AddAttribute("lang", lang); element.Add(content); } return(element); }
private void SetCultureThread() { // store the current culture _currentCulture = Thread.CurrentThread.CurrentCulture.Name; // no culture string use what is configured if (string.IsNullOrWhiteSpace(CultureString)) { return; } // if the same as current no change needed if (_currentCulture.ToLower() == CultureString.ToLower()) { return; } // set the culture to the string Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(CultureString); }
public PlatformCulture(string platformCultureString) { if (string.IsNullOrEmpty(platformCultureString)) { throw new ArgumentException("Expected culture identifier", nameof(platformCultureString)); } CultureString = platformCultureString.Replace("_", "-"); // .NET expects dash, not underscore var dashIndex = CultureString.IndexOf("-", StringComparison.Ordinal); if (dashIndex > 0) { var parts = CultureString.Split('-'); LanguageCode = parts[0]; LocaleCode = parts[1]; } else { LanguageCode = CultureString; LocaleCode = ""; } }
private void BuildJsonRecursive(XElement elementComplexType, JTokenWriter writer, string parentTrail, bool includeTexts = true, bool includeRestrictions = true, bool includeAttributes = true) { if (elementComplexType != null && elementComplexType.Element(XDocName.Sequence) != null) { List <string> propertyNamesUsed = new List <string>(); foreach (XElement refElement in elementComplexType.Element(XDocName.Sequence).Elements()) { string refName = refElement.AttributeValue("ref"); string classShortRefName = refName.Split('-')[0]; string newTrail = $"{parentTrail}/{refName}"; XElement element = GetXElementByNameAttribute(refName); XElement nextComplexType = element.Element(XDocName.ComplexType); // handle "bad" xsd with duplicate names in same group if (propertyNamesUsed.Contains(classShortRefName)) { classShortRefName += "2"; } propertyNamesUsed.Add(classShortRefName); writer.WritePropertyName(classShortRefName); writer.WriteStartObject(); if (includeAttributes) { if (nextComplexType != null && nextComplexType.Element(XDocName.Attribute) != null) { string groupId = nextComplexType.Element(XDocName.Attribute).AttributeValue("fixed"); writer.WritePropertyName("gruppeid"); writer.WriteValue(int.Parse(groupId)); } writer.WritePropertyName("serializedName"); writer.WriteValue(refName); writer.WritePropertyName("xpath"); writer.WriteValue(newTrail); } if (includeRestrictions) { writer.WritePropertyName("minOccurs"); string minOccurs = refElement.AttributeValue("minOccurs"); writer.WriteValue(string.IsNullOrEmpty(minOccurs) ? 1 : int.Parse(minOccurs)); writer.WritePropertyName("maxOccurs"); string maxOccurs = refElement.AttributeValue("maxOccurs"); writer.WriteValue(string.IsNullOrEmpty(maxOccurs) ? 1 : int.Parse(maxOccurs)); } XElement simpleTypeAnnotationElement = null; if (nextComplexType != null && nextComplexType.Element(XDocName.SimpleContent) != null) { XElement extension = nextComplexType.Element(XDocName.SimpleContent).Element(XDocName.Extension); string simpleTypeName = extension.AttributeValue("base"); XElement simpleType = xsd.Descendants(XDocName.SimpleType).FirstOrDefault(p => XmlToLinqExtensions.AttributeValue(p, "name") == simpleTypeName); simpleTypeAnnotationElement = simpleType.Element(XDocName.Annotation); XElement restriction = simpleType.Element(XDocName.Restriction); if (includeAttributes) { int orid = int.Parse(extension.Element(XDocName.Attribute).AttributeValue("fixed")); writer.WritePropertyName("orid"); writer.WriteValue(orid); } if (includeRestrictions) { string xsdDataType = restriction.AttributeValue("base"); writer.WritePropertyName("xsdDataType"); writer.WriteValue(xsdDataType); string length = restriction.Element(XDocName.Length).AttributeValue("value"); if (!string.IsNullOrEmpty(length)) { writer.WritePropertyName("length"); writer.WriteValue(int.Parse(length)); } string minLength = restriction.Element(XDocName.MinLength).AttributeValue("value"); if (!string.IsNullOrEmpty(minLength)) { writer.WritePropertyName("minLength"); writer.WriteValue(int.Parse(minLength)); } string maxLength = restriction.Element(XDocName.MaxLength).AttributeValue("value"); if (!string.IsNullOrEmpty(maxLength)) { writer.WritePropertyName("maxLength"); writer.WriteValue(int.Parse(maxLength)); } string minInclusive = restriction.Element(XDocName.MinInclusive).AttributeValue("value"); if (!string.IsNullOrEmpty(minInclusive)) { writer.WritePropertyName("minInclusive"); writer.WriteValue(int.Parse(minInclusive)); } string maxInclusive = restriction.Element(XDocName.MaxInclusive).AttributeValue("value"); if (!string.IsNullOrEmpty(maxInclusive)) { writer.WritePropertyName("maxInclusive"); writer.WriteValue(int.Parse(maxInclusive)); } string totalDigits = restriction.Element(XDocName.TotalDigits).AttributeValue("value"); if (!string.IsNullOrEmpty(totalDigits)) { writer.WritePropertyName("totalDigits"); writer.WriteValue(int.Parse(totalDigits)); } string pattern = restriction.Element(XDocName.Pattern).AttributeValue("value"); if (!string.IsNullOrEmpty(pattern)) { writer.WritePropertyName("pattern"); writer.WriteValue(pattern); } IEnumerable <XElement> enumerations = restriction.Elements(XDocName.Enumeration); if (enumerations != null && enumerations.Count() > 0) { writer.WritePropertyName("enums"); writer.WriteStartArray(); foreach (var enumeration in enumerations) { writer.WriteValue(enumeration.AttributeValue("value")); } writer.WriteEndArray(); } } } // Prefer annotation from ref element XElement annotationElement = refElement.Element(XDocName.Annotation) ?? element.Element(XDocName.Annotation); if (annotationElement != null && includeTexts) { CultureString labels = CreateCultureStringFromXElement(annotationElement, "LEDE"); if (labels.Count > 0) { writer.WritePropertyName("caption"); writer.WriteStartObject(); foreach (var label in labels) { writer.WritePropertyName(label.Key.ToString()); writer.WriteValue(label.Value); } writer.WriteEndObject(); } CultureString helptexts = CreateCultureStringFromXElement(annotationElement, "HJELP"); if (helptexts.Count > 0) { writer.WritePropertyName("help"); writer.WriteStartObject(); foreach (var text in helptexts) { writer.WritePropertyName(text.Key.ToString()); writer.WriteValue(text.Value); } writer.WriteEndObject(); } CultureString hinttexts = CreateCultureStringFromXElement(annotationElement, "HINT"); if (hinttexts.Count > 0) { writer.WritePropertyName("hint"); writer.WriteStartObject(); foreach (var text in hinttexts) { writer.WritePropertyName(text.Key.ToString()); writer.WriteValue(text.Value); } writer.WriteEndObject(); } CultureString errortexts = CreateCultureStringFromXElement(annotationElement, "FEIL"); if (simpleTypeAnnotationElement != null && errortexts.Count == 0) { errortexts = CreateCultureStringFromXElement(simpleTypeAnnotationElement, "FEIL"); } if (errortexts.Count > 0) { writer.WritePropertyName("error"); writer.WriteStartObject(); foreach (var text in errortexts) { writer.WritePropertyName(text.Key.ToString()); writer.WriteValue(text.Value); } writer.WriteEndObject(); } } // Magic BuildJsonRecursive(nextComplexType, writer, newTrail, includeTexts, includeRestrictions, includeAttributes); writer.WriteEndObject(); } } }
private void BuildModelRecursive(XElement elementComplexType, Dictionary <string, string> classes, string trail) { if (elementComplexType != null && elementComplexType.Element(XDocName.Sequence) != null) { List <string> propertyNamesUsed = new List <string>(); var propertyBuilder = new StringBuilder(); foreach (XElement refElement in elementComplexType.Element(XDocName.Sequence).Elements()) { string refName = refElement.AttributeValue("ref"); string classShortRefName = refName.Split('-')[0]; string className = refName.Replace("-", string.Empty); XElement element = GetXElementByNameAttribute(refName); XElement nextComplexType = element.Element(XDocName.ComplexType); if (nextComplexType != null && nextComplexType.Element(XDocName.Attribute) != null) { string groupId = nextComplexType.Element(XDocName.Attribute).AttributeValue("fixed"); ////propertyBuilder.AppendLine("public int GruppeId {get {return " + groupId +";} }"); } // string minOccStr = refElement.AttributeValue("minOccurs"); // int minOccurs = string.IsNullOrEmpty(minOccStr) ? 1 : int.Parse(minOccStr); // writer.AppendLine("public int MinOccurs {get {return " + groupId + ";} }"); //// string maxOccursStr = refElement.AttributeValue("maxOccurs"); int maxOccurs = string.IsNullOrEmpty(maxOccursStr) ? 1 : int.Parse(maxOccursStr); // Prefer annotation from ref element XElement annotationElement = refElement.Element(XDocName.Annotation) ?? element.Element(XDocName.Annotation); if (annotationElement != null) { CultureString labels = CreateCultureStringFromXElement(annotationElement, "LEDE"); if (labels.Count > 0) { propertyBuilder.AppendLine(); propertyBuilder.AppendLine(" /// <summary>" + labels.Get(1044).Replace("\n", string.Empty) + "</summary>"); //\n is messing with my generated code } } string propertyDataType = className; XElement simpleTypeAnnotationElement = null; if (nextComplexType != null && nextComplexType.Element(XDocName.SimpleContent) != null) { XElement extension = nextComplexType.Element(XDocName.SimpleContent).Element(XDocName.Extension); string simpleTypeName = extension.AttributeValue("base"); XElement simpleType = xsd.Descendants(XDocName.SimpleType).FirstOrDefault(p => XmlToLinqExtensions.AttributeValue(p, "name") == simpleTypeName); simpleTypeAnnotationElement = simpleType.Element(XDocName.Annotation); XElement restriction = simpleType.Element(XDocName.Restriction); // int orid = int.Parse(extension.Element(XDocName.Attribute).AttributeValue("fixed")); // writer.WritePropertyName("orid"); // writer.WriteValue(orid); //// string xsdDataType = restriction.AttributeValue("base").ToLower(); switch (xsdDataType) { case "xs:string": case "xs:normalizedstring": propertyDataType = "string"; break; case "xs:int": propertyDataType = "int"; break; case "xs:short": propertyDataType = "short"; break; case "xs:decimal": case "xs:integer": case "xs:negativeinteger": case "xs:positiveinteger": case "xs:nonnegativeinteger": case "xs:nonpositiveinteger": propertyDataType = "decimal"; break; case "xs:date": case "xs:datetime": case "xs:gday": case "xs:gmonthday": case "xs:gyear": case "xs:gyearmonth": case "xs:month": case "xs:time": case "xs:timeperiod": propertyDataType = "DateTime"; break; case "xs:boolean": propertyDataType = "bool"; break; case "xs:double": propertyDataType = "double"; break; case "xs:long": propertyDataType = "long"; break; } // string length = restriction.Element(XDocName.Length).AttributeValue("value"); // if (!string.IsNullOrEmpty(length)) // { // writer.WritePropertyName("length"); // writer.WriteValue(int.Parse(length)); // } //// string minLength = restriction.Element(XDocName.MinLength).AttributeValue("value"); if (!string.IsNullOrEmpty(minLength)) { propertyBuilder.AppendLine(" [MinLength(" + minLength + ")]"); } string maxLength = restriction.Element(XDocName.MaxLength).AttributeValue("value"); if (!string.IsNullOrEmpty(maxLength)) { propertyBuilder.AppendLine(" [MaxLength(" + maxLength + ")]"); } string minInclusive = restriction.Element(XDocName.MinInclusive).AttributeValue("value"); string maxInclusive = restriction.Element(XDocName.MaxInclusive).AttributeValue("value"); if (!string.IsNullOrEmpty(minInclusive) && !string.IsNullOrEmpty(maxInclusive)) { propertyBuilder.AppendLine(" [Range(" + minInclusive + ", " + maxInclusive + ")]"); } // string totalDigits = restriction.Element(XDocName.TotalDigits).AttributeValue("value"); // if (!string.IsNullOrEmpty(totalDigits)) // { // writer.WritePropertyName("totalDigits"); // writer.WriteValue(int.Parse(totalDigits)); // } //// string pattern = restriction.Element(XDocName.Pattern).AttributeValue("value"); if (!string.IsNullOrEmpty(pattern)) { propertyBuilder.AppendLine(" [RegularExpression(@\"" + pattern + "\")]"); } // IEnumerable<XElement> enumerations = restriction.Elements(XDocName.Enumeration); // if (enumerations != null && enumerations.Count() > 0) // { // writer.WritePropertyName("enums"); // writer.WriteStartArray(); // foreach (var enumeration in enumerations) // { // writer.WriteValue(enumeration.AttributeValue("value")); // } // writer.WriteEndArray(); // } } // handle "bad" xsd with duplicate names in same group if (propertyNamesUsed.Contains(classShortRefName)) { classShortRefName += "2"; } if (maxOccurs > 1) { propertyDataType = "List<" + propertyDataType + ">"; } propertyBuilder.AppendLine(" [XmlElement(\"" + refName + "\")]"); propertyBuilder.AppendLine(" public " + propertyDataType + " " + classShortRefName + " {get; set;}"); propertyNamesUsed.Add(classShortRefName); // Magic BuildModelRecursive(nextComplexType, classes, className); } // Add class with properties classes.Add(trail, CreateClass(trail, propertyBuilder.ToString())); } }
/// <summary> /// Creates an XML representations of a CultureString /// </summary> /// <param name="cultureString"> /// this CultureString /// </param> /// <param name="elementName"> /// Name of the XElement /// </param> /// <returns> /// XElement with language strings /// </returns> public static XElement CreateCultreStringXElement(this CultureString cultureString, string elementName) { return(CreateCultreStringXElement(cultureString, elementName, false)); }
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); }