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.fao.geonet.kernel.csw.services;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.Set;
029: import java.util.StringTokenizer;
030: import org.fao.geonet.csw.common.Csw;
031: import org.fao.geonet.csw.common.Csw.ElementSetName;
032: import org.fao.geonet.csw.common.exceptions.CatalogException;
033: import org.fao.geonet.csw.common.exceptions.InvalidParameterValueEx;
034: import org.jdom.Element;
035: import org.jdom.Namespace;
036:
037: //=============================================================================
038:
039: public abstract class AbstractOperation {
040: //---------------------------------------------------------------------------
041: //---
042: //--- API methods
043: //---
044: //---------------------------------------------------------------------------
045:
046: protected boolean checkService(Element request)
047: throws CatalogException {
048: String service = request.getAttributeValue("service");
049:
050: if (service == null)
051: return false;
052:
053: if (service.equals(Csw.SERVICE))
054: return true;
055:
056: //--- this is just a fix to the incorrect XSD schema default
057:
058: if (service.equals("http://www.opengis.net/cat/csw"))
059: return true;
060:
061: throw new InvalidParameterValueEx("service", service);
062: }
063:
064: //---------------------------------------------------------------------------
065:
066: protected void checkVersion(Element request)
067: throws CatalogException {
068: String version = request.getAttributeValue("version");
069:
070: if (version == null)
071: return;
072:
073: if (!version.equals(Csw.CSW_VERSION))
074: throw new InvalidParameterValueEx("version", version);
075: }
076:
077: //---------------------------------------------------------------------------
078:
079: protected void setAttrib(Element elem, String name, String value) {
080: if (value != null)
081: elem.setAttribute(name, value);
082: }
083:
084: //---------------------------------------------------------------------------
085:
086: protected void addElement(Element parent, String name, String value) {
087: if (value != null) {
088: Element elem = new Element(name, parent.getNamespace());
089: elem.setText(value);
090: parent.addContent(elem);
091: }
092: }
093:
094: //---------------------------------------------------------------------------
095:
096: protected void fill(Element root, String parentName,
097: String childName, String list, Namespace ns) {
098: if (list == null)
099: return;
100:
101: StringTokenizer st = new StringTokenizer(list, ",");
102:
103: Element parent = new Element(parentName, ns);
104: root.addContent(parent);
105:
106: while (st.hasMoreTokens()) {
107: Element child = new Element(childName, ns);
108: child.setText(st.nextToken());
109: parent.addContent(child);
110: }
111: }
112:
113: //---------------------------------------------------------------------------
114:
115: protected void fill(Element root, String childName, String list) {
116: if (list == null)
117: return;
118:
119: StringTokenizer st = new StringTokenizer(list, ",");
120:
121: while (st.hasMoreTokens()) {
122: Element child = new Element(childName, root.getNamespace());
123: child.setText(st.nextToken());
124: root.addContent(child);
125: }
126: }
127:
128: //---------------------------------------------------------------------------
129:
130: protected ElementSetName getElementSetName(Element parent,
131: ElementSetName defValue) throws InvalidParameterValueEx {
132: if (parent == null)
133: return defValue;
134:
135: return ElementSetName.parse(parent.getChildText(
136: "ElementSetName", parent.getNamespace()));
137: }
138:
139: //---------------------------------------------------------------------------
140:
141: protected Map<String, String> retrieveNamespaces(String namespaces) {
142: HashMap<String, String> hm = new HashMap<String, String>();
143:
144: if (namespaces != null) {
145: StringTokenizer st = new StringTokenizer(namespaces, ",");
146:
147: while (st.hasMoreTokens()) {
148: String ns = st.nextToken();
149: int pos = ns.indexOf(":");
150:
151: if (pos == -1)
152: hm.put("", ns);
153: else {
154: String prefix = ns.substring(0, pos);
155: String uri = ns.substring(pos + 1);
156:
157: hm.put(prefix, uri);
158: }
159: }
160: }
161:
162: return hm;
163: }
164:
165: //---------------------------------------------------------------------------
166: /** @return For earch typeName returns the associated namespace */
167:
168: protected Map<String, String> retrieveTypeNames(String typeNames,
169: String namespace) throws InvalidParameterValueEx {
170: Map<String, String> hmTypeNames = new HashMap<String, String>();
171: Map<String, String> hmNamespaces = retrieveNamespaces(namespace);
172:
173: if (typeNames != null) {
174: StringTokenizer st = new StringTokenizer(typeNames, ",");
175:
176: while (st.hasMoreTokens()) {
177: String typeName = st.nextToken();
178: int pos = typeName.indexOf(":");
179: String prefix = "";
180: String type = typeName;
181:
182: if (pos != -1) {
183: prefix = typeName.substring(0, pos);
184: type = typeName.substring(pos + 1);
185: }
186:
187: String ns = hmNamespaces.get(prefix);
188:
189: if (ns == null)
190: throw new InvalidParameterValueEx("typeName",
191: typeName);
192:
193: hmTypeNames.put(type, ns);
194: }
195: }
196:
197: return hmTypeNames;
198: }
199: }
200:
201: //=============================================================================
|