1 package org.java.plugin.tools.docgen; 2 3 import java.io.IOException ; 4 import java.io.InputStreamReader ; 5 6 import org.onemind.commons.java.util.FileUtils; 7 import org.onemind.jxp.CachedJxpPage; 8 import org.onemind.jxp.CachingPageSource; 9 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 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 35 protected CachedJxpPage loadJxpPage(final String id) 36 throws JxpPageNotFoundException { 37 if (!hasJxpPage(id)) { 38 throw new JxpPageNotFoundException("page " + id + " not found"); } 40 return new CachedJxpPage(this, id); 41 } 42 43 47 protected AstJxpDocument parseJxpDocument(final JxpPage page) 48 throws JxpPageParseException { 49 try { 50 JxpParser parser; 51 if (encoding == null) { 52 parser = new JxpParser(cl.getResourceAsStream( 53 getStreamName(page.getName()))); 54 } else { 55 parser = new JxpParser(new InputStreamReader ( 56 cl.getResourceAsStream(getStreamName(page.getName())), 57 encoding)); 58 } 59 return parser.JxpDocument(); 60 } catch (IOException ioe) { 61 throw new JxpPageParseException("problem parsing page " + page.getName() + ": " + ioe.getMessage(), ioe); } catch (ParseException pe) { 64 throw new JxpPageParseException("problem parsing page " + page.getName() + ": " + pe.getMessage(), pe); } 67 } 68 69 72 public boolean hasJxpPage(final String id) { 73 if (isJxpPageCached(id)) { 74 return true; 75 } 76 return cl.getResource(getStreamName(id)) != null; 77 } 78 79 private String getStreamName(final String pageName) { 80 return FileUtils.concatFilePath(base, pageName); 81 } 82 } 83 | Popular Tags |