KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > ShortenStringURIHandler


1 /**
2  * $Id: ShortenStringURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 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.valueuri;
30
31 import org.apache.tools.ant.Project;
32
33 import com.idaremedia.antx.apis.Requester;
34 import com.idaremedia.antx.helpers.Tk;
35 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
36
37 /**
38  * Value URI handler that tries to shorten a long string in a display friendly manner.
39  * This handler is very useful for shortening long file paths into something easily
40  * readable in build archive logs. The general form of the URI:
41  * <span class="src"><nobr>$shorten:string[?[maxlength][,,[left|right][,,ellipses string]]]</nobr>
42  * </span>.
43  * <p>
44  * <b>Example Usage:</b><pre>
45  * &lt;emit msgid="msg.generating.apidocs" msgarg1="<b>${$shorten:</b>@{html}}"/&gt;
46  *
47  * -- To Install --
48  * &lt;manageuris action="install"&gt;
49  * &lt;parameter name="shorten"
50  * value="com.idaremedia.antx.valueuri.ShortenStringURIHandler"/&gt;
51  * &lt;/manageuris&gt;
52  * </pre>
53  *
54  * @since JWare/AntX 0.5
55  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
56  * @version 0.5
57  * @.safety multiple
58  * @.group api,helper
59  * @see BasenameValueURIHandler
60  **/

61
62 public final class ShortenStringURIHandler extends ValueURIHandlerSkeleton
63 {
64     static final int LEFT=0;
65     static final int RIGHT=LEFT+1;
66     private static final String JavaDoc DDD="...";
67
68     /**
69      * Default trim length for shortened strings ({@value}).
70      */

71     public static final int MAXLEN= 41;
72
73     /**
74      * Default minimum length for shortened strings (<i>3</i>).
75      */

76     public static final int MINLEN= DDD.length();
77
78
79     /**
80      * Initializes a new shorten string value uri handler.
81      **/

82     public ShortenStringURIHandler()
83     {
84         super();
85     }
86
87
88     /**
89      * Tries to shorten the incoming string as requested. If string does
90      * not need shortening it is returned unchanged (property references
91      * are resolved).
92      **/

93     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
94     {
95         final Project P = clnt.getProject();
96         int maxlength = MAXLEN;
97         String JavaDoc ellipses= DDD;
98         int dropped = LEFT;
99         String JavaDoc longstring = uriFragment;
100         final int urilen= uriFragment.length();
101
102         int i = longstring.indexOf("?");
103         if (i>=0) {
104             longstring = longstring.substring(0,i++);
105             if (i<urilen) {
106                 String JavaDoc s;
107                 int j = uriFragment.indexOf(",,",i);
108                 if (j>=0) {
109                     s = Tk.resolveString(P,uriFragment.substring(i,j),true);
110                     maxlength = Tk.integerFrom(s,MAXLEN);
111
112                     i = uriFragment.indexOf(",,",j+2);
113                     if (i<0) {
114                         s = uriFragment.substring(j+2);
115                         dropped = leftOrRight(s,LEFT,P);
116                     } else {
117                         s = uriFragment.substring(j+2,i);
118                         dropped = leftOrRight(s,LEFT,P);
119                         i += 2;
120                         if (i<urilen) {
121                             s = uriFragment.substring(i);
122                             ellipses = Tk.resolveString(P,s,true);
123                         }
124                     }
125                 } else {
126                     s = Tk.resolveString(P,uriFragment.substring(i),true);
127                     maxlength = Tk.integerFrom(s,MAXLEN);
128                 }
129             }
130         }
131         if (maxlength<MINLEN) {
132             maxlength=MINLEN;
133         }
134         longstring = Tk.resolveString(P,longstring,true);
135         final int strlen = longstring.length();
136         if (strlen>maxlength) {
137             switch (dropped) {
138                 case LEFT: {
139                     int from = strlen-maxlength+ellipses.length();
140                     if (from>=strlen) {
141                         from = strlen-maxlength+DDD.length();
142                         ellipses = DDD;
143                     }
144                     longstring= ellipses+longstring.substring(from);
145                     break;
146                 }
147                 default: {
148                     int to = maxlength-ellipses.length();
149                     if (to<=0) {
150                         to = maxlength-DDD.length();
151                         ellipses = DDD;
152                     }
153                     longstring = longstring.substring(0,to)+ellipses;
154                     break;
155                 }
156             }
157         }
158         return longstring;
159     }
160
161
162     static int leftOrRight(String JavaDoc s, int dflt, Project P)
163     {
164         s = Tk.resolveString(P,s,true);
165         s = s.toLowerCase();
166
167         if ("left".equals(s)) {
168             return LEFT;
169         }
170         if ("right".equals(s)) {
171             return RIGHT;
172         }
173         return dflt;
174     }
175 }
176
177
178 /* end-of-ShortenStringURIHandler.java */
Popular Tags