    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.compiler;
    17
    18  import org.apache.commons.jxpath.AbstractFactory;
    19  import org.apache.commons.jxpath.JXPathContext;
    20  import org.apache.commons.jxpath.Pointer;
    21  import org.apache.commons.jxpath.TestBean;
    22
    23  /***
    24   * Test AbstractFactory.
    25   *
    26   * @author Dmitri Plotnikov
    27   * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
    28   */
    29  public class VariableFactory extends AbstractFactory {
    30
    31      /***
    32       */
    33      public boolean createObject(
    34          JXPathContext context,
    35          Pointer pointer,
    36          Object parent,
    37          String name,
    38          int index)
    39      {
    40          if (name.equals("testArray")) {
    41              ((TestBean[]) parent)[index] = new TestBean();
    42              return true;
    43          }
    44          else if (name.equals("stringArray")) {
    45              ((String[]) parent)[index] = "";
    46              return true;
    47          }
    48          else if (name.equals("array")) {
    49              ((String[]) parent)[index] = "";
    50              return true;
    51          }
    52          return false;
    53      }
    54
    55      /***
    56       * Create a new object and set it on the specified variable
    57       */
    58      public boolean declareVariable(JXPathContext context, String name) {
    59          if (name.equals("test")) {
    60              context.getVariables().declareVariable(name, new TestBean());
    61              return true;
    62          }
    63          else if (name.equals("testArray")) {
    64              context.getVariables().declareVariable(name, new TestBean[0]);
    65              return true;
    66          }
    67          else if (name.equals("stringArray")) {
    68              context.getVariables().declareVariable(
    69                  name,
    70                  new String[] { "Value1" });
    71              return true;
    72          }
    73          context.getVariables().declareVariable(name, null);
    74          return true;
    75      }
    76  }