View Javadoc

1   /*
2    * $Id: DefinitionsFactory.java 421151 2006-07-12 06:07:14Z wsmoak $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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        // First, resolve inheritance
81      xmlDefinitions.resolveInheritances();
82  
83        // Walk thru xml set and copy each definitions.
84      Iterator i = xmlDefinitions.getDefinitions().values().iterator();
85      while( i.hasNext() )
86        {
87        XmlDefinition xmlDefinition = (XmlDefinition)i.next();
88          putDefinition( new ComponentDefinition( xmlDefinition) );
89        }  // end loop
90     }
91      /***
92       * Return String representation.
93       * @return String representation.
94       */
95    public String toString()
96      {
97      return definitions.toString();
98      }
99  
100 }