Revision 8416
trunk/extensions/extPublish/src/org/gvsig/remoteservices/conf/mapserver/test/DDF2postgres.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.remoteservices.conf.mapserver.test; |
|
3 |
|
|
4 |
import java.io.DataInputStream; |
|
5 |
import java.io.File; |
|
6 |
import java.io.FileInputStream; |
|
7 |
import java.io.FileNotFoundException; |
|
8 |
import java.io.FileWriter; |
|
9 |
import java.io.IOException; |
|
10 |
import java.util.Vector; |
|
11 |
|
|
12 |
public class DDF2postgres{ |
|
13 |
|
|
14 |
public static void main(String[] args){ |
|
15 |
String path="/home/david/pruebas"; |
|
16 |
String squema=""; |
|
17 |
File f=new File(path); |
|
18 |
File sql=null; |
|
19 |
DataInputStream read = null; |
|
20 |
FileWriter write = null; |
|
21 |
File aux[]=f.listFiles(); |
|
22 |
int i; |
|
23 |
sql=new File(path+"/sql"); |
|
24 |
sql.deleteOnExit(); |
|
25 |
sql.mkdir(); |
|
26 |
for(i=0;i<aux.length;i++){ |
|
27 |
Vector data=new Vector(); |
|
28 |
String s3=null; |
|
29 |
int contd=0; |
|
30 |
int contf=0; |
|
31 |
sql=new File(path+"/"+"sql/"+aux[i].getName().split(".ddf")[0]+".sql"); |
|
32 |
String table=sql.getName().split(".sql")[0]; |
|
33 |
System.out.println(table); |
|
34 |
System.out.println(sql.getName()); |
|
35 |
try{ |
|
36 |
read = new DataInputStream(new FileInputStream(aux[i])); |
|
37 |
write = new FileWriter(sql); |
|
38 |
String s=read.readLine(); |
|
39 |
String s2[]=null; |
|
40 |
if(args[0]==null){ |
|
41 |
s3="INSERT INTO "+table.toLowerCase()+"("; |
|
42 |
} |
|
43 |
else{ |
|
44 |
s3="INSERT INTO "+args[0]+"."+table.toLowerCase()+"("; |
|
45 |
} |
|
46 |
String s4=""; |
|
47 |
String s5=null; |
|
48 |
while(s.compareTo("[FIELDS]")!=0){ |
|
49 |
s=read.readLine(); |
|
50 |
} |
|
51 |
s=read.readLine(); |
|
52 |
while(s.compareTo("")!=0){ |
|
53 |
s2=s.split(","); |
|
54 |
contd++; |
|
55 |
data.addElement(s2[0]); |
|
56 |
System.out.println(s2[0]); |
|
57 |
s=read.readLine(); |
|
58 |
} |
|
59 |
s4=s3; |
|
60 |
int j; |
|
61 |
for(j=0;j<data.size()-1;j++){ |
|
62 |
s4=s4+data.elementAt(j)+","; |
|
63 |
} |
|
64 |
s4=s4+data.elementAt(data.size()-1)+") VALUES("; |
|
65 |
while(s.compareTo("[DATA]")!=0){ |
|
66 |
s=read.readLine(); |
|
67 |
} |
|
68 |
s=read.readLine(); |
|
69 |
s5=s4; |
|
70 |
while(s!=null){ |
|
71 |
s2=s.split("\t"); |
|
72 |
contf=s2.length; |
|
73 |
int k; |
|
74 |
write.write(s5); |
|
75 |
for(k=0;k<s2.length-1;k++){ |
|
76 |
if(s2[k].compareTo("")==0){ |
|
77 |
write.write("null,"); |
|
78 |
System.out.print(s2[k]); |
|
79 |
} |
|
80 |
else{ |
|
81 |
write.write("\'"+s2[k]+"\',"); |
|
82 |
System.out.print(s2[k]); |
|
83 |
} |
|
84 |
} |
|
85 |
write.write("\'"+s2[s2.length-1]+"\'"); |
|
86 |
if(contd-contf>0){ |
|
87 |
int l; |
|
88 |
write.write(","); |
|
89 |
for(l=0;l<(contd-contf)-1;l++){ |
|
90 |
write.write("null,"); |
|
91 |
} |
|
92 |
write.write("null"); |
|
93 |
|
|
94 |
} |
|
95 |
write.write(");\n"); |
|
96 |
System.out.println(); |
|
97 |
s=read.readLine(); |
|
98 |
if(s!=null){ |
|
99 |
if(s.compareTo("")==0){ |
|
100 |
s=read.readLine(); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
write.close(); |
|
105 |
} |
|
106 |
catch(FileNotFoundException e1){ |
|
107 |
System.out.println("File not found."); |
|
108 |
} |
|
109 |
catch(IOException e){ |
|
110 |
System.out.println("I/O error."); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
0 | 115 |
Also available in: Unified diff