01: //WebOnSwing - Web Application Framework
02: //Copyright (C) 2003 Fernando Damian Petrola
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: package net.ar.webonswing.render.templates;
19:
20: import net.ar.webonswing.helpers.*;
21: import net.ar.webonswing.visitor.*;
22:
23: /**
24: *Permite buscar una subplantilla dentro de otra mediante una ruta hacia ella.
25: *
26: * @author Fernando Damian Petrola
27: */
28: public class TemplateFinder implements TemplateVisitor {
29: protected String theTemplatePath;
30: protected Template theTemplate;
31: protected Template theFoundTemplate;
32:
33: public TemplateFinder(Template aTemplate) {
34: theTemplate = aTemplate;
35: theFoundTemplate = aTemplate;
36: }
37:
38: public void visitTemplate(Template aTemplate) {
39: if (!theTemplatePath.equals("")) {
40: String theTemplateName = theTemplatePath;
41: int theTokenEndPosition = theTemplatePath.indexOf('.');
42:
43: if (theTokenEndPosition == -1)
44: theTemplatePath = "";
45: else {
46: theTemplateName = theTemplatePath.substring(0,
47: theTokenEndPosition);
48: theTemplatePath = theTemplatePath
49: .substring(theTokenEndPosition + 1);
50: }
51:
52: TemplateElement theFoundElement = ((DefaultTemplate) aTemplate)
53: .getElementForId(theTemplateName);
54: if (theFoundElement == null) {
55: theFoundTemplate = null;
56: //LogFactory.getLog(TemplateFinder.class).info("Cannot find a template for '" + theTemplateName + "' id");
57: return;
58: }
59:
60: Visitable theContent = theFoundElement.getContent();
61:
62: if (!(theContent instanceof Template))
63: throw new WebOnSwingException(
64: "The template element with id=\""
65: + theTemplateName
66: + "\" is not a template");
67:
68: theFoundTemplate = (Template) theContent;
69: theFoundTemplate.accept(this );
70: }
71: }
72:
73: public void visit(Visitable aVisitable) {
74: }
75:
76: public Template getTemplate(String aTemplatePath) {
77: theTemplatePath = aTemplatePath;
78: theTemplate.accept(this);
79: return theFoundTemplate;
80: }
81: }
|