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