1 /*
2 * $Header: /usr/local/cvsroot/dev/madcache/src/com/macvu/tiles/xmlDefinition/XmlCacheDefinition.java,v 1.2 2004/05/06 02:18:54 macvu Exp $
3 * $Revision: 1.2 $
4 * $Date: 2004/05/06 02:18:54 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Struts", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 */
61
62 package com.macvu.tiles.xmlDefinition;
63
64 import com.macvu.tiles.CacheComponentDefinition;
65 import org.apache.commons.logging.Log;
66 import org.apache.commons.logging.LogFactory;
67 import org.apache.struts.tiles.NoSuchDefinitionException;
68 import org.apache.struts.tiles.xmlDefinition.XmlAttribute;
69
70 import java.util.Iterator;
71
72
73 public class XmlCacheDefinition extends CacheComponentDefinition {
74
75 /***
76 * Commons Logging instance.
77 */
78 protected static Log log = LogFactory.getLog(XmlCacheDefinition.class);
79
80 /***
81 * Used for resolving inheritance.
82 */
83 private boolean isVisited = false;
84
85 /***
86 * Constructor.
87 */
88 public XmlCacheDefinition() {
89 super();
90 }
91
92 /***
93 * Add an attribute to this component.
94 *
95 * @param attribute Attribute to add.
96 */
97 public void addAttribute(XmlAttribute attribute) {
98 putAttribute(attribute.getName(), attribute.getValue());
99 }
100
101 public void addCacheInformation(XmlCacheInformation info) {
102 super.setCacheInformation(info);
103 }
104
105 /***
106 * Set isVisited.
107 */
108 public void setIsVisited(boolean isVisited) {
109 this.isVisited = isVisited;
110 }
111
112 /***
113 * Resolve inheritance.
114 * First, resolve parent's inheritance, then set path to the parent's path.
115 * Also copy attributes setted in parent, and not set in child
116 * If instance doesn't extend anything, do nothing.
117 *
118 * @throws org.apache.struts.tiles.NoSuchDefinitionException
119 * If an inheritance can not be solved.
120 */
121 public void resolveInheritance(XmlCacheDefinitionSet definitionsSet)
122 throws NoSuchDefinitionException {
123 // Already done, or not needed ?
124 if (isVisited || !isExtending())
125 return;
126
127 if (log.isDebugEnabled())
128 log.debug("Resolve definition for child name='" + getName()
129 + "' extends='" + getExtends() + "'.");
130
131 // Set as visited to avoid endless recurisvity.
132 setIsVisited(true);
133
134 // Resolve parent before itself.
135 XmlCacheDefinition parent = definitionsSet.getDefinition(getExtends());
136 if (parent == null) { // error
137 String msg = "Error while resolving definition inheritance: child '"
138 + getName() + "' can't find its ancestor '"
139 + getExtends() + "'. Please check your description file.";
140 log.error(msg);
141 // to do : find better exception
142 throw new NoSuchDefinitionException(msg);
143 }
144
145 parent.resolveInheritance(definitionsSet);
146
147 // Iterate on each parent's attribute and add it if not defined in child.
148 Iterator parentAttributes = parent.getAttributes().keySet().iterator();
149 while (parentAttributes.hasNext()) {
150 String name = (String) parentAttributes.next();
151 if (!getAttributes().containsKey(name))
152 putAttribute(name, parent.getAttribute(name));
153 }
154 // Set path and role if not setted
155 if (path == null)
156 setPath(parent.getPath());
157 if (role == null)
158 setRole(parent.getRole());
159 if (controller == null) {
160 setController(parent.getController());
161 setControllerType(parent.getControllerType());
162 }
163 }
164
165 /***
166 * Overload this definition with passed child.
167 * All attributes from child are copied to this definition. Previous attributes with
168 * same name are disguarded.
169 * Special attribute 'path','role' and 'extends' are overloaded if defined in child.
170 *
171 * @param child Child used to overload this definition.
172 */
173 public void overload(XmlCacheDefinition child) {
174 if (child.getPath() != null) {
175 path = child.getPath();
176 }
177 if (child.getExtends() != null) {
178 inherit = child.getExtends();
179 }
180 if (child.getRole() != null) {
181 role = child.getRole();
182 }
183 if (child.getController() != null) {
184 controller = child.getController();
185 controllerType = child.getControllerType();
186 }
187 // put all child attributes in parent.
188 attributes.putAll(child.getAttributes());
189 }
190
191 }