/Prototipo/Servlet/src/java/Validador.java

http://prototipomemoria.googlecode.com/ · Java · 276 lines · 203 code · 34 blank · 39 comment · 11 complexity · ac84b9ebe8789b5ffd9bb1d77c59d7d7 MD5 · raw file

  1. //import de.micromata.opengis.kml.v_2_2_0.Kml;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.Reader;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Random;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.xml.transform.Source;
  15. import javax.xml.transform.stream.StreamSource;
  16. import javax.xml.validation.Schema;
  17. import javax.xml.validation.SchemaFactory;
  18. import javax.xml.validation.Validator;
  19. import org.xml.sax.SAXException;
  20. /*
  21. * To change this template, choose Tools | Templates
  22. * and open the template in the editor.
  23. */
  24. /**
  25. *
  26. * @author Fran
  27. */
  28. public class Validador {
  29. // List<ElemGeograf> listaElementos;
  30. int maxPtosLinea = 40;
  31. int poblacion = 200;
  32. public void validar (String fileNames) throws SAXException, IOException
  33. {
  34. //System.out.println("1");
  35. // 1. Lookup a factory for the W3C XML Schema language
  36. SchemaFactory factory =
  37. SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  38. //System.out.println("2");
  39. // 2. Compile the schema.
  40. // Here the schema is loaded from a java.io.File, but you could use
  41. // a java.net.URL or a javax.xml.transform.Source instead.
  42. File schemaLocation = new File("C:\\Users\\Fran\\Desktop\\ogckml22.xsd");
  43. // System.out.println("Que pasa");
  44. Schema schema = factory.newSchema(schemaLocation);
  45. // System.out.println(" 3");
  46. // 3. Get a validator from the schema.
  47. Validator validator = schema.newValidator();
  48. // System.out.println("4 ");
  49. // 4. Parse the document you want to check.
  50. System.out.println(fileNames);
  51. FileInputStream f = new FileInputStream(new File("C:\\Users\\Fran\\Desktop\\Validaciones\\"+fileNames+".kml"));
  52. Source source = new StreamSource(f);
  53. // System.out.println("5 ");
  54. // 5. Check the document
  55. try {
  56. // System.out.println("6 ");
  57. validator.validate(source);
  58. // System.out.println("7");
  59. System.out.println(" is valid.");
  60. }
  61. catch (Exception ex) {
  62. System.out.println(" is not valid because ");
  63. // System.out.println(" 8");
  64. System.out.println(ex.getMessage());
  65. }
  66. }
  67. public void EmpezarTest()
  68. {
  69. List<ElemGeograf> lista;
  70. for(int i=0 ; i < poblacion ; i= i +1)
  71. {
  72. //Se generan los numeros randomicos de poligonos, ptos y lineas
  73. //Se hacen esos elem geograf y vuelven en una array
  74. System.out.println("Test "+i);
  75. lista = startFill();
  76. //se pasa esa lista de elem geograf a archivo
  77. this.TgenerarArchivo(i, lista);
  78. }
  79. for(int j = 0 ; j < poblacion ; j = j + 1)
  80. {
  81. String fileNames = "Test"+j;
  82. try {
  83. this.validar(fileNames);
  84. } catch (Exception ex) {
  85. System.out.println("Exception es aca: " + ex.getMessage());
  86. //ex.printStackTrace();
  87. }
  88. }
  89. }
  90. private List<ElemGeograf> startFill()
  91. {
  92. Random r = new Random();
  93. int max=300;
  94. int cantPoligonos;
  95. int cantPtos;
  96. int cantLineas;
  97. cantPoligonos = r.nextInt(max);
  98. cantPtos = r.nextInt(max);
  99. cantLineas = r.nextInt(max);
  100. return this.FillElemGeograf(cantPoligonos, cantLineas, cantPtos);
  101. }
  102. private List<ElemGeograf> FillElemGeograf(int cantPoligonos, int cantLineas, int cantPtos)
  103. {
  104. System.out.println("Pol "+cantPoligonos + " lines " +cantLineas + " Ptos "+ cantPtos);
  105. List<ElemGeograf> listaElementos = new ArrayList<ElemGeograf>();
  106. for(int topePol = 0 ; topePol < cantPoligonos ; topePol = topePol + 1)
  107. {
  108. Poligono pol= this.hacerPoligono();
  109. listaElementos.add(pol);
  110. }
  111. for(int topePto = 0 ; topePto < cantLineas ; topePto = topePto + 1)
  112. {
  113. Punto pto = this.hacerPunto();
  114. listaElementos.add(pto);
  115. }for(int topeLin = 0 ; topeLin < cantPtos ; topeLin = topeLin + 1)
  116. {
  117. Linea line = this.hacerLinea();
  118. listaElementos.add(line);
  119. }
  120. return listaElementos;
  121. }
  122. private Punto hacerPunto()
  123. {
  124. Coordenadas c1 = hacerCoordenadas();
  125. Punto po1 = new Punto(c1);
  126. return po1;
  127. }
  128. private Linea hacerLinea()
  129. {
  130. Random r = new Random();
  131. int cantidadDePuntos = r.nextInt(maxPtosLinea);
  132. while(cantidadDePuntos < 2)
  133. {
  134. cantidadDePuntos = r.nextInt(maxPtosLinea);
  135. }
  136. ArrayList<Coordenadas> ptos = new ArrayList<Coordenadas>();
  137. for(int cPto = 0 ; cPto < cantidadDePuntos; cPto = cPto + 1)
  138. {
  139. Coordenadas c = hacerCoordenadas();
  140. ptos.add(c);
  141. }
  142. Linea line = new Linea(ptos);
  143. return line;
  144. }
  145. private Poligono hacerPoligono()
  146. {
  147. Random r = new Random();
  148. int cantidadDePuntos = r.nextInt(maxPtosLinea);
  149. while(cantidadDePuntos < 4)
  150. {
  151. cantidadDePuntos = r.nextInt(maxPtosLinea);
  152. }
  153. ArrayList<Coordenadas> ptos = new ArrayList<Coordenadas>();
  154. for(int cPto = 0 ; cPto < cantidadDePuntos; cPto = cPto + 1)
  155. {
  156. Coordenadas c = hacerCoordenadas();
  157. ptos.add(c);
  158. }
  159. Poligono pol = new Poligono(ptos);
  160. return pol;
  161. }
  162. private Coordenadas hacerCoordenadas()
  163. {
  164. Random r = new Random();
  165. double lat = r.nextDouble()*100;
  166. double lon = r.nextDouble()*100;
  167. while(lat<30 || lat>60)
  168. {
  169. lat = r.nextDouble()*100;
  170. }
  171. while(lon<30 || lon>60)
  172. {
  173. lon = r.nextDouble()*100;
  174. }
  175. if(r.nextBoolean())
  176. {
  177. lat = lat * -1;
  178. }
  179. if(r.nextBoolean())
  180. {
  181. lon = lon * -1;
  182. }
  183. //System.out.println(lat + " " + lon);
  184. Coordenadas c= new Coordenadas(lat, lon);
  185. return c;
  186. }
  187. private void TLlenarArray()
  188. {
  189. Coordenadas c1 = new Coordenadas(-36.123,-56.312);
  190. Coordenadas c10 = new Coordenadas(-32.5675123,-57.8888432);
  191. Coordenadas c11 = new Coordenadas(-33.765123,-57.4999932);
  192. Coordenadas c12 = new Coordenadas(-31.14623,-57.47772);
  193. ArrayList<Coordenadas> array1 = new ArrayList<Coordenadas>();
  194. array1.add(c1);
  195. array1.add(c10);
  196. array1.add(c11);
  197. array1.add(c12);
  198. Linea l1 = new Linea(array1);
  199. Coordenadas c2 = new Coordenadas(-36.123,-56.312);
  200. Coordenadas c20 = new Coordenadas(-34.123,-57.232);
  201. Coordenadas c21 = new Coordenadas(-33.123,-58.432);
  202. Coordenadas c22 = new Coordenadas(-36.123,-56.312);
  203. ArrayList<Coordenadas> array2 = new ArrayList<Coordenadas>();
  204. array2.add(c2);
  205. array2.add(c20);
  206. array2.add(c21);
  207. array2.add(c22);
  208. Coordenadas c3 = new Coordenadas(-37.123, -56.312);
  209. Punto po1 = new Punto(c3);
  210. Poligono p1 = new Poligono (array2);
  211. // this.listaElementos = new ArrayList<ElemGeograf>();
  212. //this.listaElementos.add(l1);
  213. // this.listaElementos.add(p1);
  214. // this.listaElementos.add(po1);
  215. }
  216. private void TgenerarArchivo (int numeroDelArchivo, List<ElemGeograf> listaElementos)
  217. {
  218. //C:\Program Files\SlikSvn\bin
  219. String fileName = "C:\\Users\\Fran\\Desktop\\Validaciones\\Test"+numeroDelArchivo+".kml";
  220. //String fileName = "C:\\Users\\Fran\\prototipomemoria\\Prototipo\\Servlet\\ArchCuatro.kml";
  221. //String NombreArchivo = "ArchivoKML.kml";
  222. FileWriter fw;
  223. BufferedWriter bw;
  224. String archivo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <kml xmlns=\"http://www.opengis.net/kml/2.2\"> \n <Document>\n";
  225. for(ElemGeograf e:listaElementos)
  226. {
  227. archivo = archivo + e.ToKml(0);
  228. }
  229. archivo = archivo + "</Document> \n </kml>";
  230. try{
  231. fw = new FileWriter(fileName);
  232. bw = new BufferedWriter(fw);
  233. bw.write(archivo);
  234. bw.close();
  235. fw.close();
  236. }catch(IOException e)
  237. {
  238. e.printStackTrace();
  239. }
  240. //System.out.println("llamo aca a lo que quiero llamar");
  241. }
  242. }