comparison src/java/de/intevation/mxd/reader/FeatureLayerReader.java @ 117:6c3d880db7c5

Improved exception handling for layer reader.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 16 Jun 2011 12:05:34 +0200
parents 0db6eacad0e6
children 39957898c694
comparison
equal deleted inserted replaced
116:c8a1361ddd38 117:6c3d880db7c5
8 import com.esri.arcgis.system.IName; 8 import com.esri.arcgis.system.IName;
9 import com.esri.arcgis.system.IPropertySet; 9 import com.esri.arcgis.system.IPropertySet;
10 import org.w3c.dom.Element; 10 import org.w3c.dom.Element;
11 11
12 import de.intevation.mxd.utils.MapToXMLUtils; 12 import de.intevation.mxd.utils.MapToXMLUtils;
13 import java.io.IOException;
13 14
14 /** 15 /**
15 * Reads Layer information. 16 * Reads Layer information.
16 * 17 *
17 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> 18 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
55 /** 56 /**
56 * Reads the Layer content. 57 * Reads the Layer content.
57 * 58 *
58 * @return The layer XML element. 59 * @return The layer XML element.
59 */ 60 */
60 public Element read() 61 public Element read() {
61 throws Exception {
62 logger.debug("read()"); 62 logger.debug("read()");
63 Element layerElement = util.addLayer(); 63 Element layerElement;
64 64 try {
65 layerElement.setAttribute("name", layer.getName()); 65 layerElement = util.addLayer();
66 layerElement.setAttribute("min_scale", 66 }
67 String.valueOf(layer.getMinimumScale())); 67 catch(Exception e) {
68 layerElement.setAttribute("max_scale", 68 logger.error("Failed to create DOM-Element for Layer.");
69 String.valueOf(layer.getMaximumScale())); 69 return null;
70 if(layer.isVisible()) { 70 }
71
72 try {
73 layerElement.setAttribute("name", layer.getName());
74 }
75 catch(IOException ioe) {
76 logger.warn(
77 "Could not read name." +
78 " Setting name to \"default-layer\"");
79 layerElement.setAttribute("name", "default-layer");
80 }
81
82 try {
83 layerElement.setAttribute("min_scale",
84 String.valueOf(layer.getMinimumScale()));
85 }
86 catch(IOException ioe) {
87 logger.warn(
88 "Could not read minimum scale." +
89 " Setting minimum scale to 0.");
90 layerElement.setAttribute("min_scale", "0");
91 }
92
93 try {
94 layerElement.setAttribute("max_scale",
95 String.valueOf(layer.getMaximumScale()));
96 }
97 catch(IOException ioe) {
98 logger.warn(
99 "Could not read maximum scale." +
100 " Setting maximum scale to 0.");
101 layerElement.setAttribute("max_scale", "0");
102 }
103
104 try {
105 if(layer.isVisible()) {
106 layerElement.setAttribute("status", "on");
107 }
108 else {
109 layerElement.setAttribute("status", "off");
110 }
111 }
112 catch(IOException ioe) {
113 logger.warn(
114 "Could not read layer status." +
115 " Setting layer status to \"on\".");
71 layerElement.setAttribute("status", "on"); 116 layerElement.setAttribute("status", "on");
72 } 117 }
73 else { 118
74 layerElement.setAttribute("status", "off"); 119 int type = 0;
75 } 120 try {
76 121 type = layer.getShapeType();
77 int type = layer.getShapeType(); 122 }
123 catch(IOException ioe) {
124 logger.warn(
125 "Could not read shape type." +
126 " Setting shape type to \"none\".");
127 type = 0;
128 }
78 switch (type) { 129 switch (type) {
79 case 0: layerElement.setAttribute("type", "none"); break; 130 case 0: layerElement.setAttribute("type", "none"); break;
80 case 1: layerElement.setAttribute("type", "point"); break; 131 case 1: layerElement.setAttribute("type", "point"); break;
81 case 3: layerElement.setAttribute("type", "line"); break; 132 case 3: layerElement.setAttribute("type", "line"); break;
82 case 4: layerElement.setAttribute("type", "polygon"); break; 133 case 4: layerElement.setAttribute("type", "polygon"); break;
83 } 134 }
84 135
85 layerElement.setAttribute("definition_query", 136 try {
86 layer.getDefinitionExpression()); 137 layerElement.setAttribute("definition_query",
87 IName fcn = layer.getDataSourceName(); 138 layer.getDefinitionExpression());
88 if(fcn instanceof FeatureClassName) { 139 }
89 FeatureClassName name = (FeatureClassName)fcn; 140 catch(IOException ioe) {
90 layerElement.setAttribute("data_source", name.getName()); 141 logger.warn(
91 } 142 "Could not read definition query." +
92 else { 143 " Setting empty definition query.");
93 logger.debug ("Unknown FeatureClass name:" + fcn.getClass().toString()); 144 layerElement.setAttribute("definition_query", "");
94 } 145 }
95 146 try {
96 147 IName fcn = layer.getDataSourceName();
97 String datatype = layer.getDataSourceType(); 148 if(fcn instanceof FeatureClassName) {
98 if(datatype.equals("Shapefile Feature Class")) { 149 FeatureClassName name = (FeatureClassName)fcn;
99 layerElement.setAttribute("connection_type", "local"); 150 layerElement.setAttribute("data_source", name.getName());
100 layerElement.setAttribute( 151 }
101 "workspace", 152 else {
102 layer.getWorkspace().getPathName()); 153 logger.debug (
103 } 154 "Unknown FeatureClass name:" +
104 else if(datatype.equals("File Geodatabase Feature Class")){ 155 fcn.getClass().toString());
105 layerElement.setAttribute("connection_type", "ogr"); 156 }
106 layerElement.setAttribute("data", layer.getFeatureClass().getFeatureDataset().getName()); 157 }
107 layerElement.setAttribute( 158 catch(IOException ioe) {
108 "workspace", 159 logger.warn("Could not read datasource. Stopped reading layer.");
109 layer.getWorkspace().getPathName()); 160 return null;
110 161 }
111 } 162
112 else if(datatype.equals("SDE Feature Class")) { 163 try {
113 IPropertySet set = layer.getWorkspace().getConnectionProperties(); 164 String datatype = layer.getDataSourceType();
114 Object names[] = new Object[set.getCount()]; 165 if(datatype.equals("Shapefile Feature Class")) {
115 Object prop[] = new Object[set.getCount()]; 166 layerElement.setAttribute("connection_type", "local");
116 set.getAllProperties(names, prop); 167 layerElement.setAttribute(
117 layerElement.setAttribute("connection_type", "SDE"); 168 "workspace",
118 for(int i = 0; i < names.length; i++) { 169 layer.getWorkspace().getPathName());
119 if(names[i] != null) { 170 }
120 String[] prop_names = (String[])names[i]; 171 else if(datatype.equals("File Geodatabase Feature Class")){
121 for(int j = 0; j < prop_names.length; j++) { 172 layerElement.setAttribute("connection_type", "ogr");
122 layerElement.setAttribute( 173 layerElement.setAttribute("data", layer.getFeatureClass().getFeatureDataset().getName());
123 prop_names[j].toLowerCase(), 174 layerElement.setAttribute(
124 set.getProperty(prop_names[j]).toString()); 175 "workspace",
176 layer.getWorkspace().getPathName());
177
178 }
179 else if(datatype.equals("SDE Feature Class")) {
180 IPropertySet set = layer.getWorkspace().getConnectionProperties();
181 Object names[] = new Object[set.getCount()];
182 Object prop[] = new Object[set.getCount()];
183 set.getAllProperties(names, prop);
184 layerElement.setAttribute("connection_type", "SDE");
185 for(int i = 0; i < names.length; i++) {
186 if(names[i] != null) {
187 String[] prop_names = (String[])names[i];
188 for(int j = 0; j < prop_names.length; j++) {
189 layerElement.setAttribute(
190 prop_names[j].toLowerCase(),
191 set.getProperty(prop_names[j]).toString());
192 }
125 } 193 }
126 } 194 }
127 } 195 }
196 }
197 catch(Exception e) {
198 logger.error(
199 "Could not read layer datasource." +
200 " Stopped reading layer.");
201 return null;
128 } 202 }
129 return layerElement; 203 return layerElement;
130 } 204 }
131 } 205 }
132 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 206 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)