01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.portal.aspect.impl;
18:
19: import java.lang.reflect.Constructor;
20:
21: import org.apache.cocoon.portal.aspect.AspectDescription;
22: import org.apache.cocoon.util.ClassUtils;
23:
24: /**
25: * Utility class for aspects
26: *
27: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
28: *
29: * @version CVS $Id: AspectUtil.java 433543 2006-08-22 06:22:54Z crossley $
30: */
31: public class AspectUtil {
32:
33: /**
34: * Create a new instance
35: */
36: public static Object createNewInstance(AspectDescription desc) {
37: try {
38: Class clazz = ClassUtils.loadClass(desc.getClassName());
39: if (clazz.getName().startsWith("java.lang.")) {
40: Constructor constructor = clazz
41: .getConstructor(new Class[] { String.class });
42: String value = (desc.getDefaultValue() == null ? "0"
43: : desc.getDefaultValue());
44: return constructor.newInstance(new String[] { value });
45: }
46: if (desc.getDefaultValue() != null) {
47: Constructor constructor = clazz
48: .getConstructor(new Class[] { String.class });
49: return constructor.newInstance(new String[] { desc
50: .getDefaultValue() });
51: }
52: return clazz.newInstance();
53: } catch (Exception ignore) {
54: return null;
55: }
56: }
57:
58: public static Object convert(AspectDescription desc, Object value) {
59: try {
60: Class clazz = ClassUtils.loadClass(desc.getClassName());
61: if (clazz.getName().startsWith("java.lang.")) {
62: if (!clazz.equals(value.getClass())) {
63: Constructor constructor = clazz
64: .getConstructor(new Class[] { String.class });
65: return constructor.newInstance(new String[] { value
66: .toString() });
67: }
68: return value;
69: }
70: if (!value.getClass().equals(clazz)) {
71: // FIXME - this is catch by "ignore"
72: throw new RuntimeException(
73: "Class of aspect doesn't match description.");
74: }
75: return value;
76: } catch (Exception ignore) {
77: // if we can't convert, well we don't do it :)
78: return value;
79: }
80: }
81: }
|