View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts.tiles.xmlDefinition;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts.tiles.NoSuchDefinitionException;
28  
29  /**
30   * A set of definitions read from XML definitions file.
31  */
32  public class XmlDefinitionsSet
33  {
34      /** Defined definitions. */
35    protected Map<String, XmlDefinition> definitions;
36  
37       /**
38        * Constructor.
39        */
40    public XmlDefinitionsSet()
41     {
42     definitions = new HashMap<>();
43     }
44  
45    /**
46     * Put definition in set.
47     * @param definition Definition to add.
48     */
49    public void putDefinition(XmlDefinition definition)
50    {
51    definitions.put( definition.getName(), definition );
52    }
53  
54    /**
55     * Get requested definition.
56     * @param name Definition name.
57     */
58    public XmlDefinition getDefinition(String name)
59    {
60    return definitions.get( name );
61    }
62  
63    /**
64     * Get definitions map.
65     */
66    public Map<String, XmlDefinition> getDefinitions()
67    {
68    return definitions;
69    }
70  
71    /**
72     * Resolve extended instances.
73     */
74    public void resolveInheritances() throws NoSuchDefinitionException
75      {
76        // Walk through all definitions and resolve individual inheritance
77      for( XmlDefinition definition : definitions.values() )
78        {
79        definition.resolveInheritance( this );
80        }  // end loop
81      }
82  
83    /**
84     * Add definitions from specified child definitions set.
85     * For each definition in child, look if it already exists in this set.
86     * If not, add it, if yes, overload parent's definition with child definition.
87     * @param child Definition used to overload this object.
88     */
89    public void extend( XmlDefinitionsSet child )
90      {
91      if(child==null)
92        return;
93      for( XmlDefinition childInstance : child.getDefinitions().values() )
94        {
95        XmlDefinition parentInstance = getDefinition(childInstance.getName() );
96        if( parentInstance != null )
97          {
98          parentInstance.overload( childInstance );
99          }
100        else
101         putDefinition( childInstance );
102       } // end loop
103     }
104     /**
105      * Get String representation.
106      */
107   public String toString()
108     {
109     return "definitions=" + definitions.toString() ;
110     }
111 
112 }