r1251 - trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml
Author: agiraudet Date: 2013-05-24 17:19:46 +0200 (Fri, 24 May 2013) New Revision: 1251 Url: http://nuiton.org/projects/eugene/repository/revisions/1251 Log: correction de quelques bogues et ajout d'une nouvelle fonctionnalit?\195?\169 ?\195?\160 YamlToObjectModel.java : les imports sont d?\195?\169sormais g?\195?\169r?\195?\169s par une simple liste (comme dans Java) ex: java.lang.String -> remplacera tous les types String par java.lang.String Modified: trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlUtil.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java 2013-05-24 12:13:59 UTC (rev 1250) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java 2013-05-24 15:19:46 UTC (rev 1251) @@ -7,6 +7,7 @@ import java.io.*; import java.util.*; +import static org.nuiton.eugene.models.object.reader.yaml.YamlUtil.afterChar; import static org.nuiton.eugene.models.object.reader.yaml.YamlUtil.browseKeySetToLowerCase; /** @@ -20,20 +21,15 @@ protected String packageOM; protected Map<String, String> imports; protected Map<String, String> defaultValues; - protected boolean defaultVoid; //mots clefs - public static final String FILE_DEFAULT_VALUES = "yamlobjectmodel.default"; - public static final String DEFAULT = "default"; - public static final String SEPARATOR = "."; - public static final String ABSTRACT = "abstract"; public static final String ASSOCIATION_CLASS_NAME = "associationclassname"; public static final String ASSOCIATION_TYPE = "associationtype"; public static final String ATTRIBUTE = "attribute"; public static final String BODY_CODE = "bodeycode"; public static final String CLASS = "class"; - public static final String CLASS_ASSOCIATION = "classassociation"; + public static final String CLASS_ASSOCIATION = "associationclass"; public static final String COMMENTS = "comments"; public static final String DEFAULT_VALUE = "defaultvalue"; public static final String DOCUMENTATION = "documentation"; @@ -93,9 +89,12 @@ //TODO: gestion des labels ? -> non pas pour le moment (plutot pour la syntaxe "user friendly") //TODO: retourner boolean sur reussite chargement ? ou lever excpetions ? -> aucun des deux pour le moment //TODO: charger valeurs par defaut en fonction de la version de la syntaxe yaml (syntaxe: version), charger les valeurs à partir d'un fichier ou d'une classe ? -> a partir d'un fichier YAML + //TODO: mettre les valeurs par défaut en dur dans le code + //TODO: gérer les imports sous forme de List et récuperer le type + //TODO: pre-charger les noms et paquetages des classes pour les imports //if(valeur == null) -> charger valeur par defaut -charger celles de l'utilisateur ou celles du programme ou rien public void loadFile(File file, ObjectModel model) throws IOException { - //test - log + /*//test - log //chargement du fichier contenant les valeurs par defaut defaultValues = new LinkedHashMap<String, String>(); String defaultValuesPATH = file.getParent()+File.separator+FILE_DEFAULT_VALUES; @@ -124,15 +123,18 @@ { defaultVoid = Boolean.valueOf(String.valueOf(defaultValues.get(DEFAULT))); } - //test - log + *///test - log + //TODO: valeurs par defaut, faire interface ? ou simple map ? + defaultValues = new LinkedHashMap<String, String>(); + ObjectModelImpl modelOM = (ObjectModelImpl) model; InputStream inputModel = new FileInputStream(file); Object modelYAML = yaml.load(inputModel); inputModel.close(); List<String> ignore = new LinkedList<String>(); - ignore.add(IMPORTS); + //ignore.add(IMPORTS); ignore.add(TAG_VALUES); browseKeySetToLowerCase(modelYAML, ignore); @@ -167,55 +169,38 @@ //load imports imports = new LinkedHashMap<String, String>(); Object importsYAML = collectElement((List) modelYAML, IMPORTS); - if (importsYAML instanceof Map) { - for (Object entry : ((Map) importsYAML).entrySet()) { - if (entry instanceof Map.Entry) { - imports.put(String.valueOf(((Map.Entry) entry).getKey()), String.valueOf(((Map.Entry) entry).getValue())); - } + if(importsYAML instanceof List) + { + for(Object importYAML : (List) importsYAML) + { + imports.put(afterChar(String.valueOf(importYAML),'.'),String.valueOf(importYAML)); + log("import="+String.valueOf(importYAML)); } } //load package Object packageYAML = collectElement((List) modelYAML, PACKAGE); if (packageYAML == null) { - if(defaultValues.containsKey(PACKAGE)) - { - packageOM = defaultValues.get(PACKAGE); - } - else - { - ; - } + packageOM = ""; } else { packageOM = String.valueOf(packageYAML); } + log("package="+packageOM); Object nameYAML = collectElement((List) modelYAML, NAME); if (nameYAML == null) { - if(defaultValues.containsKey(NAME)) - { - modelOM.setName(defaultValues.get(NAME)); - } - else - { - ; - } + ; } else { modelOM.setName(String.valueOf(nameYAML)); + log("name="+String.valueOf(nameYAML)); } Object versionYAML = collectElement((List) modelYAML, VERSION); if (versionYAML == null) { - if(defaultValues.containsKey(VERSION)) - { - modelOM.setVersion(defaultValues.get(VERSION)); - } - else - { - ; - } + ; } else { modelOM.setVersion(String.valueOf(versionYAML)); + log("version="+String.valueOf(versionYAML)); } Object tagValuesYAML = collectElement((List) modelYAML, TAG_VALUES); @@ -223,6 +208,7 @@ for (Object entry : ((Map) tagValuesYAML).entrySet()) { if (entry instanceof Map.Entry) { modelOM.addTagValue(String.valueOf(((Map.Entry) entry).getKey()), String.valueOf(((Map.Entry) entry).getValue())); + log("tagValue=[key="+String.valueOf(((Map.Entry) entry).getKey())+" value="+String.valueOf(((Map.Entry) entry).getValue())+"]"); } } } @@ -232,6 +218,7 @@ ObjectModelClassImpl classOM = new ObjectModelClassImpl(); loadClass(classYAML, classOM); modelOM.addClass(classOM); + log("class="+classOM.getName()+" loaded"); } List interfacesYAML = collectAllElements((List) modelYAML, INTERFACE); @@ -239,6 +226,7 @@ ObjectModelInterfaceImpl interfaceOM = new ObjectModelInterfaceImpl(); loadInterface(interfaceYAML, interfaceOM); modelOM.addInterface(interfaceOM); + log("interface="+interfaceOM.getName()+" loaded"); } List classesAssociationYAML = collectAllElements((List) modelYAML, CLASS_ASSOCIATION); @@ -246,6 +234,7 @@ ObjectModelAssociationClassImpl classAssociationOM = new ObjectModelAssociationClassImpl(); loadAssociationClass(classAssociationYAML, classAssociationOM); modelOM.addAssociationClass(classAssociationOM); + log("classAssociation="+classAssociationOM.getName()+" loaded"); } List enumerationsYAML = collectAllElements((List) modelYAML, ENUMERATION); @@ -253,6 +242,7 @@ ObjectModelEnumerationImpl enumerationOM = new ObjectModelEnumerationImpl(); loadEnumeration(enumerationYAML, enumerationOM); modelOM.addEnumeration(enumerationOM); + log("enumeration="+enumerationOM.getName()+" loaded"); } } } @@ -265,33 +255,18 @@ Object nameYAML = collectElement((List) elementYAML, NAME); if (nameYAML == null) { - if(defaultValues.containsKey(ELEMENT+SEPARATOR+NAME)) - { - elementOM.setName(defaultValues.get(ELEMENT+SEPARATOR+NAME)); - } - else - { - ; - } + ; } else { elementOM.setName(String.valueOf(nameYAML)); + log("name="+String.valueOf(nameYAML)); } Object staticYAML = collectElement((List) elementYAML, STATIC); if (staticYAML == null) { - if(defaultValues.containsKey(ELEMENT+SEPARATOR+STATIC)) - { - elementOM.setStatic(Boolean.valueOf(defaultValues.get(ELEMENT+SEPARATOR+STATIC))); - //log - log("default value loaded "+ELEMENT+SEPARATOR+STATIC+"="+defaultValues.containsKey(ELEMENT+SEPARATOR+STATIC)); - //log - } - else - { - ; - } + ; } else { elementOM.setStatic(Boolean.valueOf(String.valueOf(staticYAML))); + log("static="+(Boolean.valueOf(String.valueOf(staticYAML)))); } Object documentationYAML = collectElement((List) elementYAML, DOCUMENTATION); @@ -299,6 +274,7 @@ ; } else { elementOM.setDocumentation(String.valueOf(documentationYAML)); + log("documentation="+String.valueOf(documentationYAML)); } Object tagValuesYAML = collectElement((List) elementYAML, TAG_VALUES); @@ -306,6 +282,7 @@ for (Object entry : ((Map) tagValuesYAML).entrySet()) { if (entry instanceof Map.Entry) { elementOM.addTagValue(String.valueOf(((Map.Entry) entry).getKey()), String.valueOf(((Map.Entry) entry).getValue())); + log("tagValue=[key="+String.valueOf(((Map.Entry) entry).getKey())+" value="+String.valueOf(((Map.Entry) entry).getValue())+"]"); } } } @@ -314,6 +291,7 @@ if (commentsYAML instanceof List) { for (Object comment : (List) commentsYAML) { elementOM.addComment(String.valueOf(comment)); + log("comment="+String.valueOf(comment)); } } @@ -324,9 +302,7 @@ ObjectModelImplRef stereotypeOM = new ObjectModelImplRef(); stereotypeOM.setName(String.valueOf(stereotype)); elementOM.addStereotype(stereotypeOM); - //log - log("stereotype="+stereotypeOM.getName()); - //log + log("stereotype="+String.valueOf(stereotype)); } } } @@ -344,6 +320,7 @@ classifierOM.setPackage(packageOM); } else { classifierOM.setPackage(String.valueOf(packageYAML)); + log("package="+String.valueOf(packageYAML)); } Object externYAML = collectElement((List) classifierYAML, EXTERN); @@ -351,6 +328,7 @@ ; } else { classifierOM.setExtern(Boolean.valueOf(String.valueOf(externYAML))); + log("extern="+String.valueOf(externYAML)); } Object innerYAML = collectElement((List) classifierYAML, INNER); @@ -358,6 +336,7 @@ ; } else { classifierOM.setInner(Boolean.valueOf(String.valueOf(innerYAML))); + log("inner="+String.valueOf(innerYAML)); } Object typeYAML = collectElement((List) classifierYAML, TYPE); @@ -365,6 +344,7 @@ ; } else { classifierOM.setType(String.valueOf(typeYAML)); + log("type="+String.valueOf(typeYAML)); } List attributesYAML = collectAllElements((List) classifierYAML, ATTRIBUTE); @@ -372,6 +352,7 @@ ObjectModelAttributeImpl attributeOM = new ObjectModelAttributeImpl(); loadAttribute(attributeYAML, attributeOM); classifierOM.addAttribute(attributeOM); + log("attribute="+attributeOM.getType()+" loaded"); } List operationsYAML = collectAllElements((List) classifierYAML, OPERATION); @@ -379,6 +360,7 @@ ObjectModelOperationImpl operationOM = new ObjectModelOperationImpl(); loadOperation(operationYAML, operationOM); classifierOM.addOperation(operationOM); + log("operation="+operationOM.getName()+" loaded"); } Object superInterfacesYAML = collectElement((List) classifierYAML, SUPER_INTERFACES); @@ -387,6 +369,7 @@ ObjectModelImplRef superInterfaceOM = new ObjectModelImplRef(); superInterfaceOM.setName(String.valueOf(superInterface)); classifierOM.addInterface(superInterfaceOM); + log("superInterface="+String.valueOf(superInterface)); } } } @@ -403,6 +386,7 @@ ; } else { classOM.setAbstract(Boolean.valueOf(String.valueOf(abstractYAML))); + log("abstract="+String.valueOf(abstractYAML)); } Object superClassesYAML = collectElement((List) classYAML, SUPER_CLASSES); @@ -412,6 +396,7 @@ //ObjectModelImplRef superClassOM = new ObjectModelImplRef(); superClassOM.setName(String.valueOf(superClass)); classOM.addSuperclass(superClassOM); + log("superClass="+String.valueOf(superClass)); } } } @@ -423,6 +408,7 @@ } } + //TODO: gerer participants sous forme de liste -ou pas ? "attribute" utile ?- public void loadAssociationClass(Object associationClassYAML, ObjectModelAssociationClassImpl associationClassOM) { if (associationClassYAML instanceof List) { loadClass(associationClassYAML, associationClassOM); @@ -434,11 +420,13 @@ ObjectModeImplAssociationClassParticipant participantOM = new ObjectModeImplAssociationClassParticipant(); participantOM.setAssociationClass(associationClassOM); + //le nom de la classe sans oublier la package devant Object nameYAML = collectElement((List) participantYAML, NAME); if (nameYAML == null) { ; } else { participantOM.setName(String.valueOf(nameYAML)); + log("name="+String.valueOf(nameYAML)); } Object attributeYAML = collectElement((List) participantYAML, ATTRIBUTE); @@ -446,11 +434,10 @@ ; } else { participantOM.setAttribute(String.valueOf(attributeYAML)); + log("attribute="+String.valueOf(attributeYAML)); } - + log("participant="+String.valueOf(nameYAML)+" loaded"); associationClassOM.addParticipant(participantOM); - - log("associationClass"); } } } @@ -466,6 +453,7 @@ ObjectModelImplRef literalOM = new ObjectModelImplRef(); literalOM.setName(String.valueOf(literalYAML)); enumerationOM.addLiteral(literalOM); + log("literal="+String.valueOf(literalsYAML)); } } } @@ -480,11 +468,12 @@ ; } else { parameterOM.setOrdering(String.valueOf(orderingYAML)); + log("ordering="+String.valueOf(orderingYAML)); } Object typeYAML = collectElement((List) parameterYAML, TYPE); if (typeYAML == null) { - ; + ;//lever exception } else { String type = String.valueOf(typeYAML); if (imports.containsKey(type)) { @@ -492,6 +481,7 @@ } else { parameterOM.setType(type); } + log("type="+parameterOM.getType()); } Object defaultValueYAML = collectElement((List) parameterYAML, DEFAULT_VALUE); @@ -499,13 +489,15 @@ ; } else { parameterOM.setDefaultValue(String.valueOf(defaultValueYAML)); + log("defaultValue="+String.valueOf(defaultValueYAML)); } Object minMultiplicityYAML = collectElement((List) parameterYAML, MIN_MULTIPLICITY); if (minMultiplicityYAML == null) { - ; + ;//deja instancié à 1 } else { parameterOM.setMinMultiplicity(Integer.valueOf(String.valueOf(minMultiplicityYAML))); + log("minMultiplicity="+Integer.valueOf(String.valueOf(minMultiplicityYAML))); } Object maxMultiplicityYAML = collectElement((List) parameterYAML, MAX_MULTIPLICITY); @@ -513,6 +505,7 @@ ; } else { parameterOM.setMaxMultiplicity(Integer.valueOf(String.valueOf(maxMultiplicityYAML))); + log("maxMultiplicity="+String.valueOf(maxMultiplicityYAML)); } Object orderedYAML = collectElement((List) parameterYAML, ORDERED); @@ -520,6 +513,7 @@ ; } else { parameterOM.setOrdered(Boolean.valueOf(String.valueOf(orderedYAML))); + log("ordered="+Boolean.valueOf(String.valueOf(orderedYAML))); } Object uniqueYAML = collectElement((List) parameterYAML, UNIQUE); @@ -527,6 +521,7 @@ ; } else { parameterOM.setUnique(Boolean.valueOf(String.valueOf(uniqueYAML))); + log("unique="+Boolean.valueOf(String.valueOf(uniqueYAML))); } } } @@ -540,13 +535,15 @@ ; } else { attributeOM.setNavigable(Boolean.valueOf(String.valueOf(navigableYAML))); + log("navigable="+Boolean.valueOf(String.valueOf(navigableYAML))); } Object associationTypeYAML = collectElement((List) attributeYAML, ASSOCIATION_TYPE); if (associationTypeYAML == null) { - ; + ;//attributeOM.setAssociationType(ObjectModelAttributeImpl.ATTRIBUTE_TYPE_COMPOSITE); } else { attributeOM.setAssociationType(String.valueOf(associationTypeYAML)); + log("associationType="+String.valueOf(associationTypeYAML)); } Object finalYAML = collectElement((List) attributeYAML, FINAL); @@ -554,6 +551,7 @@ ; } else { attributeOM.setFinal(Boolean.valueOf(String.valueOf(finalYAML))); + log("final="+Boolean.valueOf(String.valueOf(finalYAML))); } Object staticYAML = collectElement((List) attributeYAML, STATIC); @@ -561,6 +559,7 @@ ; } else { attributeOM.setStatic(Boolean.valueOf(String.valueOf(staticYAML))); + log("static="+Boolean.valueOf(String.valueOf(staticYAML))); } Object associationClassNameYAML = collectElement((List) attributeYAML, ASSOCIATION_CLASS_NAME); @@ -568,20 +567,23 @@ ; } else { attributeOM.setAssociationClassName(String.valueOf(associationClassNameYAML)); + log("associationClassName="+String.valueOf(associationClassNameYAML)); } Object reverseAttributeNameYAML = collectElement((List) attributeYAML, REVERSE_ATTRIBUTE_NAME); if (reverseAttributeNameYAML == null) { - ; + ;//attributeOM.setReverseAttributeName(""); } else { attributeOM.setReverseAttributeName(String.valueOf(reverseAttributeNameYAML)); + log("reverseAttributeName="+String.valueOf(reverseAttributeNameYAML)); } Object reverseMaxMultiplicityYAML = collectElement((List) attributeYAML, REVERSE_MAX_MULTIPLICITY); if (reverseMaxMultiplicityYAML == null) { - ; + ;//instancié à -1 } else { attributeOM.setReverseMaxMultiplicity(Integer.valueOf(String.valueOf(reverseMaxMultiplicityYAML))); + log("reverseMaxMultiplicity="+Integer.valueOf(String.valueOf(reverseMaxMultiplicityYAML))); } @@ -590,6 +592,7 @@ ; } else { attributeOM.setTransient(Boolean.valueOf(String.valueOf(transientYAML))); + log("transient="+Boolean.valueOf(String.valueOf(transientYAML))); } Object visibilityYAML = collectElement((List) attributeYAML, VISIBILITY); @@ -597,6 +600,7 @@ ; } else { attributeOM.setVisibility(String.valueOf(visibilityYAML)); + log("visibility="+String.valueOf(visibilityYAML)); } } } @@ -612,6 +616,7 @@ ; } else { operationOM.setAbstract(Boolean.valueOf(String.valueOf(abstractYAML))); + log("abstract="+Boolean.valueOf(String.valueOf(abstractYAML))); } Object visibilityYAML = collectElement((List) operationYAML, VISIBILITY); @@ -619,15 +624,17 @@ ; } else { operationOM.setVisibility(String.valueOf(visibilityYAML)); + log("visibility="+String.valueOf(visibilityYAML)); } Object returnParameterYAML = collectElement((List) operationYAML, RETURN_PARAMETER); if (returnParameterYAML == null) { ; } else { - ObjectModelParameterImpl parameterOM = new ObjectModelAttributeImpl(); + ObjectModelParameterImpl parameterOM = new ObjectModelParameterImpl(); loadParameter(returnParameterYAML, parameterOM); operationOM.setReturnParameter(parameterOM); + log("returnParameter="+parameterOM.getType()+" loaded"); } Object bodyCodeYAML = collectElement((List) operationYAML, BODY_CODE); @@ -635,6 +642,7 @@ ; } else { operationOM.setBodyCode(String.valueOf(bodyCodeYAML)); + log("bodyCode="+String.valueOf(bodyCodeYAML)); } List parametersYAML = collectAllElements((List) operationYAML, PARAMETER); @@ -642,6 +650,7 @@ ObjectModelParameterImpl parameterOM = new ObjectModelAttributeImpl(); loadParameter(parameterYAML, parameterOM); operationOM.addParameter(parameterOM); + log("parameter="+parameterOM.getType()+" loaded"); } } } Modified: trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlUtil.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlUtil.java 2013-05-24 12:13:59 UTC (rev 1250) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlUtil.java 2013-05-24 15:19:46 UTC (rev 1251) @@ -8,9 +8,9 @@ * Time: 11:53 */ public class YamlUtil { - - public static void keySetToLowerCase(Map map) { - List keys = new ArrayList(map.keySet()); + //TODO: caster proprement + public static void keySetToLowerCase(Map<Object, Object> map) { + List<Object> keys = new ArrayList<Object>(map.keySet()); for (Object obj : keys) { String key = String.valueOf(obj).toLowerCase(); Object value = map.get(obj); @@ -19,9 +19,9 @@ } } - public static void browseKeySetToLowerCase(Object obj, List ignore) { + public static void browseKeySetToLowerCase(Object obj, List<String> ignore) { if (obj instanceof Map) { - keySetToLowerCase((Map) obj); + keySetToLowerCase((Map<Object, Object>) obj); for (Object key : ((Map) obj).keySet()) { //tester si String ou caster avec valueOf @@ -35,4 +35,26 @@ } } } + + //retourne la chaîne se trouvant avant le caractère de la chaîne passée en paramètre + //exemple: beforeChar("1-2-3",'-') -> "1" + public static String beforeChar(String str, Character target) { + StringBuilder tmp = new StringBuilder(str.length() + 1); + for (Character c : str.toCharArray()) { + if (c.equals(target)) { + return tmp.toString(); + } else { + tmp.append(c); + } + } + return tmp.toString(); + } + + //retourne la chaîne se trouvant après le caractère de la chaîne passée en paramètre + //exemple: afterChar("/boot/vmlinuz",'/') -> "vmlinuz" + public static String afterChar(String str, Character target) { + StringBuilder in = new StringBuilder(str); + StringBuilder out = new StringBuilder(beforeChar(in.reverse().toString(), target)); + return out.reverse().toString(); + } }
participants (1)
-
agiraudet@users.nuiton.org