001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.eval;
024:
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.io.StringWriter;
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.TreeSet;
034:
035: import biz.hammurapi.web.HammurapiWebException;
036:
037: /**
038: * Simply evaluator factory which shall be configured by explicitly registering
039: * evaluators in the code.
040: * @author Pavel
041: */
042: public class SimpleEvaluatorFactory implements EvaluatorFactory {
043:
044: private Map extensionMap = new HashMap();
045: private Map languageMap = new HashMap();
046:
047: public EvaluationResult evaluate(String language, String source,
048: String sourceName, Map context, ClassLoader classLoader)
049: throws HammurapiWebException {
050:
051: Evaluator evaluator = (Evaluator) languageMap.get(language);
052:
053: if (evaluator == null) {
054: throw new HammurapiWebException("Not supported language: "
055: + language);
056: }
057:
058: return evaluator.evaluate(source, sourceName, context,
059: classLoader);
060: }
061:
062: public Collection getNames() {
063: return new TreeSet(languageMap.keySet());
064: }
065:
066: public EvaluationResult evaluate(String sourceName, Map context,
067: ClassLoader classLoader, ResourceLocator resourceLocator)
068: throws HammurapiWebException {
069: Iterator it = extensionMap.entrySet().iterator();
070: while (it.hasNext()) {
071: Map.Entry entry = (Map.Entry) it.next();
072: final String fullName = sourceName + entry.getValue();
073: try {
074: InputStream in = resourceLocator
075: .getResourceAsStream(fullName);
076: if (in != null) {
077: InputStreamReader reader = new InputStreamReader(in);
078: StringWriter sw = new StringWriter();
079: char[] buf = new char[1024];
080: int l;
081: while ((l = reader.read(buf)) != -1) {
082: sw.write(buf, 0, l);
083: }
084: in.close();
085: sw.close();
086: return evaluate((String) entry.getKey(), sw
087: .toString(), fullName, context, classLoader);
088: }
089: } catch (final IOException e) {
090: throw new HammurapiWebException(
091: "Could not load source from " + fullName + ": "
092: + e, e);
093: }
094: }
095: return null;
096: }
097:
098: public String getExtension(String name) {
099: return (String) extensionMap.get(name);
100: }
101:
102: public void register(String language, Evaluator evaluator,
103: String extension) {
104: languageMap.put(language, evaluator);
105: if (extension != null) {
106: extensionMap.put(language, extension);
107: }
108: }
109: }
|