KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > tiles > xmlDefinition > XmlDefinition


1 /*
2  * $Id: XmlDefinition.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.tiles.xmlDefinition;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.struts.tiles.ComponentDefinition;
27 import org.apache.struts.tiles.NoSuchDefinitionException;
28
29 /**
30   *A definition read from an XML definitions file.
31   */

32 public class XmlDefinition extends ComponentDefinition
33 {
34   /**
35    * Extends attribute value.
36    */

37   private String JavaDoc inherit;
38
39     /** Commons Logging instance. */
40    protected static Log log = LogFactory.getLog(XmlDefinition.class);
41
42   /**
43    * Used for resolving inheritance.
44    */

45   private boolean isVisited=false;
46
47
48      /**
49       * Constructor.
50       */

51    public XmlDefinition()
52    {
53    super();
54    //if(debug)
55
//System.out.println( "create definition" );
56
}
57
58   /**
59    * Add an attribute to this component.
60    *
61    * @param attribute Attribute to add.
62    */

63   public void addAttribute( XmlAttribute attribute)
64     {
65     putAttribute( attribute.getName(), attribute.getValue() );
66     }
67
68   /**
69    * Set extends.
70    *
71    * @param name Name of the extended definition.
72    */

73   public void setExtends(String JavaDoc name)
74     {
75     inherit = name;
76     }
77
78   /**
79    * Get extends.
80    *
81    * @return Name of the extended definition.
82    */

83   public String JavaDoc getExtends()
84     {
85     return inherit;
86     }
87
88   /**
89    * Get extends flag.
90    *
91    */

92   public boolean isExtending( )
93     {
94     return inherit!=null;
95     }
96
97   /**
98    * Set isVisited.
99    *
100    */

101   public void setIsVisited( boolean isVisited )
102     {
103     this.isVisited = isVisited;
104     }
105
106     /**
107      * Resolve inheritance.
108      * First, resolve parent's inheritance, then set path to the parent's path.
109      * Also copy attributes setted in parent, and not set in child
110      * If instance doesn't extend anything, do nothing.
111      * @throws NoSuchDefinitionException If an inheritance can not be solved.
112      */

113   public void resolveInheritance( XmlDefinitionsSet definitionsSet )
114     throws NoSuchDefinitionException
115     {
116       // Already done, or not needed ?
117
if( isVisited || !isExtending() )
118       return;
119
120     if(log.isDebugEnabled())
121       log.debug( "Resolve definition for child name='" + getName()
122               + "' extends='" + getExtends() + "'.");
123
124       // Set as visited to avoid endless recurisvity.
125
setIsVisited( true );
126
127       // Resolve parent before itself.
128
XmlDefinition parent = definitionsSet.getDefinition( getExtends() );
129     if( parent == null )
130       { // error
131
String JavaDoc msg = "Error while resolving definition inheritance: child '"
132                            + getName() + "' can't find its ancestor '"
133                            + getExtends() + "'. Please check your description file.";
134       log.error( msg );
135         // to do : find better exception
136
throw new NoSuchDefinitionException( msg );
137       }
138
139     parent.resolveInheritance( definitionsSet );
140
141       // Iterate on each parent's attribute and add it if not defined in child.
142
Iterator JavaDoc parentAttributes = parent.getAttributes().keySet().iterator();
143     while( parentAttributes.hasNext() )
144       {
145       String JavaDoc name = (String JavaDoc)parentAttributes.next();
146       if( !getAttributes().containsKey(name) )
147         putAttribute( name, parent.getAttribute(name) );
148       }
149       // Set path and role if not setted
150
if( path == null )
151       setPath( parent.getPath() );
152     if( role == null )
153       setRole( parent.getRole() );
154     if( controller==null )
155       {
156       setController( parent.getController());
157       setControllerType( parent.getControllerType());
158       }
159     }
160
161   /**
162    * Overload this definition with passed child.
163    * All attributes from child are copied to this definition. Previous attributes with
164    * same name are disguarded.
165    * Special attribute 'path','role' and 'extends' are overloaded if defined in child.
166    * @param child Child used to overload this definition.
167    */

168   public void overload( XmlDefinition child )
169     {
170     if( child.getPath() != null )
171       {
172       path = child.getPath();
173       }
174     if( child.getExtends() != null )
175       {
176       inherit = child.getExtends();
177       }
178     if( child.getRole() != null )
179       {
180       role = child.getRole();
181       }
182     if( child.getController()!=null )
183       {
184       controller = child.getController();
185       controllerType = child.getControllerType();
186       }
187       // put all child attributes in parent.
188
attributes.putAll( child.getAttributes());
189     }
190 }
191
Popular Tags