001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package clime.messadmin.taglib.core;
018:
019: //package org.apache.taglibs.standard.tag.common.core;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.OutputStreamWriter;
024: import java.text.DateFormat;
025: import java.util.Enumeration;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.jsp.JspException;
029: import javax.servlet.jsp.PageContext;
030:
031: /**
032: * <p>Utilities in support of tag-handler classes.</p>
033: *
034: * @author Jan Luehe
035: */
036: public class Util {
037:
038: private static final String REQUEST = "request";//$NON-NLS-1$
039: private static final String SESSION = "session";//$NON-NLS-1$
040: private static final String APPLICATION = "application";//$NON-NLS-1$
041: private static final String DEFAULT = "default";//$NON-NLS-1$
042: private static final String SHORT = "short";//$NON-NLS-1$
043: private static final String MEDIUM = "medium";//$NON-NLS-1$
044: private static final String LONG = "long";//$NON-NLS-1$
045: private static final String FULL = "full";//$NON-NLS-1$
046:
047: /*
048: * Converts the given string description of a scope to the corresponding
049: * PageContext constant.
050: *
051: * The validity of the given scope has already been checked by the
052: * appropriate TLV.
053: *
054: * @param scope String description of scope
055: *
056: * @return PageContext constant corresponding to given scope description
057: */
058: public static int getScope(String scope) {
059: int ret = PageContext.PAGE_SCOPE; // default
060:
061: if (REQUEST.equalsIgnoreCase(scope))
062: ret = PageContext.REQUEST_SCOPE;
063: else if (SESSION.equalsIgnoreCase(scope))
064: ret = PageContext.SESSION_SCOPE;
065: else if (APPLICATION.equalsIgnoreCase(scope))
066: ret = PageContext.APPLICATION_SCOPE;
067:
068: return ret;
069: }
070:
071: /*
072: * Converts the given string description of a formatting style for
073: * dates and times to the corresponding java.util.DateFormat constant.
074: *
075: * @param style String description of formatting style for dates and times
076: * @param errCode Error code to throw if given style is invalid
077: *
078: * @return java.util.DateFormat constant corresponding to given style
079: *
080: * @throws JspException if the given style is invalid
081: */
082: public static int getStyle(String style, String error)
083: throws JspException {
084: int ret = DateFormat.DEFAULT;
085:
086: if (style != null) {
087: if (DEFAULT.equalsIgnoreCase(style)) {
088: ret = DateFormat.DEFAULT;
089: } else if (SHORT.equalsIgnoreCase(style)) {
090: ret = DateFormat.SHORT;
091: } else if (MEDIUM.equalsIgnoreCase(style)) {
092: ret = DateFormat.MEDIUM;
093: } else if (LONG.equalsIgnoreCase(style)) {
094: ret = DateFormat.LONG;
095: } else if (FULL.equalsIgnoreCase(style)) {
096: ret = DateFormat.FULL;
097: } else {
098: throw new JspException(error);
099: }
100: }
101:
102: return ret;
103: }
104:
105: /**
106: * Get the value associated with a content-type attribute.
107: * Syntax defined in RFC 2045, section 5.1.
108: */
109: public static String getContentTypeAttribute(String input,
110: String name) {
111: int begin;
112: int end;
113: int index = input.toUpperCase().indexOf(name.toUpperCase());
114: if (index == -1)
115: return null;
116: index = index + name.length(); // positioned after the attribute name
117: index = input.indexOf('=', index); // positioned at the '='
118: if (index == -1)
119: return null;
120: index += 1; // positioned after the '='
121: input = input.substring(index).trim();
122:
123: if (input.charAt(0) == '"') {
124: // attribute value is a quoted string
125: begin = 1;
126: end = input.indexOf('"', begin);
127: if (end == -1)
128: return null;
129: } else {
130: begin = 0;
131: end = input.indexOf(';');
132: if (end == -1)
133: end = input.indexOf(' ');
134: if (end == -1)
135: end = input.length();
136: }
137: return input.substring(begin, end).trim();
138: }
139:
140: /**
141: * URL encodes a string, based on the supplied character encoding.
142: * This performs the same function as java.next.URLEncode.encode
143: * in J2SDK1.4, and should be removed if the only platform supported
144: * is 1.4 or higher.
145: * @param s The String to be URL encoded.
146: * @param enc The character encoding
147: * @return The URL encoded String
148: * [taken from jakarta-tomcat-jasper/jasper2
149: * org.apache.jasper.runtime.JspRuntimeLibrary.java]
150: */
151: public static String URLEncode(String s, String enc) {
152:
153: if (s == null) {
154: return "null";//$NON-NLS-1$
155: }
156:
157: if (enc == null) {
158: enc = "UTF-8"; // Is this right? //$NON-NLS-1$
159: }
160:
161: StringBuffer out = new StringBuffer(s.length());
162: ByteArrayOutputStream buf = new ByteArrayOutputStream();
163: OutputStreamWriter writer = null;
164: try {
165: writer = new OutputStreamWriter(buf, enc);
166: } catch (java.io.UnsupportedEncodingException ex) {
167: // Use the default encoding?
168: writer = new OutputStreamWriter(buf);
169: }
170:
171: for (int i = 0; i < s.length(); ++i) {
172: int c = s.charAt(i);
173: if (c == ' ') {
174: out.append('+');
175: } else if (isSafeChar(c)) {
176: out.append((char) c);
177: } else {
178: // convert to external encoding before hex conversion
179: try {
180: writer.write(c);
181: writer.flush();
182: } catch (IOException e) {
183: buf.reset();
184: continue;
185: }
186: byte[] ba = buf.toByteArray();
187: for (int j = 0; j < ba.length; ++j) {
188: out.append('%');
189: // Converting each byte in the buffer
190: out.append(Character.forDigit((ba[j] >> 4) & 0xf,
191: 16));
192: out.append(Character.forDigit(ba[j] & 0xf, 16));
193: }
194: buf.reset();
195: }
196: }
197: return out.toString();
198: }
199:
200: private static boolean isSafeChar(int c) {
201: if (c >= 'a' && c <= 'z') {
202: return true;
203: }
204: if (c >= 'A' && c <= 'Z') {
205: return true;
206: }
207: if (c >= '0' && c <= '9') {
208: return true;
209: }
210: if (c == '-' || c == '_' || c == '.' || c == '!' || c == '~'
211: || c == '*' || c == '\'' || c == '(' || c == ')') {
212: return true;
213: }
214: return false;
215: }
216:
217: /**
218: * HttpServletRequest.getLocales() returns the server's default locale
219: * if the request did not specify a preferred language.
220: * We do not want this behavior, because it prevents us from using
221: * the fallback locale.
222: * We therefore need to return an empty Enumeration if no preferred
223: * locale has been specified. This way, the logic for the fallback
224: * locale will be able to kick in.
225: */
226: public static Enumeration getRequestLocales(
227: HttpServletRequest request) {
228: Enumeration values = request.getHeaders("accept-language");//$NON-NLS-1$
229: if (values.hasMoreElements()) {
230: // At least one "accept-language". Simply return
231: // the enumeration returned by request.getLocales().
232: // System.out.println("At least one accept-language");
233: return request.getLocales();
234: } else {
235: // No header for "accept-language". Simply return
236: // the empty enumeration.
237: // System.out.println("No accept-language");
238: return values;
239: }
240: }
241: }
|