comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 339:ef1cac2854e3

* src/java/de/intevation/mxd/writer/MapScriptWriter.java: Use Relative Paths in Mapfile
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 08 Nov 2012 12:46:16 +0100
parents b3048df06b66
children dbfcb0b69a63
comparison
equal deleted inserted replaced
338:b3048df06b66 339:ef1cac2854e3
73 private mapObj map; 73 private mapObj map;
74 private MapScriptUtils msutils; 74 private MapScriptUtils msutils;
75 private String mapFilename; 75 private String mapFilename;
76 private String MS_BINDIR = "c:/mapserver/bin"; 76 private String MS_BINDIR = "c:/mapserver/bin";
77 private String prefix = ""; 77 private String prefix = "";
78 private boolean useRelativePaths = true;
78 private MS_UNITS units = MS_UNITS.MS_METERS; 79 private MS_UNITS units = MS_UNITS.MS_METERS;
79 80
80 /** 81 /**
81 * Default constructor. 82 * Default constructor.
82 * Creates a mapscript writer object with an empty map. 83 * Creates a mapscript writer object with an empty map.
137 XPathConstants.NODE); 138 XPathConstants.NODE);
138 //Write the map attributes. 139 //Write the map attributes.
139 writeMap(); 140 writeMap();
140 //Write the layers. 141 //Write the layers.
141 boolean ret = writeLayer(); 142 boolean ret = writeLayer();
143
144 // Replace the filenames if necessary
145 if (useRelativePaths) {
146 try {
147 map.setFontSet(pathForMapfile(map.getFontset().getFilename()));
148 } catch (Exception e) {
149 // Exception is expected because our working dir is not
150 // necessarily the same as the one of the Mapfile
151 }
152 try {
153 map.setSymbolSet(pathForMapfile(map.getSymbolset().getFilename()));
154 } catch (Exception e) {
155 }
156
157 }
158
142 //Save the map. 159 //Save the map.
143 if (ret) { 160 if (ret) {
144 mapObj cloneMap = map.cloneMap(); 161 mapObj cloneMap = map.cloneMap();
145 cloneMap.save(mapFilename); 162 cloneMap.save(mapFilename);
146 logger.info("Mapfile created: " + mapFilename); 163 logger.info("Mapfile created: " + mapFilename);
421 datasource = layerElement.getAttribute("workspace"); 438 datasource = layerElement.getAttribute("workspace");
422 datasource += File.separator; 439 datasource += File.separator;
423 } 440 }
424 datasource += layerElement.getAttribute("data_source"); 441 datasource += layerElement.getAttribute("data_source");
425 datasource = datasource.replaceAll("\\\\", "/"); 442 datasource = datasource.replaceAll("\\\\", "/");
426 layer.setData(datasource); 443 layer.setData(pathForMapfile(datasource));
427 } 444 }
428 else if(con_type.equals("SDE") && !type.equals("raster")) { 445 else if(con_type.equals("SDE") && !type.equals("raster")) {
429 // The data source is a sde database. 446 // The data source is a sde database.
430 logger.info( 447 logger.info(
431 "SDE datasource found." + 448 "SDE datasource found." +
921 } 938 }
922 } 939 }
923 } 940 }
924 return expression; 941 return expression;
925 } 942 }
943
944 /**
945 * Get a Path that should be used in a mapfile.
946 * Depending on the value of useRelativePaths this returns either
947 * an absolute path to a file or a relative location
948 *
949 * @param path The Path that you want to use
950 * @return The Path that should be written to the mapfile
951 */
952 public String pathForMapfile(String path) {
953 if (useRelativePaths) {
954 logger.debug(new File(mapFilename).toURI());
955 logger.debug(new File(path).toURI().getPath());
956 // Find the lowest common directory.
957 // where relativize returns a relative part
958 File candidate = new File(path);
959 File commonPath = new File(mapFilename).getParentFile();
960 File pathfile = new File(path);
961 String dirUps = "";
962 int count = 0;
963 while (commonPath != null && commonPath.getParentFile() != null
964 && candidate.isAbsolute()) {
965 candidate = new File(commonPath.toURI().relativize(
966 pathfile.toURI()).getPath());
967 commonPath = commonPath.getParentFile();
968 count++;
969 }
970 for (int i=0; i < count - 1; i++) {
971 /* The last loop already was correct*/
972 dirUps += "../";
973 }
974 if (candidate.isAbsolute()) {
975 return path;
976 }
977 return dirUps + candidate.getPath();
978 } else {
979 return path;
980 }
981 }
926 } 982 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)