1 package de.matthias_burbach.mosaique.core.model; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import de.matthias_burbach.mosaique.core.parser.JspParser; 7 8 /*** 9 * @author Matthias Burbach 10 */ 11 public class JspResolver { 12 /*** 13 * The singleton instance of this class. 14 */ 15 private static JspResolver instance = new JspResolver(null, null); 16 17 /*** 18 * The absolute path of the web application's document root to prepend 19 * to relative JSP paths. 20 * Is not used if the path finder has been set. 21 */ 22 private String root = ""; 23 24 /*** 25 * The optional path finder to map relative paths to absolute paths. 26 * Was introduced with the port to Eclipse where path logic is more 27 * complicated. 28 */ 29 private PathFinder pathFinder; 30 31 /*** 32 * The context to use by this instance. 33 */ 34 private Context context; 35 36 /*** 37 * The JSP parser to parse JSPs lazily on demand with. 38 */ 39 private JspParser parser = new JspParser(); 40 41 /*** 42 * The map of already resolved JSPs of type {@link Jsp} keyed by their 43 * relative paths of type {@link String}. 44 */ 45 private Map resolvedJsps = new HashMap(); 46 47 /*** 48 * @return The singleton instance of this class. 49 */ 50 public static JspResolver getInstance() { 51 return instance; 52 } 53 54 /*** 55 * @param pathFinder The optional path finder to map relative paths to 56 * absolute paths. 57 * Was introduced with the port to Eclipse where path 58 * logic is more complicated. 59 * @param context The context to use by this instance. 60 */ 61 public JspResolver(final PathFinder pathFinder, final Context context) { 62 this.pathFinder = pathFinder; 63 this.context = context; 64 } 65 66 /*** 67 * @param root The absolute path of the web application's document root to 68 * prepend to relative JSP paths. 69 */ 70 public void setRoot(final String root) { 71 this.root = root; 72 } 73 74 /*** 75 * Clears the internal map of already resolved JSPs. 76 */ 77 public void clear() { 78 resolvedJsps.clear(); 79 } 80 81 /*** 82 * @param relativePath The relative path of a JSP to resolve. 83 * @return The JSP resolved or <code>null</code>. 84 */ 85 public Jsp resolve(final String relativePath) { 86 Jsp jsp = (Jsp) resolvedJsps.get(relativePath); 87 try { 88 if (jsp == null) { 89 String path = null; 90 if (pathFinder != null) { 91 path = pathFinder.findAbsolutePath(relativePath); 92 } else { 93 path = root + "/" + relativePath; 94 } 95 if (path != null) { 96 jsp = parser.parse(path, context); 97 resolvedJsps.put(relativePath, jsp); 98 } 99 } 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 return jsp; 104 } 105 }