View Javadoc

1   /*
2    * $Id: XmlDefinitionsSet.java 421151 2006-07-12 06:07:14Z wsmoak $
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.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import org.apache.struts.tiles.NoSuchDefinitionException;
27  
28  /***
29   * A set of definitions read from XML definitions file.
30  */
31  public class XmlDefinitionsSet
32  {
33      /*** Defined definitions. */
34    protected Map definitions;
35  
36       /***
37        * Constructor.
38        */
39    public XmlDefinitionsSet()
40     {
41     definitions = new HashMap();
42     }
43  
44    /***
45     * Put definition in set.
46     * @param definition Definition to add.
47     */
48    public void putDefinition(XmlDefinition definition)
49    {
50    definitions.put( definition.getName(), definition );
51    }
52  
53    /***
54     * Get requested definition.
55     * @param name Definition name.
56     */
57    public XmlDefinition getDefinition(String name)
58    {
59    return (XmlDefinition)definitions.get( name );
60    }
61  
62    /***
63     * Get definitions map.
64     */
65    public Map getDefinitions()
66    {
67    return definitions;
68    }
69  
70    /***
71     * Resolve extended instances.
72     */
73    public void resolveInheritances() throws NoSuchDefinitionException
74      {
75        // Walk through all definitions and resolve individual inheritance
76      Iterator i = definitions.values().iterator();
77      while( i.hasNext() )
78        {
79        XmlDefinition definition = (XmlDefinition)i.next();
80        definition.resolveInheritance( this );
81        }  // end loop
82      }
83  
84    /***
85     * Add definitions from specified child definitions set.
86     * For each definition in child, look if it already exists in this set.
87     * If not, add it, if yes, overload parent's definition with child definition.
88     * @param child Definition used to overload this object.
89     */
90    public void extend( XmlDefinitionsSet child )
91      {
92      if(child==null)
93        return;
94      Iterator i = child.getDefinitions().values().iterator();
95      while( i.hasNext() )
96        {
97        XmlDefinition childInstance = (XmlDefinition)i.next();
98        XmlDefinition parentInstance = getDefinition(childInstance.getName() );
99        if( parentInstance != null )
100         {
101         parentInstance.overload( childInstance );
102         }
103        else
104         putDefinition( childInstance );
105       } // end loop
106     }
107     /***
108      * Get String representation.
109      */
110   public String toString()
111     {
112     return "definitions=" + definitions.toString() ;
113     }
114 
115 }