01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19: package de.schlund.pfixcore.oxm.impl;
20:
21: import java.util.Collection;
22: import java.util.Date;
23: import java.util.GregorianCalendar;
24: import java.util.HashMap;
25: import java.util.List;
26: import java.util.Map;
27: import java.util.Properties;
28: import java.util.Set;
29:
30: import de.schlund.pfixcore.oxm.impl.annotation.ClassNameAlias;
31:
32: /**
33: *
34: * @author mleidig@schlund.de
35: *
36: */
37: public class ClassNameMapping {
38:
39: Map<Class<?>, String> mappings;
40:
41: public ClassNameMapping() {
42: mappings = new HashMap<Class<?>, String>();
43: mappings.put(Integer.class, "int");
44: mappings.put(Character.class, "char");
45: mappings.put(GregorianCalendar.class, "calendar");
46: mappings.put(Date.class, "date");
47: mappings.put(Properties.class, "properties");
48: }
49:
50: public String mapClassName(Class<?> clazz) {
51: String name = null;
52: synchronized (mappings) {
53: name = mappings.get(clazz);
54: }
55: if (name == null) {
56: ClassNameAlias elementName = clazz
57: .getAnnotation(ClassNameAlias.class);
58: if (elementName != null) {
59: name = elementName.value();
60: } else {
61: if (Collection.class.isAssignableFrom(clazz)) {
62: if (List.class.isAssignableFrom(clazz)) {
63: name = "list";
64: } else if (Set.class.isAssignableFrom(clazz)) {
65: name = "set";
66: }
67: name = "collection";
68: } else if (Map.class.isAssignableFrom(clazz)) {
69: name = "map";
70: } else if (clazz.isArray()) {
71: name = "array";
72: }
73: }
74: if (name == null)
75: name = getElementName(clazz);
76: synchronized (mappings) {
77: mappings.put(clazz, name);
78: }
79: }
80: return name;
81: }
82:
83: private String getElementName(Class<?> clazz) {
84: String name = clazz.getSimpleName();
85: if (name.length() > 1 && Character.isUpperCase(name.charAt(0))
86: && Character.isUpperCase(name.charAt(1)))
87: return name;
88: return Character.toLowerCase(name.charAt(0))
89: + name.substring(1);
90: }
91:
92: }
|