    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jxpath.ri.model.dynabeans;
    17
    18  import org.apache.commons.beanutils.DynaBean;
    19  import org.apache.commons.jxpath.AbstractFactory;
    20  import org.apache.commons.jxpath.JXPathContext;
    21  import org.apache.commons.jxpath.NestedTestBean;
    22  import org.apache.commons.jxpath.Pointer;
    23
    24  /***
    25   * Test AbstractFactory.
    26   *
    27   * @author Dmitri Plotnikov
    28   * @version $Revision: 1.8 $ $Date: 2004/02/29 14:17:44 $
    29   */
    30  public class TestDynaBeanFactory extends AbstractFactory {
    31
    32      /***
    33       */
    34      public boolean createObject(
    35          JXPathContext context,
    36          Pointer pointer,
    37          Object parent,
    38          String name,
    39          int index)
    40      {
    41          if (name.equals("nestedBean")) {
    42              ((DynaBean) parent).set(
    43                  "nestedBean",
    44                  new NestedTestBean("newName"));
    45              return true;
    46          }
    47          else if (name.equals("beans")) {
    48              DynaBean bean = (DynaBean) parent;
    49              Object beans[] = (Object[]) bean.get("beans");
    50              if (beans == null || index >= beans.length) {
    51                  beans = new NestedTestBean[index + 1];
    52                  bean.set("beans", beans);
    53              }
    54              beans[index] = new NestedTestBean("newName");
    55              return true;
    56          }
    57          else if (name.equals("integers")) {
    58              DynaBean bean = (DynaBean) parent;
    59              bean.set("integers", index, new Integer(0));
    60              return true;
    61          }
    62          return false;
    63      }
    64
    65      /***
    66       */
    67      public boolean declareVariable(JXPathContext context, String name) {
    68          context.getVariables().declareVariable(name, null);
    69          return true;
    70      }
    71  }