01: /* Copyright (c) 2006-2007, Vladimir Nikic
02: All rights reserved.
03:
04: Redistribution and use of this software in source and binary forms,
05: with or without modification, are permitted provided that the following
06: conditions are met:
07:
08: * Redistributions of source code must retain the above
09: copyright notice, this list of conditions and the
10: following disclaimer.
11:
12: * Redistributions in binary form must reproduce the above
13: copyright notice, this list of conditions and the
14: following disclaimer in the documentation and/or other
15: materials provided with the distribution.
16:
17: * The name of Web-Harvest may not be used to endorse or promote
18: products derived from this software without specific prior
19: written permission.
20:
21: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31: POSSIBILITY OF SUCH DAMAGE.
32:
33: You can contact Vladimir Nikic by sending e-mail to
34: nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35: subject line.
36: */
37: package org.webharvest.runtime.processors;
38:
39: import java.io.StringReader;
40: import java.io.StringWriter;
41:
42: import javax.xml.transform.*;
43: import javax.xml.transform.stream.StreamResult;
44: import javax.xml.transform.stream.StreamSource;
45:
46: import org.webharvest.definition.XsltDef;
47: import org.webharvest.definition.BaseElementDef;
48: import org.webharvest.exception.XsltException;
49: import org.webharvest.runtime.Scraper;
50: import org.webharvest.runtime.ScraperContext;
51: import org.webharvest.runtime.variables.IVariable;
52: import org.webharvest.runtime.variables.NodeVariable;
53:
54: /**
55: * XSLT processor.
56: */
57: public class XsltProcessor extends BaseProcessor {
58:
59: private XsltDef xsltDef;
60:
61: public XsltProcessor(XsltDef xsltDef) {
62: super (xsltDef);
63: this .xsltDef = xsltDef;
64: }
65:
66: public IVariable execute(Scraper scraper, ScraperContext context) {
67: BaseElementDef xsltElementDef = xsltDef.getXmlDef();
68: IVariable xmlStr = getBodyTextContent(xsltElementDef, scraper,
69: context);
70: debug(xsltElementDef, scraper, xmlStr);
71:
72: BaseElementDef stylesheetElementDef = xsltDef
73: .getStylesheetDef();
74: IVariable stylesheetStr = getBodyTextContent(
75: stylesheetElementDef, scraper, context);
76: debug(stylesheetElementDef, scraper, stylesheetStr);
77:
78: try {
79: TransformerFactory xformFactory = TransformerFactory
80: .newInstance();
81: Source xsl = new StreamSource(new StringReader(
82: stylesheetStr.toString()));
83: Transformer stylesheet = xformFactory.newTransformer(xsl);
84: Source request = new StreamSource(new StringReader(xmlStr
85: .toString()));
86: StringWriter writer = new StringWriter();
87: Result response = new StreamResult(writer);
88: stylesheet.transform(request, response);
89:
90: return new NodeVariable(writer.toString());
91: } catch (TransformerException e) {
92: throw new XsltException("Error during XSLT transforming!",
93: e);
94: }
95: }
96:
97: }
|