/// <summary> /// Obtient tous les data qui contien l'attribut 'attributname' /// </summary> /// <param name="attributname"></param> /// <param name="replacenameparattribut"></param> /// <returns></returns> public ParamValues GetDatasWithAttribut(string attributname, bool replacenameparattribut = false) { try { ParamValues retour = new ParamValues(); WaitMutex(); foreach (ParamValuesNode datb in this.dataStore.Values) { if (datb[attributname] != "") { ParamValuesNode ee = datb.Clone(); if (replacenameparattribut) { ee.Name = datb[attributname]; } retour.Add(ee); } } return(retour); } catch (Exception e) { throw new Exception("Impossible d'obtenir des datas avec l'attribut " + attributname + " " + e.Message); } finally { ReleaseMutex(); } }
public ParamValues DeSerialize(string fluxstring, ParamValues retour = null) { try { if (retour == null) { retour = new ParamValues(); } System.Text.Json.JsonDocument jsondoc = System.Text.Json.JsonDocument.Parse(fluxstring); if (jsondoc.RootElement.ValueKind != JsonValueKind.Object) { throw new Exception("Invalid Json : Object Root Type Required"); } //Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.Parse(fluxstring); foreach (var joitem in jsondoc.RootElement.EnumerateObject().ToList()) { FromJsonSub(retour, joitem, "/param/"); } retour.AcceptChanges(); // on retire les marqueurs de changements return(retour); } catch (Exception ex) { throw new Exception("FromFluxJson " + ex.Message, ex); } }
/// <summary> /// Datavalue from fichier config xml /// </summary> /// <param name="fichierXML"></param> public void fromXML(ParamValues datavalue, string fichierXML) { try { datavalue.Clear(); XmlDocument xDoc = new XmlDocument(); xDoc.Load(fichierXML); string lastpath = "/"; foreach (XmlNode xChild in xDoc.ChildNodes) { if (xChild.NodeType == XmlNodeType.Element) { lastpath = "/" + xChild.Name.ToLower() + "/"; } if (xChild.HasChildNodes) { getdataXMLChild(datavalue, xChild, lastpath, null); } } } catch (Exception) { throw; } finally { } }
/// <summary> /// Enregistre la liste dans un fichier config xml /// </summary> /// <param name="fichierXML"></param> public void SerializeToFile(ParamValues datavalue, string fichierXML) { try { // vérif et convert ParamValues dvforxml = datavalue.Clone(); foreach (ParamValuesNode itemd in dvforxml.GetList()) { if (itemd.Name.Length > 0 && itemd.Name[0] != '/') { itemd.Name = "/noparam/" + itemd.Name; } } XmlTextWriter XmlTextWriter = new XmlTextWriter(fichierXML, System.Text.Encoding.UTF8); XmlTextWriter.Formatting = Formatting.Indented; XmlTextWriter.WriteStartDocument(); XmlTextWriter = savewriteburn(XmlTextWriter, dvforxml); XmlTextWriter.Flush(); //vide le buffer XmlTextWriter.Close(); // ferme le document } finally { } }
private ParamValuesNode GenerateNewNodeFromXML(ParamValues datavalue, string lastpath, XmlNode xChild) { ParamValuesNode bufliste = new ParamValuesNode(); try { bufliste.datavalues_parent = datavalue; bufliste.Name = lastpath.ToLower(); if (bufliste.Name.Substring(bufliste.Name.Length - 1) == "/") { bufliste.Name = bufliste.Name.Substring(0, bufliste.Name.Length - 1); } if (bufliste.Name.Length > 9 && bufliste.Name.Substring(0, 9) == "/noparam/") { bufliste.Name.Replace("/noparam/", ""); } // GESTION DES ATTRIBUTS // on tente d'obtenir les attributs (Il sont stockées sur le noeud parent) XmlAttributeCollection sattributs = null; XmlNode xParentElement = null; if (xChild.NodeType == XmlNodeType.Element) { xParentElement = xChild; // il n'avait déja pas d'enfant. } else { xParentElement = xChild.ParentNode; } if (xParentElement != null && xParentElement.NodeType == XmlNodeType.Element) { sattributs = xParentElement.Attributes; } if (sattributs != null && sattributs.Count > 0) { foreach (XmlAttribute item in sattributs) { string nameatr = item.LocalName.ToLower(); bufliste[nameatr] = item.Value; } } // dyna //if (bufliste.value != null && bufliste.value is string && bufliste.value.ToString().Contains("{!")); // Affectation de la données bufliste.SetObject(null, xChild.Value); } catch (Exception) { bufliste["datatype_error"] = "true"; } return(bufliste); }
public void Fusion(ParamValues dataext, bool ecraser = false) { foreach (ParamValuesNode itemd in dataext.GetList()) { ParamValuesNode itemsearch = this.Get(itemd.Name); if (itemsearch != null && itemsearch.Name != "") { if (ecraser) { this.Add(itemd.Clone()); } } else { this.Add(itemd.Clone()); } } }
// ------------------------------------------------------------------------------------------- #region Traitement des données /// <summary> /// Creer une liste de données /// </summary> /// <param name="dodoc">autre datadoc, Ne link pas les données, il recopie intégralement l'objet</param> public ParamValues Clone() { ParamValues retour = new ParamValues(this._DataValueName); try { WaitMutex(); foreach (ParamValuesNode itemd in this.dataStore.Values) { retour.Add(itemd.Clone()); } } finally { ReleaseMutex(); } return(retour); }
public string Serialize(ParamValues datavalue) { try { ParamValuesNodeHierarchical firstnode = ParamValuesTools.GetHierarchicalNodes(datavalue.GetList()); //Newtonsoft.Json.Linq.JToken jobject = Serialize_Recursif(firstnode,true); //Newtonsoft.Json.Formatting outjsonformating = Newtonsoft.Json.Formatting.None; //string jsontstr = jobject.ToString(outjsonformating); ////jsontstr = jsontstr.Replace("\r\n", "");//.Replace("\"", ""); ////jsontstr = System.Text.RegularExpressions.Regex.Unescape(jsontstr); //return jsontstr; var options = new JsonWriterOptions() { Indented = ConfigIndented }; using (var stream = new MemoryStream()) { using (var writer = new Utf8JsonWriter(stream, options)) { // on écrit pas le premier noeud param writer.WriteStartObject(); foreach (var subnode in firstnode.ChildrenNodes) { Serialize_Recursif(writer, subnode); } writer.WriteEndObject(); } string json = Encoding.UTF8.GetString(stream.ToArray()); return(json); } } catch (Exception ex) { throw new Exception("SerializeJson " + ex.Message, ex); } }
private void FromJsonSub(ParamValues datavalue, System.Text.Json.JsonProperty elem, string lastpath) { if (elem.Value.ValueKind == System.Text.Json.JsonValueKind.Array) { // Array invalids } else if (elem.Value.ValueKind == System.Text.Json.JsonValueKind.Object) { string newlastpath = lastpath + elem.Name + "/"; foreach (var jsubitem in elem.Value.EnumerateObject().ToList()) { FromJsonSub(datavalue, jsubitem, newlastpath); } } else { string fullname = lastpath + elem.Name; ParamValuesNode dataelement = new ParamValuesNode(); dataelement.Name = fullname; dataelement.Value = Nglib.DATA.KEYVALUES.KeyValuesSerializerJson.ReadValue(elem.Value); datavalue.Add(dataelement); } }
/// <summary> /// Chargement d'un flux(Json/Ou XML) dans le datavalue /// </summary> /// <param name="itemdv"></param> /// <param name="flux"></param> public static void FromFlux(this ParamValues datavalue, string flux, bool?isXml = null) { //clean if (string.IsNullOrWhiteSpace(flux)) { return; } flux = flux.Trim(); if (!isXml.HasValue) { isXml = flux.TrimStart().StartsWith("<"); } IParamValuesSerializer serial = ParamValuesTools.SerializerFactory(isXml.Value); serial.DeSerialize(flux, datavalue); //parse //if (flux.StartsWith("{")) this.FromFluxJson(flux); //if (flux.StartsWith("<")) fromFluxXML(datavalue,flux); }
/// <summary> /// Datavalue fromc onfig flux string xml /// </summary> /// <param name="fichierXML"></param> public ParamValues DeSerialize(string fluxstring, ParamValues retour = null) { try { if (retour == null) { retour = new ParamValues(); } //if (this.datavalue.isEncrypted) configflux = DecryptageFlux(configflux); // else if (SeyesLib3.DATA.CHAINE.Tools.limitchaine(configflux, 18) == "{!SeyesDVEncrypted") throw new Exception("Fluxxml Encrypté"); retour.Clear(); XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(fluxstring); string lastpath = "/"; foreach (XmlNode xChild in xDoc.ChildNodes) { if (xChild.NodeType == XmlNodeType.Element) { lastpath = "/" + xChild.Name.ToLower() + "/"; } if (xChild.HasChildNodes) { getdataXMLChild(retour, xChild, lastpath, null); } } retour.AcceptChanges(); // on retire les marqueurs de changements return(retour); } catch (Exception ex) { throw; } finally { } }
private void getdataXMLChild(ParamValues datavalue, XmlNode xdot, string pathac, XmlAttributeCollection sattributs) { string lastpath = pathac; ParamValuesNode bufliste = new ParamValuesNode(); XmlAttributeCollection sauvattributs = null; foreach (XmlNode xChild in xdot.ChildNodes) { //System.Console.WriteLine(xChild.Name); if (xChild.NodeType == XmlNodeType.Element) { lastpath = pathac + xChild.Name.ToLower() + "/"; if (xChild.Attributes.Count > 0) { sauvattributs = xChild.Attributes; } else { sauvattributs = null; } } if (xChild.NodeType == XmlNodeType.Text || (xChild.NodeType == XmlNodeType.Element && !xChild.HasChildNodes && xChild.Value == null)) { bufliste = GenerateNewNodeFromXML(datavalue, lastpath, xChild); // Ajout d'une nouvelle clef/valeur if (bufliste != null) { datavalue.Add(bufliste); } } else if (xChild.HasChildNodes) { getdataXMLChild(datavalue, xChild, lastpath, sauvattributs); } } }
/// <summary> /// Enregistre la liste dans un fichier config xml /// </summary> /// <param name="fichierXML"></param> public string Serialize(ParamValues datavalue) { string fluxxml = ""; //chaine xml final if (datavalue.Count() == 0) { return("<?xml version=\"1.0\" ?><param><empty>True</empty></param>"); } ParamValues dvforxml = null; // datavalue buffer try { try { // vérif et convert dvforxml = datavalue.Clone(); try { dvforxml.Sort(); } catch { } foreach (ParamValuesNode itemd in dvforxml.GetList()) { if (itemd.Name.Length > 0 && itemd.Name[0] != '/') { itemd.Name = "/noparam/" + itemd.Name; } } } catch (Exception ev) { throw new Exception("Verif : " + ev.Message); } StringBuilder builder = new StringBuilder(); using (StringWriter stringWriter = new StringWriter(builder)) { XmlTextWriter writer = new XmlTextWriter(stringWriter); // This produces UTF16 XML writer.Indentation = 1; writer.IndentChar = '\t'; writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer = savewriteburn(writer, dvforxml); writer.WriteEndDocument(); writer.Close(); } fluxxml = builder.ToString(); } catch (Exception e) { throw new Exception("Datavalue toFluxXml : " + e, e); } finally { } //if (this.datavalue.isEncrypted) fluxxml = cryptageFlux(fluxxml); return(fluxxml); }
private XmlTextWriter savewriteburn(XmlTextWriter XmlTextWriter, ParamValues databurn) { string[] exnode = new string[1]; bool start = true; bool exburn = false; int nodeouvert = 0; int iielement = 1; // ex : /root/general/param/ex/data/ // ex : /root/societe/liste/data/ int ii = 0; foreach (ParamValuesNode datatab in databurn.GetList()) { string namet = datatab.Name; if (namet[0] == '/') { namet = namet.Substring(1); } if (namet[namet.Length - 1] == '/') { namet = namet.Substring(0, namet.Length - 1); } int namti = namet.Split('/').Length; // on cree la premiere donnee if (start) { exnode = namet.Split('/'); ii = 0; foreach (string item in namet.Split('/')) { //if (ii != namet.Split('/').Length - 1) if (ii == namti - 1) // -1 pour taille -> index du tableau { XmlTextWriter = savewritenode(XmlTextWriter, datatab, item); //if (datatab.type == "Data.Burn") { exburn = true; }// bricolage du bug quil ferme pas tout ... //XmlTextWriter.WriteEndElement(); nodeouvert++; } else { XmlTextWriter.WriteStartElement(item); nodeouvert++; } ii++; } start = false; continue; } // on ferme les node en trops int comptage = 0; ii = 0; foreach (string item in namet.Split('/')) { if (item != exnode[ii]) { comptage = exnode.Length - ii; // -1? break; } ii++; } int comptage2 = comptage; for (ii = 0; comptage > 0; comptage--) { XmlTextWriter.WriteEndElement(); nodeouvert--; } // on creer les nouveau node et la donnée ii = 0; foreach (string item in namet.Split('/')) { if ((ii > nodeouvert - 1) || (exburn && ii >= nodeouvert - 1)) //&& ii != namti { if (ii == namti - 1) // -1 pour taille -> index du tableau { if (exburn) { XmlTextWriter.WriteEndElement(); // bricolage ... } exburn = false; XmlTextWriter = savewritenode(XmlTextWriter, datatab, item); //if (datatab.type == "Data.Burn") { exburn = true; } // bricolage du bug quil ferme pas tout ... //XmlTextWriter.WriteEndElement(); nodeouvert++; exnode = namet.Split('/'); } else { XmlTextWriter.WriteStartElement(item); nodeouvert++; } } ii++; } // si c'est la derniere donnée on ferme tout if (databurn.Count() == iielement) { while (nodeouvert > 0) { XmlTextWriter.WriteEndElement(); nodeouvert = nodeouvert - 1; } } iielement++; } return(XmlTextWriter); }
/// <summary> /// Exporter un flux du datavalue /// </summary> /// <param name="datavalue"></param> /// <param name="Xml">true=xml, false=json</param> /// <returns></returns> public static string ToFlux(this ParamValues datavalue, bool isXml = true) { IParamValuesSerializer serial = ParamValuesTools.SerializerFactory(isXml); return(serial.Serialize(datavalue)); }