static JProperty ConvertSection(ConvContext pctx, XElement xsct) { var name = (string)xsct.Attribute("name"); if (string.IsNullOrEmpty(name)) { throw new InvalidDataException("Missing section name."); } var cctx = pctx.CreateChild(); cctx.XParent = xsct; var xnode = xsct.Elements().FirstOrDefault(); if (xnode != null) { return(new JProperty(name, ConvertNode(cctx, xnode))); } else { return(new JProperty(name, new JObject())); } }
static JObject ConvertNode(ConvContext pctx, XElement xnode) { var jnode = new JObject(); string name = xnode.Name.LocalName; int dotpos = name.IndexOf('.'); if (dotpos >= 0) { var propName = name.Substring(dotpos + 1); var cctx = pctx.CreateChild(); foreach (XElement xchild in xnode.Elements()) { JObject jc = ConvertNode(cctx, xchild); pctx.JParent[propName] = jc; break; } return(null); } else { ConvInfo cvInfo = _cvInfoMap[name]; jnode["type"] = name; var id = (string)xnode.Attribute("id"); if (string.IsNullOrEmpty(id) == false) { pctx.IdMap[id] = true; jnode["id"] = id; } ConvertParamString(pctx, xnode, jnode, "halign"); ConvertParamString(pctx, xnode, jnode, "margin"); //ConvertThickness(pctx, xnode, jnode, "margin"); ConvertParamBool(pctx, xnode, jnode, "visible"); ConvertParamBool(pctx, xnode, jnode, "enabled"); if (pctx.Parent.ColLabel != null) { jnode["gcols"] = new JArray( pctx.Parent.ColLabel.Col1, pctx.Parent.ColLabel.Col2 ); } JObject jresult = cvInfo.Handler(pctx, xnode, jnode); if (jresult != null) { var cctx = pctx.CreateChild(); cctx.XParent = xnode; cctx.JParent = jnode; if (string.IsNullOrEmpty(cvInfo.ElementsAllowed)) { if (xnode.Elements().Any()) { throw new InvalidDataException($"The element {name} does not allow any children elements."); } } else { var jarr = new JArray(); bool isJolly = cvInfo.ElementsAllowed == "*"; string[] parts = cvInfo.ElementsAllowed.Split(' '); foreach (XElement xchild in xnode.Elements()) { if (isJolly || parts.Contains(xchild.Name.LocalName) || xchild.Name.LocalName.Contains('.')) { JObject jc = ConvertNode(cctx, xchild); if (jc != null) { jarr.Add(jc); } } else { throw new InvalidDataException($"The element {name} does not allow the {xchild.Name.LocalName} element as child."); } } if (jarr.Count != 0) { jresult["nodes"] = jarr; } } } return(jresult); } }