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.StringReader;
027: import java.io.StringWriter;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Map.Entry;
031:
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.ParserConfigurationException;
035: import javax.xml.transform.Transformer;
036: import javax.xml.transform.TransformerException;
037: import javax.xml.transform.TransformerFactory;
038: import javax.xml.transform.dom.DOMSource;
039: import javax.xml.transform.stream.StreamResult;
040: import javax.xml.transform.stream.StreamSource;
041:
042: import org.w3c.dom.Document;
043:
044: import biz.hammurapi.web.HammurapiWebException;
045: import biz.hammurapi.xml.dom.AbstractDomObject;
046: import biz.hammurapi.xml.dom.CompositeDomSerializer;
047:
048: /**
049: * Applies stylesheet to "result" context entry or to the whole context if there is no "result" entry.
050: * If "result" is present then other context entries are passed as parameters to stylesheet.
051: * Root element name for the doc is always "result".
052: * @author Pavel
053: */
054: public class XsltEvaluator implements Evaluator {
055:
056: private static final String RESULT = "result";
057:
058: public EvaluationResult evaluate(String source, String sourceName,
059: Map context, ClassLoader classLoader)
060: throws HammurapiWebException {
061: try {
062: Object result = context.get(RESULT);
063: StreamSource style = source == null ? null
064: : new StreamSource(new StringReader(source));
065:
066: // TODO Leverage factory caching for performance improvements
067: TransformerFactory factory = TransformerFactory
068: .newInstance();
069: Transformer transformer = style == null ? factory
070: .newTransformer() : factory.newTransformer(style);
071:
072: if (result != null) {
073: Iterator it = context.entrySet().iterator();
074: while (it.hasNext()) {
075: Map.Entry entry = (Entry) it.next();
076: if (!RESULT.equals(entry.getKey())) {
077: transformer.setParameter(entry.getKey()
078: .toString(), entry.getValue());
079: }
080: }
081: }
082:
083: // TODO Leverage factory caching for performance improvements
084: DocumentBuilderFactory docFactory = DocumentBuilderFactory
085: .newInstance();
086: DocumentBuilder builder = docFactory.newDocumentBuilder();
087: Document doc = builder.newDocument();
088: CompositeDomSerializer.getThreadInstance()
089: .toDomSerializable(
090: result == null ? context : result)
091: .toDom(AbstractDomObject.addElement(doc, "result"));
092:
093: StringWriter sw = new StringWriter();
094: transformer.transform(new DOMSource(doc), new StreamResult(
095: sw));
096: sw.close();
097:
098: final String output = sw.toString();
099: return new EvaluationResult() {
100:
101: public String getOutput() {
102: return output;
103: }
104:
105: public Object getResult() {
106: return null;
107: }
108:
109: };
110: } catch (TransformerException e) {
111: throw new HammurapiWebException(e);
112: } catch (ParserConfigurationException e) {
113: throw new HammurapiWebException(e);
114: } catch (IOException e) {
115: throw new HammurapiWebException(e);
116: }
117: }
118:
119: }
|