1 package org.apache.ojb.broker.query; 2 3 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.apache.ojb.broker.util.logging.Logger; 25 import org.apache.ojb.broker.util.logging.LoggerFactory; 26 27 34 public class UserAlias implements Serializable 35 { 36 private static final long serialVersionUID = 3257282552220627249L; 37 38 private Map m_mapping = new HashMap (); 39 private String m_name = null; 40 private String m_attributePath = null; 41 private boolean m_allPathsAliased = false; 42 private Logger m_logger = LoggerFactory.getLogger(UserAlias.class); 43 44 49 public UserAlias(String name) 50 { 51 m_name = name; 52 } 53 54 63 public UserAlias(String name, String attributePath, String aliasPath) 64 { 65 m_name = name; 66 m_attributePath = attributePath; 67 if (attributePath.lastIndexOf(aliasPath) == -1) 68 { 69 m_logger.warn("aliasPath should be a substring of attributePath"); 70 } 71 initMapping(attributePath, aliasPath); 72 } 73 74 82 public UserAlias(String name, String attributePath, boolean allPathsAliased) 83 { 84 m_name = name; 85 m_attributePath = attributePath; 86 m_allPathsAliased = allPathsAliased; 87 } 88 89 94 private void initMapping(String attributePath, String aliasPath) 95 { 96 Iterator aliasSegmentItr = pathToSegments(aliasPath).iterator(); 97 String currPath = ""; 98 String separator = ""; 99 while (aliasSegmentItr.hasNext()) 100 { 101 currPath = currPath + separator + (String ) aliasSegmentItr.next(); 102 int beginIndex = attributePath.indexOf(currPath); 103 if (beginIndex == -1) 104 { 105 break; 106 } 107 int endIndex = beginIndex + currPath.length(); 108 m_mapping.put(attributePath.substring(0, endIndex), m_name); 109 separator = "."; 110 } 111 } 112 113 private ArrayList pathToSegments(String path) 114 { 115 ArrayList segments = new ArrayList (); 116 int sp = path.indexOf('.'); 117 while (sp != -1) 118 { 119 segments.add(path.substring(0, sp)); 120 path = path.substring(sp + 1); 121 sp = path.indexOf('.'); 122 } 123 segments.add(path); 124 return segments; 125 } 126 127 130 public String getName() 131 { 132 return m_name; 133 } 134 135 141 public String getAlias(String path) 142 { 143 if (m_allPathsAliased && m_attributePath.lastIndexOf(path) != -1) 144 { 145 return m_name; 146 } 147 Object retObj = m_mapping.get(path); 148 if (retObj != null) 149 { 150 return (String ) retObj; 151 } 152 return null; 153 } 154 155 160 public void add(String path) 161 { 162 m_mapping.put(path, m_name); 163 } 164 } | Popular Tags |