001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.jxp;
022:
023: import java.io.*;
024: import org.onemind.commons.java.util.FileUtils;
025: import org.onemind.jxp.parser.*;
026:
027: /**
028: * Represents a JxpPageSource using the file system
029: * @author TiongHiang Lee (thlee@onemindsoft.org)
030: *
031: */
032: public class FilePageSource extends CachingPageSource {
033:
034: /** do modification check **/
035: private boolean _modCheck;
036:
037: /** the path prefix * */
038: private String _pathPrefix;
039:
040: /**
041: * Constructor
042: * @param pathPrefix the prefix
043: */
044: public FilePageSource(String pathPrefix) {
045: _pathPrefix = pathPrefix;
046: }
047:
048: /**
049: * Constructor
050: * @param pathPrefix the path prefix
051: * @param encoding the encoding
052: */
053: public FilePageSource(String pathPrefix, String encoding) {
054: this (pathPrefix);
055: setEncoding(encoding);
056: }
057:
058: /**
059: * {@inheritDoc}
060: */
061: public final String getStreamName(String pageName) {
062: return FileUtils.concatFilePath(getPathPrefix(), pageName);
063: }
064:
065: /**
066: * Return the pathPrefix
067: * @return the pathPrefix.
068: */
069: public final String getPathPrefix() {
070: return _pathPrefix;
071: }
072:
073: /**
074: * Set the pathPrefix
075: * @param pathPrefix The pathPrefix to set.
076: */
077: public final void setPathPrefix(String pathPrefix) {
078: _pathPrefix = pathPrefix;
079: }
080:
081: /**
082: * Return whether doing file modification check
083: * @return true if modification check is turned on
084: */
085: public boolean getModCheck() {
086: return _modCheck;
087: }
088:
089: /**
090: * Set whether to do file modification check
091: * @param b true to turn on modification check
092: */
093: public void setModCheck(boolean b) {
094: _modCheck = true;
095: }
096:
097: /**
098: * {@inheritDoc}
099: */
100: protected boolean isExpired(CachedJxpPage page) {
101: File f = new File(getStreamName(page.getName()));
102: return (page.getPageTimestamp() != f.lastModified());
103: }
104:
105: /**
106: * {@inheritDoc}
107: */
108: protected boolean hasStream(String pageName) {
109: return new File(getStreamName(pageName)).exists();
110: }
111:
112: /**
113: * {@inheritDoc}
114: */
115: protected InputStream loadStream(CachedJxpPage page)
116: throws IOException {
117: File f = new File(getStreamName(page.getName()));
118: page.setPageTimepstamp(f.lastModified());
119: return new FileInputStream(f);
120: }
121: }
|