01: /*
02: * mesopotamia @mesopotamia.version@
03: * Multilingual parser and repository.
04: * Copyright (C) 2005 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://http://www.hammurapi.biz
21: * e-Mail: support@hammurapi.biz
22: */
23: package org.mesopotamia;
24:
25: import java.util.HashMap;
26: import java.util.Iterator;
27: import java.util.Map;
28:
29: import javax.xml.transform.TransformerException;
30:
31: import org.apache.xpath.XPathAPI;
32: import org.w3c.dom.Element;
33: import org.w3c.dom.Node;
34: import org.w3c.dom.traversal.NodeIterator;
35:
36: import biz.hammurapi.config.ConfigurationException;
37: import biz.hammurapi.config.Context;
38: import biz.hammurapi.config.DomConfigurable;
39:
40: public class ExtensionLanguageSelector implements LanguageSelector,
41: DomConfigurable {
42: private Map extensionMap = new HashMap();
43:
44: public ExtensionLanguageSelector() {
45: super ();
46: }
47:
48: public Language select(Source source) {
49: Iterator it = extensionMap.entrySet().iterator();
50: while (it.hasNext()) {
51: Map.Entry entry = (Map.Entry) it.next();
52: if (source.getName().endsWith((String) entry.getKey())) {
53: return (Language) entry.getValue();
54: }
55: }
56: return null;
57: }
58:
59: public void configure(Node configNode, Context context)
60: throws ConfigurationException {
61: try {
62: NodeIterator nit = XPathAPI.selectNodeIterator(configNode,
63: "mapping");
64: Element e;
65: while ((e = (Element) nit.nextNode()) != null) {
66: if (!e.hasAttribute("extension")) {
67: throw new ConfigurationException(
68: "'extension' attribute is mandatory in 'mapping' element");
69: }
70: if (!e.hasAttribute("language")) {
71: throw new ConfigurationException(
72: "'language' attribute is mandatory in 'mapping' element");
73: }
74: if (!e.hasAttribute("version")) {
75: throw new ConfigurationException(
76: "'version' attribute is mandatory in 'mapping' element");
77: }
78:
79: extensionMap.put(e.getAttribute("extension"),
80: new Language(e.getAttribute("language"), e
81: .getAttribute("version"), null));
82: }
83: } catch (TransformerException e) {
84: throw new ConfigurationException("Cannot configure " + this
85: + ": " + e, e);
86: }
87: }
88:
89: }
|