001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.template;
014:
015: import java.io.IOException;
016: import java.io.InputStream;
017:
018: /**
019: * A <CODE>StreamDataSource</CODE> implements a DataSource
020: * as a wrapper around a Stream.
021: *
022: * @author <A href="mailto:joachim.karrer@mercatis.de">Joachim Karrer</A>
023: */
024: public class StreamTemplateSource implements TemplateSource {
025: /**
026: * enable debug output
027: */
028: private static final boolean DEBUG = true;
029:
030: /**
031: * <p>the <code>InputStream</code> from which we are reading data.</p>
032: */
033: private transient InputStream iStream;
034:
035: /**
036: * <p>the last time the <code>InputStream</code> was updated.</p>
037: */
038: private long modificationTime;
039:
040: /**
041: * <p>generates a new StreamDataSource with the given Stream</p>
042: *
043: * @param iStream the InputStream from which the template is read.
044: */
045: public StreamTemplateSource(InputStream iStream) {
046: setInputStream(iStream);
047: }
048:
049: /**
050: * <p>sets the InputStream and the modificationTime</p>
051: *
052: * @param iStream the InputStream from which the template is read.
053: */
054: public void setInputStream(InputStream iStream) {
055: if (iStream == null) {
056: throw new IllegalArgumentException(
057: "stream is null, this is invalid!!");
058: }
059: this .iStream = iStream;
060: setModificationTime();
061: }
062:
063: /**
064: * <p>sets the modificationTime to the currentTimeMillis</p>
065: */
066: public void setModificationTime() {
067: modificationTime = System.currentTimeMillis();
068: }
069:
070: /**
071: * <p>returns the canonical name of the inputStream (uaaaah!)</p>
072: *
073: * @return <p>always null, because stream is always to be parsed</p>
074: */
075: public String getCanonicalName() {
076: return null;
077: }
078:
079: /**
080: * <p>Returns the time the content of this stream
081: * was last modified.</p>
082: * <p>The return value is used to decide whether to reparse a
083: * Source or not. Reparsing is done if the value returned
084: * here differs from the value returned at the last processing
085: * time.</p>
086: * <p><em>Attention: Modificationtime is only updated if a new stream is set!</em></p>
087: *
088: * @return long a modification time
089: */
090: public long lastModified() {
091: return modificationTime;
092: }
093:
094: /**
095: * Gets an InputStream of the File.
096: *
097: * @return the actually set InputStream
098: */
099: public InputStream getInputStream() throws IOException {
100: iStream.reset();
101: return iStream;
102: }
103: }
|