KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: TruncateStringURIHandler.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 will truncate a long string to some prescribed max length.
39  * The general form of the URI:
40  * <span class="src"><nobr>$truncate:string[?[maxlength][,,[left|right]]]</nobr>
41  * </span>.
42  * <p>
43  * <b>Example Usage:</b><pre>
44  * &lt;foreach i="file" files="thefiles"&gt;
45  * &lt;property name="next" value="${$basename:@(file)|<b>$truncate:?21</b>|$lowercase:}.dat"/&gt;
46  * &lt;emit msgid="progress.next" msgarg1="${next}"/&gt;
47  * ...
48  *
49  * -- To Install --
50  * &lt;manageuris action="install"&gt;
51  * &lt;parameter name="truncate"
52  * value="com.idaremedia.antx.valueuri.TruncateStringURIHandler"/&gt;
53  * &lt;/manageuris&gt;
54  * </pre>
55  *
56  * @since JWare/AntX 0.5
57  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
58  * @version 0.5
59  * @.safety multiple
60  * @.group api,helper
61  * @see ShortenStringURIHandler
62  **/

63
64 public final class TruncateStringURIHandler extends ValueURIHandlerSkeleton
65 {
66
67     /**
68      * Default max threshold for truncated strings ({@value}).
69      */

70     public static final int MAXLEN= ShortenStringURIHandler.MAXLEN;
71
72
73     /**
74      * Default minimum length for truncated strings (<i>1</i>).
75      */

76     public static final int MINLEN= 1;
77
78
79     /**
80      * Tries to truncate the incoming string as requested. If string does
81      * not need shortening it is returned unchanged (property references
82      * are resolved).
83      **/

84     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
85     {
86         final Project P = clnt.getProject();
87         int maxlength = MAXLEN;
88         int dropped = ShortenStringURIHandler.RIGHT;
89         String JavaDoc longstring = uriFragment;
90
91         int i = longstring.indexOf("?");
92         if (i>=0) {
93             longstring = longstring.substring(0,i++);
94             if (i<uriFragment.length()) {
95                 String JavaDoc s;
96                 int j = uriFragment.indexOf(",,",i);
97                 if (j>=0) {
98                     s = Tk.resolveString(P,uriFragment.substring(i,j),true);
99                     maxlength = Tk.integerFrom(s,MAXLEN);
100                     s = uriFragment.substring(j+2);
101                     dropped = ShortenStringURIHandler.leftOrRight(s,dropped,P);
102                 } else {
103                     s = Tk.resolveString(P,uriFragment.substring(i),true);
104                     maxlength = Tk.integerFrom(s,MAXLEN);
105                 }
106             }
107         }
108
109         if (maxlength<MINLEN) {
110             maxlength=MINLEN;
111         }
112         longstring = Tk.resolveString(P,longstring,true);
113         final int strlen = longstring.length();
114         if (strlen>maxlength) {
115             switch (dropped) {
116                 case ShortenStringURIHandler.LEFT: {
117                     int from = strlen-maxlength;
118                     longstring= longstring.substring(from);
119                     break;
120                 }
121                 default: {
122                     longstring = longstring.substring(0,maxlength);
123                     break;
124                 }
125             }
126         }
127         return longstring;
128     }
129 }
130
131 /* end-of-TruncateStringURIHandler.java */
Popular Tags