| Line | Hits | Source |
|---|---|---|
| 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><url></code> tag. | |
| 51 | * | |
| 52 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 53 | */ | |
| 54 | 7 | public 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. */ | |
| 63 | 2 | private static final String[] OPTIONS_ARRAY = { LANGUAGE, COUNTRY }; |
| 64 | ||
| 65 | /** Set of i18n options. */ | |
| 66 | 1 | 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() { | |
| 80 | 1 | super.cleanup(); |
| 81 | 1 | this.value = null; |
| 82 | 1 | this.i18n = null; |
| 83 | 1 | } |
| 84 | ||
| 85 | /** | |
| 86 | * The url value. | |
| 87 | * | |
| 88 | * @param value The url value. | |
| 89 | */ | |
| 90 | public void setValue(String value) { | |
| 91 | 5 | this.value = value; |
| 92 | 5 | } |
| 93 | ||
| 94 | /** | |
| 95 | * The url value. | |
| 96 | * | |
| 97 | * @return The url value. | |
| 98 | */ | |
| 99 | public String getValue() { | |
| 100 | 5 | 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 { | |
| 110 | 3 | if (!OPTIONS.contains(i18n)) { |
| 111 | 0 | Object[] args = { i18n }; |
| 112 | 0 | throw new TagException(Ji18nMessages.MESSAGE_ILLEGAL_I18N_OPTION, args); |
| 113 | } | |
| 114 | 3 | this.i18n = i18n; |
| 115 | 3 | } |
| 116 | ||
| 117 | /** | |
| 118 | * Whether localize by language or country. | |
| 119 | * | |
| 120 | * @return Whether localize by language or country. | |
| 121 | */ | |
| 122 | public String getI18n() { | |
| 123 | 1 | 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 { | |
| 133 | 3 | printLocalizedURL(); |
| 134 | 3 | 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 { | |
| 143 | 3 | JspWriter out = pageContext.getOut(); |
| 144 | 3 | String src = getValue(); |
| 145 | ||
| 146 | 3 | if (src.startsWith("/")) { |
| 147 | 3 | out.print(getContextPath()); |
| 148 | } | |
| 149 | ||
| 150 | 3 | if ((this.i18n != null) && (src.lastIndexOf('/') < src.lastIndexOf('.'))) { |
| 151 | 2 | Locale locale = UserLocale.getLocale(); |
| 152 | 2 | int lastPeriod = src.lastIndexOf('.'); |
| 153 | ||
| 154 | 2 | out.print(src.substring(0, lastPeriod)); // Up to '.' |
| 155 | 2 | out.print('_'); |
| 156 | 2 | out.print(locale.getLanguage()); |
| 157 | 2 | if (useCountry(locale)) { |
| 158 | 1 | out.print('_'); |
| 159 | 1 | out.print(locale.getCountry()); |
| 160 | } | |
| 161 | 2 | out.print(src.substring(lastPeriod)); // From '.' onward |
| 162 | } else { | |
| 163 | 1 | out.print(src); |
| 164 | } | |
| 165 | 3 | } |
| 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) { | |
| 174 | 2 | if ((this.i18n == null) || (!this.i18n.equals(COUNTRY))) { |
| 175 | 1 | return false; |
| 176 | } | |
| 177 | 1 | 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() { | |
| 186 | 3 | HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); |
| 187 | 3 | return request.getContextPath(); |
| 188 | } | |
| 189 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |