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.io.Serializable;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletRequest;
29
30 import org.apache.struts.tiles.ComponentDefinition;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.NoSuchDefinitionException;
33
34 /***
35 * A factory for definitions.
36 * This factory allows to retrieve definitions by their keys.
37 */
38 public class DefinitionsFactory implements Serializable
39 {
40 /*** Underlying map containing all definitions.*/
41 protected Map definitions;
42
43 /***
44 * Get a definition by its name.
45 * @param name Name of the definition.
46 * @param request Servlet request.
47 * @param servletContext Servlet context.
48 * @throws DefinitionsFactoryException An error occur while getting
49 * definition.
50 * @throws NoSuchDefinitionException No definition found for specified name
51 * Implementation can throw more accurate exception as a subclass of this
52 * exception.
53 */
54 public ComponentDefinition getDefinition(String name, ServletRequest request, ServletContext servletContext)
55 throws NoSuchDefinitionException, DefinitionsFactoryException
56 {
57 return (ComponentDefinition)definitions.get(name);
58 }
59
60 /***
61 * Put definition in set.
62 * @param definition Definition to put.
63 */
64 public void putDefinition(ComponentDefinition definition)
65 {
66 definitions.put( definition.getName(), definition );
67 }
68
69 /***
70 * Constructor.
71 * Create a factory initialized with definitions from {@link XmlDefinitionsSet}.
72 * @param xmlDefinitions Resolved definition from XmlDefinitionSet.
73 * @throws NoSuchDefinitionException If an error occurs while resolving inheritance
74 */
75 public DefinitionsFactory(XmlDefinitionsSet xmlDefinitions)
76 throws NoSuchDefinitionException
77 {
78 definitions = new HashMap();
79
80
81 xmlDefinitions.resolveInheritances();
82
83
84 Iterator i = xmlDefinitions.getDefinitions().values().iterator();
85 while( i.hasNext() )
86 {
87 XmlDefinition xmlDefinition = (XmlDefinition)i.next();
88 putDefinition( new ComponentDefinition( xmlDefinition) );
89 }
90 }
91 /***
92 * Return String representation.
93 * @return String representation.
94 */
95 public String toString()
96 {
97 return definitions.toString();
98 }
99
100 }