示例#1
0
文件: TextTools.cs 项目: NueGy/NgLib
        /// <summary>
        /// Transforme un dictionary de positions en une chaine
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static string JoinText(IDictionary <string, string> datas, TEXT.TextStructure schema)
        {
            try
            {
                StringBuilder retour = new StringBuilder();

                int lastPositionPlusSize = 0;
                int partnumber           = 0;
                foreach (var ipart in schema.GetPartsOrdered())
                {
                    TextStructurePart partschema = ipart.Value;


                    // obtenir la donnée
                    string orgnData = null;
                    if (datas.ContainsKey(partschema.NodeName))
                    {
                        orgnData = datas[partschema.NodeName];
                    }
                    if (string.IsNullOrWhiteSpace(orgnData) && partschema != null && partschema.CompleteNumber)
                    {
                        orgnData = "0";                                                                                         // si complete number, ce sera forcément des 0
                    }
                    retour.Append(JoinTextItem(schema, partschema, orgnData, partnumber, lastPositionPlusSize));
                    partnumber++;
                }
                return(retour.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("JoinText {0}", ex.Message), ex);
            }
        }
示例#2
0
        public bool DeletePart(int Position)
        {
            TextStructurePart ipart = this.GetPart(Position);

            if (ipart == null)
            {
                return(false);
            }
            return(this.Parts.Remove(ipart));
        }
示例#3
0
 public bool SetPart(TextStructurePart part)
 {
     if (part.Position < 0)
     {
         return(false);
     }
     DeletePart(part.Position);
     this.Parts.Add(part);
     return(true);
 }
示例#4
0
        public string GetName(int position)
        {
            TextStructurePart ipart = this.GetPart(position);

            if (ipart == null)
            {
                return(null);
            }
            else
            {
                return(ipart.Name);
            }
        }
示例#5
0
        public void SetStructure(DATAVALUES.DataValues datastruct)
        {
            if (datastruct == null)
            {
                return;
            }
            this.OriginalStructureModel = datastruct;
            // INFOS
            string txtmodestr = datastruct.GetString("TextMode");

            if (txtmodestr.Equals("CSV", StringComparison.InvariantCultureIgnoreCase))
            {
                this.TextMode = TextModeEnum.CSV; this.TextModeCSVSeparator = ';';
            }
            else if (txtmodestr.Equals("TXT", StringComparison.InvariantCultureIgnoreCase))
            {
                this.TextMode = TextModeEnum.TXT; this.TextModeCSVSeparator = ' ';
            }

            string TextModeCSVSeparatorstr = datastruct.GetString("TextModeCSVSeparator");

            if (TextModeCSVSeparatorstr.Length > 0)
            {
                this.TextModeCSVSeparator = TextModeCSVSeparatorstr[0];
            }

            // PARTIES
            List <DATAVALUES.DataValuesNode> partsdv = datastruct.GetDatas(DefaultPartsNodes);

            foreach (DATAVALUES.DataValuesNode item in partsdv)
            {
                string            namem = item.NodeName;
                TextStructurePart ipart = this.GetPart(namem);
                if (ipart == null)
                {
                    if (string.IsNullOrWhiteSpace(item["position"]))
                    {
                        continue;
                    }
                    ipart = new TextStructurePart();
                    ipart.Fusion(item);
                    this.Parts.Add(ipart);
                }
                else
                {
                    ipart.Fusion(item, true);
                }
            }
        }
示例#6
0
        public TextStructurePart SetPart(int Position, int Lenght = 0, string Name = null)
        {
            TextStructurePart retour = new TextStructurePart();

            retour.Position = Position;
            retour.Name     = Name;
            retour.Length   = Lenght;
            if (this.SetPart(retour))
            {
                return(retour);
            }
            else
            {
                return(null);
            }
        }
示例#7
0
        public void SetStructureFromCSV(string inputString, bool Header = true, bool safe = true)
        {
            int iposition = 0;

            string[] inputStringT = inputString.Split(this.TextModeCSVSeparator);
            foreach (string partstring in inputStringT)
            {
                TextStructurePart ipart = new TextStructurePart();
                ipart.Position = iposition;

                if (Header)
                {
                    ipart.Name = inputStringT[ipart.Position].Trim();
                }
                this.SetPart(ipart);
                iposition++;
            }
        }
示例#8
0
 public void SetStructure(Dictionary <int, string> PositionAndName)
 {
     if (PositionAndName == null)
     {
         return;
     }
     foreach (int Position in PositionAndName.Keys)
     {
         TextStructurePart ipart = this.GetPart(Position);
         if (ipart == null)
         {
             string namem = PositionAndName[Position];
             ipart          = new TextStructurePart();
             ipart.Position = Position;
             ipart.Name     = namem;
             this.Parts.Add(ipart);
         }
     }
 }
示例#9
0
 public int GetPosition(string namePart)
 {
     try
     {
         TextStructurePart ipart = this.GetPart(namePart);
         if (ipart == null)
         {
             return(-1);
         }
         else
         {
             return(ipart.Position);
         }
     }
     catch (Exception e)
     {
         return(-2);
     }
 }
示例#10
0
文件: TextTools.cs 项目: NueGy/NgLib
        private static Dictionary <string, string> SplitTextTXT(string inputString, TEXT.TextStructure schema)
        {
            try
            {
                int sizestring                     = inputString.Length;
                int lastPositionPlusSize           = 0;
                Dictionary <string, string> retour = new Dictionary <string, string>();
                foreach (var ipart in schema.GetPartsOrdered())
                {
                    TextStructurePart partschema = ipart.Value;

                    int itemPosition    = partschema.Position; // Attention position logique et non réel  1=0
                    int itemSize        = partschema.Length;
                    int itemPositionEnd = itemPosition + itemSize;
                    if (itemPositionEnd > sizestring)
                    {
                        itemSize = sizestring - itemPositionEnd;
                    }

                    if (sizestring < itemPosition || itemSize < 1)  // hors limite (la taille de la chaine est plus petite que ce qui a été défini)
                    {
                        retour.Add(partschema.NodeName, null);
                        continue;
                    }

                    // on découpe la partie qui nous interresse
                    string datastr = inputString.Substring(itemPosition, itemSize);

                    if (!schema.ReadDisallowTrim && datastr != null)
                    {
                        datastr = datastr.Trim();
                    }
                    retour.Add(partschema.NodeName, datastr);
                    lastPositionPlusSize = itemPositionEnd;
                }
                return(retour);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SplitText TXT {0}", ex.Message), ex);
            }
        }
示例#11
0
        public void SetStructure(System.Data.DataTable tableStruct)
        {
            //if (TypeFile != TypeFileEnum.CSV) throw new Exception("(DEV) SetStructure a partir de DataTable est disponible que pour le CSV");
            int position = 0;

            foreach (System.Data.DataColumn col in tableStruct.Columns)
            {
                string            namem = col.ColumnName;
                TextStructurePart ipart = this.GetPart(namem);
                if (ipart == null)
                {
                    ipart          = new TextStructurePart();
                    ipart.Position = position;
                    ipart.Name     = namem;
                    ipart.Length   = col.MaxLength;
                    this.Parts.Add(ipart);
                    position++;
                }
            }
        }
示例#12
0
文件: TextTools.cs 项目: NueGy/NgLib
        private static string JoinTextItem(TEXT.TextStructure schema, TextStructurePart partschema, string orgnData, int partNumber, int lastPositionPlusSize)
        {
            try
            {
                // Valider / Transformer / dynamiser la donnée
                // !!!



                if (schema.TextMode == TextModeEnum.CSV) // Génération simple d'un CSV
                {
                    if (partNumber == 0)
                    {
                        return(orgnData);                 // on met pas de séparateur pour le premier champ
                    }
                    else
                    {
                        return(schema.TextModeCSVSeparator.ToString() + orgnData);
                    }
                }
                else if (schema.TextMode == TextModeEnum.TXT)
                {
                    // on ajoute des blancs ou des zero pour avoir la bonne taille
                    orgnData             = FORMAT.StringUtilities.Complete(orgnData, partschema.Length, partschema.CompleteNumber, true);
                    lastPositionPlusSize = partschema.Position + partschema.Length;

                    //Ajouter des blancs si il y as un trou (!!! es vraiement nécessaire ?)
                    if (lastPositionPlusSize < (partschema.Position))
                    {
                        return(new string(schema.TextModeCSVSeparator, partschema.Position - lastPositionPlusSize));
                    }
                    return(orgnData);
                }
                return(string.Empty);
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#13
0
        public void ToSchema(TextStructurePart item)
        {
            if (this.OptionsPart != null)
            {
                item.Fusion(this.OptionsPart);
            }
            item.Name           = this.Name;
            item.Position       = this.Position;
            item.DataBaseColumn = this.DataBaseColumn;
            item.DefaultValue   = this.DefaultValue;


            // Traitements spécifiques régulièrement utilisé réécrit pour plus de facilité d'accès
            if (!string.IsNullOrEmpty(this.ValidateRegex))
            {
                item[FORMAT.StringTransform.DefaultPrefix + "regex"] = this.ValidateRegex;
            }
            if (this.CompleteNumber)
            {
                item[FORMAT.StringTransform.DefaultPrefix + "CompleteNumber"] = "true";
            }
        }
示例#14
0
文件: TextTools.cs 项目: NueGy/NgLib
 private static Dictionary <string, string> SplitTextCSV(string inputString, TEXT.TextStructure schema)
 {
     try
     {
         string[] inputStringT = inputString.Split(schema.TextModeCSVSeparator);
         Dictionary <string, string> retour = new Dictionary <string, string>();
         foreach (var ipart in schema.GetPartsOrdered())
         {
             TextStructurePart partschema = ipart.Value;
             if (inputStringT.Length < partschema.Position)
             {
                 retour.Add(partschema.NodeName, null);
                 continue;
             }
             retour.Add(partschema.NodeName, inputStringT[ipart.Key]);
         }
         return(retour);
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("SplitText CSV {0}", ex.Message), ex);
     }
 }
示例#15
0
        ///// <summary>
        ///// Enrichir un objet avec des données provenant de la base
        ///// </summary>
        ///// <param name="line"></param>
        ///// <param name="row"></param>
        ///// <returns></returns>
        //public bool ReadDataRow(TexteManipulate line, System.Data.DataRow row)
        //{
        //    bool retour = false;
        //    TextStructure structureline = line.GetStructure();
        //    if (structureline == null) return false;
        //    foreach (TextStructurePart part in structureline.Parts.Values)
        //    {

        //        if (!string.IsNullOrWhiteSpace(part.DataBaseColumn))
        //        {
        //            line[part.Position] = MANIPULATE.DATASET.DataSetTools.GetRowString(row, part.DataBaseColumn);

        //            // !!! ajouter des action/manipulation lors de la lecture en base

        //            retour = true; // on confirme qu'il y as eu des lectures
        //        }
        //    }
        //    return retour;
        //}



        #endregion



        #region Serial Reflexion

        //public List<Tobj> DeSerializationReflexion<Tobj>(params string[] LinesString) where Tobj : new()
        //{
        //    System.Type objType = typeof(Tobj);
        //    List<Tobj> retour = new List<Tobj>();
        //    try
        //    {
        //        GetStructureLineByReflextion(objType);
        //        AttributePart[] attritems = AttributePart.GetObjectPartAttributes(objType);

        //        foreach (string LineString in LinesString)
        //        {
        //            MANIPULATE.TEXT.TextLine textparts = new TextLine(Structures.DefaultStructureLine);
        //            textparts.FillString(LineString);
        //            Tobj lineData = new Tobj();

        //            foreach (AttributePart item in attritems)
        //            {
        //                if (item.Position<0) continue;
        //                MANIPULATE.TEXT.TextPart part = textparts.GetPart(item.Position);
        //                if(part==null) continue;
        //                string resultStr = part.ToString(true, true);
        //                object resultData = ConvertPlus.ChangeType(resultStr, item.ObjectPropertyInfo.PropertyType);
        //                item.ObjectPropertyInfo.SetValue(lineData, resultData,null);
        //            }
        //            retour.Add(lineData);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("UnSerialize " + ex.Message, ex);
        //    }

        //    return retour;
        //}



        ///// <summary>
        ///// !!! a developper
        ///// </summary>
        ///// <typeparam name="Tobj"></typeparam>
        ///// <param name="objs"></param>
        ///// <returns></returns>
        //public string[] SerializationReflexion<Tobj>(List<Tobj> objs) where Tobj : new()
        //{
        //    System.Type objType = typeof(Tobj);
        //    //List<string> retour = new List<string>();
        //    List<DATA.MANIPULATE.TEXT.TextLine> texteLines = new List<TextLine>();
        //    try
        //    {


        //        GetStructureLineByReflextion(objType);
        //        AttributePart[] attritems = AttributePart.GetObjectPartAttributes(objType);


        //        foreach (Tobj obj in objs)
        //        {
        //            DATA.MANIPULATE.TEXT.TextLine line = new TextLine(Structures.DefaultStructureLine);

        //            foreach (AttributePart item in attritems)
        //            {
        //                if (item.Position < 0) continue;
        //                object resultData = item.ObjectPropertyInfo.GetValue(obj, null);
        //                string resultString = string.Empty;
        //                if (resultData != null) resultString = Convert.ToString(resultData);
        //                line.SetString(item.Position, resultString);
        //            }
        //            texteLines.Add(line);
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("SerializationReflexion " + ex.Message, ex);
        //    }
        //    return this.SerializationTextLines(texteLines);
        //}



        public TextStructure GetStructureLineByReflextion(System.Type objType)
        {
            try
            {
                TextStructure StructureLineObj = null;

                TextPartAttribute[] attrs    = TextPartAttribute.GetObjectPartAttributes(objType);
                TextLineAttribute   attrline = TextLineAttribute.GetObjectLineAttributes(objType);

                if (attrline == null)
                {
                    return(null);                 //throw new Exception("Attribute line Empty");
                }
                if (string.IsNullOrWhiteSpace(attrline.StructureLineName))
                {
                    return(null);
                }
                if (Structures.StructuresLines.Count == null)
                {
                    Structures.DefaultStructureLine = new TextStructure();
                }

                StructureLineObj = this.Structures.GetStructure(attrline.StructureLineName);
                if (StructureLineObj == null)
                {
                    StructureLineObj = new TextStructure(attrline.StructureLineName);
                }

                StructureLineObj.TextMode = attrline.TextMode;


                // enrichir si la structure existe deja
                foreach (TextPartAttribute item in attrs)
                {
                    TextStructurePart ispart = null;
                    if (!string.IsNullOrWhiteSpace(item.Name))
                    {
                        ispart = StructureLineObj.GetPart(item.Name);                                        // recherche par le nom
                    }
                    else if (item.Position >= 0)
                    {
                        ispart = StructureLineObj.GetPart(item.Position);                          // recherche par la position
                    }
                    if (ispart == null)
                    {
                        if (StructureLineObj.TextMode == TextModeEnum.TXT && item.Length == 0)
                        {
                            continue;                                                                    //inutile ...
                        }
                        ispart          = new TextStructurePart();
                        ispart.Position = item.Position;
                        ispart.Length   = item.Length;
                        ispart.Name     = item.Name;

                        //if (item.StringTransformOptions != null)
                        //    foreach (var item in item.StringTransformOptions)
                        //    {

                        //    }

                        /*
                         * if (!string.IsNullOrWhiteSpace(item.Regex))
                         * {
                         *  if (ispart.data == null) ispart.data = new DATAVALUES.DataValues_data();
                         *  ispart.data["chaine_regex"] = item.Regex;
                         * }*/

                        ispart.CompleteNumber = item.CompleteNumber;
                    }
                    StructureLineObj.SetPart(ispart);
                }


                this.Structures.SetStructure(StructureLineObj);
                return(StructureLineObj);
            }
            catch (Exception ex)
            {
                throw new Exception("GetStructureLineByReflextion " + ex.Message);
            }
        }