KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > DefaultTemplateView


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * 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  * $Id: DefaultTemplateView.java,v 1.14 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.util.*;
23
24 import org.apache.log4j.*;
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28 import org.enhydra.barracuda.core.util.dom.DOMUtil;
29 import org.enhydra.barracuda.plankton.*;
30 import org.enhydra.barracuda.plankton.data.*;
31
32 /**
33  * This class provides the default implementation for
34  * a TemplateView. It provides a View for components to
35  * render themselves in.
36  */

37 public class DefaultTemplateView extends DefaultView implements TemplateView {
38
39     protected static final Logger logger = Logger.getLogger(DefaultTemplateView.class.getName());
40
41     protected Node masterTemplate = null;
42     protected Map dirMap = null; //map of directives, accessed by directive name
43
protected StateMap idMap = null; //map of directives, accessed by id name
44
protected String JavaDoc idAttrName = "id";
45     protected String JavaDoc dirAttrName = "class";
46
47     /**
48      * Public noargs constructor. This creates a view which is not bound
49      * to any particular node. You must bind the view to a node before you
50      * can actually use it for anything
51      */

52     public DefaultTemplateView() {}
53
54     /**
55      * Create a view and bind it to a node.
56      *
57      * @param node the node the view should be bound to
58      */

59     public DefaultTemplateView(Node node) {
60         if (node!=null) setNode(node);
61     }
62
63     /**
64      * Create a view and bind it to a node. Specify the name
65      * of the attribute to be used to search for a directive
66      * list (defaults to "class")
67      *
68      * @param node the node the view should be bound to
69      * @param idAttrName name of the id attribute
70      * @param dirAttrName name of the attribute to search for
71      * directives in
72      */

73     public DefaultTemplateView(Node node, String JavaDoc idAttrName, String JavaDoc dirAttrName) {
74         if (idAttrName!=null) setIDAttrName(idAttrName);
75         if (dirAttrName!=null) setDirAttrName(dirAttrName);
76         if (node!=null) setNode(node);
77     }
78
79     /**
80      * Create a view and bind it to a node. Pass in a
81      * pre-poulated directive map.
82      *
83      * @param node the node the view should be bound to
84      * @param idAttrName name of the id attribute
85      * @param idMap the directive map to be used
86      */

87     public DefaultTemplateView(Node node, String JavaDoc idAttrName, StateMap idMap) {
88         if (idAttrName!=null) setIDAttrName(idAttrName);
89         if (idMap!=null) setDirIDMap(idMap);
90         if (node!=null) setNode(node);
91     }
92
93
94     //--------------- DefaultTemplateView ------------------------
95
/**
96      * Get the master template
97      *
98      * @return the master template
99      */

100     public Node getMasterTemplate() {
101         return masterTemplate;
102     }
103     
104     /**
105      * Set the directive ID map. This map allows you to lookup
106      * directives associated with a given id. Each item in the
107      * map should correspond to a list of directives.
108      *
109      * @param iidMap the directive map
110      */

111     public void setDirIDMap(StateMap iidMap) {
112         idMap = iidMap;
113     }
114     
115     /**
116      * Get the directive ID map. This map allows you to lookup
117      * directives associated with a given id.
118      *
119      * @return the idMap for this view
120      */

121     public StateMap getDirIDMap() {
122         return idMap;
123     }
124     
125     /**
126      * Set the id attr name. Specify the name of the
127      * id attribute
128      *
129      * @param iidAttrName the name of the id attribute
130      */

131     public void setIDAttrName(String JavaDoc iidAttrName) {
132         idAttrName = iidAttrName;
133     }
134     
135     /**
136      * Get the id attr name (defaults to "id").
137      *
138      * @return the id attr name
139      */

140     public String JavaDoc getIDAttrName() {
141         return idAttrName;
142     }
143     
144     /**
145      * Set the directive attr name. Specify the name of the
146      * attribute to search for a list of directives in.
147      *
148      * @param idirAttrName the name of the attribute to search
149      * for a list of directives in
150      */

151     public void setDirAttrName(String JavaDoc idirAttrName) {
152         dirAttrName = idirAttrName;
153     }
154     
155     /**
156      * Get the directive attr name (defaults to "class").
157      *
158      * @return the directive attr name
159      */

160     public String JavaDoc getDirAttrName() {
161         return dirAttrName;
162     }
163     
164     /**
165      * Look up a directive by it's string representation (rather
166      * than reparsing it). May be null if the template directive
167      * is not part of the master template.
168      *
169      * @param dirStr a string representation of a directive
170      * @return the actual template directive that corresponds to the
171      * String representation.
172      */

173     public TemplateDirective lookupDir(String JavaDoc dirStr) {
174         return (TemplateDirective) dirMap.get(dirStr);
175     }
176
177     /**
178      * Look up a list of directives based on a given id
179      *
180      * @param idStr the id in question
181      * @return a list of template directive for the given id
182      */

183     public List lookupDirsByID(String JavaDoc idStr) {
184         if (idMap==null || idStr==null || idStr.trim().length()<1) return null;
185         Object JavaDoc val = idMap.getState(idStr);
186         if (logger.isDebugEnabled()) logger.debug("Looking up directives by id: "+idStr+" val: "+val);
187         if (val==null) return null;
188         if (val instanceof List) return (List) val;
189         else {
190             List list = TemplateDirective.getAllInstances(val.toString());
191             idMap.putState(idStr, list);
192             return list;
193         }
194     }
195
196
197     /**
198      * Here we are going to look for custom header, footer, and
199      * body elements
200      */

201     protected void customSearchForTemplates(Node curnode) {
202         if (logger.isDebugEnabled()) logger.debug("custom search for templates on node:"+curnode);
203
204         //set the template if we don't already have it
205
if (masterTemplate==null) {
206             masterTemplate = node.cloneNode(true);
207             dirMap = new HashMap();
208 // dirMap = new TreeMap();
209
}
210         
211         //now look for directives and store a reference to them in the dir map
212
//(note that we don't track of the id's here...the idMap must be manually
213
//provided if you wish to use it)
214
//..Elements
215
if (curnode instanceof Element) {
216             Element el = (Element) curnode;
217             String JavaDoc classAttr = el.getAttribute(getDirAttrName());
218             if (classAttr==null) return;
219             if (logger.isDebugEnabled()) logger.debug("dir attr("+getDirAttrName()+"):"+classAttr);
220             List dirList = TemplateDirective.getAllInstances(classAttr);
221             Iterator it = dirList.iterator();
222             while (it.hasNext()) {
223                 TemplateDirective td = (TemplateDirective) it.next();
224                 if (logger.isDebugEnabled()) logger.debug("dir:"+td.toString());
225                 dirMap.put(td.toString(), td);
226             }
227         }
228     }
229
230     public String JavaDoc toString() {
231         return "TemplateView:"+getName()+" (bound to Node:"+DOMUtil.getID(node)+")";
232     }
233
234     //--------------- Cloneable ----------------------------------
235
//csc_092101.1_start
236
/**
237      * When a template view is cloned, the underlying masterTemplate node is
238      * set to null; this ensures the new node to which this view is bound
239      * will in fact be used for the template
240      */

241     public Object JavaDoc clone() {
242         try {
243             DefaultTemplateView dtv = (DefaultTemplateView) super.clone();
244             dtv.masterTemplate = null;
245             return dtv;
246        } catch (Exception JavaDoc e) {
247             return null;
248        }
249     }
250     //csc_092101.1_end
251
}
Popular Tags