View Javadoc
1 /* 2 * WKB4J - WKB reader for geographical mapping toolkits 3 * (C) 2002,2003, David Garnier, dgarnier@users.sourceforge.net 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation, 8 * version 2.1 of the License. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * or visit the web to http://www.gnu.org. 19 * 20 */ 21 package org.wkb4j.factories; 22 23 import org.apache.log4j.Logger; 24 import org.wkb4j.engine.WKBFactory; 25 import org.wkb4j.engine.WKBGeometryTypes; 26 27 /*** 28 * Classes implementing WKBFactory should extend this class. In particular, 29 * this class validates the WKBFormat, making sure that things happen only when they should. 30 * Among other things, it checks that geometrical objects are correctly nested: Points can only exist in 31 * LineString and LinearRing objects, GeometryCollection can contain any type of objects... 32 * This is somehow similar to a "validating" XML parser. 33 * Creation date: 6 juil. 2002 22:07:15 34 * @author David Garnier 35 * @version $Revision: 1.9 $ $Date: 2003/08/02 23:52:18 $ 36 */ 37 public abstract class AbstractWKBFactory 38 implements WKBFactory, WKBGeometryTypes { 39 protected static Logger log = Logger.getLogger(AbstractWKBFactory.class); 40 41 protected final int geometryDepth = 10; 42 43 /*** Points to the top of the stack.*/ 44 protected int depthPointer = 0; 45 protected int[] typeStack = new int[geometryDepth]; 46 protected boolean inWork = false; 47 protected boolean inUnit = false; 48 49 protected String[] words = null; 50 protected int[] values = null; 51 52 public AbstractWKBFactory() { 53 init(); 54 } 55 56 protected void init() { 57 depthPointer = 0; 58 } 59 60 /*** 61 * @see org.wkb4j.engine.WKBFactory#beginWork() 62 */ 63 public void beginWork() { 64 if (inWork == false) { 65 inWork = true; 66 } else { 67 throw new IllegalStateException("Already working on a dataset"); 68 } 69 } 70 71 /*** 72 * @see org.wkb4j.engine.WKBFactory#endWork() 73 */ 74 public void endWork() { 75 if (inWork == true) { 76 inWork = false; 77 } else { 78 throw new IllegalStateException("Not currently working on a dataset"); 79 } 80 } 81 82 /*** 83 * @see org.wkb4j.engine.WKBFactory#abortWork() 84 */ 85 public void abortWork() { 86 if (inWork != true) { 87 log.error("Not currently working on a dataset"); 88 } 89 inWork = false; 90 } 91 92 /*** 93 * @see org.wkb4j.engine.WKBFactory#beginUnit() 94 */ 95 public void beginUnit(String[] words, int[] values) { 96 97 if (inUnit == false) { 98 inUnit = true; 99 } else { 100 throw new IllegalStateException("Already working on a unit"); 101 } 102 if (words.length == values.length) { 103 setWords(words); 104 setValues(values); 105 } 106 } 107 /*** 108 * @see org.wkb4j.engine.WKBFactory#beginUnit() 109 */ 110 public void beginUnit(int srid) { 111 112 if (inUnit == false) { 113 inUnit = true; 114 } else { 115 throw new IllegalStateException("Already working on a unit"); 116 } 117 words = new String[] { "srid" }; 118 values = new int[] { srid }; 119 } 120 121 /*** 122 * @see org.wkb4j.engine.WKBFactory#endUnit() 123 */ 124 public void endUnit() { 125 if (inUnit == true) { 126 inUnit = false; 127 } else { 128 throw new IllegalStateException("Not currently working on a unit"); 129 } 130 } 131 132 /*** 133 * @see org.wkb4j.engine.WKBFactory#abortUnit() 134 */ 135 public void abortUnit() { 136 if (inUnit != true) { 137 log.error("Not currently working on a unit"); 138 } 139 inUnit = false; 140 } 141 142 /*** 143 * @see org.wkb4j.engine.WKBFactory#beginGeometryCollection() 144 */ 145 public boolean beginGeometryCollection(int count) { 146 /* This check that we are actually allowed to create a GeometryCollection at this point: 147 * a GeometryCollection cannot be nested. 148 */ 149 if (depthPointer == 0) { 150 advancePointer(wkbGeometryCollection); 151 return true; 152 } else { 153 return false; 154 } 155 156 } 157 158 /*** 159 * @see org.wkb4j.engine.WKBFactory#endGeometryCollection() 160 */ 161 public boolean endGeometryCollection() { 162 return rewindPointer(wkbGeometryCollection); 163 } 164 165 /*** 166 * @see org.wkb4j.engine.WKBFactory#abortGeometryCollection() 167 */ 168 public void abortGeometryCollection() { 169 } 170 171 /*** 172 * @see org.wkb4j.engine.WKBFactory#abortGeometryCollection() 173 */ 174 public boolean newGeometryCollectionComponent() { 175 return typeStack[depthPointer]==wkbGeometryCollection; 176 } 177 178 /*** 179 * @see org.wkb4j.engine.WKBFactory#beginLineString() 180 */ 181 public boolean beginLineString(int count) { 182 if (depthPointer == 0 183 || typeStack[depthPointer] == wkbGeometryCollection 184 || typeStack[depthPointer] == wkbMultiLineString) { 185 advancePointer(wkbLineString); 186 return true; 187 } else { 188 return false; 189 } 190 } 191 192 /*** 193 * @see org.wkb4j.engine.WKBFactory#endLineString() 194 */ 195 public boolean endLineString() { 196 return rewindPointer(wkbLineString); 197 } 198 199 /*** 200 * @see org.wkb4j.engine.WKBFactory#abortLineString() 201 */ 202 public void abortLineString() { 203 } 204 205 /*** 206 * @see org.wkb4j.engine.WKBFactory#beginMultiLineString() 207 */ 208 public boolean beginMultiLineString(int count) { 209 if (depthPointer == 0 210 || typeStack[depthPointer] == wkbGeometryCollection) { 211 advancePointer(wkbMultiLineString); 212 return true; 213 } else { 214 return false; 215 } 216 } 217 218 /*** 219 * @see org.wkb4j.engine.WKBFactory#endMultiLineString() 220 */ 221 public boolean endMultiLineString() { 222 return rewindPointer(wkbMultiLineString); 223 } 224 225 /*** 226 * @see org.wkb4j.engine.WKBFactory#abortMultiLineString() 227 */ 228 public void abortMultiLineString() { 229 } 230 231 /*** 232 * @see org.wkb4j.engine.WKBFactory#beginMultiPolygon() 233 */ 234 public boolean beginMultiPolygon(int count) { 235 if (depthPointer == 0 236 || typeStack[depthPointer] == wkbGeometryCollection) { 237 advancePointer(wkbMultiPolygon); 238 return true; 239 } else { 240 return false; 241 } 242 } 243 244 /*** 245 * @see org.wkb4j.engine.WKBFactory#endMultiPolygon() 246 */ 247 public boolean endMultiPolygon() { 248 return rewindPointer(wkbMultiPolygon); 249 } 250 251 /*** 252 * @see org.wkb4j.engine.WKBFactory#abortMultiPolygon() 253 */ 254 public void abortMultiPolygon() { 255 } 256 257 /*** 258 * @see org.wkb4j.engine.WKBFactory#beginPolygon() 259 */ 260 public boolean beginPolygon(int count) { 261 if (depthPointer == 0 || typeStack[depthPointer] == wkbMultiPolygon || typeStack[depthPointer] == wkbGeometryCollection) { 262 advancePointer(wkbPolygon); 263 return true; 264 } else { 265 return false; 266 } 267 } 268 269 /*** 270 * @see org.wkb4j.engine.WKBFactory#endPolygon() 271 */ 272 public boolean endPolygon() { 273 return rewindPointer(wkbPolygon); 274 } 275 276 /*** 277 * @see org.wkb4j.engine.WKBFactory#abortPolygon() 278 */ 279 public void abortPolygon() { 280 } 281 282 /*** 283 * @see org.wkb4j.engine.WKBFactory#beginMultiPoint() 284 */ 285 public boolean beginMultiPoint(int count) { 286 if (depthPointer == 0 287 || typeStack[depthPointer] == wkbGeometryCollection) { 288 advancePointer(wkbMultiPoint); 289 return true; 290 } else { 291 return false; 292 } 293 } 294 295 /*** 296 * @see org.wkb4j.engine.WKBFactory#endMultiPoint() 297 */ 298 public boolean endMultiPoint() { 299 return rewindPointer(wkbMultiPoint); 300 } 301 302 /*** 303 * @see org.wkb4j.engine.WKBFactory#abortMultiPoint() 304 */ 305 public void abortMultiPoint() { 306 } 307 308 /*** 309 * @see org.wkb4j.engine.WKBFactory#beginPoint() 310 */ 311 public boolean beginPoint() { 312 if (depthPointer == 0 313 || typeStack[depthPointer] == wkbMultiPoint 314 || typeStack[depthPointer] == wkbGeometryCollection) { 315 advancePointer(wkbPoint); 316 return true; 317 } else { 318 return false; 319 } 320 } 321 322 /*** 323 * @see org.wkb4j.engine.WKBFactory#endPoint() 324 */ 325 public boolean endPoint() { 326 return rewindPointer(wkbPoint); 327 } 328 329 /*** 330 * @see org.wkb4j.engine.WKBFactory#abortPoint() 331 */ 332 public void abortPoint() { 333 } 334 /*** 335 * @see org.wkb4j.engine.WKBFactory#beginLineString() 336 */ 337 public boolean beginLinearRing(int count) { 338 /* LinearRing can't be a top-level geometry.*/ 339 if (typeStack[depthPointer] == wkbPolygon) { 340 advancePointer(linearRing); 341 return true; 342 } else { 343 return false; 344 } 345 } 346 347 /*** 348 * @see org.wkb4j.engine.WKBFactory#endLinearRing() 349 */ 350 public boolean endLinearRing() { 351 return rewindPointer(linearRing); 352 } 353 354 /*** 355 * @see org.wkb4j.engine.WKBFactory#abortLinearRing() 356 */ 357 public void abortLinearRing() { 358 } 359 360 /*** 361 * @see org.wkb4j.engine.WKBFactory#addPoints(double[]) 362 */ 363 public boolean addPoints(double[] points) { 364 return (points.length > 0) && (points.length % 2 == 0); 365 } 366 367 /*** 368 * @see org.wkb4j.engine.WKBFactory#addPoints(double[]) 369 */ 370 public boolean addPoints3D(double[] points) { 371 return (points.length > 0) && (points.length % 3 == 0); 372 } 373 374 private final void advancePointer(int type) { 375 depthPointer++; 376 typeStack[depthPointer] = type; 377 } 378 379 private final boolean rewindPointer(int type) { 380 boolean retval = (typeStack[depthPointer] == type); 381 if (depthPointer > 0) { 382 depthPointer--; 383 } else { 384 log.error("Something tried to decrement a null depthPointer."); 385 } 386 return retval; 387 } 388 /*** 389 * Returns the values. 390 * @return int[] 391 */ 392 public int[] getValues() { 393 return values; 394 } 395 396 /*** 397 * Returns the words. 398 * @return String[] 399 */ 400 public String[] getWords() { 401 return words; 402 } 403 404 /*** 405 * Sets the values. 406 * @param values The values to set 407 */ 408 protected void setValues(int[] values) { 409 this.values = values; 410 } 411 412 /*** 413 * Sets the words. 414 * @param words The words to set 415 */ 416 protected void setWords(String[] words) { 417 this.words = words; 418 } 419 420 abstract public void reset(); 421 422 }

This page was automatically generated by Maven