Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ognl/ArrayPropertyAccessor.java


1   //--------------------------------------------------------------------------
2   //  Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
3   //  All rights reserved.
4   //
5   //  Redistribution and use in source and binary forms, with or without
6   //  modification, are permitted provided that the following conditions are
7   //  met:
8   //
9   //  Redistributions of source code must retain the above copyright notice,
10  //  this list of conditions and the following disclaimer.
11  //  Redistributions in binary form must reproduce the above copyright
12  //  notice, this list of conditions and the following disclaimer in the
13  //  documentation and/or other materials provided with the distribution.
14  //  Neither the name of the Drew Davidson nor the names of its contributors
15  //  may be used to endorse or promote products derived from this software
16  //  without specific prior written permission.
17  //
18  //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  //  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  //  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  //  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  //  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  //  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  //  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  //  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  //  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28  //  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  //  DAMAGE.
30  //--------------------------------------------------------------------------
31  package ognl;
32  
33  import java.lang.reflect.Array;
34  import java.util.Map;
35  
36  /**
37   * Implementation of PropertyAccessor that uses numbers and dynamic subscripts as
38   * properties to index into Java arrays.
39   * @author Luke Blanshard (blanshlu@netscape.net)
40   * @author Drew Davidson (drew@ognl.org)
41   */
42  public class ArrayPropertyAccessor extends ObjectPropertyAccessor
43      implements PropertyAccessor // This is here to make javadoc show this class as an implementor
44  {
45      public Object getProperty( Map context, Object target, Object name ) throws OgnlException
46      {
47          Object      result = null;
48  
49          if (name instanceof String) {
50              if (name.equals("length")) {
51                  result = new Integer( Array.getLength(target) );
52              } else {
53                  result = super.getProperty( context, target, name );
54              }
55          } else {
56              Object      index = name;
57  
58              if (index instanceof DynamicSubscript) {
59                  int     len = Array.getLength(target);
60  
61                  switch (((DynamicSubscript)index).getFlag()) {
62                      case DynamicSubscript.ALL:
63                          result = Array.newInstance( target.getClass().getComponentType(), len );
64                          System.arraycopy( target, 0, result, 0, len );
65                          break;
66                      case DynamicSubscript.FIRST:
67                          index = new Integer((len > 0) ? 0 : -1);
68                          break;
69                      case DynamicSubscript.MID:
70                          index = new Integer((len > 0) ? (len / 2) : -1);
71                          break;
72                      case DynamicSubscript.LAST:
73                          index = new Integer((len > 0) ? (len - 1) : -1);
74                          break;
75                  }
76              }
77              if (result == null) {
78                  if (index instanceof Number) {
79                      int     i = ((Number)index).intValue();
80  
81                      result = (i >= 0) ? Array.get(target, i) : null;
82                  } else {
83                      throw new NoSuchPropertyException(target, index);
84                  }
85              }
86          }
87          return result;
88      }
89  
90      public void setProperty( Map context, Object target, Object name, Object value ) throws OgnlException
91      {
92          Object          index = name;
93          boolean         isNumber = (index instanceof Number);
94  
95          if (isNumber || (index instanceof DynamicSubscript)) {
96              TypeConverter       converter = ((OgnlContext)context).getTypeConverter();
97              Object              convertedValue;
98  
99              convertedValue = converter.convertValue(context, target, null, name.toString(), value, target.getClass().getComponentType());
100             if (isNumber) {
101                 int     i = ((Number)index).intValue();
102 
103                 if (i >= 0) {
104                     Array.set(target, i, convertedValue);
105                 }
106             } else {
107                 int     len = Array.getLength(target);
108 
109                 switch ( ((DynamicSubscript)index).getFlag() ) {
110                     case DynamicSubscript.ALL:
111                         System.arraycopy(target, 0, convertedValue, 0, len);
112                         return;
113                     case DynamicSubscript.FIRST:
114                         index = new Integer((len > 0) ? 0 : -1);
115                         break;
116                     case DynamicSubscript.MID:
117                         index = new Integer((len > 0) ? (len / 2) : -1);
118                         break;
119                     case DynamicSubscript.LAST:
120                         index = new Integer((len > 0 ) ? (len - 1) : -1);
121                         break;
122                 }
123             }
124         } else {
125             if (name instanceof String) {
126                 super.setProperty(context, target, name, value);
127             } else {
128                 throw new NoSuchPropertyException(target, index);
129             }
130         }
131     }
132 }