KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > core > urltemplates > URLTemplates


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.core.urltemplates;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * The key class to get information from url-template-config.
25  */

26 public class URLTemplates
27 {
28     private HashMap JavaDoc/*< String, URLTemplate >*/ _templates = new HashMap JavaDoc/*< String, URLTemplate >*/();
29     private HashMap JavaDoc/*< String, Map< String, String > >*/ _templateRefGroups =
30             new HashMap JavaDoc/*< String, Map< String, String > >*/();
31
32     /**
33      * Add a template from url-template-config by name.
34      *
35      * @param templateName the name of the template.
36      * @param template the template to add.
37      */

38     public void addTemplate( String JavaDoc templateName, URLTemplate template )
39     {
40         if ( templateName == null || templateName.length() == 0 )
41         {
42             throw new IllegalArgumentException JavaDoc( "Template name cannot be null or empty." );
43         }
44
45         if ( template == null )
46         {
47             throw new IllegalArgumentException JavaDoc( "URLTemplate cannot be null." );
48         }
49
50         _templates.put( templateName, template );
51     }
52
53     /**
54      * Retrieve a template from url-template-config by name.
55      * Always returns a copy of a URLTemplate with the same
56      * parsed template data but its own cleared set of
57      * token values for the substitue() methods.
58      * This allows multiple client requests access to
59      * the same parsed template structure, without requiring
60      * it to be parsed for each request.
61      *
62      * @param templateName the name of the template.
63      * @return a URLTemplate copy with its own empty map for storing
64      * token replacement values.
65      */

66     public URLTemplate getTemplate( String JavaDoc templateName )
67     {
68         URLTemplate template = ( URLTemplate ) _templates.get( templateName );
69         if ( template == null ) return null;
70
71         return new URLTemplate( template );
72     }
73
74     /**
75      * Add a template reference group from url-template-config by name.
76      *
77      * @param refGroupName the name of the template reference group.
78      * @param templateRefGroup the template reference group.
79      */

80     public void addTemplateRefGroup( String JavaDoc refGroupName, Map JavaDoc/*< String, String >*/ templateRefGroup )
81     {
82         if ( refGroupName == null || refGroupName.length() == 0 )
83         {
84             throw new IllegalArgumentException JavaDoc( "Template Reference Group name cannot be null or empty." );
85         }
86
87         if ( templateRefGroup == null || templateRefGroup.size() == 0 )
88         {
89             throw new IllegalArgumentException JavaDoc( "Template Reference Group cannot be null or empty." );
90         }
91
92         _templateRefGroups.put( refGroupName, templateRefGroup );
93     }
94
95     /**
96      * Retrieve a template name from a reference group in url-template-config.
97      *
98      * @param refGroupName the name of the template reference group.
99      * @param key the key to the particular template reference in the group.
100      * @return a template name from the reference group.
101      */

102     public String JavaDoc getTemplateNameByRef( String JavaDoc refGroupName, String JavaDoc key )
103     {
104         String JavaDoc templateName = null;
105         Map JavaDoc/*< String, String >*/ templateRefGroup = ( Map JavaDoc ) _templateRefGroups.get( refGroupName );
106         if ( templateRefGroup != null )
107         {
108             templateName = ( String JavaDoc ) templateRefGroup.get( key );
109         }
110
111         return templateName;
112     }
113 }
114
Popular Tags