001: /* CVS ID: $Id: XHTMLDocument.java,v 1.1.1.1 2002/10/02 18:42:54 wastl Exp $ */
002: package net.wastl.webmail.ui.xml;
003:
004: import net.wastl.webmail.server.*;
005: import net.wastl.webmail.exceptions.*;
006:
007: import org.w3c.dom.*;
008: import org.xml.sax.SAXException;
009:
010: import javax.xml.transform.*;
011: import javax.xml.transform.dom.DOMSource;
012: import javax.xml.transform.stream.StreamSource;
013: import javax.xml.transform.stream.StreamResult;
014:
015: import java.io.*;
016:
017: /**
018: * XHTMLDocument.java
019: *
020: * Created: Mon Mar 27 16:05:52 2000
021: *
022: * Copyright (C) 2000 Sebastian Schaffert
023: *
024: * This program is free software; you can redistribute it and/or
025: * modify it under the terms of the GNU General Public License
026: * as published by the Free Software Foundation; either version 2
027: * of the License, or (at your option) any later version.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
032: * GNU General Public License for more details.
033: *
034: * You should have received a copy of the GNU General Public License
035: * along with this program; if not, write to the Free Software
036: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
037: */
038:
039: /**
040: * Constructs HTML-Documents using a Stylesheet and a XML Document.
041: *
042: * @author Sebastian Schaffert
043: * @version
044: */
045:
046: public class XHTMLDocument extends
047: net.wastl.webmail.ui.html.HTMLDocument {
048:
049: public XHTMLDocument(Document xml, String xsl)
050: throws WebMailException {
051:
052: StringWriter writer = new StringWriter();
053:
054: long start_t = System.currentTimeMillis();
055: try {
056: DOMSource msg_xml = new DOMSource((Node) xml);
057: StreamSource msg_xsl = new StreamSource("file://" + xsl);
058: StreamResult msg_result = new StreamResult(writer);
059:
060: TransformerFactory factory = TransformerFactory
061: .newInstance();
062:
063: Transformer processor = factory.newTransformer(msg_xsl);
064:
065: //processor.setDiagnosticsOutput(System.err);
066: processor.transform(msg_xml, msg_result);
067: } catch (Exception ex) {
068: System.err.println("Error transforming XML with " + xsl
069: + " to XHTML.");
070: WebMailServer.getStorage()
071: .log(
072: Storage.LOG_INFO,
073: "Error transforming XML with " + xsl
074: + " to XHTML.");
075: throw new WebMailException(ex.getMessage());
076: }
077: long end_t = System.currentTimeMillis();
078: //System.err.println("Transformation XML --> XHTML took "+(end_t-start_t)+" ms.");
079: WebMailServer.getStorage().log(
080: Storage.LOG_DEBUG,
081: "Transformation XML --> XHTML took "
082: + (end_t - start_t) + " ms.");
083:
084: content = writer.toString();
085: }
086:
087: public XHTMLDocument(Document xml, Templates stylesheet)
088: throws WebMailException {
089: StringWriter writer = new StringWriter();
090:
091: long start_t = System.currentTimeMillis();
092: try {
093: DOMSource msg_xml = new DOMSource((Node) xml);
094: StreamResult msg_result = new StreamResult(writer);
095:
096: Transformer processor = stylesheet.newTransformer();
097: processor.transform(msg_xml, msg_result);
098: } catch (Exception ex) {
099: System.err.println("Error transforming XML to XHTML.");
100: ex.printStackTrace();
101: throw new WebMailException(ex.toString());
102: }
103: long end_t = System.currentTimeMillis();
104: //System.err.println("Transformation (with precompiled stylesheet) XML --> XHTML took "+(end_t-start_t)+" ms.");
105: WebMailServer.getStorage().log(
106: Storage.LOG_DEBUG,
107: "Transformation (with precompiled stylesheet) XML --> XHTML took "
108: + (end_t - start_t) + " ms.");
109:
110: content = writer.toString();
111: }
112:
113: public String toString() {
114: return content;
115: }
116:
117: public int length() {
118: return content.length();
119: }
120:
121: } // XHTMLDocument
|