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