Author: chatellier Date: 2009-01-21 17:56:44 +0000 (Wed, 21 Jan 2009) New Revision: 1311 Added: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaJavaValidator.java Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaMetaGenerator.java Log: Ajout d'un d?\195?\169but de code qui fait quelque check concernant Java. Type manquant, identifier invalide, noms dupliqu?\195?\169s... Added: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaJavaValidator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaJavaValidator.java (rev 0) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaJavaValidator.java 2009-01-21 17:56:44 UTC (rev 1311) @@ -0,0 +1,117 @@ +/* *##% + * Copyright (C) 2008 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.topia.generator; + +import org.codelutin.generator.models.object.ObjectModel; +import org.codelutin.generator.models.object.ObjectModelAttribute; +import org.codelutin.generator.models.object.ObjectModelClass; +import org.codelutin.generator.models.object.validator.ObjectModelValidator; + +/** + * Validateur qui valide : + * - les types des attributs + * - les nom des attributs + * - les duplication d'attibuts + * + * @author chatellier + * @version $Revision: 1.0 $ + * + * Last update : $Date: 21 janv. 2009 $ + * By : $Author: chatellier $ + */ +public class TopiaJavaValidator extends ObjectModelValidator { + + /** + * Constructor. + * + * @param model + */ + public TopiaJavaValidator(ObjectModel model) { + super(model); + } + + /* + * @see org.codelutin.generator.models.object.validator.ObjectModelValidator#validateAttribute(org.codelutin.generator.models.object.ObjectModelAttribute) + */ + @Override + protected boolean validateAttribute(ObjectModelAttribute attr) { + + boolean isValid = true; + + // type null ou vide + if (attr.getType() == null || attr.getType().isEmpty()) { + isValid = false; + + addError(attr, "Invalid name \"" + attr.getType() + "\""); + } + + // name = java reserved keywords + if (!isJavaIdentifier(attr.getName())) { + isValid = false; + + addError(attr, "Attribute name " + attr.getName() + + " is not valid java identifier"); + } + + // test sur les mots réservés ? + + return isValid; + + } + + /* + * @see org.codelutin.generator.models.object.validator.ObjectModelValidator#validateClass(org.codelutin.generator.models.object.ObjectModelClass) + */ + @Override + protected boolean validateClass(ObjectModelClass clazz) { + + // TODO test if attribute name is duplicated + + return super.validateClass(clazz); + } + + /* + * @see org.codelutin.generator.models.object.validator.ObjectModelValidator#validateModel(org.codelutin.generator.models.object.ObjectModel) + */ + @Override + protected boolean validateModel(ObjectModel model) { + + // TODO test if default package name is not good + + return super.validateModel(model); + } + + /** + * Returns true if s is a legal Java identifier. + * + * @param s + * @return true if s is a legal Java identifier + */ + public static boolean isJavaIdentifier(String s) { + if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) { + return false; + } + for (int i = 1; i < s.length(); i++) { + if (!Character.isJavaIdentifierPart(s.charAt(i))) { + return false; + } + } + return true; + } +} Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaMetaGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaMetaGenerator.java 2009-01-16 18:53:25 UTC (rev 1310) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaMetaGenerator.java 2009-01-21 17:56:44 UTC (rev 1311) @@ -101,6 +101,7 @@ "Nom de classe incompatible avec certains SGBD"); validators.add(classValidator); + validators.add(new TopiaJavaValidator(model)); validators.add(new TopiaRelationValidator(model)); boolean isValid = true;