r1244 - in trunk: . eugene eugene/src/main/java/org/nuiton/eugene/models/object/reader eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml eugene-plantuml-templates eugene-plantuml-templates/src eugene-plantuml-templates/src/main eugene-plantuml-templates/src/main/java eugene-plantuml-templates/src/main/java/org eugene-plantuml-templates/src/main/java/org/nuiton eugene-plantuml-templates/src/main/java/org/nuiton/eugene eugene-plantuml-templates/src/main/java/org/nuiton/eugene/p
Author: agiraudet Date: 2013-05-13 12:01:52 +0200 (Mon, 13 May 2013) New Revision: 1244 Url: http://nuiton.org/projects/eugene/repository/revisions/1244 Log: ajout du package yaml dans eugene/src/main/java/org/nuiton/eugene/models/object/reader ajout de la classe YamlToObjectModel au package yaml : classe permettant de charger un ObjectModel en m?\195?\169moire ?\195?\160 partir d'un mod?\195?\168le sous forme de fichier yaml modification de la classe YamlObjectModelReader dans eugene/src/main/java/org/nuiton/eugene/models/object/reader : modification de la m?\195?\169thode readFileToModel qui appelle une m?\195?\169thode la classe YamlToObjectModel chargeant l'ObjectModel correspondant au fichier yaml pass?\195?\169 en param?\195?\168tre ajout d'une d?\195?\169pendance vers snakeyaml 1.12 dans eugene/pom.xml ajout du module eugene-plantuml-templates dans pom.xml ajout du module eugene-plantuml-templates : module permettant de g?\195?\169n?\195?\169rer un diagramme au format .plantuml puis .png lors de la compilation du projet Added: trunk/eugene-plantuml-templates/ trunk/eugene-plantuml-templates/LICENSE.txt trunk/eugene-plantuml-templates/README.txt trunk/eugene-plantuml-templates/changelog.txt trunk/eugene-plantuml-templates/pom.xml trunk/eugene-plantuml-templates/src/ trunk/eugene-plantuml-templates/src/main/ trunk/eugene-plantuml-templates/src/main/java/ trunk/eugene-plantuml-templates/src/main/java/org/ trunk/eugene-plantuml-templates/src/main/java/org/nuiton/ trunk/eugene-plantuml-templates/src/main/java/org/nuiton/eugene/ trunk/eugene-plantuml-templates/src/main/java/org/nuiton/eugene/plantuml/ trunk/eugene-plantuml-templates/src/main/java/org/nuiton/eugene/plantuml/PlantumlTemplatesGenerator.java trunk/eugene-plantuml-templates/src/main/resources/ trunk/eugene-plantuml-templates/src/test/ trunk/eugene-plantuml-templates/src/test/java/ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java Modified: trunk/eugene/pom.xml trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/YamlObjectModelReader.java trunk/pom.xml Modified: trunk/eugene/pom.xml =================================================================== --- trunk/eugene/pom.xml 2013-05-05 08:32:43 UTC (rev 1243) +++ trunk/eugene/pom.xml 2013-05-13 10:01:52 UTC (rev 1244) @@ -122,6 +122,12 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.yaml</groupId> + <artifactId>snakeyaml</artifactId> + <version>1.12</version> + </dependency> + </dependencies> <!-- ************************************************************* --> Modified: trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/YamlObjectModelReader.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/YamlObjectModelReader.java 2013-05-05 08:32:43 UTC (rev 1243) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/YamlObjectModelReader.java 2013-05-13 10:01:52 UTC (rev 1244) @@ -1,8 +1,7 @@ -package org.nuiton.eugene.models.object.reader; - /* * #%L * EUGene :: EUGene + * * $Id$ * $HeadURL$ * %% @@ -24,10 +23,14 @@ * #L% */ +package org.nuiton.eugene.models.object.reader; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.ModelHelper; import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.reader.yaml.YamlToObjectModel; +import org.yaml.snakeyaml.error.YAMLException; import java.io.File; import java.io.IOException; @@ -56,9 +59,17 @@ } @Override - protected void readFileToModel(File file, - ObjectModel model) throws IOException { - //TODO + protected void readFileToModel(File file, ObjectModel model) throws IOException { + try { + //TODO + //test + //NOTE: model deja instance + YamlToObjectModel tmp = new YamlToObjectModel(); + tmp.test(file, model); + //test + } catch (YAMLException e) { + throw new IOException("Unable to parse ObjectModel input file : " + file, e); + } } } Added: 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 (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/reader/yaml/YamlToObjectModel.java 2013-05-13 10:01:52 UTC (rev 1244) @@ -0,0 +1,229 @@ +package org.nuiton.eugene.models.object.reader.yaml; + +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.xml.*; +import org.yaml.snakeyaml.Yaml; + +import java.io.*; +import java.util.*; + +/** + * User: agiraudet + * Date: 06/05/13 + * Time: 12:08 + * <p/> + * Note1: le parsage se base sur la syntaxe 5-1uml.yaml + * Note2: surcharger les méthodes ou le langage pour les prochaines syntaxes ? + */ +public class YamlToObjectModel { + protected Object data; + protected Yaml yaml; + protected ObjectModelImpl model; + + private String _log = "/tmp/log.yaml.txt"; + + public YamlToObjectModel() { + //TODO : mettre toutes les clefs des Maps en minuscules + //TODO : instancier le parseur YAML (éviter une instance par fichier chargé) + this.startLog(); + } + + private void startLog() { + FileWriter log = null; + try { + log = new FileWriter(this._log, false); + log.write("[LOG] start\n"); + log.close(); + } catch (IOException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + private void writeLog(String str) { + FileWriter log = null; + try { + log = new FileWriter(this._log, true); + log.write("[LOG] " + str + "\n"); + log.close(); + } catch (IOException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + public void test(File file, ObjectModel _model) throws IOException { + //load file + InputStream input = new FileInputStream(file); + this.yaml = new Yaml(); + this.data = this.yaml.load(input); + input.close(); + //file loaded + + this.model = (ObjectModelImpl) _model; + + this.loadModel(); + + } + + //TODO : utiliser design pattern + //TODO : utiliser genericite + public void loadModel() { + if (this.data instanceof ArrayList) { + this.model.setName(String.valueOf(this.collectElement((List) this.data, "name"))); + this.model.setVersion(String.valueOf(this.collectElement((List) this.data, "version"))); + + List _classesYAML = this.collectAllElement((List) this.data, "class"); + for (Object _classYAML : _classesYAML) { + this.loadClass(_classYAML); + } + + } else { + ;//TODO : lever exception + } + } + + public void loadClass(Object _classYAML) { + if (_classYAML instanceof List) { + ObjectModelClassImpl _classOM = new ObjectModelClassImpl(); + + this.loadStereotypes((List) _classYAML, _classOM); + this.loadTagValues((List) _classYAML, _classOM); + this.loadComments((List) _classYAML, _classOM); + + _classOM.setName(String.valueOf(this.collectElement((List) _classYAML, "name"))); + //Object _final = this.collectElement((List) _classYAML, "final");//final not implemented by ObjectModel + Object _abstract = this.collectElement((List) _classYAML, "abstract"); + //TODO : utiliser operateur ternaire + if (_abstract instanceof Boolean) { + _classOM.setAbstract((Boolean) _abstract); + } else//sale + { + _classOM.setAbstract(Boolean.valueOf(String.valueOf(_abstract))); + } + + //TODO : loadAttributes + //TODO : loadOperations + + + this.model.addClass(_classOM); + } else { + ;//TODO : lever exception + } + } + + public void loadInterface(Object _interface) { + ;//TODO + } + + public void loadAssociationClass(Object _associationClass) { + ;//TODO + } + + public void loadEnumeration(Object _enumeration) { + ;//TODO + } + + public void loadAttribute(Object _attribute) { + ;//TODO + } + + public void loadOperation(Object _operation) { + ;//TODO + } + + public void loadStereotypes(List _data, ObjectModelElementImpl _element) { + Object stereotypes = this.collectElement(_data, "stereotype"); + if (stereotypes instanceof List) { + for (Object stereotype : (List) stereotypes) { + ObjectModelImplRef ref = new ObjectModelImplRef(); + ref.setName(String.valueOf(stereotype)); + _element.addStereotype(ref); + } + } + } + + //TODO : pour model + public void loadComments(List _data, ObjectModelElementImpl _element) { + Object comments = this.collectElement(_data, "comments"); + if (comments instanceof List) { + for (Object comment : (List) comments) { + _element.addComment(String.valueOf(comment)); + } + } + } + + //TODO : pour model + public void loadTagValues(List _data, ObjectModelElementImpl _element) { + Object tagValues = this.collectElement(_data, "tagValues"); + if (tagValues instanceof Map) { + for (Object entry : ((Map) tagValues).entrySet()) { + String key = String.valueOf(((Map.Entry) entry).getKey()); + String value = String.valueOf(((Map.Entry) entry).getValue()); + _element.addTagValue(key, value); + } + } + } + + public Object collectElement(List _data, Object _key) { + for (Object obj : _data) { + if (obj instanceof Map) { + if (((Map) obj).containsKey(_key)) { + return ((Map) obj).get(_key); + } + } + } + return null; + } + + public List collectList(List _data, Object _key) { + for (Object obj : _data) { + if (obj instanceof Map) { + if (((Map) obj).containsKey(_key)) { + if (((Map) obj).get(_key) instanceof List) { + return (List) ((Map) obj).get(_key); + /*List res = new LinkedList(); + for(Object tmp : (List) ((Map) obj).get(_key)) + { + res.add(tmp); + } + return res;*/ + } + } + } + } + return null; + } + + public Map collectMap(List _data, Object _key) { + for (Object obj : _data) { + if (obj instanceof Map) { + if (((Map) obj).containsKey(_key)) { + if (((Map) obj).get(_key) instanceof Map) { + return (Map) ((Map) obj).get(_key); + /*Map res = new LinkedHashMap(); + for(Object tmp : ((Map) ((Map) obj).get(_key)).entrySet()) + { + if(tmp instanceof Map.Entry)//ou cast + { + res.put(((Map.Entry) tmp).getKey(),((Map.Entry) tmp).getValue()); + } + } + return res;*/ + } + } + } + } + return null; + } + + public List collectAllElement(List _data, Object _key) { + List res = new LinkedList(); + for (Object obj : _data) { + if (obj instanceof Map) { + if (((Map) obj).containsKey(_key)) { + res.add(((Map) obj).get(_key)); + } + } + } + return res; + } +} Added: trunk/eugene-plantuml-templates/LICENSE.txt =================================================================== Added: trunk/eugene-plantuml-templates/README.txt =================================================================== Added: trunk/eugene-plantuml-templates/changelog.txt =================================================================== Added: trunk/eugene-plantuml-templates/pom.xml =================================================================== --- trunk/eugene-plantuml-templates/pom.xml (rev 0) +++ trunk/eugene-plantuml-templates/pom.xml 2013-05-13 10:01:52 UTC (rev 1244) @@ -0,0 +1,61 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + #%L + EUGene :: EUGene + + $HeadURL: http://svn.nuiton.org/svn/eugene/trunk/eugene-java-templates/pom.xml $ + %% + Copyright (C) 2004 - 2010 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.nuiton</groupId> + <artifactId>eugene</artifactId> + <version>2.6.3-SNAPSHOT</version> + </parent> + + <groupId>org.nuiton.eugene</groupId> + <artifactId>eugene-plantuml-templates</artifactId> + + <dependencies> + + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>eugene</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>net.sourceforge.plantuml</groupId> + <artifactId>plantuml</artifactId> + <version>7962</version> + </dependency> + + </dependencies> + + <name>EUGene :: PlantUML templates</name> + + <inceptionYear>2013</inceptionYear> + +</project> + + + Added: trunk/eugene-plantuml-templates/src/main/java/org/nuiton/eugene/plantuml/PlantumlTemplatesGenerator.java =================================================================== --- trunk/eugene-plantuml-templates/src/main/java/org/nuiton/eugene/plantuml/PlantumlTemplatesGenerator.java (rev 0) +++ trunk/eugene-plantuml-templates/src/main/java/org/nuiton/eugene/plantuml/PlantumlTemplatesGenerator.java 2013-05-13 10:01:52 UTC (rev 1244) @@ -0,0 +1,191 @@ +package org.nuiton.eugene.plantuml; + +import net.sourceforge.plantuml.GeneratedImage; +import net.sourceforge.plantuml.SourceFileReader; +import org.nuiton.eugene.models.object.*; + +import java.io.*; +import java.util.*; + +// http://maven-site.nuiton.org/eugene/objectmodel/ObjectModel_Interfaces.png +//TODO : Plexus ? + +public class PlantumlTemplatesGenerator extends ObjectModelGenerator { + + @Override + public void generateFromModel(Writer output, ObjectModel input) throws IOException { + /*//log + FileWriter log = new FileWriter(File.separator+"tmp"+File.separator+"test2.log"); + log.close(); + *///log + + // utiliser le systeme de templates a l'avenir + + Map<ObjectModelAttribute,ObjectModelClassifier> linksOME = new LinkedHashMap<ObjectModelAttribute, ObjectModelClassifier>(); + + // debut du fichier + output.write("@startuml\n\n"); + // parcours des classes + for(ObjectModelClass class_tmp : input.getClasses()) + { + // sale + String classStereotype = ""; + if(class_tmp.getStereotypes().size()>0) + { + classStereotype = "<<"; + for(String str : class_tmp.getStereotypes()) + { + classStereotype = classStereotype+","+str; + } + classStereotype = classStereotype+">>"; + } + /*if(class_tmp.getStereotypes().contains("entity")) + { + classStereotype = " <<entity>>"; + }*/ + String className = class_tmp.getName(); + // ajout de la classe + output.write("class "+className+classStereotype+" {\n"); + // parcours des attributs de la classe + for(ObjectModelAttribute attribute_tmp : class_tmp.getAttributes()) + { + String attributeName = attribute_tmp.getName(); + //String attributeType = afterLastPoint(attribute_tmp.getType()); + String attributeType = attribute_tmp.getType(); + // si l'attribut n'est lie a aucune classe + if(attribute_tmp.referenceClassifier()) + { + // ajout du lien + linksOME.put(attribute_tmp,class_tmp); + } + else + { + // ajout de l'attribut + output.write(" -"+attributeName+" : "+attributeType+"\n"); + } + } + // parcours des operations + for(ObjectModelOperation operation_tmp : class_tmp.getOperations()) + { + String operationName = operation_tmp.getName(); + //String operationReturnType = afterLastPoint(operation_tmp.getReturnType()); + String operationReturnType = operation_tmp.getReturnType(); + // ajout de l'operation + output.write(" +"+operationName+"("); + // parcours des parametres + boolean first = true; + for(ObjectModelParameter param : operation_tmp.getParameters()) + { + String parameterName = param.getName(); + //String parameterType = afterLastPoint(param.getType()); + String parameterType = param.getType(); + if(first) + { + first = false; + } + else + { + output.write(", "); + } + // ajout du parametre + output.write(parameterName+" : "+parameterType); + } + // ajout du type de retour + output.write(") : "+operationReturnType+"\n");// attention aux listes ! + } + output.write("}\n\n"); + + } + + // parcours des enumerations + for(ObjectModelEnumeration enumeration_tmp : input.getEnumerations()) + { + String enumerationName = enumeration_tmp.getName(); + String enumerationStereotype = ""; + if(enumeration_tmp.getStereotypes().contains("entity")) + { + enumerationStereotype = " <<entity>>"; + } + // ajout de l'enumeration + output.write("enum "+enumerationName+enumerationStereotype+" {\n"); + output.write("}\n\n"); + } + + // parcours des interfaces + for(ObjectModelInterface interface_tmp : input.getInterfaces()) + { + String interfaceName = interface_tmp.getName(); + String interfaceStereotype = ""; + if(interface_tmp.getStereotypes().contains("entity")) + { + interfaceStereotype = " <<entity>>"; + } + // ajout de l'interface + output.write("interface "+interfaceName+interfaceStereotype+" {\n"); + // ajout des methodes + output.write("}\n\n"); + } + + //liaisons + for(ObjectModelAttribute attribute_tmp : linksOME.keySet()) + { + if(attribute_tmp.isComposite() && attribute_tmp.referenceClassifier()) + { + output.write(linksOME.get(attribute_tmp).getName()+" *-- "+attribute_tmp.getClassifier().getName()+"\n"); + } + else if(attribute_tmp.isAggregate() && attribute_tmp.referenceClassifier())//else if + { + output.write(linksOME.get(attribute_tmp).getName()+" o-- "+attribute_tmp.getClassifier().getName()+"\n"); + } + else if(!attribute_tmp.isNavigable() && attribute_tmp.referenceClassifier())//probleme avec les compositions et aggregations -> double lien + { + output.write(attribute_tmp.getClassifier().getName()+" --> "+linksOME.get(attribute_tmp).getName()+"\n"); + } + else if(attribute_tmp.isNavigable() && attribute_tmp.referenceClassifier()) + { + output.write(attribute_tmp.getClassifier().getName()+" -- "+linksOME.get(attribute_tmp).getName()+"\n"); + } + } + + // fin du fichier + output.write("\n@enduml"); + } + + // exemple : afterLastPoint("org.nuiton.testeugene.generator") return "generator" + public static String afterLastPoint(String input) + { + String str = new StringBuffer(input).reverse().toString(); + String res = ""; + for(Character car : str.toCharArray()) + { + if(car.equals('.')) + { + return res; + } + else + { + res = car.toString()+res; + } + } + return res; + } + + @Override + public void applyTemplate(ObjectModel model, File destDir) throws IOException { + super.applyTemplate(model, destDir); + try { + File plantuml = new File(destDir+File.separator+this.getFilenameForModel(model)); + SourceFileReader reader = new SourceFileReader(plantuml); + List<GeneratedImage> lst = reader.getGeneratedImages(); + File diagram = new File(destDir+File.separator+model.getName()+".png"); + diagram = lst.get(0).getPngFile(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Override + public String getFilenameForModel(ObjectModel model) { + return model.getName()+".plantuml"; + } +} Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2013-05-05 08:32:43 UTC (rev 1243) +++ trunk/pom.xml 2013-05-13 10:01:52 UTC (rev 1244) @@ -43,6 +43,7 @@ <module>eugene-java-templates</module> <!--module>eugene-jpa-templates</module--> <module>eugene-maven-plugin</module> + <module>eugene-plantuml-templates</module> </modules> <dependencyManagement>
participants (1)
-
agiraudet@users.nuiton.org