View Javadoc
1   package org.nuiton.eugene.models.object.reader;
2   
3   /*
4    * #%L
5    * EUGene :: EUGene
6    * %%
7    * Copyright (C) 2004 - 2013 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Sets;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.nuiton.eugene.ModelHelper;
29  import org.nuiton.eugene.ModelReader;
30  import org.nuiton.eugene.models.extension.tagvalue.InvalidStereotypeSyntaxException;
31  import org.nuiton.eugene.models.extension.tagvalue.InvalidTagValueSyntaxException;
32  import org.nuiton.eugene.models.object.ObjectModel;
33  import org.nuiton.eugene.models.object.ObjectModelClass;
34  import org.nuiton.eugene.models.object.ObjectModelPackage;
35  import org.nuiton.eugene.models.extension.io.ModelExtensionReader;
36  import org.nuiton.eugene.models.object.xml.ObjectModelImpl;
37  import org.nuiton.eugene.models.object.xml.ObjectModelPackageImpl;
38  import org.nuiton.util.FileUtil;
39  
40  import java.io.File;
41  import java.io.IOException;
42  import java.util.LinkedHashSet;
43  import java.util.Set;
44  
45  /**
46   * Abstract object model reader.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 2.6.3
50   */
51  public abstract class AbstractObjectModelReader extends ModelReader<ObjectModel> {
52  
53      private static final Log log = LogFactory.getLog(AbstractObjectModelReader.class);
54  
55      @Override
56      public String getModelType() {
57          return ModelHelper.ModelType.OBJECT.getAlias();
58      }
59  
60      protected abstract void readFileToModel(File file, ObjectModel model) throws IOException;
61  
62      protected void beforeReadFile(File... files) {
63  
64      }
65  
66      @Override
67      public ObjectModel read(File... files) throws IOException {
68  
69          beforeReadFile(files);
70  
71          ObjectModel model = new ObjectModelImpl();
72  
73          ModelExtensionReader<ObjectModel> modelExtensionReader = new ModelExtensionReader<>(isVerbose(), strictLoading, model);
74  
75          for (File file : files) {
76  
77              readFileToModel(file, model);
78  
79              addAllSubPackages((ObjectModelImpl) model);
80  
81              // recherche est charge le fichier propriete associe au modele
82              File dir = file.getParentFile();
83              String ext = FileUtil.extension(file);
84              String name = FileUtil.basename(file, "." + ext);
85              File propFile = new File(dir, name + ".properties");
86              if (!propFile.exists()) {
87                  if (isVerbose()) {
88                      log.info("Pas de fichier de propriétés " + propFile + " associé au model");
89                  }
90              } else {
91                  if (isVerbose()) {
92                      log.info("Lecture du fichier de propriétés " + propFile + " associé au model");
93                  }
94                  try {
95                      modelExtensionReader.read(propFile);
96                  } catch (InvalidTagValueSyntaxException | InvalidStereotypeSyntaxException e) {
97                      // FIXME
98                      throw new IllegalStateException(e);
99                  }
100             }
101 
102         }
103 
104 
105         if (log.isDebugEnabled()) {
106             for (ObjectModelClass m : model.getClasses()) {
107                 log.debug("loaded class in model: " + m.getName());
108             }
109         }
110         return model;
111     }
112 
113     /**
114      * Add all missing sub packages in a model.
115      *
116      * @param model the model to scan
117      */
118     protected void addAllSubPackages(ObjectModelImpl model) {
119 
120         Set<String> subPackageNames = new LinkedHashSet<>();
121 
122         for (ObjectModelPackage aPackage : Sets.newHashSet(model.getPackages())) {
123 
124             String aPackageName = aPackage.getName();
125             if (verbose) {
126                 log.info("Treat package: " + aPackageName);
127             }
128 
129             if (subPackageNames.add(aPackageName)) {
130 
131                 addSubPackages(model, aPackageName);
132 
133             }
134 
135         }
136 
137     }
138 
139     protected void addSubPackages(ObjectModelImpl model, String aPackageName) {
140 
141         String subPackageName = null;
142         ObjectModelPackageImpl parentPackage = null;
143 
144         for (String part : aPackageName.split("\\.")) {
145 
146             if (subPackageName == null) {
147                 subPackageName = part;
148             } else {
149                 subPackageName += "." + part;
150             }
151 
152             ObjectModelPackageImpl subPackage = (ObjectModelPackageImpl) model.getPackage(subPackageName);
153 
154             if (subPackage == null) {
155 
156                 subPackage = new ObjectModelPackageImpl();
157                 subPackage.setName(subPackageName);
158                 model.addPackage(subPackage);
159                 if (verbose) {
160                     log.info("Add sub package: " + subPackageName);
161                 }
162 
163             }
164 
165             if (subPackage.getParentPackage() == null && parentPackage != null) {
166 
167 
168                 if (verbose) {
169                     log.info("Set parent package " + parentPackage.getName() + " to " + part);
170                 }
171                 subPackage.setParentPackage(parentPackage);
172             }
173 
174             parentPackage = subPackage;
175 
176         }
177     }
178 
179 }