1 57 58 package org.apache.commons.discovery.resource.names; 59 60 import java.util.Hashtable ; 61 62 import org.apache.commons.discovery.ResourceNameDiscover; 63 import org.apache.commons.discovery.ResourceNameIterator; 64 import org.apache.commons.discovery.log.DiscoveryLogFactory; 65 import org.apache.commons.logging.Log; 66 67 68 80 public class DiscoverMappedNames 81 extends ResourceNameDiscoverImpl 82 implements ResourceNameDiscover 83 { 84 private static Log log = DiscoveryLogFactory.newLog(DiscoverMappedNames.class); 85 public static void setLog(Log _log) { 86 log = _log; 87 } 88 89 private Hashtable mapping = new Hashtable (); 91 93 public DiscoverMappedNames() { 94 } 95 96 public void map(String fromName, String toName) { 97 mapping.put(fromName, toName); 98 } 99 100 public void map(String fromName, String [] toNames) { 101 mapping.put(fromName, toNames); 102 } 103 104 107 public ResourceNameIterator findResourceNames(final String resourceName) { 108 if (log.isDebugEnabled()) { 109 log.debug("find: resourceName='" + resourceName + "', mapping to constants"); 110 } 111 112 final Object obj = mapping.get(resourceName); 113 114 final String [] names; 115 if (obj instanceof String ) { 116 names = new String [] { (String )obj }; 117 } else if (obj instanceof String []) { 118 names = (String [])obj; 119 } else { 120 names = null; 121 } 122 123 return new ResourceNameIterator() { 124 125 private int idx = 0; 126 127 public boolean hasNext() { 128 if (names != null) { 129 while (idx < names.length && names[idx] == null) { 130 idx++; 131 } 132 return idx < names.length; 133 } 134 return false; 135 } 136 137 public String nextResourceName() { 138 return hasNext() ? names[idx++] : null; 139 } 140 }; 141 } 142 } 143 | Popular Tags |