001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.rimfaxe.webserver.runtime;
057:
058: import java.io.IOException;
059: import java.util.Enumeration;
060:
061: import java.lang.reflect.Method;
062:
063: import java.io.Writer;
064: import java.io.Reader;
065: import java.io.IOException;
066: import java.io.InputStreamReader;
067:
068: import java.beans.PropertyDescriptor;
069: import java.beans.IndexedPropertyDescriptor;
070: import java.beans.PropertyEditor;
071: import java.beans.PropertyEditorManager;
072:
073: import java.security.AccessController;
074: import java.security.PrivilegedExceptionAction;
075: import java.security.PrivilegedActionException;
076:
077: import javax.servlet.RequestDispatcher;
078: import javax.servlet.ServletException;
079: import javax.servlet.ServletRequest;
080: import javax.servlet.ServletContext;
081: import javax.servlet.http.HttpServletRequest;
082: import javax.servlet.http.HttpServletResponse;
083: import javax.servlet.http.HttpSession;
084: import javax.servlet.jsp.JspWriter;
085: import javax.servlet.jsp.tagext.BodyContent;
086:
087: import com.rimfaxe.webserver.compiler.jsp.JasperException;
088: import com.rimfaxe.webserver.compiler.jsp.Constants;
089:
090: /**
091: * Bunch of util methods that are used by code generated for useBean,
092: * getProperty and setProperty.
093: *
094: * The __begin, __end stuff is there so that the JSP engine can
095: * actually parse this file and inline them if people don't want
096: * runtime dependencies on this class. However, I'm not sure if that
097: * works so well right now. It got forgotten at some point. -akv
098: *
099: * @author Mandar Raje
100: */
101: public class JspRuntimeLibrary {
102:
103: protected static class PrivilegedIntrospectHelper implements
104: PrivilegedExceptionAction {
105:
106: private Object bean;
107: private String prop;
108: private String value;
109: private ServletRequest request;
110: private String param;
111: private boolean ignoreMethodNF;
112:
113: PrivilegedIntrospectHelper(Object bean, String prop,
114: String value, ServletRequest request, String param,
115: boolean ignoreMethodNF) {
116: this .bean = bean;
117: this .prop = prop;
118: this .value = value;
119: this .request = request;
120: this .param = param;
121: this .ignoreMethodNF = ignoreMethodNF;
122: }
123:
124: public Object run() throws Exception {
125: internalIntrospecthelper(bean, prop, value, request, param,
126: ignoreMethodNF);
127: return null;
128: }
129: }
130:
131: // __begin convertMethod
132: public static Object convert(String propertyName, String s,
133: Class t, Class propertyEditorClass) throws JasperException {
134: try {
135: if (s == null) {
136: if (t.equals(Boolean.class) || t.equals(Boolean.TYPE))
137: s = "false";
138: else
139: return null;
140: }
141: if (propertyEditorClass != null) {
142: return getValueFromBeanInfoPropertyEditor(t,
143: propertyName, s, propertyEditorClass);
144: } else if (t.equals(Boolean.class)
145: || t.equals(Boolean.TYPE)) {
146: if (s.equalsIgnoreCase("on")
147: || s.equalsIgnoreCase("true"))
148: s = "true";
149: else
150: s = "false";
151: return new Boolean(s);
152: } else if (t.equals(Byte.class) || t.equals(Byte.TYPE)) {
153: return new Byte(s);
154: } else if (t.equals(Character.class)
155: || t.equals(Character.TYPE)) {
156: return s.length() > 0 ? new Character(s.charAt(0))
157: : null;
158: } else if (t.equals(Short.class) || t.equals(Short.TYPE)) {
159: return new Short(s);
160: } else if (t.equals(Integer.class)
161: || t.equals(Integer.TYPE)) {
162: return new Integer(s);
163: } else if (t.equals(Float.class) || t.equals(Float.TYPE)) {
164: return new Float(s);
165: } else if (t.equals(Long.class) || t.equals(Long.TYPE)) {
166: return new Long(s);
167: } else if (t.equals(Double.class) || t.equals(Double.TYPE)) {
168: return new Double(s);
169: } else if (t.equals(String.class)) {
170: return s;
171: } else if (t.equals(java.io.File.class)) {
172: return new java.io.File(s);
173: } else if (t.getName().equals("java.lang.Object")) {
174: return new Object[] { s };
175: } else {
176: return getValueFromPropertyEditorManager(t,
177: propertyName, s);
178: }
179: } catch (Exception ex) {
180: throw new JasperException(ex);
181: }
182: }
183:
184: // __end convertMethod
185:
186: // __begin introspectMethod
187: public static void introspect(Object bean, ServletRequest request)
188: throws JasperException {
189: //System.out.println("introspect");
190: Enumeration e = request.getParameterNames();
191: while (e.hasMoreElements()) {
192: String name = (String) e.nextElement();
193: String value = request.getParameter(name);
194: //System.out.println("introspect "+name+","+value);
195: introspecthelper(bean, name, value, request, name, true);
196: }
197: }
198:
199: // __end introspectMethod
200:
201: // __begin introspecthelperMethod
202: public static void introspecthelper(Object bean, String prop,
203: String value, ServletRequest request, String param,
204: boolean ignoreMethodNF) throws JasperException {
205: if (System.getSecurityManager() != null) {
206: try {
207: PrivilegedIntrospectHelper dp = new PrivilegedIntrospectHelper(
208: bean, prop, value, request, param,
209: ignoreMethodNF);
210: AccessController.doPrivileged(dp);
211: } catch (PrivilegedActionException pe) {
212: Exception e = pe.getException();
213: throw (JasperException) e;
214: }
215: } else {
216: internalIntrospecthelper(bean, prop, value, request, param,
217: ignoreMethodNF);
218: }
219: }
220:
221: private static void internalIntrospecthelper(Object bean,
222: String prop, String value, ServletRequest request,
223: String param, boolean ignoreMethodNF)
224: throws JasperException {
225: java.lang.reflect.Method method = null;
226: Class type = null;
227: Class propertyEditorClass = null;
228: try {
229: java.beans.BeanInfo info = java.beans.Introspector
230: .getBeanInfo(bean.getClass());
231: if (info != null) {
232: java.beans.PropertyDescriptor pd[] = info
233: .getPropertyDescriptors();
234: for (int i = 0; i < pd.length; i++) {
235: if (pd[i].getName().equals(prop)) {
236: method = pd[i].getWriteMethod();
237: type = pd[i].getPropertyType();
238: propertyEditorClass = pd[i]
239: .getPropertyEditorClass();
240: break;
241: }
242: }
243: }
244: if (method != null) {
245: if (type.isArray()) {
246: if (request == null) {
247: throw new JasperException(
248: Constants
249: .getString(
250: "jsp.error.beans.setproperty.noindexset",
251: new Object[] {}));
252: }
253: ;
254: Class t = type.getComponentType();
255: String[] values = request.getParameterValues(param);
256: //XXX Please check.
257: if (values == null)
258: return;
259: if (t.equals(String.class)) {
260: method.invoke(bean, new Object[] { values });
261: } else {
262: Object tmpval = null;
263: createTypedArray(prop, bean, method, values, t,
264: propertyEditorClass);
265: }
266: } else {
267: if (value == null
268: || (param != null && value.equals("")))
269: return;
270: Object oval = convert(prop, value, type,
271: propertyEditorClass);
272: if (oval != null)
273: method.invoke(bean, new Object[] { oval });
274: }
275: }
276: } catch (Exception ex) {
277: throw new JasperException(ex);
278: }
279: if (!ignoreMethodNF && (method == null)) {
280: if (type == null) {
281: throw new JasperException(Constants.getString(
282: "jsp.error.beans.noproperty", new Object[] {
283: prop, bean.getClass().getName() }));
284: } else {
285: throw new JasperException(Constants
286: .getString(
287: "jsp.error.beans.nomethod.setproperty",
288: new Object[] { prop,
289: bean.getClass().getName() }));
290: }
291: }
292: }
293:
294: // __end introspecthelperMethod
295:
296: //-------------------------------------------------------------------
297: // functions to convert builtin Java data types to string.
298: //-------------------------------------------------------------------
299: // __begin toStringMethod
300: public static String toString(Object o) {
301: return (o == null) ? "" : o.toString();
302: }
303:
304: public static String toString(byte b) {
305: return new Byte(b).toString();
306: }
307:
308: public static String toString(boolean b) {
309: return new Boolean(b).toString();
310: }
311:
312: public static String toString(short s) {
313: return new Short(s).toString();
314: }
315:
316: public static String toString(int i) {
317: return new Integer(i).toString();
318: }
319:
320: public static String toString(float f) {
321: return new Float(f).toString();
322: }
323:
324: public static String toString(long l) {
325: return new Long(l).toString();
326: }
327:
328: public static String toString(double d) {
329: return new Double(d).toString();
330: }
331:
332: public static String toString(char c) {
333: return new Character(c).toString();
334: }
335:
336: // __end toStringMethod
337:
338: /**
339: * Create a typed array.
340: * This is a special case where params are passed through
341: * the request and the property is indexed.
342: */
343: public static void createTypedArray(String propertyName,
344: Object bean, Method method, String[] values, Class t,
345: Class propertyEditorClass) throws JasperException {
346: try {
347: if (propertyEditorClass != null) {
348: Object[] tmpval = new Integer[values.length];
349: for (int i = 0; i < values.length; i++) {
350: tmpval[i] = getValueFromBeanInfoPropertyEditor(t,
351: propertyName, values[i],
352: propertyEditorClass);
353: }
354: method.invoke(bean, new Object[] { tmpval });
355: } else if (t.equals(Integer.class)) {
356: Integer[] tmpval = new Integer[values.length];
357: for (int i = 0; i < values.length; i++)
358: tmpval[i] = new Integer(values[i]);
359: method.invoke(bean, new Object[] { tmpval });
360: } else if (t.equals(Byte.class)) {
361: Byte[] tmpval = new Byte[values.length];
362: for (int i = 0; i < values.length; i++)
363: tmpval[i] = new Byte(values[i]);
364: method.invoke(bean, new Object[] { tmpval });
365: } else if (t.equals(Boolean.class)) {
366: Boolean[] tmpval = new Boolean[values.length];
367: for (int i = 0; i < values.length; i++)
368: tmpval[i] = new Boolean(values[i]);
369: method.invoke(bean, new Object[] { tmpval });
370: } else if (t.equals(Short.class)) {
371: Short[] tmpval = new Short[values.length];
372: for (int i = 0; i < values.length; i++)
373: tmpval[i] = new Short(values[i]);
374: method.invoke(bean, new Object[] { tmpval });
375: } else if (t.equals(Long.class)) {
376: Long[] tmpval = new Long[values.length];
377: for (int i = 0; i < values.length; i++)
378: tmpval[i] = new Long(values[i]);
379: method.invoke(bean, new Object[] { tmpval });
380: } else if (t.equals(Double.class)) {
381: Double[] tmpval = new Double[values.length];
382: for (int i = 0; i < values.length; i++)
383: tmpval[i] = new Double(values[i]);
384: method.invoke(bean, new Object[] { tmpval });
385: } else if (t.equals(Float.class)) {
386: Float[] tmpval = new Float[values.length];
387: for (int i = 0; i < values.length; i++)
388: tmpval[i] = new Float(values[i]);
389: method.invoke(bean, new Object[] { tmpval });
390: } else if (t.equals(Character.class)) {
391: Character[] tmpval = new Character[values.length];
392: for (int i = 0; i < values.length; i++)
393: tmpval[i] = new Character(values[i].charAt(0));
394: method.invoke(bean, new Object[] { tmpval });
395: } else if (t.equals(int.class)) {
396: int[] tmpval = new int[values.length];
397: for (int i = 0; i < values.length; i++)
398: tmpval[i] = Integer.parseInt(values[i]);
399: method.invoke(bean, new Object[] { tmpval });
400: } else if (t.equals(byte.class)) {
401: byte[] tmpval = new byte[values.length];
402: for (int i = 0; i < values.length; i++)
403: tmpval[i] = Byte.parseByte(values[i]);
404: method.invoke(bean, new Object[] { tmpval });
405: } else if (t.equals(boolean.class)) {
406: boolean[] tmpval = new boolean[values.length];
407: for (int i = 0; i < values.length; i++)
408: tmpval[i] = (Boolean.valueOf(values[i]))
409: .booleanValue();
410: method.invoke(bean, new Object[] { tmpval });
411: } else if (t.equals(short.class)) {
412: short[] tmpval = new short[values.length];
413: for (int i = 0; i < values.length; i++)
414: tmpval[i] = Short.parseShort(values[i]);
415: method.invoke(bean, new Object[] { tmpval });
416: } else if (t.equals(long.class)) {
417: long[] tmpval = new long[values.length];
418: for (int i = 0; i < values.length; i++)
419: tmpval[i] = Long.parseLong(values[i]);
420: method.invoke(bean, new Object[] { tmpval });
421: } else if (t.equals(double.class)) {
422: double[] tmpval = new double[values.length];
423: for (int i = 0; i < values.length; i++)
424: tmpval[i] = Double.valueOf(values[i]).doubleValue();
425: method.invoke(bean, new Object[] { tmpval });
426: } else if (t.equals(float.class)) {
427: float[] tmpval = new float[values.length];
428: for (int i = 0; i < values.length; i++)
429: tmpval[i] = Float.valueOf(values[i]).floatValue();
430: method.invoke(bean, new Object[] { tmpval });
431: } else if (t.equals(char.class)) {
432: char[] tmpval = new char[values.length];
433: for (int i = 0; i < values.length; i++)
434: tmpval[i] = values[i].charAt(0);
435: method.invoke(bean, new Object[] { tmpval });
436: } else {
437: Object[] tmpval = new Integer[values.length];
438: for (int i = 0; i < values.length; i++) {
439: tmpval[i] = getValueFromPropertyEditorManager(t,
440: propertyName, values[i]);
441: }
442: method.invoke(bean, new Object[] { tmpval });
443: }
444: } catch (Exception ex) {
445: throw new JasperException("error in invoking method");
446: }
447: }
448:
449: /**
450: * Escape special shell characters.
451: * @param unescString The string to shell-escape
452: * @return The escaped shell string.
453: */
454:
455: public static String escapeQueryString(String unescString) {
456: if (unescString == null)
457: return null;
458:
459: String escString = "";
460: String shellSpChars = "&;`'\"|*?~<>^()[]{}$\\\n";
461:
462: for (int index = 0; index < unescString.length(); index++) {
463: char nextChar = unescString.charAt(index);
464:
465: if (shellSpChars.indexOf(nextChar) != -1)
466: escString += "\\";
467:
468: escString += nextChar;
469: }
470: return escString;
471: }
472:
473: /**
474: * Decode an URL formatted string.
475: * @param s The string to decode.
476: * @return The decoded string.
477: */
478:
479: public static String decode(String encoded) {
480: // speedily leave if we're not needed
481: if (encoded == null)
482: return null;
483: if (encoded.indexOf('%') == -1 && encoded.indexOf('+') == -1)
484: return encoded;
485:
486: //allocate the buffer - use byte[] to avoid calls to new.
487: byte holdbuffer[] = new byte[encoded.length()];
488:
489: char holdchar;
490: int bufcount = 0;
491:
492: for (int count = 0; count < encoded.length(); count++) {
493: char cur = encoded.charAt(count);
494: if (cur == '%') {
495: holdbuffer[bufcount++] = (byte) Integer.parseInt(
496: encoded.substring(count + 1, count + 3), 16);
497: if (count + 2 >= encoded.length())
498: count = encoded.length();
499: else
500: count += 2;
501: } else if (cur == '+') {
502: holdbuffer[bufcount++] = (byte) ' ';
503: } else {
504: holdbuffer[bufcount++] = (byte) cur;
505: }
506: }
507: // REVISIT -- remedy for Deprecated warning.
508: //return new String(holdbuffer,0,0,bufcount);
509: return new String(holdbuffer, 0, bufcount);
510: }
511:
512: // __begin lookupReadMethodMethod
513: public static Object handleGetProperty(Object o, String prop)
514: throws JasperException {
515: if (o == null) {
516: throw new JasperException(Constants.getString(
517: "jsp.error.beans.nullbean", new Object[] {}));
518: }
519: Object value = null;
520: try {
521: java.lang.reflect.Method method = getReadMethod(o
522: .getClass(), prop);
523: value = method.invoke(o, null);
524: } catch (Exception ex) {
525: throw new JasperException(ex);
526: }
527: return value;
528: }
529:
530: // __end lookupReadMethodMethod
531:
532: public static void handleSetProperty(Object bean, String prop,
533: Object value) throws JasperException {
534: try {
535: Method method = getWriteMethod(bean.getClass(), prop);
536: method.invoke(bean, new Object[] { value });
537: } catch (Exception ex) {
538: throw new JasperException(ex);
539: }
540: }
541:
542: public static void handleSetProperty(Object bean, String prop,
543: int value) throws JasperException {
544: try {
545: Method method = getWriteMethod(bean.getClass(), prop);
546: method.invoke(bean, new Object[] { new Integer(value) });
547: } catch (Exception ex) {
548: throw new JasperException(ex);
549: }
550: }
551:
552: public static void handleSetProperty(Object bean, String prop,
553: short value) throws JasperException {
554: try {
555: Method method = getWriteMethod(bean.getClass(), prop);
556: method.invoke(bean, new Object[] { new Short(value) });
557: } catch (Exception ex) {
558: throw new JasperException(ex);
559: }
560: }
561:
562: public static void handleSetProperty(Object bean, String prop,
563: long value) throws JasperException {
564: try {
565: Method method = getWriteMethod(bean.getClass(), prop);
566: method.invoke(bean, new Object[] { new Long(value) });
567: } catch (Exception ex) {
568: throw new JasperException(ex);
569: }
570: }
571:
572: public static void handleSetProperty(Object bean, String prop,
573: double value) throws JasperException {
574: try {
575: Method method = getWriteMethod(bean.getClass(), prop);
576: method.invoke(bean, new Object[] { new Double(value) });
577: } catch (Exception ex) {
578: throw new JasperException(ex);
579: }
580: }
581:
582: public static void handleSetProperty(Object bean, String prop,
583: float value) throws JasperException {
584: try {
585: Method method = getWriteMethod(bean.getClass(), prop);
586: method.invoke(bean, new Object[] { new Float(value) });
587: } catch (Exception ex) {
588: throw new JasperException(ex);
589: }
590: }
591:
592: public static void handleSetProperty(Object bean, String prop,
593: char value) throws JasperException {
594: try {
595: Method method = getWriteMethod(bean.getClass(), prop);
596: method.invoke(bean, new Object[] { new Character(value) });
597: } catch (Exception ex) {
598: throw new JasperException(ex);
599: }
600: }
601:
602: public static void handleSetProperty(Object bean, String prop,
603: byte value) throws JasperException {
604: try {
605: Method method = getWriteMethod(bean.getClass(), prop);
606: method.invoke(bean, new Object[] { new Byte(value) });
607: } catch (Exception ex) {
608: throw new JasperException(ex);
609: }
610: }
611:
612: public static void handleSetProperty(Object bean, String prop,
613: boolean value) throws JasperException {
614: try {
615: Method method = getWriteMethod(bean.getClass(), prop);
616: method.invoke(bean, new Object[] { new Boolean(value) });
617: } catch (Exception ex) {
618: throw new JasperException(ex);
619: }
620: }
621:
622: public static java.lang.reflect.Method getWriteMethod(
623: Class beanClass, String prop) throws JasperException {
624: java.lang.reflect.Method method = null;
625: Class type = null;
626: try {
627: java.beans.BeanInfo info = java.beans.Introspector
628: .getBeanInfo(beanClass);
629: if (info != null) {
630: java.beans.PropertyDescriptor pd[] = info
631: .getPropertyDescriptors();
632: for (int i = 0; i < pd.length; i++) {
633: if (pd[i].getName().equals(prop)) {
634: method = pd[i].getWriteMethod();
635: type = pd[i].getPropertyType();
636: break;
637: }
638: }
639: } else {
640: // just in case introspection silently fails.
641: throw new JasperException(Constants.getString(
642: "jsp.error.beans.nobeaninfo",
643: new Object[] { beanClass.getName() }));
644: }
645: } catch (Exception ex) {
646: throw new JasperException(ex);
647: }
648: if (method == null) {
649: if (type == null) {
650: throw new JasperException(Constants.getString(
651: "jsp.error.beans.noproperty", new Object[] {
652: prop, beanClass.getName() }));
653: } else {
654: throw new JasperException(Constants.getString(
655: "jsp.error.beans.nomethod.setproperty",
656: new Object[] { prop, beanClass.getName() }));
657: }
658: }
659: return method;
660: }
661:
662: public static java.lang.reflect.Method getReadMethod(
663: Class beanClass, String prop) throws JasperException {
664: java.lang.reflect.Method method = null;
665: Class type = null;
666: try {
667: java.beans.BeanInfo info = java.beans.Introspector
668: .getBeanInfo(beanClass);
669: if (info != null) {
670: java.beans.PropertyDescriptor pd[] = info
671: .getPropertyDescriptors();
672: for (int i = 0; i < pd.length; i++) {
673: if (pd[i].getName().equals(prop)) {
674: method = pd[i].getReadMethod();
675: type = pd[i].getPropertyType();
676: break;
677: }
678: }
679: } else {
680: // just in case introspection silently fails.
681: throw new JasperException(Constants.getString(
682: "jsp.error.beans.nobeaninfo",
683: new Object[] { beanClass.getName() }));
684: }
685: } catch (Exception ex) {
686: throw new JasperException(ex);
687: }
688: if (method == null) {
689: if (type == null) {
690: throw new JasperException(Constants.getString(
691: "jsp.error.beans.noproperty", new Object[] {
692: prop, beanClass.getName() }));
693: } else {
694: throw new JasperException(Constants.getString(
695: "jsp.error.beans.nomethod", new Object[] {
696: prop, beanClass.getName() }));
697: }
698: }
699:
700: return method;
701: }
702:
703: //*********************************************************************
704: // PropertyEditor Support
705:
706: public static Object getValueFromBeanInfoPropertyEditor(
707: Class attrClass, String attrName, String attrValue,
708: Class propertyEditorClass) throws JasperException {
709: try {
710: PropertyEditor pe = (PropertyEditor) propertyEditorClass
711: .newInstance();
712: pe.setAsText(attrValue);
713: return pe.getValue();
714: } catch (Exception ex) {
715: throw new JasperException("Unable to convert string '"
716: + attrValue + "' to class " + attrClass.getName()
717: + " for attribute " + attrName + ": " + ex);
718: }
719: }
720:
721: public static Object getValueFromPropertyEditorManager(
722: Class attrClass, String attrName, String attrValue)
723: throws JasperException {
724: try {
725: PropertyEditor propEditor = PropertyEditorManager
726: .findEditor(attrClass);
727: if (propEditor != null) {
728: propEditor.setAsText(attrValue);
729: return propEditor.getValue();
730: } else {
731: throw new IllegalArgumentException(
732: "Property Editor not registered with the PropertyEditorManager");
733: }
734: } catch (IllegalArgumentException ex) {
735: throw new JasperException("Unable to convert string '"
736: + attrValue + "' to class " + attrClass.getName()
737: + " for attribute " + attrName + ": " + ex);
738: }
739: }
740:
741: // ************************************************************************
742: // General Purpose Runtime Methods
743: // ************************************************************************
744:
745: /**
746: * Convert a possibly relative resource path into a context-relative
747: * resource path that starts with a '/'.
748: *
749: * @param request The servlet request we are processing
750: * @param relativePath The possibly relative resource path
751: */
752: public static String getContextRelativePath(ServletRequest request,
753: String relativePath) {
754:
755: if (relativePath.startsWith("/"))
756: return (relativePath);
757: if (!(request instanceof HttpServletRequest))
758: return (relativePath);
759: HttpServletRequest hrequest = (HttpServletRequest) request;
760: String uri = (String) request
761: .getAttribute("javax.servlet.include.servlet_path");
762: if (uri == null)
763: uri = hrequest.getServletPath();
764: return (uri.substring(0, uri.lastIndexOf('/')) + '/' + relativePath);
765:
766: }
767:
768: /**
769: * Perform a RequestDispatcher.include() operation, with optional flushing
770: * of the response beforehand.
771: *
772: * @param request The servlet request we are processing
773: * @param response The servlet response we are processing
774: * @param relativePath The relative path of the resource to be included
775: * @param out The JspWriter to whom we are currently writing
776: * @param flush Should we flush before the include is processed?
777: *
778: * @exception IOException if thrown by the included servlet
779: * @exception ServletException if thrown by the included servlet
780: */
781: public static void include(HttpServletRequest request,
782: HttpServletResponse response, String relativePath,
783: JspWriter out, boolean flush) throws IOException,
784: ServletException {
785:
786: if (flush && !(out instanceof BodyContent))
787: out.flush();
788:
789: // FIXME - It is tempting to use request.getRequestDispatcher() to
790: // resolve a relative path directly, but Catalina currently does not
791: // take into account whether the caller is inside a RequestDispatcher
792: // include or not. Whether Catalina *should* take that into account
793: // is a spec issue currently under review. In the mean time,
794: // replicate Jasper's previous behavior
795:
796: String resourcePath = getContextRelativePath(request,
797: relativePath);
798: //System.out.println("Include file from -> "+resourcePath);
799:
800: //System.out.println("New Request Dispatcher");
801: RequestDispatcher rd = request
802: .getRequestDispatcher(resourcePath);
803:
804: //System.out.println("call Request Dispatcher -> "+rd);
805: rd.include(request, new ServletResponseWrapperInclude(response,
806: out));
807: //rd.include(request,response);
808: //System.out.println("call Request Dispatcher -> DONE!");
809: }
810:
811: }
|