1 23 24 package org.objectweb.jorm.xml2mi.lib; 25 26 import org.objectweb.jorm.metainfo.api.*; 27 import org.objectweb.jorm.metainfo.api.Class; 28 import org.objectweb.jorm.xml2mi.api.MappingParser; 29 import org.objectweb.jorm.util.io.api.PathExplorer; 30 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 31 import org.objectweb.jorm.api.PException; 32 import org.objectweb.util.monolog.api.Logger; 33 import org.objectweb.util.monolog.api.LoggerFactory; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 import org.w3c.dom.Element ; 36 37 import java.util.Map ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 41 47 public abstract class BasicMappingParser 48 extends ParserHelper 49 implements MappingParser { 50 51 55 protected Manager metaInfoManager; 56 57 60 protected Class currentClass; 61 62 65 protected PathExplorer pathExplorer; 66 67 72 protected Map idvalue2genclassref; 73 74 static String fileSeparator = System.getProperty("file.separator"); 75 76 80 87 public void setMetaInfoManager(Manager mim) { 88 metaInfoManager = mim; 89 } 90 91 96 public void setPathExplorer(PathExplorer pathExpl) { 97 pathExplorer = pathExpl; 98 if (pathExplorer == null) 99 pathExplorer = new DirJavaExplorer(); 100 } 101 102 109 public void setCurrentClass(Class currentClass) { 110 this.currentClass = currentClass; 111 } 112 113 114 125 protected NameDef getRefNameDef(String linkend, String tagName) { 126 TypedElement typedElement = null; 127 NameDef nameDef = null; 128 String fieldName = null; 129 String namedefName = null; 130 int idx1 = linkend.indexOf("/"); 131 if (idx1 == 0) { 132 int idx2 = linkend.indexOf("/", idx1 + 1); 134 String fqcn = linkend.substring(1, idx2); 135 int idx3 = linkend.indexOf("/", idx2 + 1); 136 fieldName = linkend.substring(idx2 + 1, idx3); 137 namedefName = linkend.substring(idx3 + 1); 138 if (logger.isLoggable(BasicLevel.DEBUG)) { 139 logger.log(BasicLevel.DEBUG, 140 " fqcn " + fqcn + 141 " fieldname " + fieldName + 142 " namdefname " + namedefName); 143 } 144 fqcn = fqcn.replace('.', fileSeparator.charAt(0)) + ".pd"; 145 if (logger.isLoggable(BasicLevel.DEBUG)) { 146 if (motable.contains(fqcn)) { 147 logger.log(BasicLevel.DEBUG, "fqcn is there " + fqcn); 148 } 149 else { 150 logger.log(BasicLevel.DEBUG,"fqcn is not there " + fqcn); 151 } 152 } 153 Class clazz = (Class ) motable.get(fqcn); 154 typedElement = clazz.getTypedElement(fieldName); 155 156 if (logger.isLoggable(BasicLevel.DEBUG)) { 157 logger.log(BasicLevel.DEBUG, 158 "Get the field " + fieldName + 159 "defined in <" + clazz.getFQName() + 160 ">: <" + typedElement + ">"); 161 } 162 } else { 163 fieldName = linkend.substring(0, idx1); 165 namedefName = linkend.substring(idx1 + 1); 166 typedElement = currentClass.getTypedElement(fieldName); 167 if (logger.isLoggable(BasicLevel.DEBUG)) { 168 logger.log(BasicLevel.DEBUG, 169 "Get the field [" + fieldName + 170 "] in current class <" + currentClass.getFQName() + 171 ">: <" + typedElement + ">"); 172 } 173 } 174 if (typedElement instanceof ClassRef) { 175 ClassRef classRef = (ClassRef) typedElement; 176 if (logger.isLoggable(BasicLevel.DEBUG)) { 177 logger.log(BasicLevel.DEBUG, 178 "Get the ref name def of the ClassRef " + fieldName); 179 } 180 nameDef = classRef.getRefNameDef(namedefName); 181 } else if (typedElement instanceof GenClassRef) { 182 GenClassRef genClassRef = (GenClassRef) typedElement; 183 if (logger.isLoggable(BasicLevel.DEBUG)) { 184 logger.log(BasicLevel.DEBUG, 185 "Get the ref name def of the GenClassRef " + fieldName); 186 } 187 nameDef = genClassRef.getRefNameDef(namedefName); 188 } 189 if (logger.isLoggable(BasicLevel.DEBUG)) { 190 logger.log(BasicLevel.DEBUG, "Returned name def: " + nameDef); 191 } 192 return nameDef; 193 } 194 195 202 protected NameDef getNameDefFromSuper(String linkend) { 203 int idx1 = linkend.indexOf("/"); 204 if (idx1 == 0) { 205 int idx2 = linkend.indexOf("/", idx1 + 1); 207 String namedefName = null; 208 String fqcn = null; 209 if (idx2 != -1) { 210 fqcn = linkend.substring(1, idx2); 212 namedefName = linkend.substring(idx2 + 1); 213 } else { 214 fqcn = linkend.substring(1); 216 namedefName = ""; 217 } 218 Class superClass = currentClass.getSuperClass(fqcn); 219 Iterator ndIt = superClass.getNameDefs().iterator(); 220 while(ndIt.hasNext()) { 221 NameDef nd = (NameDef) ndIt.next(); 222 if (namedefName.equals(nd.getName())) { 223 if (logger.isLoggable(BasicLevel.DEBUG)) { 224 logger.log(BasicLevel.DEBUG, "Returned name def: " + nd); 225 } 226 return nd; 227 } 228 229 } 230 } 231 if (logger.isLoggable(BasicLevel.DEBUG)) { 232 logger.log(BasicLevel.DEBUG, "No nameDef found for the linkend " + linkend); 233 } 234 return null; 235 } 236 237 protected PrimitiveElement getPrimitiveElement(String linkend) { 238 PrimitiveElement pe = null; 239 int idx1 = linkend.indexOf("/"); 240 if (idx1 == 0) { 241 int idx2 = linkend.indexOf("/", idx1 + 1); 243 if (idx2 != -1) { 244 String fqcn = linkend.substring(1, idx2); 246 String fieldname = linkend.substring(idx2 + 1); 247 Class superClass = currentClass.getSuperClass(fqcn); 248 pe = (PrimitiveElement) superClass.getTypedElement(fieldname); 249 } 250 } else { 251 pe = (PrimitiveElement) currentClass.getTypedElement(linkend); 253 } 254 return pe; 255 } 256 257 263 protected String getFieldName(Element nameDef) { 264 logger.log(BasicLevel.DEBUG, "Fetching field name of the name def element " + nameDef.getNodeName()); 265 Element currentElem = nameDef; 266 while (currentElem != null && 267 !currentElem.getNodeName().equals("field")) { 268 logger.log(BasicLevel.DEBUG, "current: " + currentElem.getNodeName()); 269 currentElem = (Element ) currentElem.getParentNode(); 270 } 271 if (currentElem != null) { 272 if (logger.isLoggable(BasicLevel.DEBUG)) { 273 logger.log(BasicLevel.DEBUG, "last: " + currentElem.getNodeName() 274 + " => name=" + currentElem.getAttribute("name")); 275 } 276 return currentElem.getAttribute("name"); 277 } else { 278 return null; 279 } 280 } 281 282 283 289 protected NameDef getClassRefNameDef(Class clazz, 290 String fieldName, String name) { 291 NameDef res = null; 292 TypedElement typedElement = clazz.getTypedElement(fieldName); 293 logger.log(BasicLevel.DEBUG, "Fetching field <" + clazz.getFQName() + "." + fieldName + ">: <" + typedElement + ">"); 294 if (typedElement instanceof ClassRef) { 295 ClassRef classRef = (ClassRef) typedElement; 296 logger.log(BasicLevel.DEBUG, "Fetching name def of the ClassRef " + fieldName); 297 res = classRef.getRefNameDef(name); 298 } else if (typedElement instanceof GenClassRef) { 299 GenClassRef genClassRef = (GenClassRef) typedElement; 300 logger.log(BasicLevel.DEBUG, "Fetching name def of the ClassRef inside GenClassRef*: " + fieldName); 301 while ((genClassRef != null) && (!genClassRef.isClassRef())) { 302 genClassRef = genClassRef.getGenClassRef(); 303 } 304 if (genClassRef != null) { 305 res = genClassRef.getRefNameDef(name); 306 } 307 } 308 logger.log(BasicLevel.DEBUG, "Returned name def: " + res); 309 return res; 310 } 311 312 322 protected int getGenClassNestingLevel(Element fromElem, String fieldName) { 323 int nestingLevel = 0; 324 Element currentElem = (Element ) fromElem.getParentNode(); 325 while ((currentElem != null) && 326 (!currentElem.getNodeName().equals("field"))) { 327 currentElem = (Element ) currentElem.getParentNode(); 328 if ((currentElem.getNodeName().equals("class-ref")) || 329 (currentElem.getNodeName().equals("gen-class-ref"))) { 330 nestingLevel++; 331 } 332 } 333 if (currentElem != null) { 334 fieldName = currentElem.getAttribute("field-name"); 335 } 336 return nestingLevel; 337 } 338 339 345 protected NameDef getGenClassRefNameDef(Class clazz, String fieldName, 346 String name, int nestingLevel) { 347 NameDef res = null; 348 GenClassRef genClassRef = 349 getGenClassRef(clazz, fieldName, nestingLevel); 350 if (genClassRef != null) { 351 res = genClassRef.getRefNameDef(name); 352 } 353 return res; 354 } 355 356 357 363 protected GenClassRef getGenClassRef(Class clazz, String fieldName, 364 int nestingLevel) { 365 GenClassRef res = null; 366 TypedElement typedElement = clazz.getTypedElement(fieldName); 367 if (typedElement instanceof GenClassRef) { 368 res = (GenClassRef) typedElement; 369 GenClassRef genClassRef = (GenClassRef) typedElement; 370 while ((genClassRef != null) && (nestingLevel != 0)) { 371 res = genClassRef.getGenClassRef(); 372 nestingLevel--; 373 } 374 } 375 return res; 376 } 377 378 388 public abstract void parseMapping(Element mappingElem, Mapping mapping) throws PException; 389 390 395 public void setidvalue2genclassref(Map idvalue2genclassref) { 396 this.idvalue2genclassref = idvalue2genclassref; 397 } 398 399 404 public void setmotable(Hashtable motable) { 405 this.motable = motable; 406 } 407 408 412 417 public void setLogger(Logger logger) { 418 this.logger = logger; 419 } 420 421 426 public void setLoggerFactory(LoggerFactory loggerfactory) { 427 this.loggerFactory = loggerfactory; 428 if (logger == null && loggerFactory != null) { 429 logger = loggerFactory.getLogger("org.objectweb.jorm.xml2mi.rdb"); 430 } 431 } 432 433 public Logger getLogger() { 434 return logger; 435 } 436 437 public LoggerFactory getLoggerFactory() { 438 return loggerFactory; 439 } 440 } 441 | Popular Tags |