Coverage details for org.chwf.taglib.ji18n.UrlTag

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.taglib.ji18n;
34  
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.HashSet;
38 import java.util.Locale;
39 import java.util.Set;
40  
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.jsp.JspException;
43 import javax.servlet.jsp.JspWriter;
44  
45 import org.chwf.i18n.UserLocale;
46 import org.chwf.taglib.base.LifeCycleTagSupport;
47 import org.chwf.taglib.base.TagException;
48  
49 /**
50  * Tag handler for the <code>&lt;url&gt;</code> tag.
51  *
52  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
53  */
547public class UrlTag extends LifeCycleTagSupport {
55  
56   /** Value for i18n attribute to localize by language. */
57   public static final String LANGUAGE = "LANGUAGE";
58  
59   /** Value for i18n attribute to localize by country. */
60   public static final String COUNTRY = "COUNTRY";
61  
62   /** Array of i18n options. */
632  private static final String[] OPTIONS_ARRAY = { LANGUAGE, COUNTRY };
64   
65   /** Set of i18n options. */
661  private static final Set OPTIONS = new HashSet(Arrays.asList(OPTIONS_ARRAY));
67  
68   /** The url value. */
69   private String value;
70  
71   /** Whether to include the country. */
72   private String i18n;
73  
74   /**
75    * Clean up data for tag handler reuse and pooling. Should be overridden in
76    * tag handler. Subclasses should always invoke their superclass's cleanup
77    * method: <code>super.cleanup()</code>
78    */
79   public void cleanup() {
801    super.cleanup();
811    this.value = null;
821    this.i18n = null;
831  }
84  
85   /**
86    * The url value.
87    *
88    * @param value The url value.
89    */
90   public void setValue(String value) {
915    this.value = value;
925  }
93  
94   /**
95    * The url value.
96    *
97    * @return The url value.
98    */
99   public String getValue() {
1005    return this.value;
101   }
102  
103   /**
104    * Whether localize by language or country.
105    *
106    * @param i18n Whether localize by language or country.
107    * @throws JspException If attribute is assigned an illegal value.
108    */
109   public void setI18n(String i18n) throws JspException {
1103    if (!OPTIONS.contains(i18n)) {
1110      Object[] args = { i18n };
1120      throw new TagException(Ji18nMessages.MESSAGE_ILLEGAL_I18N_OPTION, args);
113     }
1143    this.i18n = i18n;
1153  }
116   
117   /**
118    * Whether localize by language or country.
119    *
120    * @return Whether localize by language or country.
121    */
122   public String getI18n() {
1231    return i18n;
124   }
125  
126   /**
127    * Wraps the (trimmed) tag body with the defined element.
128    *
129    * @return EVAL_PAGE
130    * @throws IOException For write errors.
131    */
132   public int doEnd() throws IOException {
1333    printLocalizedURL();
1343    return EVAL_PAGE;
135   }
136  
137   /**
138    * Print the localicalize URL.
139    *
140    * @throws IOException For I/O errors.
141    */
142   private void printLocalizedURL() throws IOException {
1433    JspWriter out = pageContext.getOut();
1443    String src = getValue();
145  
1463    if (src.startsWith("/")) {
1473      out.print(getContextPath());
148     }
149  
1503    if ((this.i18n != null) && (src.lastIndexOf('/') < src.lastIndexOf('.'))) {
1512      Locale locale = UserLocale.getLocale();
1522      int lastPeriod = src.lastIndexOf('.');
153  
1542      out.print(src.substring(0, lastPeriod)); // Up to '.'
1552      out.print('_');
1562      out.print(locale.getLanguage());
1572      if (useCountry(locale)) {
1581        out.print('_');
1591        out.print(locale.getCountry());
160       }
1612      out.print(src.substring(lastPeriod)); // From '.' onward
162     } else {
1631      out.print(src);
164     }
1653  }
166  
167   /**
168    * Returns true if locale has a country.
169    *
170    * @param locale The locale.
171    * @return true if locale has a country.
172    */
173   private boolean useCountry(Locale locale) {
1742    if ((this.i18n == null) || (!this.i18n.equals(COUNTRY))) {
1751      return false;
176     }
1771    return (locale.getCountry() != null) && (!locale.getCountry().equals(""));
178   }
179  
180   /**
181    * Get the context path.
182    *
183    * @return The context path.
184    */
185   private String getContextPath() {
1863    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
1873    return request.getContextPath();
188   }
189 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.