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.services.schema;
025:
026: import java.io.File;
027: import java.util.HashMap;
028: import java.util.Map;
029: import jeeves.exceptions.BadInputEx;
030: import jeeves.exceptions.BadParameterEx;
031: import jeeves.exceptions.OperationAbortedEx;
032: import jeeves.interfaces.Service;
033: import jeeves.server.ServiceConfig;
034: import jeeves.server.context.ServiceContext;
035: import jeeves.utils.Util;
036: import jeeves.utils.XmlFileCacher;
037: import org.fao.geonet.GeonetContext;
038: import org.fao.geonet.constants.Geonet;
039: import org.fao.geonet.kernel.DataManager;
040: import org.jdom.Element;
041: import org.jdom.Namespace;
042:
043: //=============================================================================
044:
045: public class Info implements Service {
046: //--------------------------------------------------------------------------
047: //---
048: //--- Init
049: //---
050: //--------------------------------------------------------------------------
051:
052: public void init(String appPath, ServiceConfig params)
053: throws Exception {
054: this .appPath = appPath;
055: }
056:
057: //--------------------------------------------------------------------------
058: //---
059: //--- Service
060: //---
061: //--------------------------------------------------------------------------
062:
063: public Element exec(Element params, ServiceContext context)
064: throws Exception {
065: GeonetContext gc = (GeonetContext) context
066: .getHandlerContext(Geonet.CONTEXT_NAME);
067: DataManager dm = gc.getDataManager();
068:
069: String langCode = context.getLanguage();
070:
071: Element response = new Element("response");
072:
073: for (Object o : params.getChildren()) {
074: Element elem = (Element) o;
075: String name = elem.getName();
076:
077: if (name.equals("element"))
078: response.addContent(handleElement(dm, langCode, elem));
079:
080: else if (name.equals("codelist"))
081: response.addContent(handleCodelist(dm, langCode, elem));
082:
083: else
084: throw new BadParameterEx("element", name);
085: }
086:
087: return response;
088: }
089:
090: //--------------------------------------------------------------------------
091: //---
092: //--- Private methods
093: //---
094: //--------------------------------------------------------------------------
095:
096: private Element handleElement(DataManager dm, String langCode,
097: Element elem) throws Exception {
098: return handleObject(dm, langCode, elem, "labels.xml");
099: }
100:
101: //--------------------------------------------------------------------------
102:
103: private Element handleCodelist(DataManager dm, String langCode,
104: Element elem) throws Exception {
105: return handleObject(dm, langCode, elem, "codelists.xml");
106: }
107:
108: //--------------------------------------------------------------------------
109:
110: private Element handleObject(DataManager dm, String langCode,
111: Element elem, String fileName) throws BadInputEx,
112: OperationAbortedEx {
113: String schema = Util.getAttrib(elem, "schema");
114: String name = Util.getAttrib(elem, "name");
115:
116: name = normalizeNamespace(elem, name);
117:
118: if (name == null)
119: return buildError(elem, UNKNOWN_NAMESPACE);
120:
121: if (!dm.existsSchema(schema))
122: return buildError(elem, UNKNOWN_SCHEMA);
123:
124: File file = getFile(langCode, schema, fileName);
125:
126: if (file == null)
127: throw new OperationAbortedEx("File not found for : "
128: + schema + "/" + fileName);
129:
130: XmlFileCacher xfc = cache.get(file);
131:
132: if (xfc == null) {
133: xfc = new XmlFileCacher(file);
134: cache.put(file, xfc);
135: }
136:
137: try {
138: Element entries = xfc.get();
139:
140: for (Object o : entries.getChildren()) {
141: Element currElem = (Element) o;
142: String currName = currElem.getAttributeValue("name");
143:
144: currName = normalizeNamespace(entries, currName);
145:
146: if (currName == null)
147: throw new OperationAbortedEx(
148: "No namespace found for : " + currName);
149:
150: if (name.equals(currName))
151: return (Element) currElem.clone();
152: }
153:
154: return buildError(elem, NOT_FOUND);
155: } catch (Exception e) {
156: throw new OperationAbortedEx("Can't load xml file : "
157: + file, e);
158: }
159: }
160:
161: //--------------------------------------------------------------------------
162:
163: private String normalizeNamespace(Element elem, String name) {
164: int pos = name.indexOf(":");
165:
166: if (pos == -1)
167: return name;
168:
169: String prefix = name.substring(0, pos);
170:
171: Namespace ns = elem.getNamespace(prefix);
172:
173: if (ns == null)
174: return null;
175:
176: return ns.getURI() + name.substring(pos);
177: }
178:
179: //--------------------------------------------------------------------------
180:
181: private File getFile(String langCode, String schema, String fileName) {
182: File file = new File(appPath + "xml/schemas/" + schema
183: + "/loc/" + langCode + "/" + fileName);
184:
185: if (file.exists())
186: return file;
187:
188: //--- let's try the default language 'en'
189: file = new File(appPath + "xml/schemas/" + schema + "/loc/en/"
190: + fileName);
191:
192: if (file.exists())
193: return file;
194:
195: return null;
196: }
197:
198: //--------------------------------------------------------------------------
199:
200: private Element buildError(Element elem, String error) {
201: elem = (Element) elem.clone();
202: elem.setAttribute("error", error);
203:
204: return elem;
205: }
206:
207: //--------------------------------------------------------------------------
208: //---
209: //--- Variables
210: //---
211: //--------------------------------------------------------------------------
212:
213: private static final String UNKNOWN_SCHEMA = "unknown-schema";
214: private static final String UNKNOWN_NAMESPACE = "unknown-namespace";
215: private static final String NOT_FOUND = "not-found";
216:
217: //--------------------------------------------------------------------------
218:
219: private String appPath;
220:
221: private Map<File, XmlFileCacher> cache = new HashMap<File, XmlFileCacher>();
222: }
223:
224: //=============================================================================
|