KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: BasenameValueURIHandler.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 java.io.File JavaDoc;
32
33 import org.apache.tools.ant.Project;
34
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.helpers.Tk;
37 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
38
39 /**
40  * Value URI handler that is an inline variant of the standard Ant &lt;basename&gt;
41  * task. The general format of the URI is:
42  * <span class="src">$basename:path[?suffix]</span>. The path will be resolved relative
43  * to the uri's owning project if necessary and like the &lt;basename&gt; task and empty
44  * path string is interpreted as the project's base directory.
45  * <p>
46  * <b>Example Usage:</b><pre>
47  * &lt;parameter name="label" value="${$property:other-mainlibs.dir|$basename:}"/&gt;
48  *
49  * -- To Install --
50  * &lt;manageuris action="install"&gt;
51  * &lt;parameter name="basename"
52  * value="com.idaremedia.antx.valueuri.BasenameValueURIHandler"/&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  **/

62
63 public final class BasenameValueURIHandler extends ValueURIHandlerSkeleton
64 {
65     /**
66      * Initializes a new truefalse value uri handler.
67      **/

68     public BasenameValueURIHandler()
69     {
70         super();
71     }
72
73
74     /**
75      * Converts a Ant-isque boolean string into a normalized
76      * "<span class="src">true</span>" or "<span class="src">false</span>".
77      * If a custom pair of true/false strings specified as query parameters,
78      * one of these strings will be returned instead.
79      **/

80     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
81     {
82         final Project P = clnt.getProject();
83         String JavaDoc path = uriFragment;
84         String JavaDoc suffix = null;
85
86         int i = uriFragment.indexOf("?");
87         if (i>=0) {
88             path = uriFragment.substring(0,i++);
89             if (i<uriFragment.length()) {
90                 suffix = Tk.resolveString(P,uriFragment.substring(i),true);
91             }
92         }
93         path = Tk.resolveString(P,path,true);
94         return baseFrom(path,suffix,clnt);
95     }
96
97
98
99     /**
100      * Extracts the extensionless base name of given file.
101      * @param inpath path as passed in uri fragment
102      * @param suffix [optional] suffix to be stripped (if exists)
103      * @param clnt call controls (non-null)
104      * @return basename (never <i>null</i>)
105      **/

106     private String JavaDoc baseFrom(String JavaDoc inpath, String JavaDoc suffix, Requester clnt)
107     {
108         File JavaDoc file;
109         if (inpath.length()==0) {
110             file = clnt.getProject().getBaseDir();
111             if (file==null) {
112                 return null;
113             }
114         } else {
115             file = clnt.getProject().resolveFile(inpath);
116         }
117         /* --
118          * This is a duplicate of the Ant basename task's core
119          * functionality to ensure we do not stray from that impl.
120          */

121         String JavaDoc filename = file.getName();
122         if (!Tk.isWhitespace(suffix) && filename.endsWith(suffix)) {
123             int pos = filename.length() - suffix.length();
124             if (pos>0 && suffix.charAt(0)!= '.'
125                 && filename.charAt(pos-1)=='.') {
126                 pos--;
127             }
128             filename = filename.substring(0,pos);
129         }
130         return filename;
131     }
132 }
133
134 /* end-of-BasenameValueURIHandler.java */
Popular Tags