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.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.util.Collection;
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.Map;
035: import java.util.TreeSet;
036:
037: import biz.hammurapi.sql.DataAccessObject;
038: import biz.hammurapi.sql.RowsetConfigurableContainer;
039: import biz.hammurapi.sql.SQLProcessor;
040: import biz.hammurapi.web.HammurapiWebException;
041:
042: public class ResultSetEvaluatorFactory extends
043: RowsetConfigurableContainer implements EvaluatorFactory,
044: DataAccessObject {
045: private String sql;
046:
047: public ResultSetEvaluatorFactory(String sql) {
048: this .sql = sql;
049: }
050:
051: private Map extensionMap = new HashMap();
052:
053: public EvaluationResult evaluate(String language, String source,
054: String sourceName, Map context, ClassLoader classLoader)
055: throws HammurapiWebException {
056:
057: Evaluator evaluator = (Evaluator) get(language);
058:
059: if (evaluator == null) {
060: throw new HammurapiWebException("Not supported language: "
061: + language);
062: }
063:
064: return evaluator.evaluate(source, sourceName, context,
065: classLoader);
066: }
067:
068: public Collection getNames() {
069: return new TreeSet(getComponentNames());
070: }
071:
072: public void setSQLProcessor(SQLProcessor processor)
073: throws SQLException {
074: processor.processSelect(sql, null, this );
075: }
076:
077: public EvaluationResult evaluate(String sourceName, Map context,
078: ClassLoader classLoader, ResourceLocator resourceLocator)
079: throws HammurapiWebException {
080: Iterator it = extensionMap.entrySet().iterator();
081: while (it.hasNext()) {
082: Map.Entry entry = (Map.Entry) it.next();
083: final String fullName = sourceName + entry.getValue();
084: try {
085: InputStream in = resourceLocator
086: .getResourceAsStream(fullName);
087: if (in != null) {
088: InputStreamReader reader = new InputStreamReader(in);
089: StringWriter sw = new StringWriter();
090: char[] buf = new char[1024];
091: int l;
092: while ((l = reader.read(buf)) != -1) {
093: sw.write(buf, 0, l);
094: }
095: in.close();
096: sw.close();
097: return evaluate((String) entry.getKey(), sw
098: .toString(), fullName, context, classLoader);
099: }
100: } catch (final IOException e) {
101: throw new HammurapiWebException(
102: "Could not load source from " + fullName + ": "
103: + e, e);
104: }
105: }
106: return null;
107: }
108:
109: public boolean process(ResultSet rs) throws SQLException {
110: String extension = rs.getString("TEMPLATE_EXTENSION");
111: if (extension != null && extension.trim().length() > 0) {
112: extensionMap.put(rs.getString("NAME"), extension);
113: }
114: return super .process(rs);
115: }
116:
117: public String getExtension(String name) {
118: return (String) extensionMap.get(name);
119: }
120:
121: }
|