View Javadoc
1   /*
2    * #%L
3    * EUGene :: Maven plugin
4    * %%
5    * Copyright (C) 2006 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.plugin.writer;
24  
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.util.DirectoryScanner;
27  import org.codehaus.plexus.util.IOUtil;
28  import org.nuiton.eugene.writer.ChainedFileWriter;
29  import org.nuiton.eugene.writer.ChainedFileWriterConfiguration;
30  import org.nuiton.eugene.writer.WriterReport;
31  import org.nuiton.plugin.PluginHelper;
32  import org.nuiton.util.FileUtil;
33  
34  import java.io.File;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.util.Enumeration;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.zip.ZipEntry;
42  import java.util.zip.ZipFile;
43  
44  /**
45   * To write model files from zargo files.
46   *
47   * @author tchemit
48   * @since 2.0.0
49   */
50  @Component(role = ChainedFileWriter.class, hint = "zargo2xmi")
51  public class ZargoChainedFileWriter extends BaseChainedFileWriter {
52  
53      public static final String[] XMI_FILE_FILTER = new String[]{"*.xmi",
54                                                                  "**/*.xmi"};
55  
56      @Override
57      public String getInputProtocol() {
58          return "zargo";
59      }
60  
61      @Override
62      public String getOutputProtocol(String modelType) {
63          // next writer to use is a xmi to model one
64          return "xmi";
65      }
66  
67      @Override
68      public boolean acceptModel(String modelType) {
69          // accept all models
70          return acceptObjectModelOrStateModel(modelType);
71      }
72  
73      @Override
74      public boolean acceptInclude(String include) {
75          return include.startsWith("zargo:") ||
76                 include.endsWith(".zargo") || include.endsWith(".zuml");
77      }
78  
79      @Override
80      public String getDefaultIncludes() {
81          return "**/*.zargo";
82      }
83  
84      @Override
85      public String getDefaultInputDirectory() {
86          return "src/main/xmi";
87      }
88  
89      @Override
90      public String getDefaultOutputDirectory() {
91          return "xmi";
92      }
93  
94      @Override
95      public String getDefaultTestInputDirectory() {
96          return "src/test/xmi";
97      }
98  
99      @Override
100     public String getDefaultTestOutputDirectory() {
101         return "test-xmi";
102     }
103 
104     @Override
105     public void generate(ChainedFileWriterConfiguration configuration,
106                          File outputDirectory,
107                          Map<File, List<File>> filesByRoot,
108                          Map<File, List<File>> resourcesByFile) throws IOException {
109 
110         for (Map.Entry<File, List<File>> entry : filesByRoot.entrySet()) {
111             File inputDirectory = entry.getKey();
112             List<File> files = entry.getValue();
113 
114             if (configuration.isVerbose()) {
115                 getLog().info("Expanding " + files.size() + " xmi file(s) from " + inputDirectory);
116             }
117 
118             for (File file : files) {
119 
120                 File mirrorDirectory = FileUtil.getRelativeFile(
121                         inputDirectory,
122                         outputDirectory,
123                         file.getParentFile()
124                 );
125 
126                 expandFile(file, mirrorDirectory, XMI_FILE_FILTER, configuration);
127 
128                 // copy resources associated with the file
129                 copyResources(configuration,
130                               outputDirectory,
131                               inputDirectory,
132                               file,
133                               resourcesByFile
134                 );
135             }
136         }
137     }
138 
139     public void expandFile(File src,
140                            File dst,
141                            String[] includes,
142                            ChainedFileWriterConfiguration configuration) throws IOException {
143         ZipFile zipFile = new ZipFile(src);
144         Enumeration<? extends ZipEntry> entries = zipFile.entries();
145         while (entries.hasMoreElements()) {
146             ZipEntry nextElement = entries.nextElement();
147             String name = nextElement.getName();
148             for (String include : includes) {
149                 if (DirectoryScanner.match(include, name)) {
150                     if (configuration.isVerbose()) {
151                         getLog().info("matching name : " + name +
152                                       " with pattern " + include);
153                     }
154                     File dstFile = new File(dst, name);
155                     if (configuration.isOverwrite() ||
156                         !dstFile.exists() ||
157                         nextElement.getTime() > dstFile.lastModified()) {
158 
159                         if (configuration.isVerbose()) {
160                             getLog().info("will expand : " + name + " to " + dstFile);
161                         }
162 
163                         WriterReport writerReport = getWriterReport();
164                         if (writerReport != null) {
165                             writerReport.addFile(getClass().getName(), dstFile, false);
166                         }
167 
168                         PluginHelper.createDirectoryIfNecessary(dst);
169                         InputStream inputStream =
170                                 zipFile.getInputStream(nextElement);
171                         try (FileOutputStream outStream = new FileOutputStream(dstFile)) {
172                             IOUtil.copy(inputStream, outStream, 2048);
173                         }
174                     }
175 
176                 }
177             }
178         }
179     }
180 }