01: /*
02: * Copyright (C) 2004 TiongHiang Lee
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Email: thlee@onemindsoft.org
19: */
20:
21: package org.onemind.jxp;
22:
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.net.URL;
26: import org.onemind.commons.java.util.FileUtils;
27:
28: /**
29: * A JxpResourceSource uses resources from classpath as pages
30: * @author TiongHiang Lee (thlee@onemindsoft.org)
31: *
32: */
33: public class ResourceStreamPageSource extends CachingPageSource {
34:
35: /** the prefix * */
36: private String _pathPrefix;
37:
38: /**
39: * {@inheritDoc}
40: */
41: public ResourceStreamPageSource(String pathPrefix) {
42: _pathPrefix = pathPrefix;
43: }
44:
45: /**
46: * {@inheritDoc}
47: */
48: public ResourceStreamPageSource(String pathPrefix, String encoding) {
49: this (pathPrefix);
50: setEncoding(encoding);
51: }
52:
53: /**
54: * Get the path prefix
55: * @return the path prefix
56: */
57: public final String getPathPrefix() {
58: return _pathPrefix;
59: }
60:
61: /**
62: * {@inheritDoc}
63: */
64: public final String getStreamName(String pageName) {
65: return FileUtils.concatFilePath(getPathPrefix(), pageName);
66: }
67:
68: /**
69: * Set the pathPrefix
70: * @param pathPrefix The pathPrefix to set.
71: */
72: public final void setPathPrefix(String pathPrefix) {
73: _pathPrefix = pathPrefix;
74: }
75:
76: /**
77: * {@inheritDoc}
78: */
79:
80: protected boolean isExpired(CachedJxpPage page) {
81: return false;
82: }
83:
84: /**
85: * {@inheritDoc}
86: */
87: protected boolean hasStream(String pageName) {
88: return getClass().getResource(getStreamName(pageName)) != null;
89: }
90:
91: /**
92: * {@inheritDoc}
93: */
94: protected InputStream loadStream(CachedJxpPage page)
95: throws IOException {
96: return getClass().getResourceAsStream(
97: getStreamName(page.getName()));
98: }
99: }
|