1 23 package org.objectweb.jorm.lib; 24 25 import org.objectweb.jorm.metainfo.api.GenClassRef; 26 import org.objectweb.jorm.metainfo.api.Class; 27 import org.objectweb.jorm.metainfo.api.ClassRef; 28 import org.objectweb.jorm.metainfo.api.MetaObject; 29 import org.objectweb.jorm.naming.api.PNameCoder; 30 import org.objectweb.jorm.api.PMapper; 31 import org.objectweb.jorm.api.PClassMapping; 32 import org.objectweb.jorm.genclass.lib.GenClassMapping; 33 34 45 public class JormPathHelper { 46 47 public final static char SEP = '/'; 48 public final static String ELEMENT = SEP + "element"; 49 50 56 public static String getPath(GenClassRef gcr, boolean element) { 57 MetaObject parent = gcr.getParent(); 58 if (parent instanceof Class ) { 59 return getPath((Class ) parent) + SEP + gcr.getName() 60 + (element ? ELEMENT : ""); 61 } else if (parent instanceof GenClassRef) { 62 GenClassRef p = (GenClassRef) parent; 63 return getPath(p, false) + SEP + p.getGenClassName() 64 + (element ? ELEMENT : ""); 65 } else { 66 return null; 67 } 68 } 69 70 75 public static String getPath(ClassRef cr) { 76 MetaObject parent = cr.getParent(); 77 if (parent instanceof Class ) { 78 return getPath((Class ) parent) + SEP + cr.getName(); 79 } else if (parent instanceof GenClassRef) { 80 GenClassRef p = (GenClassRef) parent; 81 return getPath(p, false) + SEP + p.getGenClassName() + ELEMENT; 82 } else { 83 return null; 84 } 85 } 86 87 public static String replaceFieldOwner(String path, String newOwner) { 88 if (path == null) { 89 return null; 90 } 91 int idx = path.indexOf(SEP); 92 if (idx == -1) { 93 return path; 94 } 95 if (idx == 0) { 96 idx = path.indexOf(SEP, 1); 97 } 98 if (idx == -1) { 99 return path; 100 } 101 return getPath(newOwner) + path.substring(idx); 102 } 103 104 109 public static String getPath(Class clazz) { 110 return getPath(clazz.getFQName()); 111 } 112 113 public static String getPath(String fqclassname) { 114 return SEP + fqclassname; 115 } 116 117 public static String getPath(String sourceclassName, 118 String refFieldName, 119 String [] genClassNames) { 120 StringBuffer cn = new StringBuffer (); 121 cn.append(getPath(sourceclassName)); 122 cn.append(SEP); 123 cn.append(refFieldName); 124 for (int i = 1; i < genClassNames.length; i++) { 125 cn.append(SEP); 126 cn.append(genClassNames[i - 1]); 127 } 128 return cn.toString(); 129 } 130 131 136 public static PNameCoder getPNameCoder(String path, PMapper mapper) { 137 if (path == null || path.length() == 0 || mapper == null) { 138 return null; 139 } 140 int idx = path.indexOf(SEP); 141 if (idx == -1) { 142 PClassMapping pcm = mapper.lookup(path); 144 if (pcm == null) { 145 return null; 146 } else { 147 PNameCoder pnc = pcm.getClassPNameCoder(); 148 if (pnc == null) { 149 pnc = pcm.getPBinder(); 150 } 151 return pnc; 152 } 153 } else if (idx == 0) { 154 return getPNameCoder(path.substring(1), mapper); 155 } else { 156 return getPNameCoder(path.substring(idx + 1), 158 mapper.lookup(path.substring(0, idx))); 159 } 160 } 161 162 167 private static PNameCoder getPNameCoder(String path, PClassMapping pcm) { 168 if (pcm == null) { 169 return null; 170 } 171 int idx = path.indexOf(SEP); 172 if (pcm instanceof GenClassMapping) { 173 if (idx == -1) { 174 return pcm.getPBinder(); 175 } else { 176 String subpath = path.substring(idx + 1); 177 if ((SEP + subpath).startsWith(ELEMENT)) { 178 return pcm.getPNameCoder(); 179 } 180 return getPNameCoder( 181 subpath, 182 pcm.getGenClassMapping()); 183 } 184 } else { 185 if (idx == -1) { 186 return pcm.getPNameCoder(path); 187 } else { 188 return getPNameCoder( 189 path.substring(idx + 1), 190 pcm.getGenClassMapping(path.substring(0, idx))); 191 } 192 } 193 } 194 195 196 202 public static PClassMapping getPClassMapping(String path, 203 PMapper mapper) { 204 if (path == null || path.length() == 0 || mapper == null) { 205 return null; 206 } 207 int idx = path.indexOf(SEP); 208 if (idx == -1) { 209 return mapper.lookup(path); 212 } else if (idx == 0) { 213 return getPClassMapping(path.substring(1), mapper); 214 } 215 return getPClassMapping( 216 path.substring(idx + 1), 217 mapper.lookup(path.substring(0, idx))); 218 } 219 220 private static PClassMapping getPClassMapping(String path, 221 PClassMapping pcm) { 222 if (pcm == null) { 223 return null; 224 } 225 int idx = path.indexOf(SEP); 226 if (pcm instanceof GenClassMapping) { 227 if (idx == -1) { 228 return pcm.getGenClassMapping(); 229 } else { 230 String subpath = path.substring(idx + 1); 231 if ((SEP + subpath).startsWith(ELEMENT)) { 232 return null; 233 } 234 return getPClassMapping( 235 subpath, 236 pcm.getGenClassMapping()); 237 } 238 } else { 239 if (idx == -1) { 240 return pcm.getGenClassMapping(path); 241 } else { 242 return getPClassMapping( 243 path.substring(idx + 1), 244 pcm.getGenClassMapping(path.substring(0, idx))); 245 } 246 } 247 } 248 249 public static String getOriginClass(String path) { 250 if (path == null || path.length() == 0) { 251 return null; 252 } 253 int idx = path.indexOf(SEP); 254 if (idx == 0) { 255 idx = path.indexOf(SEP, idx); 256 } 257 if (idx == -1) { 258 return path; 259 } else { 260 int idx2 = path.indexOf(SEP, idx + 1); 261 if (idx2 == -1) { 262 return path.substring(idx + 1); 263 } else { 264 return path.substring(idx + 1, idx2); 265 } 266 } 267 } 268 } 269
| Popular Tags
|