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;
025:
026: import java.util.*;
027:
028: import org.jdom.*;
029:
030: import org.wfp.vam.intermap.Constants;
031:
032: /**
033: * This class holds informations about map servers listed in the map servers
034: * configuration file.
035: */
036: public class DefaultMapServers {
037:
038: private static Element mapServers;
039: private static Element mapContexts;
040: private static Hashtable htContexts;
041: private static Element _defaultContext;
042:
043: /**
044: * Initializes map servers configuration
045: *
046: * @param config an Element
047: *
048: */
049: public static void init(Element config) {
050: // Get map servers from the configuration file
051: mapServers = getServersEl(config);
052: mapContexts = getContextsEl(config);
053: htContexts = getContextsHt(config);
054:
055: _defaultContext = getDefaultContext(config);
056: // mapContexts = elContexts;
057: }
058:
059: private static Element getServersEl(Element config) {
060: Element elServers = (Element) (config
061: .getChild(Constants.MAP_SERVERS).detach());
062: List<Element> servers = elServers.getChildren();
063:
064: // Set the id attribute for each server
065: int n = 1;
066: for (Element elServer : servers)
067: elServer.setAttribute(Constants.MAP_SERVER_ID, "" + n++);
068:
069: return elServers;
070: }
071:
072: private static Element getContextsEl(Element config) {
073: Element elContexts = (Element) config.getChild(
074: Constants.MAP_CONTEXTS).clone();
075: List<Element> contextList = elContexts
076: .getChildren(Constants.MAP_CONTEXT);
077:
078: // Set the id attribute for each server
079: int n = 1;
080: for (Element elContext : contextList)
081: elContext.setAttribute(Constants.MAP_CONTEXT_ID, "" + n++);
082:
083: // System.out.println(Xml.getString(elContexts));
084: return elContexts;
085: }
086:
087: /**
088: * @author ETj
089: */
090: private static Element getDefaultContext(Element config) {
091: Element elContexts = config.getChild(Constants.MAP_CONTEXTS);
092: Element defCon = (Element) elContexts.getChild(
093: Constants.MAP_DEFAULTCONTEXT).clone();
094:
095: return defCon;
096: }
097:
098: private static Hashtable getContextsHt(Element config) {
099: Element elContexts = (Element) config.getChild(
100: Constants.MAP_CONTEXTS).clone();
101: List<Element> lContexts = elContexts.getChildren();
102:
103: // Set the id attribute for each server
104: Hashtable htContexts = new Hashtable();
105:
106: int n = 1;
107: for (Element elContext : lContexts)
108: htContexts.put(new Integer(n++), elContext);
109:
110: return htContexts;
111: }
112:
113: /**
114: * Selects all default map servers
115: *
116: * @return an Element containing all default map servers
117: *
118: */
119: public static Element getServers() {
120: return (Element) mapServers.clone();
121: }
122:
123: /**
124: * Selects all default map contexts
125: *
126: * @return an Element containing all default contexts
127: *
128: */
129: public static Element getContexts() {
130: return (Element) mapContexts.clone();
131: }
132:
133: public static Element getContext(int id) {
134: Element context = (Element) htContexts.get(new Integer(id));
135: return (Element) context.clone();
136: }
137:
138: /**
139: * @author ETj
140: */
141: public static Element getDefaultContext() {
142: return (Element) _defaultContext.clone();
143: }
144:
145: /**
146: * Selects all default map servers having the selected type attribute
147: *
148: * @param type map server type
149: *
150: * @return an Element containing the selected map servers
151: *
152: */
153: public static Element getServers(String type) {
154: List servers = mapServers.getChildren();
155: Element elServers = new Element(Constants.MAP_SERVERS);
156:
157: // Select map servers having the selected type attribute
158: for (Iterator i = servers.iterator(); i.hasNext();) {
159: Element elServer = (Element) i.next();
160: if (elServer.getAttributeValue(Constants.MAP_SERVER_TYPE) == type)
161: elServers.addContent(elServer);
162: }
163:
164: return elServers;
165: }
166:
167: /**
168: * Returns the type of the selected default map server
169: *
170: * @param id a String
171: *
172: * @return the type of the mapserver identified by id, 0 if no server
173: * found
174: *
175: * @throws Exception
176: *
177: */
178: public static int getType(String id) throws Exception {
179: List servers = mapServers.getChildren();
180:
181: // Look for the mapserver identified by id and return its type
182: for (Iterator i = servers.iterator(); i.hasNext();) {
183: Element server = (Element) i.next();
184: if (server.getAttributeValue(Constants.MAP_SERVER_ID)
185: .equals(id))
186: return Integer.parseInt(server
187: .getAttributeValue(Constants.MAP_SERVER_TYPE));
188: }
189: // Return 0 if no server found
190: return 0;
191: }
192:
193: /**
194: * Returns the url of the selected default mapserver
195: *
196: * @param id a String
197: *
198: * @return the url of the mapserver identified by id, null if no server
199: * found
200: *
201: * @throws Exception
202: *
203: */
204: public static String getUrl(String id) throws Exception {
205: List servers = mapServers.getChildren();
206:
207: // Look for the mapserver identified by id and return its url
208: for (Iterator i = servers.iterator(); i.hasNext();) {
209: Element server = (Element) i.next();
210: if (server.getAttributeValue(Constants.MAP_SERVER_ID)
211: .equals(id))
212: return server.getChildText(Constants.MAP_SERVER_URL);
213: }
214: // Return null if no server found
215: return null;
216: }
217:
218: }
|