KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > skin > DefinitionCatalog


1 /*
2  * $Id: DefinitionCatalog.java 54929 2004-10-16 16:38:42Z germuska $
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 package org.apache.struts.webapp.tiles.skin;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.apache.struts.tiles.ComponentDefinition;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.DefinitionsUtil;
33 import org.apache.struts.tiles.FactoryNotFoundException;
34 import org.apache.struts.tiles.NoSuchDefinitionException;
35
36 /**
37  * A catalog of available definitions.
38  */

39 public class DefinitionCatalog
40 {
41       /** debug flag */
42     public static boolean debug = true;
43     /** Attribute carrying definition readable name */
44    public static final String JavaDoc LABEL_NAME_ATTRIBUTE = "skin.label";
45     /** Attribute carrying the list of definition names */
46    public static final String JavaDoc DEFINITION_LIST_ATTRIBUTE = "skin.list";
47
48    /**
49     * Map of skins, by their keys
50     */

51    private Map JavaDoc definitions = new HashMap JavaDoc();
52    /**
53     * Map of skins, by their keys
54     */

55    private ComponentDefinition defaultDefinition;
56
57    /**
58     * List of names
59     */

60    private List JavaDoc names = new ArrayList JavaDoc();
61
62    /**
63     * List of keys
64     */

65    private List JavaDoc keys = new ArrayList JavaDoc();
66
67    /**
68     * Constructor.
69     * Initialize catalog from definitions factory.
70     * @param HttpRequest request
71     * @param ServletContext context
72     * @throws FactoryNotFoundException, DefinitionsFactoryException
73     */

74    public DefinitionCatalog( String JavaDoc catalogName, HttpServletRequest JavaDoc request, ServletContext JavaDoc context)
75      throws FactoryNotFoundException, DefinitionsFactoryException
76    {
77      // Get definition containing list of definitions
78
ComponentDefinition catalogDef = DefinitionsUtil.getDefinition( catalogName, request, context);
79    if(debug)
80      System.out.println( "Got definition " + catalogDef );
81      // Get list of definition names
82
List JavaDoc list = (List JavaDoc)catalogDef.getAttribute( DEFINITION_LIST_ATTRIBUTE );
83    Iterator JavaDoc i = list.iterator();
84    while(i.hasNext() )
85      {
86      String JavaDoc name = (String JavaDoc)i.next();
87      System.out.println( "add " + name );
88      ComponentDefinition def = DefinitionsUtil.getDefinition(name, request, context);
89      if(def==null)
90        throw new NoSuchDefinitionException("Can't find definition '" + name + "'" );
91      add( name, def );
92      } // end loop
93
if(debug)
94      System.out.println( "Catalog initialized" );
95    }
96
97    /**
98     * Get definition identified by key.
99     * @param key
100     * @return Definition associated to key
101     */

102    public ComponentDefinition get(Object JavaDoc key)
103    {
104    if(key==null)
105      return getDefault();
106    return (ComponentDefinition)definitions.get(key);
107    }
108
109    /**
110     * Get definition identified by key.
111     * @param key
112     * @return Definition associated to key
113     */

114    public ComponentDefinition getDefault()
115    {
116    return defaultDefinition;
117    }
118
119    /**
120     * Return List of names of definitions presents in catalog.
121     * Names are user readable names. Returned list has the same order as list
122     * returned by getKeys.
123     * @return List
124     */

125    public List JavaDoc getNames()
126    {
127     return names;
128    }
129
130    /**
131     * Get list of keys of definitions present in catalog.
132     * A key is used to retrieve a skin from catalog.
133     * @return List
134     */

135    public List JavaDoc getKeys()
136    {
137     return keys;
138    }
139
140    /**
141     * Check if requested key is valid in catalog.
142     * Return null otherwise
143     * @return valid key or null
144     */

145    public String JavaDoc getKey( String JavaDoc key )
146    {
147    if( definitions.get(key) != null)
148     return key;
149
150    return null;
151    }
152
153    /**
154     * Add a skin definition
155     * @param definition
156     */

157    public void add(String JavaDoc key, ComponentDefinition definition)
158    {
159      // Intitialize default definition with first definition encountered
160
if( defaultDefinition == null )
161      {
162      defaultDefinition = definition;
163      }
164      // store definition
165
definitions.put( key , definition);
166    Object JavaDoc name = definition.getAttribute(LABEL_NAME_ATTRIBUTE);
167    if( name == null )
168      name = key;
169    names.add( name );
170    keys.add(key);
171    }
172 }
173
Popular Tags