KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > info > DefaultsValueURIHandler


1 /**
2  * $Id: DefaultsValueURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-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.info;
30
31 import org.apache.tools.ant.Project;
32
33 import com.idaremedia.antx.Defaults;
34 import com.idaremedia.antx.Iteration;
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.helpers.Tk;
37 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
38
39 /**
40  * Simple value URI handler that returns default settings for various AntX fixture
41  * elements. In particular the current Iteration's {@linkplain Defaults Defaults}
42  * are checked. This handler is not installed automatically by the AntX runtime,
43  * you must install it explicitly like the example below. Default value URIs are often
44  * linked to either the <span class="src">$default:</span> or the
45  * <span class="src">$df:</span> scheme.
46  * <p/>
47  * If the named default is not one of the known builtin default attributes, this
48  * handler will look for a project/fixture property named like:
49  * <span class="src">defaults.<i>default-name</i></span> where <i>default-name</i>
50  * is the fragment that comes after the <span class="src">$default:</span> prefix
51  * in the value URI. The looked for property's name is <em>always</em> lowercased; for
52  * example, the URI <span class="src">$default:CopyLeft</span> will be matched against
53  * a project property <span class="src">defaults.copyleft</span>.
54  * <p/>
55  * To allow you to specify inlined default values for missing property-based defaults,
56  * this handler understands the alternate "<span class="src">@(propertyname)</span>"
57  * format in its URI fragment query portion (the bits after the "&#63;"); see example
58  * below. Note that inlined default values are limited to property-based defaults; they
59  * are never used for builtin fixture defaults. When an inlined default value is
60  * used, it is used as specified (no lowercasing is applied).
61  * <p/>
62  * <b>Example Usage:</b><pre>
63  * &lt;assign var="_loop.failquick" value="${<b>$default:</b>haltiferror}"/&gt;
64  * &lt;property name="finetrace" value="${<b>$default:</b>assertions}"/&gt;
65  * &lt;attribute name="license" default="${<b>$default:</b>license?LGPL}"/&gt;
66  * &lt;parameter name="license" value="${<b>$default:</b>license?@(project.license)}"/&gt;
67  *
68  * -- To Install --
69  * &lt;manageuris action="install"&gt;
70  * &lt;parameter name="default"
71  * value="com.idaremedia.antx.valueuri.info.DefaultsValueURIHandler"/&gt;
72  * &lt;parameter name="df"
73  * value="com.idaremedia.antx.valueuri.info.DefaultsValueURIHandler"/&gt;
74  * &lt;/manageuris&gt;
75  * </pre>
76  *
77  * @since JWare/AntX 0.5
78  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
79  * @version 0.5
80  * @.safety multiple
81  * @.group api,helper
82  * @see DefaultsPropertyNameURIHandler
83  **/

84
85 public class DefaultsValueURIHandler extends ValueURIHandlerSkeleton
86 {
87     /**
88      * Initializes a new defaults value uri handler.
89      **/

90     public DefaultsValueURIHandler()
91     {
92     }
93
94
95
96     /**
97      * Returns the embedded default value from the full uri if there.
98      * Otherwise returns <i>null</i>.
99      */

100     public String JavaDoc getDefaultValue(String JavaDoc fullUri, Requester clnt)
101     {
102         if (fullUri!=null && fullUri.length()>4) {
103             int i;
104             if (fullUri.charAt(0)=='$' && (i=fullUri.indexOf(":"))>0) {
105                 String JavaDoc fallback = null;
106                 i= fullUri.lastIndexOf("?");
107                 if (i>0) {
108                     if ((i+1)<fullUri.length()) {
109                         fallback = fullUri.substring(i+1);
110                         fallback = Tk.resolveString(clnt.getProject(),fallback,true);
111                     } else {
112                         fallback = "";
113                     }
114                 }
115                 return fallback;
116             }
117         }
118         return null;
119     }
120
121
122
123     /**
124      * Returns the associated default setting if possible. Will
125      * return <i>null</i> if default name unrecognized.
126      **/

127     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
128     {
129         Defaults dflt = Iteration.defaultdefaults();
130         String JavaDoc fragment = Tk.lowercaseFrom(uriFragment);
131
132         // Ask the Iteration (or subclass) first!
133
String JavaDoc value = dflt.valueFrom(fragment,clnt);
134         if (value!=null) {
135             return value;
136         }
137
138         // Determine if an inlined default was specified. We should
139
// not assume the us-locale string's length is same as the original
140
// so we renormalize the fragment minus the default if needed.
141

142         final Project project = clnt.getProject();
143         String JavaDoc fallback = null;
144         int i= uriFragment.lastIndexOf("?");
145         if (i>0) {
146             if ((i+1)<uriFragment.length()) {
147                 fallback = uriFragment.substring(i+1);//NB:case used as passed-in!
148
fallback = Tk.resolveString(project,fallback,true);
149             } else {
150                 fallback = "";
151             }
152             fragment = Tk.lowercaseFrom(uriFragment.substring(0,i));
153         }
154         String JavaDoc property = dflt.defaultPropertiesPrefix(project)+fragment;
155         value = Tk.getTheProperty(project,property);
156
157         return value==null ? fallback : value;
158     }
159 }
160
161 /* end-of-DefaultsValueURIHandler.java */
Popular Tags