• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     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 
18 package java.beans;
19 
20 import java.util.HashMap;
21 import java.util.Map;
22 
23 public class PropertyEditorManager {
24 
25     private static String[] path = { "org.apache.harmony.beans.editors" }; //$NON-NLS-1$
26 
27     private static final Map<Class<?>, Class<?>> registeredEditors = new HashMap<Class<?>, Class<?>>();
28 
PropertyEditorManager()29     public PropertyEditorManager() {
30     }
31 
registerEditor(Class<?> targetType, Class<?> editorClass)32     public static void registerEditor(Class<?> targetType, Class<?> editorClass) {
33         if (targetType == null) {
34             throw new NullPointerException();
35         }
36 
37         SecurityManager sm = System.getSecurityManager();
38         if (sm != null) {
39             sm.checkPropertiesAccess();
40         }
41         if (editorClass != null) {
42             registeredEditors.put(targetType, editorClass);
43         } else {
44             registeredEditors.remove(targetType);
45         }
46     }
47 
findEditor(Class<?> targetType)48     public static synchronized PropertyEditor findEditor(Class<?> targetType) {
49         if (targetType == null) {
50             throw new NullPointerException();
51         }
52 
53         Class<?> editorClass = null;
54         PropertyEditor editor = null;
55 
56         editorClass = registeredEditors.get(targetType);
57 
58         if (editorClass == null) {
59             String editorClassName = targetType.getName() + "Editor"; //$NON-NLS-1$
60             ClassLoader loader = targetType.getClassLoader();
61 
62             if (loader == null) {
63                 loader = Thread.currentThread().getContextClassLoader();
64             }
65 
66             try {
67                 editorClass = Class.forName(editorClassName, true, loader);
68             } catch (ClassNotFoundException cnfe) {
69                 String shortEditorClassName = editorClassName
70                         .substring(editorClassName.lastIndexOf(".") + 1); //$NON-NLS-1$
71 
72                 if (targetType.isPrimitive()) {
73                     shortEditorClassName = shortEditorClassName.substring(0, 1)
74                             .toUpperCase()
75                             + shortEditorClassName.substring(1);
76                 }
77 
78                 for (String element : path) {
79                     editorClassName = element + "." + shortEditorClassName; //$NON-NLS-1$
80 
81                     try {
82                         editorClass = Class.forName(editorClassName, true,
83                                 loader);
84                         break;
85                     } catch (Exception e) {
86                     }
87                 }
88             } catch (Exception e) {
89             }
90         }
91 
92         if (editorClass != null) {
93             try {
94                 editor = (PropertyEditor) editorClass.newInstance();
95             } catch (Exception e) {
96             }
97         }
98 
99         return editor;
100     }
101 
setEditorSearchPath(String[] apath)102     public static synchronized void setEditorSearchPath(String[] apath) {
103         SecurityManager sm = System.getSecurityManager();
104         if (sm != null) {
105             sm.checkPropertiesAccess();
106         }
107 
108         path = apath;
109     }
110 
getEditorSearchPath()111     public static synchronized String[] getEditorSearchPath() {
112         return path;
113     }
114 }
115