1 23 24 package org.objectweb.jorm.mapper.fos.xml2mi; 25 26 import org.objectweb.jorm.xml2mi.lib.BasicMappingParser; 27 import org.objectweb.jorm.metainfo.api.Class; 28 import org.objectweb.jorm.metainfo.api.Mapping; 29 import org.objectweb.jorm.metainfo.api.NameDef; 30 import org.objectweb.jorm.metainfo.api.GenClassRef; 31 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 32 import org.objectweb.jorm.metainfo.api.TypedElement; 33 import org.objectweb.jorm.api.PException; 34 import org.objectweb.jorm.mapper.fos.metainfo.FosMapping; 35 import org.objectweb.jorm.mapper.fos.metainfo.FosClassMapping; 36 import org.objectweb.jorm.mapper.fos.metainfo.FosGenClassMapping; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 import org.w3c.dom.Node ; 41 42 import java.util.Iterator ; 43 44 47 public class FosParser extends BasicMappingParser { 48 57 private void parseClassMapping(Element classMappingElem, Mapping mapping) { 58 FosClassMapping fosClassMapping = (FosClassMapping) mapping.createClassMapping(""); 59 fosClassMapping.setLogger(logger); 60 Class c = (Class ) fosClassMapping.getLinkedMO(); 61 Iterator it = c.getFields().iterator(); 62 while (it.hasNext()) { 63 Object fd = it.next(); 64 if (fd instanceof PrimitiveElement) { 65 fosClassMapping.addFieldMapping(((PrimitiveElement) fd).getName()); 66 } 67 } 68 NodeList children = classMappingElem.getChildNodes(); 69 String dirname = null; 70 for (int i = 0; i < children.getLength(); i++) { 71 Node child = children.item(i); 72 String childName = child.getNodeName(); 73 if (logger.isLoggable(BasicLevel.DEBUG)) { 74 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 75 } 76 if (childName.equals("fos-dir-name")) { 77 dirname = child.getFirstChild().getNodeValue(); 78 if (logger.isLoggable(BasicLevel.DEBUG)) { 79 logger.log(BasicLevel.DEBUG, "read dirname: " + dirname); 80 } 81 } else if (childName.equals("id-mapping")) { 82 String linkend = ((Element) child).getAttribute("link-end"); 83 NameDef nd = getIdNameDef(fosClassMapping, linkend); 85 if (nd.isNameRef()) { 86 Iterator it2 = nd.iterateField(); 87 while (it2.hasNext()) { 88 fosClassMapping.addFieldMapping((String ) it2.next()); 89 } 90 } else { 91 fosClassMapping.addFieldMapping(nd.getFieldName()); 92 } 93 fosClassMapping.createIdentifierMapping(nd); 94 } else if (childName.equals("fos-ref-mapping")) { 95 String linkend = ((Element) child).getAttribute("link-end"); 96 NameDef nd = getRefNameDef(linkend, childName); 99 if (nd.isNameRef()) { 100 Iterator it2 = nd.iterateField(); 101 while (it2.hasNext()) { 102 fosClassMapping.addFieldMapping((String ) it2.next()); 103 } 104 } else { 105 fosClassMapping.addFieldMapping(nd.getFieldName()); 106 } 107 fosClassMapping.createReferenceMapping(null, nd); 108 } else if (childName.equals("#text")) { 109 } else { 110 logger.log(BasicLevel.WARN, "element <" + childName + "> unknown !"); 111 } 112 if (logger.isLoggable(BasicLevel.DEBUG)) 113 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 114 } 115 fosClassMapping.setDirName(dirname); 116 } 117 118 127 private void parseGenClassMapping(Element genClassMappingElem, 128 Mapping mapping) { 129 130 String linkend = genClassMappingElem.getAttribute("link-end"); 131 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 132 getLogger().log(BasicLevel.DEBUG, 133 "linkend of fos-gen-class-mapping <" + linkend + ">"); 134 } 135 GenClassRef genClassRef = (GenClassRef) idvalue2genclassref.get(linkend); 136 if ((genClassRef != null) && 137 (getLogger().isLoggable(BasicLevel.DEBUG))) { 138 getLogger().log(BasicLevel.DEBUG, 139 "GenClassRef name " + genClassRef.getName()); 140 } 141 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 143 getLogger().log(BasicLevel.DEBUG, 144 "create a new BasicFosGenClassMapping for the current " + 145 "Class (" + currentClass.getName() + ")"); 146 } 147 FosGenClassMapping fosGenClassMapping = ((FosMapping) mapping) 148 .createGenClassMapping("", genClassRef); 149 fosGenClassMapping.setLogger(logger); 150 Iterator it = genClassRef.getIndexFields().iterator(); 151 while (it.hasNext()) { 152 fosGenClassMapping.addFieldMapping(((TypedElement) it.next()).getName()); 153 } 154 NodeList children = genClassMappingElem.getChildNodes(); 155 String dirname = null; 156 for (int i = 0; i < children.getLength(); i++) { 157 Node child = children.item(i); 158 String childName = child.getNodeName(); 159 if (logger.isLoggable(BasicLevel.DEBUG)) { 160 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 161 } 162 if (childName.equals("fos-dir-name")) { 163 dirname = child.getFirstChild().getNodeValue(); 164 if (logger.isLoggable(BasicLevel.DEBUG)) { 165 logger.log(BasicLevel.DEBUG, "read dirname: " + dirname); 166 } 167 } else if (childName.equals("id-mapping")) { 168 String namedefName = ((Element) child).getAttribute("link-end"); 170 NameDef nd = genClassRef.getIdNameDef(namedefName); 172 if (nd.isNameRef()) { 173 Iterator it2 = nd.iterateField(); 174 while (it2.hasNext()) { 175 fosGenClassMapping.addFieldMapping((String ) it2.next()); 176 } 177 } else { 178 fosGenClassMapping.addFieldMapping(nd.getFieldName()); 179 } 180 if (logger.isLoggable(BasicLevel.DEBUG)) { 181 logger.log(BasicLevel.DEBUG, 182 "Fetching the ID name def of the GenClassRef " + namedefName); 183 if (nd.isFieldName()) 184 logger.log(BasicLevel.DEBUG, "fieldName: " + nd.getFieldName()); 185 else if (nd.isNameRef()) 186 logger.log(BasicLevel.DEBUG, "NameRef: " + nd.getNameRef()); 187 } 188 fosGenClassMapping.createIdentifierMapping(nd); 190 } else if (childName.equals("fos-ref-mapping")) { 191 String namedefName = ((Element) child).getAttribute("link-end"); 192 NameDef nd = null; 193 if (genClassRef.isPrimitive()) { 195 logger.log(BasicLevel.WARN, "You define a generic class of " + 196 "primitive and a useless name-def for the elements: " + 197 "generic class id=" + genClassRef.getGenClassId()); 198 continue; 199 } else if (genClassRef.isClassRef()) { 200 if (logger.isLoggable(BasicLevel.DEBUG)) { 201 logger.log(BasicLevel.DEBUG, 202 "Get the ref name def of the GenClassRef " + namedefName); 203 } 204 nd = genClassRef.getClassRef().getRefNameDef(namedefName); 205 if (nd.isNameRef()) { 206 Iterator it2 = nd.iterateField(); 207 while (it2.hasNext()) { 208 fosGenClassMapping.addFieldMapping((String ) it2.next()); 209 } 210 } else { 211 fosGenClassMapping.addFieldMapping(nd.getFieldName()); 212 } 213 } else if (genClassRef.isGenClassRef()) { 214 if (logger.isLoggable(BasicLevel.DEBUG)) { 215 logger.log(BasicLevel.DEBUG, 216 "Get the ref name def of the ClassRef " + namedefName); 217 } 218 nd = genClassRef.getGenClassRef().getRefNameDef(namedefName); 219 if (nd.isNameRef()) { 220 Iterator it2 = nd.iterateField(); 221 while (it2.hasNext()) { 222 fosGenClassMapping.addFieldMapping((String ) it2.next()); 223 } 224 } else { 225 fosGenClassMapping.addFieldMapping(nd.getFieldName()); 226 } 227 } 228 String ruleName = ((Element) child).getAttribute("rule-name"); 229 fosGenClassMapping.createReferenceMapping(ruleName, nd); 231 } else if (childName.equals("#text")) { 232 } else { 233 logger.log(BasicLevel.WARN, "element <" + childName + "> unknown !"); 234 } 235 if (logger.isLoggable(BasicLevel.DEBUG)) { 236 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 237 } 238 } 239 fosGenClassMapping.setDirName(dirname); 240 } 241 242 252 public void parseMapping(Element mappingElem, Mapping mapping) throws PException { 253 if (mappingElem.getNodeName().equals("fos-class-mapping")) { 254 parseClassMapping(mappingElem, mapping); 255 } else if (mappingElem.getNodeName().equals("fos-gen-class-mapping")) { 256 parseGenClassMapping(mappingElem, mapping); 257 } else { 258 throw new PException("Mapping element not supported by FOS: " + mappingElem.getNodeName()); 259 } 260 } 261 } 262 | Popular Tags |