01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
17:
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import org.apache.commons.jxpath.AbstractFactory;
22: import org.apache.commons.jxpath.JXPathContext;
23: import org.apache.commons.jxpath.NestedTestBean;
24: import org.apache.commons.jxpath.Pointer;
25: import org.apache.commons.jxpath.TestBean;
26: import org.apache.commons.jxpath.TestMixedModelBean;
27:
28: /**
29: * Test AbstractFactory.
30: *
31: * @author Dmitri Plotnikov
32: * @version $Revision: 1.7 $ $Date: 2004/02/29 14:17:45 $
33: */
34: public class TestMixedModelFactory extends AbstractFactory {
35:
36: /**
37: * Create a new instance and put it in the collection on the parent object.
38: * Return <b>false</b> if this factory cannot create the requested object.
39: */
40: public boolean createObject(JXPathContext context, Pointer pointer,
41: Object parent, String name, int index) {
42: if (name.equals("nestedBean")) {
43: ((TestBean) parent).setNestedBean(new NestedTestBean(
44: "newName"));
45: return true;
46: } else if (name.equals("beans")) {
47: TestBean bean = (TestBean) parent;
48: if (bean.getBeans() == null
49: || index >= bean.getBeans().length) {
50: bean.setBeans(new NestedTestBean[index + 1]);
51: }
52: bean.getBeans()[index] = new NestedTestBean("newName");
53: return true;
54: } else if (name.equals("map")) {
55: ((TestBean) parent).setMap(new HashMap());
56: return true;
57: } else if (name.equals("TestKey5")) {
58: TestBean tb = new TestBean();
59: tb.setNestedBean(null);
60: tb.setBeans(null);
61: ((Map) parent).put(name, tb);
62: return true;
63: } else if (name.equals("matrix")) {
64: int[][] matrix = new int[2][];
65: matrix[0] = new int[1];
66: // matrix[1] = new int[2];
67: ((TestMixedModelBean) parent).setMatrix(matrix);
68: return true;
69: }
70: return false;
71: }
72:
73: public boolean declareVariable(JXPathContext context, String name) {
74: return false;
75: }
76: }
|