1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
76 Iterator i = definitions.values().iterator();
77 while( i.hasNext() )
78 {
79 XmlDefinition definition = (XmlDefinition)i.next();
80 definition.resolveInheritance( this );
81 }
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 }
106 }
107 /***
108 * Get String representation.
109 */
110 public String toString()
111 {
112 return "definitions=" + definitions.toString() ;
113 }
114
115 }