| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.framework; |
| 12 | |
| 13 | import static org.jtiger.framework.SequenceFactory.newSequence; |
| 14 | import java.util.Properties; |
| 15 | import java.util.Map; |
| 16 | import java.util.HashMap; |
| 17 | import java.util.Set; |
| 18 | import java.io.InputStream; |
| 19 | import java.io.IOException; |
| 20 | |
| 21 | /** |
| 22 | * Provides an alias name to a class name using {@link #CLASS_ALIAS_MAPPING_RESOURCE a resource} as the mapping. This |
| 23 | * resource forms part of the JTiger core. |
| 24 | * |
| 25 | * @see ClassAliasMapping |
| 26 | * @author %javadoc.author.tag% |
| 27 | * @version %version%<br/> |
| 28 | * <i>Build Number %build.number%</i><br/> |
| 29 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
| 30 | */ |
| 31 | public final class ClassAliasMappingFactory |
| 32 | { |
| 33 | /** |
| 34 | * The value that aliases are prefixed with before being obtained by calling |
| 35 | * {@link ClassAliasMapping#getMappedClassName(String)}. That is, if an alias exists in the resource as "alias", the value |
| 36 | * that is mapped to that alias can be obtained by calling |
| 37 | * <code>{@link ClassAliasMapping#getMappedClassName(String) theClassAliasMapping.getMappedClassName(ALIAS_PREFIX + "alias")}</code> |
| 38 | */ |
| 39 | public static final String ALIAS_PREFIX = "~"; |
| 40 | |
| 41 | /** |
| 42 | * The name of the resource that provides the class name mapping. This resource forms part of the JTiger core. |
| 43 | */ |
| 44 | public static final String CLASS_ALIAS_MAPPING_RESOURCE = "/org/jtiger/classaliases.properties"; |
| 45 | |
| 46 | private ClassAliasMappingFactory() |
| 47 | { |
| 48 | |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns a new <code>ClassAliasMapping</code> instance. |
| 53 | * |
| 54 | * @return A new <code>ClassAliasMapping</code> instance. |
| 55 | */ |
| 56 | public static ClassAliasMapping newClassAliasMapping() |
| 57 | { |
| 58 | return new ClassAliasMappingImpl(); |
| 59 | } |
| 60 | |
| 61 | private static final class ClassAliasMappingImpl implements ClassAliasMapping |
| 62 | { |
| 63 | private static final Map<String, String> m; |
| 64 | private static boolean initialised; |
| 65 | |
| 66 | static |
| 67 | { |
| 68 | m = new HashMap<String, String>(); |
| 69 | } |
| 70 | |
| 71 | ClassAliasMappingImpl() |
| 72 | { |
| 73 | |
| 74 | } |
| 75 | |
| 76 | public String getMappedClassName(final String alias) throws NullPointerException |
| 77 | { |
| 78 | if(alias == null) |
| 79 | { |
| 80 | throw new NullPointerException(); |
| 81 | } |
| 82 | |
| 83 | if(!initialised) |
| 84 | { |
| 85 | initialiseMapping(); |
| 86 | initialised = true; |
| 87 | } |
| 88 | |
| 89 | return ClassAliasMappingImpl.m.get(alias.toLowerCase()); |
| 90 | } |
| 91 | |
| 92 | public ReadOnlyArray<String> getAliases() |
| 93 | { |
| 94 | if(!initialised) |
| 95 | { |
| 96 | initialiseMapping(); |
| 97 | initialised = true; |
| 98 | } |
| 99 | |
| 100 | final Set<String> keys = ClassAliasMappingImpl.m.keySet(); |
| 101 | return newSequence(keys.toArray(new String[keys.size()])); |
| 102 | } |
| 103 | |
| 104 | private static void initialiseMapping() |
| 105 | { |
| 106 | final InputStream in = ClassAliasMappingImpl.class.getResourceAsStream(CLASS_ALIAS_MAPPING_RESOURCE); |
| 107 | final Properties props = new Properties(); |
| 108 | |
| 109 | try |
| 110 | { |
| 111 | props.load(in); |
| 112 | |
| 113 | final Set<Object> names = props.keySet(); |
| 114 | |
| 115 | for(Object o : names) |
| 116 | { |
| 117 | final String name = (String)o; |
| 118 | final String aliasedName = new StringBuilder(ALIAS_PREFIX).append(name.toLowerCase()).toString(); |
| 119 | ClassAliasMappingImpl.m.put(aliasedName, props.getProperty(name)); |
| 120 | } |
| 121 | } |
| 122 | catch(IOException e) |
| 123 | { |
| 124 | throw new IllegalStateException(e.getMessage(), e); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |