KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > core > urls > DefaultTemplatedURLFormatter


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.urls;
19
20 import org.apache.beehive.netui.core.urltemplates.URLTemplate;
21 import org.apache.beehive.netui.core.urltemplates.URLTemplateDescriptor;
22 import org.apache.beehive.netui.util.config.bean.UrlConfig;
23 import org.apache.beehive.netui.util.config.ConfigUtil;
24
25 import javax.servlet.ServletRequest JavaDoc;
26
27
28 /**
29  * Default implementation of TemplatedURLFormatter for formatting URLs
30  * based on templates from a URL template config file.
31  */

32 public class DefaultTemplatedURLFormatter implements TemplatedURLFormatter
33 {
34     private static final String JavaDoc DEFAULT_TEMPLATE_REF = "default-url-templates";
35
36     /**
37      * Format the given URL using a URL template, if defined in a URL
38      * template config file. The {@link URIContext}
39      * encapsulates some additional data needed to write out the string form.
40      * E.g. It defines if the "&" entity or the
41      * '&' character should be used to separate quary parameters.
42      *
43      * @param request the current ServletRequest.
44      * @param uri the MutableURI to be formatted into a String.
45      * @param key key for the URL template type to use for formatting the URI
46      * @param uriContext data required to write out the string form.
47      * @return the URL as a <code>String</code>
48      */

49     public String JavaDoc getTemplatedURL( ServletRequest JavaDoc request, MutableURI uri, String JavaDoc key, URIContext uriContext )
50     {
51         // Look for the template config and get the right template.
52
// If it is found, apply the value to the template.
53
String JavaDoc result = null;
54         String JavaDoc templateName = URLTemplateDescriptor.getInstance().getURLTemplateRef( DEFAULT_TEMPLATE_REF, key );
55
56         if ( templateName != null )
57         {
58             URLTemplate template = URLTemplateDescriptor.getInstance().getURLTemplate( templateName );
59             result = formatURIWithTemplate( request, uri, uriContext, template );
60         }
61         else
62         {
63             // no template found, just return the uri as a String...
64
result = uri.getURIString( uriContext );
65         }
66
67         return result;
68     }
69
70     private String JavaDoc formatURIWithTemplate( ServletRequest JavaDoc request, MutableURI uri,
71                                           URIContext uriContext, URLTemplate template )
72     {
73         String JavaDoc scheme = uri.getScheme();
74         String JavaDoc host = uri.getHost();
75         int port = uri.getPort();
76
77         if ( scheme == null || scheme.length() == 0 ) { scheme = request.getScheme(); }
78
79         if ( host == null || host.length() == 0 ) { host = request.getServerName(); }
80
81         if ( port < 0 ) { port = request.getServerPort(); }
82
83         template.substitute( URLTemplateDescriptor.SCHEME_TOKEN, scheme );
84         template.substitute( URLTemplateDescriptor.DOMAIN_TOKEN, host );
85         template.substitute( URLTemplateDescriptor.PORT_TOKEN, port );
86         template.substitute( URLTemplateDescriptor.PATH_TOKEN, uri.getPath() );
87
88         String JavaDoc query = null;
89         query = uri.getQuery( uriContext );
90
91         if ( query == null ) {
92             query = "";
93         }
94
95         template.substitute( URLTemplateDescriptor.QUERY_STRING_TOKEN, query );
96
97         String JavaDoc fragment = uri.getFragment();
98
99         if ( fragment == null ) { fragment = ""; }
100
101         template.substitute( URLTemplateDescriptor.FRAGMENT_TOKEN, fragment );
102
103         return template.toString();
104     }
105 }
106
Popular Tags