r1247 - trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml
Author: agiraudet Date: 2013-05-15 17:59:44 +0200 (Wed, 15 May 2013) New Revision: 1247 Url: http://nuiton.org/projects/eugene/repository/revisions/1247 Log: ajout de YamlUtil.java Added: 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 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-15 15:38:03 UTC (rev 1246) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java 2013-05-15 15:59:44 UTC (rev 1247) @@ -8,15 +8,19 @@ import java.util.*; import static org.nuiton.eugene.models.object.reader.yaml.YamlUtil.browseKeySetToLowerCase; + /** * User: agiraudet * Date: 15/05/13 * Time: 11:15 */ + +//TODO: gestion des labels + public class YamlToObjectModel { protected Yaml yaml; protected String packageOM; - protected Map<String,String> imports; + protected Map<String, String> imports; //mots clefs public static final String IMPORTS = "imports"; @@ -62,17 +66,15 @@ private boolean _log = false; + private void log(String str) throws IOException { FileWriter log = null; String path = "/tmp/log.yaml.txt"; - if(_log) - { + if (_log) { log = new FileWriter(path, _log); log.write("[LOG] " + str + "\n"); log.close(); - } - else - { + } else { log = new FileWriter(path, _log); log.write("[LOG] start v1.0\n"); log.close(); @@ -80,8 +82,7 @@ } } - public YamlToObjectModel() - { + public YamlToObjectModel() { yaml = new Yaml(); } @@ -96,7 +97,7 @@ ignore.add("tagvalues"); browseKeySetToLowerCase(modelYAML, ignore); - loadModel(modelYAML,modelOM); + loadModel(modelYAML, modelOM); } public Object collectElement(List data, Object key) { @@ -122,96 +123,75 @@ return res; } - public void loadModel(Object modelYAML, ObjectModelImpl modelOM) - { - if(modelYAML instanceof List) - { + public void loadModel(Object modelYAML, ObjectModelImpl modelOM) { + if (modelYAML instanceof List) { //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 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())); } } } //load package Object packageYAML = collectElement((List) modelYAML, PACKAGE); - if(packageYAML == null) - { + if (packageYAML == null) { ; - } - else - { + } else { packageOM = String.valueOf(packageYAML); } Object nameYAML = collectElement((List) modelYAML, NAME); - if(nameYAML == null) - { + if (nameYAML == null) { ; - } - else - { + } else { modelOM.setName(String.valueOf(nameYAML)); } Object versionYAML = collectElement((List) modelYAML, VERSION); - if(versionYAML == null) - { + if (versionYAML == null) { ; - } - else - { + } else { modelOM.setVersion(String.valueOf(versionYAML)); } Object tagValuesYAML = collectElement((List) modelYAML, TAG_VALUES); - if(tagValuesYAML instanceof Map) - { - 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())); + if (tagValuesYAML instanceof Map) { + 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())); } } } List classesYAML = collectAllElements((List) modelYAML, CLASS); - for(Object classYAML : classesYAML) - { + for (Object classYAML : classesYAML) { ObjectModelClassImpl classOM = new ObjectModelClassImpl(); - loadClass(classYAML,classOM); + loadClass(classYAML, classOM); modelOM.addClass(classOM); } List interfacesYAML = collectAllElements((List) modelYAML, INTERFACE); - for(Object interfaceYAML : interfacesYAML) - { + for (Object interfaceYAML : interfacesYAML) { ObjectModelInterfaceImpl interfaceOM = new ObjectModelInterfaceImpl(); - loadInterface(interfaceYAML,interfaceOM); + loadInterface(interfaceYAML, interfaceOM); modelOM.addInterface(interfaceOM); } List classesAssociationYAML = collectAllElements((List) modelYAML, CLASS_ASSOCIATION); - for(Object classAssociationYAML : classesAssociationYAML) - { + for (Object classAssociationYAML : classesAssociationYAML) { ObjectModelAssociationClassImpl classAssociationOM = new ObjectModelAssociationClassImpl(); - loadAssociationClass(classAssociationYAML,classAssociationOM); + loadAssociationClass(classAssociationYAML, classAssociationOM); modelOM.addAssociationClass(classAssociationOM); } List enumerationsYAML = collectAllElements((List) modelYAML, ENUMERATION); - for(Object enumerationYAML : enumerationsYAML) - { + for (Object enumerationYAML : enumerationsYAML) { ObjectModelEnumerationImpl enumerationOM = new ObjectModelEnumerationImpl(); - loadEnumeration(enumerationYAML,enumerationOM); + loadEnumeration(enumerationYAML, enumerationOM); modelOM.addEnumeration(enumerationOM); } @@ -219,71 +199,53 @@ //return true/false -> chargement reussi ? } - public void loadElement(Object elementYAML, ObjectModelElementImpl elementOM) - { - if(elementYAML instanceof List) - { + public void loadElement(Object elementYAML, ObjectModelElementImpl elementOM) { + if (elementYAML instanceof List) { //TODO: setDeclaringElement //TODO: setObjectModelImpl //TODO: addModifier Object nameYAML = collectElement((List) elementYAML, NAME); - if(nameYAML == null) - { + if (nameYAML == null) { ; - } - else - { + } else { elementOM.setName(String.valueOf(nameYAML)); } Object staticYAML = collectElement((List) elementYAML, STATIC); - if(staticYAML == null) - { + if (staticYAML == null) { ; - } - else - { + } else { elementOM.setStatic(Boolean.valueOf(String.valueOf(staticYAML))); } Object documentationYAML = collectElement((List) elementYAML, DOCUMENTATION); - if(documentationYAML == null) - { + if (documentationYAML == null) { ; - } - else - { + } else { elementOM.setDocumentation(String.valueOf(documentationYAML)); } Object tagValuesYAML = collectElement((List) elementYAML, TAG_VALUES); - if(tagValuesYAML instanceof Map) - { - 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())); + if (tagValuesYAML instanceof Map) { + 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())); } } } Object commentsYAML = collectElement((List) elementYAML, COMMENTS); - if(commentsYAML instanceof List) - { - for(Object comment : (List) commentsYAML) - { + if (commentsYAML instanceof List) { + for (Object comment : (List) commentsYAML) { elementOM.addComment(String.valueOf(comment)); } } Object stereotypesYAML = collectElement((List) elementYAML, STEREOTYPES); { - if(stereotypesYAML instanceof List) - { - for(Object stereotype : (List) stereotypesYAML) - { + if (stereotypesYAML instanceof List) { + for (Object stereotype : (List) stereotypesYAML) { ObjectModelImplRef stereotypeOM = new ObjectModelImplRef(); stereotypeOM.setName(String.valueOf(stereotype)); elementOM.addStereotype(stereotypeOM); @@ -293,75 +255,57 @@ } } - public void loadClassifier(Object classifierYAML, ObjectModelClassifierImpl classifierOM) - { - if(classifierYAML instanceof List) - { + public void loadClassifier(Object classifierYAML, ObjectModelClassifierImpl classifierOM) { + if (classifierYAML instanceof List) { //TODO: addDependency loadElement(classifierYAML, classifierOM); Object packageYAML = collectElement((List) classifierYAML, PACKAGE); - if(packageYAML == null) - { + if (packageYAML == null) { classifierOM.setPackage(packageOM); - } - else - { + } else { classifierOM.setPackage(String.valueOf(packageYAML)); } Object externYAML = collectElement((List) classifierYAML, EXTERN); - if(externYAML == null) - { + if (externYAML == null) { ; - } - else - { + } else { classifierOM.setExtern(Boolean.valueOf(String.valueOf(externYAML))); } Object innerYAML = collectElement((List) classifierYAML, INNER); - if(innerYAML == null) - { + if (innerYAML == null) { ; - } - else - { + } else { classifierOM.setInner(Boolean.valueOf(String.valueOf(innerYAML))); } Object typeYAML = collectElement((List) classifierYAML, TYPE); - if(typeYAML == null) - { + if (typeYAML == null) { ; - } - else - { + } else { classifierOM.setType(String.valueOf(typeYAML)); } List attributesYAML = collectAllElements((List) classifierYAML, ATTRIBUTE); - for(Object attributeYAML : attributesYAML) - { + for (Object attributeYAML : attributesYAML) { ObjectModelAttributeImpl attributeOM = new ObjectModelAttributeImpl(); loadAttribute(attributeYAML, attributeOM); classifierOM.addAttribute(attributeOM); } List operationsYAML = collectAllElements((List) classifierYAML, OPERATION); - for(Object operationYAML : operationsYAML) - { + for (Object operationYAML : operationsYAML) { ObjectModelOperationImpl operationOM = new ObjectModelOperationImpl(); loadOperation(operationYAML, operationOM); classifierOM.addOperation(operationOM); } Object superInterfacesYAML = collectElement((List) classifierYAML, SUPER_INTERFACES); - if(superInterfacesYAML instanceof List) - { - for(Object superInterface : (List) superInterfacesYAML) - { + if (superInterfacesYAML instanceof List) { + for (Object superInterface : (List) superInterfacesYAML) { ObjectModelImplRef superInterfaceOM = new ObjectModelImplRef(); superInterfaceOM.setName(String.valueOf(superInterface)); classifierOM.addInterface(superInterfaceOM); @@ -370,29 +314,22 @@ } } - public void loadClass(Object classYAML, ObjectModelClassImpl classOM) - { - if(classYAML instanceof List) - { + public void loadClass(Object classYAML, ObjectModelClassImpl classOM) { + if (classYAML instanceof List) { //TODO: addInnerClassifier loadClassifier(classYAML, classOM); Object abstractYAML = collectElement((List) classYAML, ABSTRACT); - if(abstractYAML == null) - { + if (abstractYAML == null) { ; - } - else - { + } else { classOM.setAbstract(Boolean.valueOf(String.valueOf(abstractYAML))); } Object superClassesYAML = collectElement((List) classYAML, SUPER_CLASSES); - if(superClassesYAML instanceof List) - { - for(Object superClass : (List) superClassesYAML) - { + if (superClassesYAML instanceof List) { + for (Object superClass : (List) superClassesYAML) { ObjectModelImplRef superClassOM = new ObjectModelImplRef(); superClassOM.setName(String.valueOf(superClass)); classOM.addSuperclass(superClassOM); @@ -401,40 +338,31 @@ } } - public void loadInterface(Object interfaceYAML, ObjectModelInterfaceImpl interfaceOM) - { - if(interfaceYAML instanceof List) - { + public void loadInterface(Object interfaceYAML, ObjectModelInterfaceImpl interfaceOM) { + if (interfaceYAML instanceof List) { loadClassifier(interfaceYAML, interfaceOM); } } - public void loadAssociationClass(Object associationClassYAML, ObjectModelAssociationClassImpl associationClassOM) - { - if(associationClassYAML instanceof List) - { + public void loadAssociationClass(Object associationClassYAML, ObjectModelAssociationClassImpl associationClassOM) { + if (associationClassYAML instanceof List) { loadClass(associationClassYAML, associationClassOM); - List participantsYAML = collectAllElements((List) associationClassYAML, PARTICIPANT); - for(Object participantYAML : participantsYAML) - { + List participantsYAML = collectAllElements((List) associationClassYAML, PARTICIPANT); + for (Object participantYAML : participantsYAML) { ObjectModeImplAssociationClassParticipant participant = new ObjectModeImplAssociationClassParticipant(); //TODO: } } } - public void loadEnumeration(Object enumerationYAML, ObjectModelEnumerationImpl enumerationOM) - { - if(enumerationYAML instanceof List) - { + public void loadEnumeration(Object enumerationYAML, ObjectModelEnumerationImpl enumerationOM) { + if (enumerationYAML instanceof List) { loadElement(enumerationYAML, enumerationOM); Object literalsYAML = collectElement((List) enumerationYAML, LITERAL); - if(literalsYAML instanceof List) - { - for(Object literalYAML : (List) literalsYAML) - { + if (literalsYAML instanceof List) { + for (Object literalYAML : (List) literalsYAML) { ObjectModelImplRef literalOM = new ObjectModelImplRef(); literalOM.setName(String.valueOf(literalYAML)); enumerationOM.addLiteral(literalOM); @@ -443,249 +371,177 @@ } } - public void loadParameter(Object parameterYAML, ObjectModelParameterImpl parameterOM) - { - if(parameterYAML instanceof List) - { + public void loadParameter(Object parameterYAML, ObjectModelParameterImpl parameterOM) { + if (parameterYAML instanceof List) { loadElement(parameterYAML, parameterOM); Object orderingYAML = collectElement((List) parameterYAML, ORDERING); - if(orderingYAML == null) - { + if (orderingYAML == null) { ; - } - else - { + } else { parameterOM.setOrdering(String.valueOf(orderingYAML)); } Object typeYAML = collectElement((List) parameterYAML, TYPE); - if(typeYAML == null) - { + if (typeYAML == null) { ; - } - else - { + } else { String type = String.valueOf(typeYAML); - if(imports.containsKey(type)) - { + if (imports.containsKey(type)) { parameterOM.setType(imports.get(type)); - } - else - { + } else { parameterOM.setType(type); } } Object defaultValueYAML = collectElement((List) parameterYAML, DEFAULT_VALUE); - if(defaultValueYAML == null) - { + if (defaultValueYAML == null) { ; - } - else - { + } else { parameterOM.setDefaultValue(String.valueOf(defaultValueYAML)); } Object minMultiplicityYAML = collectElement((List) parameterYAML, MIN_MULTIPLICITY); - if(minMultiplicityYAML == null) - { + if (minMultiplicityYAML == null) { ; - } - else - { + } else { parameterOM.setMinMultiplicity(Integer.valueOf(String.valueOf(minMultiplicityYAML))); } Object maxMultiplicityYAML = collectElement((List) parameterYAML, MAX_MULTIPLICITY); - if(maxMultiplicityYAML == null) - { + if (maxMultiplicityYAML == null) { ; - } - else - { + } else { parameterOM.setMaxMultiplicity(Integer.valueOf(String.valueOf(maxMultiplicityYAML))); } Object orderedYAML = collectElement((List) parameterYAML, ORDERED); - if(orderedYAML == null) - { + if (orderedYAML == null) { ; - } - else - { + } else { parameterOM.setOrdered(Boolean.valueOf(String.valueOf(orderedYAML))); } Object uniqueYAML = collectElement((List) parameterYAML, UNIQUE); - if(uniqueYAML == null) - { + if (uniqueYAML == null) { ; - } - else - { + } else { parameterOM.setUnique(Boolean.valueOf(String.valueOf(uniqueYAML))); } } } - public void loadAttribute(Object attributeYAML, ObjectModelAttributeImpl attributeOM) - { - if(attributeYAML instanceof List) - { + public void loadAttribute(Object attributeYAML, ObjectModelAttributeImpl attributeOM) { + if (attributeYAML instanceof List) { loadParameter(attributeYAML, attributeOM); Object navigableYAML = collectElement((List) attributeYAML, NAVIGABLE); - if(navigableYAML == null) - { + if (navigableYAML == null) { ; - } - else - { + } else { attributeOM.setNavigable(Boolean.valueOf(String.valueOf(navigableYAML))); } Object associationTypeYAML = collectElement((List) attributeYAML, ASSOCIATION_TYPE); - if(associationTypeYAML == null) - { + if (associationTypeYAML == null) { ; - } - else - { + } else { attributeOM.setAssociationType(String.valueOf(associationTypeYAML)); } Object finalYAML = collectElement((List) attributeYAML, FINAL); - if(finalYAML == null) - { + if (finalYAML == null) { ; - } - else - { + } else { attributeOM.setFinal(Boolean.valueOf(String.valueOf(finalYAML))); } Object staticYAML = collectElement((List) attributeYAML, STATIC); - if(staticYAML == null) - { + if (staticYAML == null) { ; - } - else - { + } else { attributeOM.setStatic(Boolean.valueOf(String.valueOf(staticYAML))); } Object associationClassNameYAML = collectElement((List) attributeYAML, ASSOCIATION_CLASS_NAME); - if(associationClassNameYAML == null) - { + if (associationClassNameYAML == null) { ; - } - else - { + } else { attributeOM.setAssociationClassName(String.valueOf(associationClassNameYAML)); } Object reverseAttributeNameYAML = collectElement((List) attributeYAML, REVERSE_ATTRIBUTE_NAME); - if(reverseAttributeNameYAML == null) - { + if (reverseAttributeNameYAML == null) { ; - } - else - { + } else { attributeOM.setReverseAttributeName(String.valueOf(reverseAttributeNameYAML)); } Object reverseMaxMultiplicityYAML = collectElement((List) attributeYAML, REVERSE_MAX_MULTIPLICITY); - if(reverseMaxMultiplicityYAML == null) - { + if (reverseMaxMultiplicityYAML == null) { ; - } - else - { + } else { attributeOM.setReverseMaxMultiplicity(Integer.valueOf(String.valueOf(reverseMaxMultiplicityYAML))); } Object transientYAML = collectElement((List) attributeYAML, TRANSIENT); - if(transientYAML == null) - { + if (transientYAML == null) { ; - } - else - { + } else { attributeOM.setTransient(Boolean.valueOf(String.valueOf(transientYAML))); } Object visibilityYAML = collectElement((List) attributeYAML, VISIBILITY); - if(visibilityYAML == null) - { + if (visibilityYAML == null) { ; - } - else - { + } else { attributeOM.setVisibility(String.valueOf(visibilityYAML)); } } } - public void loadOperation(Object operationYAML, ObjectModelOperationImpl operationOM) - { - if(operationYAML instanceof List) - { + public void loadOperation(Object operationYAML, ObjectModelOperationImpl operationOM) { + if (operationYAML instanceof List) { //TODO: addExceptionParameter loadElement(operationYAML, operationOM); Object abstractYAML = collectElement((List) operationYAML, ABSTRACT); - if(abstractYAML == null) - { + if (abstractYAML == null) { ; - } - else - { + } else { operationOM.setAbstract(Boolean.valueOf(String.valueOf(abstractYAML))); } Object visibilityYAML = collectElement((List) operationYAML, VISIBILITY); - if(visibilityYAML == null) - { + if (visibilityYAML == null) { ; - } - else - { + } else { operationOM.setVisibility(String.valueOf(visibilityYAML)); } Object returnParameterYAML = collectElement((List) operationYAML, RETURN_PARAMETER); - if(returnParameterYAML == null) - { + if (returnParameterYAML == null) { ; - } - else - { + } else { ObjectModelParameterImpl parameterOM = new ObjectModelAttributeImpl(); loadParameter(returnParameterYAML, parameterOM); operationOM.setReturnParameter(parameterOM); } Object bodyCodeYAML = collectElement((List) operationYAML, BODY_CODE); - if(bodyCodeYAML == null) - { + if (bodyCodeYAML == null) { ; - } - else - { + } else { operationOM.setBodyCode(String.valueOf(bodyCodeYAML)); } List parametersYAML = collectAllElements((List) operationYAML, PARAMETER); - { - for(Object parameterYAML : parametersYAML) - { - ObjectModelParameterImpl parameterOM = new ObjectModelAttributeImpl(); - loadParameter(parameterYAML, parameterOM); - operationOM.addParameter(parameterOM); - } + for (Object parameterYAML : parametersYAML) { + ObjectModelParameterImpl parameterOM = new ObjectModelAttributeImpl(); + loadParameter(parameterYAML, parameterOM); + operationOM.addParameter(parameterOM); } } } Added: 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 (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlUtil.java 2013-05-15 15:59:44 UTC (rev 1247) @@ -0,0 +1,37 @@ +package org.nuiton.eugene.models.object.reader.yaml; + +import java.util.*; + +/** + * User: agiraudet + * Date: 14/05/13 + * Time: 11:53 + */ +public class YamlUtil { + + public static void keySetToLowerCase(Map map) { + List keys = new ArrayList(map.keySet()); + for (Object obj : keys) { + String key = String.valueOf(obj).toLowerCase(); + Object value = map.get(obj); + map.remove(obj); + map.put(key, value); + } + } + + public static void browseKeySetToLowerCase(Object obj, List ignore) { + if (obj instanceof Map) { + keySetToLowerCase((Map) obj); + + for (Object key : ((Map) obj).keySet()) { + if (!ignore.contains(key)) { + browseKeySetToLowerCase(((Map) obj).get(key), ignore); + } + } + } else if (obj instanceof List) { + for (Object tmp : (List) obj) { + browseKeySetToLowerCase(tmp, ignore); + } + } + } +}
participants (1)
-
agiraudet@users.nuiton.org