/InterfazGrafico/LibreriaClases/Propiedades/PEstructura.cs

http://vdata.codeplex.com · C# · 195 lines · 154 code · 13 blank · 28 comment · 7 complexity · 9682c5c3c2dfb853111347bccc634aa7 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. using InterfazGrafico.LibreriaClases.Clases;
  6. using InterfazGrafico.LibreriaConstantes;
  7. using InterfazGrafico.LibreriaConstantes.Elementos;
  8. namespace InterfazGrafico.LibreriaClases.Propiedades
  9. {
  10. /// <summary>
  11. /// Almacena las propiedades de una estructura.
  12. /// </summary>
  13. public class PEstructura : Propiedades
  14. {
  15. /// <summary>
  16. /// Nombre.
  17. /// </summary>
  18. public string Nombre
  19. {
  20. get
  21. {
  22. return this.getPropiedadString(CEstructura.Nombre);
  23. }
  24. set
  25. {
  26. this.setOrAddPropiedad(CEstructura.Nombre, value);
  27. }
  28. }
  29. /// <summary>
  30. /// Lista de nombres de los campos.
  31. /// </summary>
  32. public List<string> ListaNombresCampos
  33. {
  34. get
  35. {
  36. return this.getPropiedadListaString(CEstructura.NombreCampos);
  37. }
  38. set
  39. {
  40. this.setOrAddPropiedad(CEstructura.NombreCampos, value);
  41. }
  42. }
  43. /// <summary>
  44. /// Lista de las posiciones iniciales de los campos.
  45. /// </summary>
  46. public List<int> ListaPosicionInicialCampos
  47. {
  48. get
  49. {
  50. return this.getPropiedadListaInt(CEstructura.PosicionCampos);
  51. }
  52. set
  53. {
  54. this.setOrAddPropiedad(CEstructura.PosicionCampos, value);
  55. }
  56. }
  57. /// <summary>
  58. /// Lista de las longitudes de los campos.
  59. /// </summary>
  60. public List<int> ListaLongitudCampos
  61. {
  62. get
  63. {
  64. return this.getPropiedadListaInt(CEstructura.LongitudCampos);
  65. }
  66. set
  67. {
  68. this.setOrAddPropiedad(CEstructura.LongitudCampos, value);
  69. }
  70. }
  71. /// <summary>
  72. /// Constructor por defecto.
  73. /// </summary>
  74. public PEstructura()
  75. : base()
  76. {
  77. }
  78. /// <summary>
  79. /// Obtiene los parámetros a partir del fichero XML indicado.
  80. /// </summary>
  81. /// <param name="fichero">Fichero de configuración XML</param>
  82. override public void leer(string fichero)
  83. {
  84. try
  85. {
  86. base.leer(fichero);
  87. XmlDocument xml = LeerXml.AbrirFicheroParametros(fichero);
  88. try
  89. { this.Nombre = LeerXml.ObtenerValorCampo(xml, CEstructura.RutaNombre); }
  90. catch (Exception)
  91. { this.Nombre = string.Empty; };
  92. try
  93. {
  94. int numeroCampos = LeerXml.ObtenerNumeroNodosCampo(xml, CEstructura.RutaCampos);
  95. for (int indiceCampo = 0; indiceCampo < numeroCampos; indiceCampo++)
  96. {
  97. string nombre = string.Empty;
  98. try
  99. { nombre = LeerXml.ObtenerValorCampo(xml, CEstructura.RutaCampos, indiceCampo, CEstructura.NodoCamposCampoNombre); }
  100. catch (Exception)
  101. { continue; }
  102. this.ListaNombresCampos.Add(nombre);
  103. // Posicion inicial
  104. int posicion = 0;
  105. try
  106. { posicion = Int32.Parse(LeerXml.ObtenerValorCampo(xml, CEstructura.RutaCampos, indiceCampo, CEstructura.NodoCamposCampoPosicion));
  107. if (posicion < 0)
  108. {
  109. if (this.ListaNombresCampos.Count == 0)
  110. posicion = 0;
  111. else
  112. posicion = this.ListaPosicionInicialCampos[this.ListaPosicionInicialCampos.Count - 1] + this.ListaLongitudCampos[this.ListaLongitudCampos.Count - 1];
  113. }
  114. }
  115. catch (Exception)
  116. {
  117. if (this.ListaNombresCampos.Count == 0)
  118. posicion = 0;
  119. else
  120. posicion = this.ListaPosicionInicialCampos[this.ListaPosicionInicialCampos.Count - 1] + this.ListaLongitudCampos[this.ListaLongitudCampos.Count - 1];
  121. }
  122. this.ListaPosicionInicialCampos.Add(posicion);
  123. // Longitud
  124. int longitud = 0;
  125. try
  126. {
  127. longitud = Int32.Parse(LeerXml.ObtenerValorCampo(xml, CEstructura.RutaCampos, indiceCampo, CEstructura.NodoCamposCampoLongitud));
  128. if (longitud <= 0)
  129. longitud = 1;
  130. }
  131. catch (Exception)
  132. { longitud = 1; }
  133. this.ListaLongitudCampos.Add(longitud);
  134. }
  135. }
  136. catch (Exception)
  137. {
  138. this.ListaNombresCampos = new List<string>();
  139. this.ListaPosicionInicialCampos = new List<int>();
  140. this.ListaLongitudCampos = new List<int>();
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. throw new ApplicationException(Constantes.ERROR_LECTURA_FICHERO_ESTRUCTURAS, ex);
  146. }
  147. }
  148. /// <summary>
  149. /// Escribe los parámetros al fichero XML indicado.
  150. /// </summary>
  151. /// <param name="fichero">Fichero de configuración XML</param>
  152. public override void escribir(string fichero)
  153. {
  154. try
  155. {
  156. base.escribir(fichero);
  157. XmlWriter xml = EscribirXml.AbrirFicheroParametros(fichero);
  158. EscribirXml.InicioElemento(xml, CEstructura.NodoRaiz);
  159. EscribirXml.EscribeNodo(xml, CEstructura.NodoNombre, Nombre);
  160. EscribirXml.InicioElemento(xml, CEstructura.NodoCampos);
  161. int i = 0;
  162. foreach (string nombre in ListaNombresCampos)
  163. {
  164. EscribirXml.InicioElemento(xml, CEstructura.NodoCamposCampo);
  165. EscribirXml.EscribeNodo(xml, CEstructura.NodoCamposCampoNombre, nombre);
  166. EscribirXml.EscribeNodo(xml, CEstructura.NodoCamposCampoPosicion, ListaPosicionInicialCampos[i].ToString());
  167. EscribirXml.EscribeNodo(xml, CEstructura.NodoCamposCampoLongitud, ListaLongitudCampos[i].ToString());
  168. EscribirXml.FinElemento(xml);
  169. i++;
  170. }
  171. EscribirXml.FinElemento(xml);
  172. EscribirXml.FinElemento(xml);
  173. EscribirXml.Flush(xml);
  174. EscribirXml.Close(xml);
  175. }
  176. catch (Exception ex)
  177. {
  178. throw new ApplicationException(Constantes.ERROR_ESCRITURA_FICHERO_ESTRUCTURAS, ex);
  179. }
  180. }
  181. }
  182. }