KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > init > MessageValueURIHandler


1 /**
2  * $Id: MessageValueURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.init;
30
31 import com.idaremedia.apis.UIStringManager;
32
33 import com.idaremedia.antx.Iteration;
34 import com.idaremedia.antx.UISMContext;
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.helpers.Strings;
37 import com.idaremedia.antx.helpers.Tk;
38 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
39
40 /**
41  * Simple value URI handler that returns the named message from an AntX
42  * resource bundle. The messages resource bundle should be activated before any URI
43  * refers to it (see <span class="src">&lt;managebundles&gt;</span> or
44  * <span class="src">&lt;overlay-msgs&gt;</span>). This handler is not installed
45  * automatically by the AntX runtime, you must install it explicitly like the example
46  * below. Message value URIs are often linked to either the <span class="src">$str:</span>
47  * or the <span class="src">$message:</span> scheme.
48  * <p/>
49  * You can specify up to two message template arguments (for locations {2} and
50  * {3}) by using a simple URI form like: <span class="src">$str:strid[?arg2[,,arg3]]</span>.
51  * Everything after the <span class="src">strid</span> is optional.
52  * <p/>
53  * To allow you to specify the two optional message arguments from property values,
54  * the message URI handler understands the alternate "<span class="src">@(propertyname)</span>"
55  * format in its fragment's query portion; see example below. Note that the message arguments
56  * are separated by two commas like "<span class="src">,,</span>" <em>not</em> an
57  * ampersand. This allows XML-based scripts to define arguments without the need to
58  * encode the ampersand reserved for use with XML entities.
59  * <p/>
60  * <b>Example Usage:</b><pre>
61  * &lt;macrodef name="api-docs"&gt;
62  * &lt;attribute name="id"
63  * default="${<b>$str:</b>label.id?@(id)}"/&gt;
64  * &lt;attribute name="label"
65  * default="${<b>$str:</b>label.main?@(id),,@(rev)}"/&gt;
66  * ...
67  *
68  * -- To Install --
69  * &lt;manageuris action="install"&gt;
70  * &lt;parameter name="str"
71  * value="com.idaremedia.antx.init.MessageValueURIHandler"/&gt;
72  * &lt;parameter name="message"
73  * value="com.idaremedia.antx.init.MessageValueURIHandler"/&gt;
74  * &lt;/manageuris&gt;
75  * </pre>
76  *
77  * @since JWare/AntX 0.5
78  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
79  * @version 0.5
80  * @.safety multiple
81  * @.group api,helper
82  **/

83
84 public final class MessageValueURIHandler extends ValueURIHandlerSkeleton
85 {
86     /**
87      * Initializes a new message value URI handler.
88      **/

89     public MessageValueURIHandler()
90     {
91     }
92  
93
94     /**
95      * Tries to load+form the named message string from the nearest UI
96      * string manager. If no string manager is installed, this method
97      * will use the default string manager.
98      **/

99     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
100     {
101         String JavaDoc msgid = uriFragment;
102         
103         String JavaDoc arg0= clnt.getName();
104         if (arg0==null) {
105             arg0= Strings.UNDEFINED;
106         }
107
108         boolean shorten= Iteration.defaultdefaults().isShortLocationsEnabled();
109         String JavaDoc arg1= Tk.shortStringFrom(shorten, clnt.getLocation());
110         
111         Object JavaDoc[] args = new Object JavaDoc[]{arg0,arg1};
112
113         int i = uriFragment.lastIndexOf("?");
114         if (i>0){
115             msgid = uriFragment.substring(0,i++);
116             if (i<uriFragment.length()) {
117                 String JavaDoc arg2=null,arg3=null;
118                 arg2 = uriFragment.substring(i);
119                 i = arg2.indexOf(",,");
120                 if (i>0) {
121                     if ((i+2)<arg2.length()) {
122                         arg3 = arg2.substring(i+2);
123                     }
124                     arg2 = arg2.substring(0,i);
125                 }
126                 arg2 = Tk.resolveString(clnt.getProject(),arg2,true);
127                 if (arg3!=null) {
128                     arg3 = Tk.resolveString(clnt.getProject(),arg3,true);
129                     args = new Object JavaDoc[]{arg0,arg1,arg2,arg3};
130                 } else {
131                     args = new Object JavaDoc[]{arg0,arg1,arg2};
132                 }
133             }
134             
135         }
136         UIStringManager sm = UISMContext.getStringManagerNoNull();
137         return sm.mget(msgid,args,msgid);
138     }
139 }
140
141 /* end-of-MessageValueURIHandler.java */
Popular Tags