/// <summary> /// Shortcat to quickly get BXL code as string without setting output stream /// </summary> /// <param name="e"> data to generate BXL </param> /// <param name="options"> </param> /// <returns> bxl representation of xml data </returns> public string Convert(XElement e, BxlGeneratorOptions options = null) { var sw = new StringWriter(); _output = sw; ExtractAndWriteNamespaces(e); Write(e, sw, options); return(sw.ToString()); }
/// <summary> /// Perform rendering of givin xml data into output stream /// </summary> /// <param name="sourceXmlData"> Xml data to render </param> /// <param name="output"> stream to send output </param> /// <param name="options"> BXL generation options </param> public void Write(XElement sourceXmlData, TextWriter output, BxlGeneratorOptions options = null) { options = options ?? new BxlGeneratorOptions(); _level = 0; if (options.NoRootElement) { foreach (var element in sourceXmlData.Elements()) { WriteElement(element, true, options); } } else { WriteElement(sourceXmlData, _ns.Count > 0, options); } }
private string EvaluateOrderKey(XAttribute attribute, BxlGeneratorOptions options) { var oa = attribute.Annotation <OrderAnnotation>(); if (null != oa) { return(oa.Value); } var name = attribute.Name.LocalName; var inlineorder = 1; if (options.InlineAttributesByDefault) { inlineorder = 0; } if (-1 != Array.IndexOf(options.InlineAlwaysAttributes, name)) { inlineorder = 0; } if (-1 != Array.IndexOf(options.NewlineAlwaysAttributes, name)) { inlineorder = 0; } var grouporder = Array.IndexOf(options.FirstPlaceAttributes, name); if (-1 == grouporder) { grouporder = 99; } var result = string.Format("{0:00}{1:00}{2}", inlineorder, grouporder, name.ToUpper()); oa = new OrderAnnotation { Value = result }; attribute.AddAnnotation(oa); return(result); }
private void WriteAttribute(XAttribute a, bool tripple, BxlGeneratorOptions options) { if (-1 != Array.IndexOf(options.SkipAttributes, a.Name.LocalName)) { return; } var name = a.Name.LocalName; var ns = a.Name.Namespace; if (ns.ToString().IsNotEmpty()) { var prefix = _ns[ns.ToString()]; name = prefix + "::" + name; } var inline = EvaluateOrderKey(a, options).StartsWith("00"); if (inline) { if (!_firstAttribute) { _output.Write(","); } _output.Write(" "); _output.Write(name); _output.Write("="); _output.Write(Escape(a.Value)); } else { Newline(); Indent(); _output.Write(name); _output.Write("="); _output.Write(Escape(a.Value, tripple)); } }
/// <summary> /// Generates BXL code from XML with given settings /// </summary> /// <param name="sourcexml"> </param> /// <param name="options"> </param> /// <returns> </returns> public string Generate(XElement sourcexml, BxlGeneratorOptions options = null) { return(new BxlGenerator().Convert(sourcexml, options)); }
private void WriteElement(XElement e, bool newline, BxlGeneratorOptions options) { if (newline) { Newline(); } Indent(); var ename = e.Name.LocalName; var ns = e.Name.Namespace; if (ns.ToString().IsNotEmpty()) { var prefix = _ns[ns.ToString()]; ename = prefix + "::" + ename; } _output.Write(ename); _level++; _firstAttribute = true; var id = e.Attribute("id"); var code = e.Attribute("code"); var name = e.Attribute("name"); var idval = e.Id(); var writecode = id != null && code != null && code.Value != id.Value; if (idval.IsNotEmpty()) { _output.Write(" " + Escape(idval)); _firstAttribute = false; } if (writecode && _firstAttribute) { _output.Write(" " + Escape(code.Value)); _firstAttribute = false; } if (name != null) { if (_firstAttribute) { _output.Write(" name=" + Escape(name.Value)); _firstAttribute = false; } else { _output.Write(", " + Escape(name.Value)); } } if (writecode && !_firstAttribute) { _output.Write(", code=" + Escape(code.Value)); } foreach ( var a in e.Attributes().OrderBy(x => EvaluateOrderKey(x, options)).Where(x => EvaluateOrderKey(x, options).StartsWith("00")) .ToArray() ) { if (a.Name == "id" || a.Name == "code" || a.Name == "name") { continue; } WriteAttribute(a, false, options); _firstAttribute = false; } var selfstr = e.Nodes().OfType <XText>().Select(x => x.Value ?? "").ConcatString(Environment.NewLine); if (selfstr.IsNotEmpty()) { selfstr = Escape(selfstr, options.UseTrippleQuotOnValues); _output.Write(" : "); _output.Write(selfstr); } foreach ( var a in e.Attributes().OrderBy(x => EvaluateOrderKey(x, options)).Where(x => EvaluateOrderKey(x, options).StartsWith("01")) .ToArray() ) { if (a.Name == "id" || a.Name == "code" || a.Name == "name") { continue; } WriteAttribute(a, true, options); } foreach (var e2 in e.Elements()) { WriteElement(e2, true, options); } _level--; }
/// <summary> /// Converts given Xml file to BXL code /// </summary> /// <param name="filename"> xml file to convert (default FileResolver will be used) </param> /// <param name="options"> </param> /// <returns> BXL code representing given data </returns> /// <remarks> /// Uses default BXL generation settings, if you need more complex generation, use <see cref="BxlGenerator" /> direcly /// </remarks> public static string ConvertFile(string filename, BxlGeneratorOptions options = null) { return(new BxlGenerator().Convert(XElement.Load(filename), options)); }
/// <summary> /// Converts given XElement to BXL code /// </summary> /// <param name="data"> xml to convert </param> /// <param name="options"> </param> /// <returns> BXL code representing given data </returns> /// <remarks> /// Uses default BXL generation settings, if you need more complex generation, use <see cref="BxlGenerator" /> direcly /// </remarks> public static string Convert(XElement data, BxlGeneratorOptions options = null) { return(new BxlGenerator().Convert(data, options)); }