01: package org.java.plugin.tools.docgen;
02:
03: import java.io.IOException;
04: import java.io.InputStreamReader;
05:
06: import org.onemind.commons.java.util.FileUtils;
07: import org.onemind.jxp.CachedJxpPage;
08: import org.onemind.jxp.CachingPageSource;
09: import org.onemind.jxp.JxpPage;
10: import org.onemind.jxp.JxpPageNotFoundException;
11: import org.onemind.jxp.JxpPageParseException;
12: import org.onemind.jxp.parser.AstJxpDocument;
13: import org.onemind.jxp.parser.JxpParser;
14: import org.onemind.jxp.parser.ParseException;
15:
16: /**
17: * JXP page source configured to load templates from the classpath.
18: * @version $Id$
19: */
20: final class ClassPathPageSource extends CachingPageSource {
21: private final String base;
22: private final ClassLoader cl;
23: private final String encoding;
24:
25: ClassPathPageSource(final String aBase, final String anEncoding) {
26: super ();
27: base = aBase;
28: encoding = anEncoding;
29: cl = getClass().getClassLoader();
30: }
31:
32: /**
33: * @see org.onemind.jxp.CachingPageSource#loadJxpPage(java.lang.String)
34: */
35: @Override
36: protected CachedJxpPage loadJxpPage(final String id)
37: throws JxpPageNotFoundException {
38: if (!hasJxpPage(id)) {
39: throw new JxpPageNotFoundException(
40: "page " + id + " not found"); //$NON-NLS-1$ //$NON-NLS-2$
41: }
42: return new CachedJxpPage(this , id);
43: }
44:
45: /**
46: * @see org.onemind.jxp.CachingPageSource#parseJxpDocument(
47: * org.onemind.jxp.JxpPage)
48: */
49: @Override
50: protected AstJxpDocument parseJxpDocument(final JxpPage page)
51: throws JxpPageParseException {
52: try {
53: JxpParser parser;
54: if (encoding == null) {
55: parser = new JxpParser(cl
56: .getResourceAsStream(getStreamName(page
57: .getName())));
58: } else {
59: parser = new JxpParser(new InputStreamReader(cl
60: .getResourceAsStream(getStreamName(page
61: .getName())), encoding));
62: }
63: return parser.JxpDocument();
64: } catch (IOException ioe) {
65: throw new JxpPageParseException("problem parsing page " //$NON-NLS-1$
66: + page.getName() + ": " + ioe.getMessage(), ioe); //$NON-NLS-1$
67: } catch (ParseException pe) {
68: throw new JxpPageParseException("problem parsing page " //$NON-NLS-1$
69: + page.getName() + ": " + pe.getMessage(), pe); //$NON-NLS-1$
70: }
71: }
72:
73: /**
74: * @see org.onemind.jxp.JxpPageSource#hasJxpPage(java.lang.String)
75: */
76: @Override
77: public boolean hasJxpPage(final String id) {
78: if (isJxpPageCached(id)) {
79: return true;
80: }
81: return cl.getResource(getStreamName(id)) != null;
82: }
83:
84: private String getStreamName(final String pageName) {
85: return FileUtils.concatFilePath(base, pageName);
86: }
87: }
|