001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.impl;
025:
026: import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.type.*;
027:
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031: import org.jdom.Element;
032:
033: /**
034: * @author ETj
035: */
036: public class WMSLayerImpl implements WMSLayer {
037: // attributes
038: private boolean _queryable;
039: // private int cascaded; // TODO
040: // private boolean opaque = false;
041: // private boolean noSubsets = false;
042: // private int fixedWidth;
043: // private int fixedheight;
044:
045: // children
046: private String _name = null; // 0..1
047: private String _title = null; // 1..1
048: private String _abstract = null;
049: // TODO keywordlist
050: private List<String> _crsList = new ArrayList<String>(); // CRS 0..n
051: private WMSEX_GeographicBoundingBox _geoBB = null; // EX_GeographicBoundingBox 0..1
052: private List<WMSBoundingBox> _bbList = new ArrayList<WMSBoundingBox>(); // BoundingBox 0..n
053: // WMS 11x defined also Extent element "Extent element indicates what _values_ along a dimension are valid.". This info is now held in dimension
054: private List<WMSDimension> _dimensionList = new ArrayList<WMSDimension>(); // 0..n
055: // TODO attribution authorityurl identifier
056: private List<WMSMetadataURL> _metadataURLList = new ArrayList<WMSMetadataURL>(); // MetadataURL 0..n
057: // TODO dataurl featurelisturl
058: private List<WMSStyle> _stylesList = new ArrayList<WMSStyle>(); // Style 0..n
059: // TODO minscaleden maxscaleden
060: private List<WMSLayer> _layerList = new ArrayList<WMSLayer>(); // Layer 0..n
061:
062: private WMSLayerImpl __parent = null; // not in spec, but we can use it to find inherited info
063:
064: private WMSLayerImpl() {
065: }
066:
067: static public WMSLayer newInstance() {
068: return new WMSLayerImpl();
069: }
070:
071: /**
072: * Method parse
073: */
074: public static WMSLayerImpl parse(Element eLayer) {
075: if (eLayer == null)
076: return null;
077:
078: WMSLayerImpl layer = new WMSLayerImpl();
079:
080: layer.setQueryable(Utils.getBooleanAttrib(eLayer
081: .getAttributeValue("queryable"), false));
082:
083: layer.setName(eLayer.getChildText("Name"));
084: layer.setTitle(eLayer.getChildText("Title"));
085: layer.setAbstract(eLayer.getChildText("Abstract"));
086: layer.setCRS((List<Element>) eLayer.getChildren("CRS"));
087: layer.setGeoBB(parseGeoBB(eLayer));
088: layer.setBoundingBox((List<Element>) eLayer
089: .getChildren("BoundingBox"));
090:
091: List<Element> extentList = (List<Element>) eLayer
092: .getChildren("Extent");
093: for (Element edim : (List<Element>) eLayer
094: .getChildren("Dimension")) {
095: WMSDimension dim = WMSFactory.parseDimension(edim);
096: dim.setExtent(extentList);
097: layer.addDimension(dim);
098: }
099: for (Element emdu : (List<Element>) eLayer
100: .getChildren("MetadataURL"))
101: layer.addMetadataURL(WMSFactory.parseMetadataURL(emdu));
102: layer.setStyles((List<Element>) eLayer.getChildren("Style"));
103: layer.setLayers((List<Element>) eLayer.getChildren("Layer"),
104: layer);
105:
106: return layer;
107: }
108:
109: /**
110: * Geographic BBox definition changed from 1.1.1 to 1.3.0.
111: * We have to parse either.
112: */
113: private static WMSEX_GeographicBoundingBox parseGeoBB(Element eLayer) {
114: // 1.3.0 element is equivalent to 19115 gbb
115: Element egbb = eLayer.getChild("EX_GeographicBoundingBox");
116: if (egbb != null) {
117: return WMSFactory.parseGeoBBox(egbb);
118: }
119:
120: // 1.1.1
121: egbb = eLayer.getChild("LatLonBoundingBox");
122: if (egbb != null) {
123: return WMSFactory.parseGeoBBox111(egbb);
124: }
125:
126: return null;
127: }
128:
129: public boolean isQueryable() {
130: return _queryable;
131: }
132:
133: /**
134: * Sets Queryable
135: */
136: public void setQueryable(boolean queryable) {
137: _queryable = queryable;
138: }
139:
140: /**
141: * Sets Name
142: */
143: public void setName(String name) {
144: _name = name;
145: }
146:
147: /**
148: * Returns Name
149: */
150: public String getName() {
151: return _name;
152: }
153:
154: /**
155: * Sets Title
156: */
157: public void setTitle(String title) {
158: _title = title;
159: }
160:
161: /**
162: * Returns Title
163: */
164: public String getTitle() {
165: return _title;
166: }
167:
168: /**
169: * Sets Abstract
170: */
171: public void setAbstract(String abs) {
172: _abstract = abs;
173: }
174:
175: /**
176: * Returns Abstract
177: */
178: public String getAbstract() {
179: return _abstract;
180: }
181:
182: private void setCRS(List<Element> crsList) {
183: for (Element ecrs : crsList) {
184: String crsvalue = ecrs.getText();
185: // in pre 111 version, multile crs may be enclosed in one element, space separated
186: for (String s : crsvalue.split(" ")) {
187: addCRS(s);
188: }
189: }
190: }
191:
192: public void addCRS(String crs) {
193: _crsList.add(crs);
194: }
195:
196: public Iterable<String> getCRSIterator() {
197: return new Iterable<String>() {
198: public Iterator<String> iterator() {
199: return _crsList.iterator();
200: }
201: };
202: }
203:
204: /**
205: * Method setGeoBB
206: */
207: public void setGeoBB(WMSEX_GeographicBoundingBox geoBB) {
208: _geoBB = geoBB;
209: }
210:
211: public WMSEX_GeographicBoundingBox getGeoBB() {
212: if (_geoBB != null)
213: return _geoBB;
214: else if (__parent != null)
215: return __parent.getGeoBB();
216: else {
217: System.out
218: .println("WMSLayerImpl: ERROR: no geoBB defined for layer and its ancestors.");
219: return null; // be nice and comprehensive and don't fire any Exception
220: }
221: }
222:
223: private void setBoundingBox(List<Element> bblist) {
224: for (Element ebb : bblist) {
225: WMSBoundingBox obb = WMSFactory.parseBoundingBox(ebb);
226: addBoundingBox(obb);
227: }
228: }
229:
230: /**
231: * Method addBoundingBox
232: */
233: public void addBoundingBox(WMSBoundingBox boundingBox) {
234: _bbList.add(boundingBox);
235: }
236:
237: public Iterable<WMSBoundingBox> getBoundingBoxIterator() {
238: return new Iterable<WMSBoundingBox>() {
239: public Iterator<WMSBoundingBox> iterator() {
240: return _bbList.iterator();
241: }
242: };
243: }
244:
245: public void addDimension(WMSDimension dimension) {
246: _dimensionList.add(dimension);
247: }
248:
249: public Iterable<WMSDimension> getDimensionIterator() {
250: return new Iterable<WMSDimension>() {
251: public Iterator<WMSDimension> iterator() {
252: return _dimensionList.iterator();
253: }
254: };
255: }
256:
257: public WMSDimension getDimension(String name) {
258: for (WMSDimension dim : _dimensionList)
259: if (dim.getName().equals(name))
260: return dim;
261:
262: return null;
263: }
264:
265: /**
266: * Method addMetadataURL
267: */
268: public void addMetadataURL(WMSMetadataURL metadataURL) {
269: _metadataURLList.add(metadataURL);
270: }
271:
272: public Iterable<WMSMetadataURL> getMetadataURLIterator() {
273: return new Iterable<WMSMetadataURL>() {
274: public Iterator<WMSMetadataURL> iterator() {
275: return _metadataURLList.iterator();
276: }
277: };
278: }
279:
280: /**
281: * Method setStyles
282: */
283: private void setStyles(List<Element> styleList) {
284: for (Element eStyle : styleList) {
285: addStyle(eStyle);
286: }
287: }
288:
289: /**
290: * Method addStyle
291: */
292: private void addStyle(Element eStyle) {
293: WMSStyle style = WMSFactory.parseStyle(eStyle);
294: addStyle(style);
295: }
296:
297: public void addStyle(WMSStyle style) {
298: _stylesList.add(style);
299: }
300:
301: public Iterable<WMSStyle> getStyleIterator() {
302: return new Iterable<WMSStyle>() {
303: public Iterator<WMSStyle> iterator() {
304: return _stylesList.iterator();
305: }
306: };
307: }
308:
309: public int getStyleSize() {
310: return _stylesList.size();
311: }
312:
313: /**
314: * Method getStyle
315: *
316: * @return the WMSStyle with given name, or null if none was found
317: */
318: public WMSStyle getStyle(String name) {
319: for (WMSStyle style : _stylesList) {
320: if (style.getName().equals(name))
321: return style;
322: }
323: return null;
324: }
325:
326: public WMSStyle getStyle(int index) {
327: if (-1 < index && index < _stylesList.size())
328: return _stylesList.get(index);
329: else
330: return null;
331: }
332:
333: /**
334: * Method setLayers
335: */
336: private void setLayers(List<Element> layerList, WMSLayerImpl parent) {
337: for (Element eLayer : layerList) {
338: WMSLayerImpl layer = parse(eLayer);
339: layer.setParent(parent);
340: addLayer(layer);
341: }
342: }
343:
344: /**
345: * Method addLayer
346: */
347: public void addLayer(WMSLayer layer) {
348: _layerList.add(layer);
349: }
350:
351: public WMSLayer getLayer(String name) {
352: if (name.equals(getName()))
353: return this ;
354:
355: for (WMSLayer layer : _layerList) {
356: WMSLayer ret = layer.getLayer(name);
357: if (ret != null)
358: return ret;
359: }
360:
361: return null; // Layer not found
362: }
363:
364: public Iterable<WMSLayer> getLayerIterator() {
365: return new Iterable<WMSLayer>() {
366: public Iterator<WMSLayer> iterator() {
367: return _layerList.iterator();
368: }
369: };
370: }
371:
372: private void setParent(WMSLayerImpl parent) {
373: __parent = parent;
374: }
375:
376: public WMSLayer getParent() {
377: return __parent;
378: }
379:
380: }
|