001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
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: package net.ar.webonswing.managers.templates;
019:
020: import java.io.*;
021: import java.net.*;
022: import java.util.*;
023:
024: import net.ar.webonswing.helpers.*;
025: import net.ar.webonswing.render.templates.*;
026: import net.ar.webonswing.render.templates.html.*;
027:
028: public class HtmlTemplateManager implements TemplateManager,
029: Serializable {
030: protected boolean templateCacheActive = true;
031: protected Hashtable templateAliases;
032: protected Vector resourcesPaths;
033:
034: public HtmlTemplateManager() {
035: templateAliases = new Hashtable();
036: resourcesPaths = new Vector();
037: }
038:
039: public HtmlTemplateManager(Hashtable aTemplateTable) {
040: templateAliases = aTemplateTable;
041: resourcesPaths = new Vector();
042: }
043:
044: public HtmlTemplateManager(Vector aVector) {
045: for (Iterator i = aVector.iterator(); i.hasNext();) {
046: HtmlTemplateManagerEntry theEntry = (HtmlTemplateManagerEntry) i
047: .next();
048: templateAliases.put(theEntry, theEntry);
049: }
050: }
051:
052: /**
053: * Obtiene una plantilla a partir de un nombre que puede llegar a ser una ruta completa para alcanzar una subplantilla
054: * ej:
055: * "WinMenu.JMenuBar.MenuItem"
056: *
057: */
058: public Template getTemplateForName(String aTemplateName) {
059: try {
060: HtmlTemplate theTemplate;
061: String theTemplatePath;
062: theTemplatePath = "";
063: int theTokenEndPosition = aTemplateName.indexOf('.');
064:
065: if (theTokenEndPosition != -1) {
066: theTemplatePath = aTemplateName
067: .substring(theTokenEndPosition + 1);
068: aTemplateName = aTemplateName.substring(0,
069: theTokenEndPosition);
070: }
071:
072: theTemplate = getTemplate(aTemplateName);
073:
074: if (theTemplate == null)
075: throw new TemplateNotFoundException(
076: "Cannot find the template '" + aTemplateName
077: + "'");
078:
079: HtmlTemplate theSubTemplate = getSubTemplate(
080: (HtmlTemplate) theTemplate.clone(), theTemplatePath);
081:
082: if (theSubTemplate == null)
083: throw new TemplateNotFoundException(
084: "Cannot find the subtemplate '"
085: + theTemplatePath
086: + "' in the template: \n" + theTemplate
087: + "\n\n");
088:
089: return theSubTemplate;
090: } catch (CloneNotSupportedException e) {
091: throw new WebOnSwingException("Cannot clone template '"
092: + aTemplateName + "'", e);
093: }
094: }
095:
096: private HtmlTemplate getTemplate(String aTemplateName) {
097: HtmlTemplateManagerEntry theEntry = (HtmlTemplateManagerEntry) templateAliases
098: .get(aTemplateName);
099:
100: if (theEntry != null) {
101: HtmlTemplate theTemplate = (HtmlTemplate) theEntry
102: .getTemplate();
103:
104: if (theTemplate == null || !isTemplateCacheActive())
105: theEntry.setTemplate(theTemplate = HtmlTemplateFactory
106: .createTemplateFromResource(theEntry
107: .getResourcePath()));
108:
109: return theTemplate;
110: } else {
111: for (Iterator i = resourcesPaths.iterator(); i.hasNext();) {
112: String thePath = (String) i.next();
113:
114: String theResourceName = thePath + "/" + aTemplateName
115: + ".html";
116: URL theResourceURL = getClass().getResource(
117: theResourceName);
118:
119: if (theResourceURL != null) {
120: HtmlTemplate theTemplate = HtmlTemplateFactory
121: .createTemplateFromResource(theResourceName);
122: HtmlTemplateManagerEntry theNewEntry = new HtmlTemplateManagerEntry(
123: aTemplateName, theResourceName, theTemplate);
124: templateAliases.put(theNewEntry, theNewEntry);
125: return theTemplate;
126: }
127: }
128: }
129:
130: return null;
131: }
132:
133: /**
134: * Va recorriendo los elementos de la plantilla dada recursivamente hasta llegar al fin de la ruta especificada
135: *
136: * @param aTemplate
137: * @param aPath
138: * @return
139: */
140: public static HtmlTemplate getSubTemplate(Template aTemplate,
141: String aPath) {
142: return (HtmlTemplate) new TemplateFinder(aTemplate)
143: .getTemplate(aPath);
144: }
145:
146: /**
147: * Idem anterior pero devuelve un clone del encontrado.
148: *
149: * @param aTemplate
150: * @param aPath
151: * @return
152: */
153: public static HtmlTemplate getClonedSubTemplate(Template aTemplate,
154: String aPath) {
155: try {
156: DefaultTemplate theFoundTemplate = getSubTemplate(
157: aTemplate, aPath);
158:
159: if (theFoundTemplate == null)
160: throw new TemplateNotFoundException(
161: "Cannot find sub template for the path '"
162: + aPath + "' in the template: \n"
163: + aTemplate + "\n\n");
164:
165: return (HtmlTemplate) theFoundTemplate.clone();
166: } catch (CloneNotSupportedException e) {
167: throw new WebOnSwingException("Cannot clone template '"
168: + aTemplate + "'", e);
169: }
170: }
171:
172: /**
173: * Acelerador para generar KeyPositionTemplates en una ventana de Swing
174: *
175: */
176: public KeyPositionTemplate getKeyPositionTemplateForName(
177: String aTemplateName) {
178: return new KeyPositionHtmlTemplate(
179: (HtmlTemplate) getTemplateForName(aTemplateName));
180: }
181:
182: /**
183: * Idem anterior pero encontrando previamente una subplantilla de otra
184: */
185: public KeyPositionTemplate getKeyPositionTemplateForSubTemplate(
186: Template aTemplate, String aPath) {
187: return new KeyPositionHtmlTemplate(getClonedSubTemplate(
188: aTemplate, aPath));
189: }
190:
191: public Hashtable getTemplateTable() {
192: return templateAliases;
193: }
194:
195: public void setTemplateTable(Hashtable aTable) {
196: templateAliases = aTable;
197: }
198:
199: public Vector getTemplateSpecification() {
200: Vector theResult = new Vector();
201:
202: for (Iterator i = templateAliases.entrySet().iterator(); i
203: .hasNext();) {
204: Map.Entry theMapEntry = (Map.Entry) i.next();
205: theResult.add(new HtmlTemplateManagerEntry(theMapEntry
206: .getKey().toString(), theMapEntry.getValue()
207: .toString(), null));
208: }
209:
210: return theResult;
211: }
212:
213: public Vector getResources_path() {
214: return resourcesPaths;
215: }
216:
217: public void setResources_path(Vector resources_path) {
218: this .resourcesPaths = resources_path;
219: }
220:
221: public Hashtable getTemplates() {
222: return templateAliases;
223: }
224:
225: public void setTemplates(Hashtable templates) {
226: this .templateAliases = templates;
227: }
228:
229: public boolean isTemplateCacheActive() {
230: return templateCacheActive;
231: }
232:
233: public void setTemplateCacheActive(boolean aB) {
234: templateCacheActive = aB;
235: }
236: }
|