Revision 19533
trunk/libraries/libDataSourceBaseDrivers/src-test/org/gvsig/data/datastores/vectorial/driver/DataSourceTest.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import junit.framework.TestCase; |
|
6 |
|
|
7 |
import org.gvsig.data.DataSourceManager; |
|
8 |
import org.gvsig.data.IDataSource; |
|
9 |
import org.gvsig.data.IDataSourceParameters; |
|
10 |
import org.gvsig.data.IDataStoreParameters; |
|
11 |
import org.gvsig.data.datastores.vectorial.driver.dbf.DBFDataSource; |
|
12 |
import org.gvsig.data.datastores.vectorial.driver.dbf.DBFDataSourceParameters; |
|
13 |
import org.gvsig.data.datastores.vectorial.driver.shp.ShpDataSource; |
|
14 |
import org.gvsig.data.datastores.vectorial.driver.shp.ShpDataSourceParameters; |
|
15 |
import org.gvsig.data.exception.ReadException; |
|
16 |
import org.gvsig.data.vectorial.IFeatureCollection; |
|
17 |
import org.gvsig.data.vectorial.IFeatureStore; |
|
18 |
|
|
19 |
public class DataSourceTest extends TestCase { |
|
20 |
private File dbffile = new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile()); |
|
21 |
private File shpfile = new File(DataStoreTest.class.getResource("data/prueba.shp").getFile()); |
|
22 |
|
|
23 |
public void testDBFSources(){ |
|
24 |
org.gvsig.data.datastores.vectorial.driver.dbf.Register.selfRegister(); |
|
25 |
org.gvsig.data.datastores.vectorial.driver.Register.selfRegister(); |
|
26 |
DataSourceManager dsm=DataSourceManager.getManager(); |
|
27 |
|
|
28 |
IDataSourceParameters dsp = dsm.createDataSourceParameters( |
|
29 |
DBFDataSource.DATASOURCE_NAME); |
|
30 |
((DBFDataSourceParameters)dsp).setSource(new File(dbffile.getParent())); |
|
31 |
|
|
32 |
IDataSource src = dsm.createDataSource(dsp); |
|
33 |
|
|
34 |
IDataStoreParameters[] params = null; |
|
35 |
try { |
|
36 |
params = src.getSources(); |
|
37 |
} catch (ReadException e) { |
|
38 |
e.printStackTrace(); |
|
39 |
fail(); |
|
40 |
} |
|
41 |
assertEquals(1, params.length); |
|
42 |
|
|
43 |
|
|
44 |
IFeatureStore fs; |
|
45 |
try { |
|
46 |
fs = (IFeatureStore) dsm.createDataStore(params[0]); |
|
47 |
fs.open(); |
|
48 |
|
|
49 |
IFeatureCollection fc = (IFeatureCollection) fs.getDataCollection(); |
|
50 |
|
|
51 |
assertEquals(9, fc.size()); |
|
52 |
|
|
53 |
fs.close(); |
|
54 |
} catch (Exception e) { |
|
55 |
e.printStackTrace(); |
|
56 |
fail(); |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
} |
|
61 |
|
|
62 |
public void testSHPSources(){ |
|
63 |
org.gvsig.data.datastores.vectorial.driver.shp.Register.selfRegister(); |
|
64 |
org.gvsig.data.datastores.vectorial.driver.Register.selfRegister(); |
|
65 |
DataSourceManager dsm=DataSourceManager.getManager(); |
|
66 |
|
|
67 |
IDataSourceParameters dsp = dsm.createDataSourceParameters( |
|
68 |
ShpDataSource.DATASOURCE_NAME); |
|
69 |
((ShpDataSourceParameters)dsp).setSource(new File(shpfile.getParent())); |
|
70 |
|
|
71 |
IDataSource src = dsm.createDataSource(dsp); |
|
72 |
|
|
73 |
IDataStoreParameters[] params = null; |
|
74 |
try { |
|
75 |
params = src.getSources(); |
|
76 |
} catch (ReadException e) { |
|
77 |
e.printStackTrace(); |
|
78 |
fail(); |
|
79 |
} |
|
80 |
assertEquals(1, params.length); |
|
81 |
|
|
82 |
|
|
83 |
IFeatureStore fs; |
|
84 |
try { |
|
85 |
fs = (IFeatureStore) dsm.createDataStore(params[0]); |
|
86 |
fs.open(); |
|
87 |
|
|
88 |
IFeatureCollection fc = (IFeatureCollection) fs.getDataCollection(); |
|
89 |
|
|
90 |
assertEquals(9, fc.size()); |
|
91 |
|
|
92 |
fs.close(); |
|
93 |
} catch (Exception e) { |
|
94 |
e.printStackTrace(); |
|
95 |
fail(); |
|
96 |
} |
|
97 |
|
|
98 |
|
|
99 |
} |
|
100 |
} |
|
0 | 101 |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/shp/ShpDataSourceParameters.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.shp; |
|
2 |
|
|
3 |
import org.gvsig.data.datasource.driver.file.DataSourceFileDriverParameters; |
|
4 |
|
|
5 |
public class ShpDataSourceParameters extends DataSourceFileDriverParameters { |
|
6 |
public static String DATASOURCE_NAME="ShpDataSource"; |
|
7 |
|
|
8 |
|
|
9 |
public String getDataSourceName() { |
|
10 |
return DATASOURCE_NAME; |
|
11 |
} |
|
12 |
|
|
13 |
} |
|
0 | 14 |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/shp/Register.java | ||
---|---|---|
5 | 5 |
public class Register { |
6 | 6 |
public static void selfRegister() { |
7 | 7 |
DataSourceManager dsm = DataSourceManager.getManager(); |
8 |
|
|
9 |
dsm.registerDriver(ShpDriver.DRIVER_NAME,
|
|
10 |
ShpDriver.class,
|
|
8 |
|
|
9 |
dsm.registerDriver(ShpDriver.DRIVER_NAME, |
|
10 |
ShpDriver.class, |
|
11 | 11 |
ShpDriverParameters.class |
12 | 12 |
); |
13 |
dsm.registerDataSource( |
|
14 |
ShpDataSource.DATASOURCE_NAME, |
|
15 |
ShpDataSource.class, |
|
16 |
ShpDataSourceParameters.class |
|
17 |
); |
|
13 | 18 |
} |
14 |
|
|
15 | 19 |
|
20 |
|
|
16 | 21 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/shp/ShpDataSource.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.shp; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import org.gvsig.data.IDataSourceParameters; |
|
6 |
import org.gvsig.data.datastores.vectorial.driver.IDriverParameters; |
|
7 |
import org.gvsig.data.datastores.vectorial.driver.dbf.DBFDataSource; |
|
8 |
import org.gvsig.data.datastores.vectorial.driver.shp.fileshp.SHP; |
|
9 |
|
|
10 |
public class ShpDataSource extends DBFDataSource { |
|
11 |
public static String DATASOURCE_NAME="ShpDataSource"; |
|
12 |
protected ShpDataSourceParameters parameters; |
|
13 |
|
|
14 |
protected boolean isValid(File file) { |
|
15 |
if (!file.exists()){ |
|
16 |
return false; |
|
17 |
} |
|
18 |
if (!(file.getName().toLowerCase().endsWith(".shp"))){ |
|
19 |
return false; |
|
20 |
} |
|
21 |
File shx = SHP.getShxFile(file); |
|
22 |
if (!shx.exists()){ |
|
23 |
return false; |
|
24 |
} |
|
25 |
File dbf = SHP.getDbfFile(file); |
|
26 |
return super.isValid(dbf); |
|
27 |
} |
|
28 |
|
|
29 |
|
|
30 |
protected IDriverParameters newParameter(File file){ |
|
31 |
ShpDriverParameters param = new ShpDriverParameters(); |
|
32 |
param.setSHPFile(file); |
|
33 |
param.setSHXFile(SHP.getShxFile(file)); |
|
34 |
param.setDBFFile(SHP.getDbfFile(file)); |
|
35 |
return param; |
|
36 |
} |
|
37 |
|
|
38 |
public void init(IDataSourceParameters parameters) { |
|
39 |
this.parameters = (ShpDataSourceParameters)parameters; |
|
40 |
this.path = this.parameters.getSource(); |
|
41 |
} |
|
42 |
|
|
43 |
} |
|
0 | 44 |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/dbf/Register.java | ||
---|---|---|
5 | 5 |
public class Register { |
6 | 6 |
public static void selfRegister() { |
7 | 7 |
DataSourceManager dsm = DataSourceManager.getManager(); |
8 |
|
|
9 |
dsm.registerDriver(DBFDriver.DRIVER_NAME,
|
|
10 |
DBFDriver.class,
|
|
8 |
|
|
9 |
dsm.registerDriver(DBFDriver.DRIVER_NAME, |
|
10 |
DBFDriver.class, |
|
11 | 11 |
DBFDriverParameters.class |
12 | 12 |
); |
13 |
dsm.registerDataSource( |
|
14 |
DBFDataSource.DATASOURCE_NAME, |
|
15 |
DBFDataSource.class, |
|
16 |
DBFDataSourceParameters.class |
|
17 |
); |
|
13 | 18 |
} |
14 |
|
|
15 | 19 |
|
20 |
|
|
16 | 21 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/dbf/DBFDataSource.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.dbf; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
|
|
6 |
import org.gvsig.data.DataSourceManager; |
|
7 |
import org.gvsig.data.IDataSourceParameters; |
|
8 |
import org.gvsig.data.IDataStoreParameters; |
|
9 |
import org.gvsig.data.datasource.driver.file.AbstractDataSourceFileDriver; |
|
10 |
import org.gvsig.data.datastores.vectorial.driver.DriverStore; |
|
11 |
import org.gvsig.data.datastores.vectorial.driver.IDriverParameters; |
|
12 |
import org.gvsig.data.datastores.vectorial.driver.IDriverStoreParameters; |
|
13 |
import org.gvsig.data.datastores.vectorial.driver.exception.FileNotFoundDriverException; |
|
14 |
import org.gvsig.data.exception.ReadException; |
|
15 |
|
|
16 |
public class DBFDataSource extends AbstractDataSourceFileDriver { |
|
17 |
|
|
18 |
public static String DATASOURCE_NAME="DBFDataSource"; |
|
19 |
|
|
20 |
protected DBFDataSourceParameters parameters; |
|
21 |
protected File path; |
|
22 |
|
|
23 |
|
|
24 |
public IDataStoreParameters[] getSources() throws ReadException { |
|
25 |
if (!this.path.exists()){ |
|
26 |
new FileNotFoundDriverException(this.getName()+": Path not found '"+this.path+"'",new Exception()); |
|
27 |
} |
|
28 |
DataSourceManager dsm=DataSourceManager.getManager(); |
|
29 |
|
|
30 |
String files[] = this.path.list(); |
|
31 |
int i; |
|
32 |
File theFile; |
|
33 |
ArrayList fileList = new ArrayList(); |
|
34 |
IDriverParameters param; |
|
35 |
IDataStoreParameters dsp = null; |
|
36 |
|
|
37 |
for (i=0;i<files.length;i++){ |
|
38 |
theFile = new File(this.path,files[i]); |
|
39 |
|
|
40 |
if (this.isValid(theFile)){ |
|
41 |
param= this.newParameter(theFile); |
|
42 |
|
|
43 |
|
|
44 |
dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME); |
|
45 |
|
|
46 |
((IDriverStoreParameters)dsp).setDriverParameters(param); |
|
47 |
|
|
48 |
fileList.add(dsp); |
|
49 |
|
|
50 |
} |
|
51 |
|
|
52 |
|
|
53 |
} |
|
54 |
IDataStoreParameters[] x = new IDataStoreParameters[1]; |
|
55 |
x[0]=dsp; |
|
56 |
return (IDataStoreParameters[])fileList.toArray(x); |
|
57 |
} |
|
58 |
|
|
59 |
protected IDriverParameters newParameter(File file){ |
|
60 |
DBFDriverParameters param = new DBFDriverParameters(); |
|
61 |
param.setDBFFile(file); |
|
62 |
return param; |
|
63 |
} |
|
64 |
|
|
65 |
protected boolean isValid(String file){ |
|
66 |
return this.isValid(new File(file)); |
|
67 |
|
|
68 |
} |
|
69 |
|
|
70 |
protected boolean isValid(File file){ |
|
71 |
if (!file.exists()){ |
|
72 |
return false; |
|
73 |
} |
|
74 |
return (file.getName().toLowerCase().endsWith(".dbf")); |
|
75 |
} |
|
76 |
|
|
77 |
public String getName() { |
|
78 |
return this.DATASOURCE_NAME; |
|
79 |
} |
|
80 |
|
|
81 |
public void init(IDataSourceParameters parameters) { |
|
82 |
this.parameters = (DBFDataSourceParameters)parameters; |
|
83 |
this.path = this.parameters.getSource(); |
|
84 |
} |
|
85 |
|
|
86 |
|
|
87 |
} |
|
0 | 88 |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/dbf/DBFDataSourceParameters.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.dbf; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import org.gvsig.data.datasource.driver.file.DataSourceFileDriverParameters; |
|
6 |
|
|
7 |
public class DBFDataSourceParameters extends DataSourceFileDriverParameters { |
|
8 |
|
|
9 |
private static String DATASOURCE_NAME="DBFDataSource"; |
|
10 |
|
|
11 |
public String getDataSourceName() { |
|
12 |
return DATASOURCE_NAME; |
|
13 |
} |
|
14 |
|
|
15 |
} |
|
0 | 16 |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datasource/driver/file/DataSourceFileDriver.java | ||
---|---|---|
1 |
package org.gvsig.data.datasource.driver.file; |
|
2 |
|
|
3 |
import org.gvsig.data.IDataSource; |
|
4 |
import org.gvsig.data.IDataSourceParameters; |
|
5 |
import org.gvsig.data.IDataStoreParameters; |
|
6 |
|
|
7 |
public class DataSourceFileDriver implements IDataSource{ |
|
8 |
public static final String DATASOURCE_NAME = "DataSourceFileDriver"; |
|
9 |
private IDataSourceParameters parameters; |
|
10 |
private IDataStoreParameters[] storeParameters; |
|
11 |
|
|
12 |
public void init(IDataSourceParameters parameters) { |
|
13 |
this.parameters=parameters; |
|
14 |
} |
|
15 |
|
|
16 |
public IDataStoreParameters[] getSources() { |
|
17 |
return storeParameters; |
|
18 |
} |
|
19 |
|
|
20 |
public String getName() { |
|
21 |
return DATASOURCE_NAME; |
|
22 |
} |
|
23 |
|
|
24 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datasource/driver/file/AbstractDataSourceFileDriver.java | ||
---|---|---|
1 |
package org.gvsig.data.datasource.driver.file; |
|
2 |
|
|
3 |
import java.net.URI; |
|
4 |
|
|
5 |
import org.gvsig.data.IDataSourceParameters; |
|
6 |
import org.gvsig.data.IDataStoreParameters; |
|
7 |
import org.gvsig.data.vectorial.AbstractDataSourceFile; |
|
8 |
|
|
9 |
public abstract class AbstractDataSourceFileDriver extends AbstractDataSourceFile{ |
|
10 |
static String DATASOURCE_NAME = "DataSourceFileDriver"; |
|
11 |
|
|
12 |
|
|
13 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datasource/driver/file/DataSourceFileDriverParameters.java | ||
---|---|---|
1 | 1 |
package org.gvsig.data.datasource.driver.file; |
2 | 2 |
|
3 |
import java.io.File; |
|
4 |
import java.net.URI; |
|
5 |
|
|
3 | 6 |
import org.gvsig.data.IDataSourceParameters; |
4 | 7 |
|
5 |
public class DataSourceFileDriverParameters implements IDataSourceParameters{ |
|
8 |
public abstract class DataSourceFileDriverParameters implements IDataSourceParameters{ |
|
9 |
protected File source; |
|
6 | 10 |
|
11 |
public void setSource(File source){ |
|
12 |
this.source = source; |
|
13 |
} |
|
7 | 14 |
|
8 |
public String getDataSourceName() { |
|
9 |
// TODO Auto-generated method stub |
|
10 |
return null; |
|
15 |
public File getSource(){ |
|
16 |
return this.source; |
|
11 | 17 |
} |
12 | 18 |
|
13 | 19 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datasource/driver/file/Register.java | ||
---|---|---|
6 | 6 |
public static void selfRegister() { |
7 | 7 |
DataSourceManager dsm = DataSourceManager.getManager(); |
8 | 8 |
|
9 |
dsm.registerDataSource(DataSourceFileDriver.DATASOURCE_NAME, |
|
10 |
DataSourceFileDriver.class, |
|
9 |
dsm.registerDataSource(AbstractDataSourceFileDriver.DATASOURCE_NAME,
|
|
10 |
AbstractDataSourceFileDriver.class,
|
|
11 | 11 |
DataSourceFileDriverParameters.class |
12 | 12 |
); |
13 | 13 |
} |
Also available in: Unified diff