8

i am just testing some things for the opensource software Geonetwork and now I want to export a shape file from my postGIS database to my computer but I cant test it cause I always get a findbugs warning : Hard coded reference to an absolute pathname in org.fao.geonet.services.resources.Download.exec(Element, ServiceContext)

Does anyone know how to suppress or to avoid this warning? Or another solution how I can test if the export works?

Here is the Code:

  ` Map parameter1= new HashMap();
    parameter1.put("dbtype", "postgis");        
    parameter1.put("host", "localhost");        
    parameter1.put("port", "5433");  
    parameter1.put("database", "geonetwork");
    parameter1.put("schema", "mydata");
    parameter1.put("user", "postgres");        
    parameter1.put("passwd", "dominik1");
    parameter1.put("Expose primary keys", false);
    parameter1.put(PostgisNGDataStoreFactory.VALIDATECONN, true);
    parameter1.put(PostgisNGDataStoreFactory.MAX_OPEN_PREPARED_STATEMENTS, 100);
    parameter1.put(PostgisNGDataStoreFactory.LOOSEBBOX, true);
    parameter1.put(PostgisNGDataStoreFactory.PREPARED_STATEMENTS, true); 
    System.out.println("parameter1: " + parameter1);

    DataStore pds = new PostgisNGDataStoreFactory().createDataStore(parameter1);
    FeatureSource fs = pds.getFeatureSource("dano");

    SimpleFeatureCollection fc = (SimpleFeatureCollection) fs.getFeatures();
    SimpleFeature f = (SimpleFeature) fc.toArray()[0];

    // create shapefile

    File sfile = new File("C:/Users/Tinis/Downloads/dano.zip");            
    parameter1 = new HashMap();
    parameter1.put("url", sfile.toURI().toURL());
    parameter1.put("create spatial index", Boolean.FALSE);


    DirectoryDataStore dds = new DirectoryDataStore(new File(sfile.getAbsolutePath()), (FileStoreFactory) new ShapefileDataStoreFactory.ShpFileStoreFactory(new ShapefileDataStoreFactory(), parameter1));    

    dds.createSchema(fc.getSchema());
    String typeName = dds.getTypeNames()[0];
    SimpleFeatureSource featureSource = dds.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

    // write data to shapefile
    Transaction t = new DefaultTransaction("addTransaction");
    featureStore.setTransaction(t);
    featureStore.addFeatures(fc);
    t.commit();
    t.close(); `

The line which gives the findbugs warning is just after the // create shapefile comment

I hope someone can help me with this problem. Thank you!

Patrick Giersch
  • 83
  • 1
  • 1
  • 3

2 Answers2

8

Make a xxx.properties file (ResourceBundle) with an entry:

danoZip = C:/Users/Tinis/Downloads/dano.zip

And do it indirect:

ResourceBundle bundle = ResourceBundle.getBundle("xxx");

String danoZipPath = bundle.getString("danoZip");

File sfile = new File(danoZipPath);            

In this way you put the configuration of a hard-coded file in a configurable properties file.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thank you for answering! Do you mean bundle.getString instead of bundle.get? – Patrick Giersch Aug 20 '14 at 14:34
  • @PatrickGiersch of course, I will correct. – Joop Eggen Aug 20 '14 at 14:36
  • I tried this now but something has gone wrong!? Exc : java.util.MissingResourceException: Can't find bundle for base name shpprop, locale de_DE ------- My Code: ResourceBundle bundle = ResourceBundle.getBundle("shpprop"); String danoShpPath = bundle.getString("danoShp"); File sfile = new File(danoShpPath); -------- with shpprop.properties which is in the same package like my Java File: ------ danoShp = C:/Users/Tinis/Downloads/dano.shp What did I wrong? – Patrick Giersch Aug 20 '14 at 16:00
  • The path in getBundle is absolute, needs the package name as path. And a file `"shpprop.properties"`. Case-sensitive too. `getBundle("org.sioux.myapp.shprop")` though `/` is often used for .properties. – Joop Eggen Aug 20 '14 at 16:06
  • 1
    Thank you, working now. Can't give you vote up cause 15 reputation is needed. Anyway thank you very much! :-) – Patrick Giersch Aug 21 '14 at 10:32
  • Just a typo: It must be `ResourceBundle.getBundle("xxx");` – aProgger Oct 16 '16 at 09:16
  • @aProgger thanks, corrected – Joop Eggen Oct 16 '16 at 19:25
1

You can use a filter file to describe bugs to ignore.

http://findbugs.sourceforge.net/manual/filter.html

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
  • I have done it like this in the findbugs_excludes.xml: Then I run the package in Eclipse as "Maven Install" While installing I get the same error and after it the change which I made in findbugs_excludes.xml is undone. Strange, why did this happen? – Patrick Giersch Aug 20 '14 at 14:33