KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateConfigDocument.UrlTemplateConfig;
21 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateDocument;
22 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefGroupDocument;
23 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefDocument;
24 import org.apache.beehive.netui.util.logging.Logger;
25 import org.apache.xmlbeans.XmlCursor;
26 import org.apache.xmlbeans.XmlException;
27
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 /**
36  * Methods for configuring and retrieving the URLTemplates object.
37  */

38 public class URLTemplatesFactory
39 {
40     private static final Logger _log = Logger.getInstance( DefaultURLTemplateFileParser.class );
41
42     /** Default value for path from the web app to the URL templates. */
43     public static final String JavaDoc DEFAULT_URL_TEMPLATE_CONFIG_FILE_PATH = "/WEB-INF/beehive-url-template-config.xml";
44
45     // Path to the URL templates.
46
private String JavaDoc _configFilePath = DEFAULT_URL_TEMPLATE_CONFIG_FILE_PATH;
47
48     // Parser to use on the URL template config file.
49
private URLTemplateFileParser _parser = new DefaultURLTemplateFileParser();
50
51     // The set of known tokens in a valid template.
52
private Collection JavaDoc _knownTokens = null;
53
54     // The set of required tokens in a valid template.
55
private Collection JavaDoc _requiredTokens = null;
56
57     /**
58      * Allow clients to set their own URL template config file name or path.
59      *
60      * @param configFilePath An absolute path from the web app root tothe URL template config file.
61      */

62     public void setConfigFilePath( String JavaDoc configFilePath )
63     {
64         if ( configFilePath == null )
65         {
66             throw new IllegalArgumentException JavaDoc( "Config file path cannot be null." );
67         }
68
69         _configFilePath = configFilePath;
70     }
71
72     /**
73      * Allow clients to set their own parser.
74      * <p/>
75      * The parser is used on the URL template config file,
76      * WEB-INF/url-template-config.xml. This can be used to support older
77      * file formats.
78      *
79      * @param parser The parser to use on the URL template config file.
80      */

81     public void setParser( URLTemplateFileParser parser )
82     {
83         if ( parser == null )
84         {
85             throw new IllegalArgumentException JavaDoc( "Parser cannot be null." );
86         }
87
88         _parser = parser;
89     }
90
91     /**
92      * Allow clients to define a set of known tokens for the
93      * template verification. Tokens are expected to be qualified
94      * in braces. E.g. {url:path}
95      * <p/>
96      * The template verification will ensure the known tokens in the
97      * URL template conforms to a valid format.
98      *
99      * @param knownTokens The set of known tokens for a valid template.
100      */

101     public void setKnownTokens( Collection JavaDoc knownTokens )
102     {
103         _knownTokens = knownTokens;
104     }
105
106     /**
107      * Allow clients to define a set of required tokens for the
108      * template verification. Tokens are expected to be qualified
109      * in braces. E.g. {url:path}
110      * <p/>
111      * The template verification will ensure the URL template conforms to
112      * a valid format for known tokens and contains the required tokens.
113      *
114      * @param requiredTokens The set of required tokens in a valid template.
115      */

116     public void setRequiredTokens( Collection JavaDoc requiredTokens )
117     {
118         _requiredTokens = requiredTokens;
119     }
120
121     /**
122      * Parses the URL template config file, WEB-INF/url-template-config.xml,
123      * and returns the templates.
124      *
125      * @return The URL templates found in the config file.
126      */

127     public URLTemplates getTemplates( ServletContext JavaDoc servletContext )
128     {
129         URLTemplates urlTemplates = new URLTemplates();
130         InputStream JavaDoc stream = null;
131
132         try
133         {
134             UrlTemplateConfig urlTemplateConfig = null;
135             stream = servletContext.getResourceAsStream( _configFilePath );
136             if ( stream != null )
137             {
138                 urlTemplateConfig = _parser.parse( stream );
139                 urlTemplates = getTemplatesFromConfig( urlTemplateConfig );
140             }
141             else
142             {
143                 // No descriptor
144
_log.info( "Running without URL template descriptor, " + _configFilePath );
145             }
146         }
147         catch ( XmlException xe )
148         {
149             // Bad descriptor
150
_log.error( "Malformed URL template descriptor in " + _configFilePath, xe );
151         }
152         catch ( IOException JavaDoc ioe )
153         {
154             // Bad descriptor
155
_log.error( "Problem parsing URL template descriptor in " + _configFilePath, ioe );
156         }
157         finally
158         {
159             // Close the stream
160
if ( stream != null )
161             {
162                 try
163                 {
164                     stream.close();
165                 }
166                 catch ( Exception JavaDoc ignore )
167                 {
168                 }
169             }
170         }
171
172         return urlTemplates;
173     }
174
175     /**
176      * Loads the templates from a parsed URL template config document.
177      *
178      * @param urlTemplateConfig the parsed XMLBean document for the URL template config.
179      * @return The URL templates found in the config document.
180      */

181     protected URLTemplates getTemplatesFromConfig( UrlTemplateConfig urlTemplateConfig )
182     {
183         URLTemplates urlTemplates = new URLTemplates();
184
185         // Load templates
186
UrlTemplateDocument.UrlTemplate[] templates = urlTemplateConfig.getUrlTemplateArray();
187         for ( int i = 0; i < templates.length; i++ )
188         {
189             String JavaDoc name = templates[i].getName().trim();
190             String JavaDoc value = templates[i].getValue().trim();
191             if ( value != null )
192             {
193                 value = value.trim();
194                 if ( _log.isDebugEnabled() )
195                 {
196                     _log.debug( "[URLTemplate] " + name + " = " + value );
197                 }
198                 URLTemplate urlTemplate = new URLTemplate( value, _knownTokens, _requiredTokens );
199                 urlTemplate.verify();
200                 urlTemplates.addTemplate( name, urlTemplate );
201             }
202         }
203
204         // Load template refs
205
UrlTemplateRefGroupDocument.UrlTemplateRefGroup[] templateRefGroups = urlTemplateConfig.getUrlTemplateRefGroupArray ();
206         for ( int i = 0; i < templateRefGroups.length; i++ )
207         {
208             HashMap JavaDoc refGroup = new HashMap JavaDoc();
209             String JavaDoc refGroupName = templateRefGroups[i].getName().trim();
210             UrlTemplateRefDocument.UrlTemplateRef[] templateRefs = templateRefGroups[i].getUrlTemplateRefArray();
211             for ( int j = 0; j < templateRefs.length; j++ )
212             {
213                 String JavaDoc key = templateRefs[j].getKey().toString().trim();
214                 String JavaDoc name = templateRefs[j].getTemplateName().trim();
215                 if ( _log.isDebugEnabled() )
216                 {
217                     _log.debug( "[" + refGroupName + " URLTemplate] " + key + " = " + name );
218                 }
219                 refGroup.put( key, name );
220             }
221             urlTemplates.addTemplateRefGroup( refGroupName, refGroup );
222         }
223
224         return urlTemplates;
225     }
226 }
227
Popular Tags