KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > aspect > impl > AspectUtil


1 /*
2  * Copyright 1999-2002,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.cocoon.portal.aspect.impl;
17
18 import java.lang.reflect.Constructor JavaDoc;
19
20 import org.apache.cocoon.portal.aspect.AspectDescription;
21 import org.apache.cocoon.util.ClassUtils;
22
23
24
25 /**
26  * Utility class for aspects
27  *
28  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
29  *
30  * @version CVS $Id: AspectUtil.java 123407 2004-12-27 13:51:59Z cziegeler $
31  */

32 public class AspectUtil {
33
34     /**
35      * Create a new instance
36      */

37     public static Object JavaDoc createNewInstance(AspectDescription desc) {
38         try {
39             Class JavaDoc clazz = ClassUtils.loadClass(desc.getClassName());
40             if ( clazz.getName().startsWith("java.lang.")) {
41                 Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
42                 String JavaDoc value = (desc.getDefaultValue() == null ? "0" : desc.getDefaultValue());
43                 return constructor.newInstance(new String JavaDoc[] {value});
44             }
45             if ( desc.getDefaultValue() != null ) {
46                 Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
47                 return constructor.newInstance(new String JavaDoc[] {desc.getDefaultValue()});
48             }
49             return clazz.newInstance();
50         } catch (Exception JavaDoc ignore) {
51             return null;
52         }
53     }
54     
55     public static Object JavaDoc convert(AspectDescription desc, Object JavaDoc value) {
56         try {
57             Class JavaDoc clazz = ClassUtils.loadClass(desc.getClassName());
58             if ( clazz.getName().startsWith("java.lang.")) {
59                 if ( !clazz.equals(value.getClass())) {
60                     Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
61                     return constructor.newInstance(new String JavaDoc[] {value.toString()});
62                 }
63                 return value;
64             }
65             if ( !value.getClass().equals(clazz) ) {
66                 // FIXME - this is catch by "ignore"
67
throw new RuntimeException JavaDoc("Class of aspect doesn't match description.");
68             }
69             return value;
70         } catch (Exception JavaDoc ignore) {
71             // if we can't convert, well we don't do it :)
72
return value;
73         }
74     }
75 }
76
Popular Tags