View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.archetype;
28  
29  import com.rapiddweller.benerator.BeneratorFactory;
30  import com.rapiddweller.common.ConfigurationError;
31  import com.rapiddweller.common.IOUtil;
32  
33  import java.io.File;
34  import java.io.FileNotFoundException;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  import java.io.Serializable;
40  import java.net.URL;
41  import java.util.Collection;
42  import java.util.Locale;
43  import java.util.Properties;
44  
45  /**
46   * Represents a Benerator project archetype.<br/><br/>
47   * Created at 18.02.2009 07:34:50
48   *
49   * @author Volker Bergmann
50   * @since 0.5.9
51   */
52  public class Archetype implements Serializable {
53  
54    private static final long serialVersionUID = 2552120042802481049L;
55  
56    private final String id;
57    private final URL url;
58    private final URL iconUrl;
59    private final String description;
60  
61    /**
62     * Instantiates a new Archetype.
63     *
64     * @param url the url
65     */
66    public Archetype(URL url) {
67      try {
68        String urlString = url.toString();
69        this.id = urlString.substring(urlString.lastIndexOf('/') + 1);
70        this.url = url;
71        URL infoUrl = new URL(url + "/ARCHETYPE-INF");
72        iconUrl = new URL(infoUrl + "/icon.gif");
73        this.description = resolveDescription(id, infoUrl);
74      } catch (Exception e) {
75        throw new ConfigurationError("Error reading archetype info from " + url, e);
76      }
77    }
78  
79    private static String resolveDescription(String id, URL infoUrl) throws IOException {
80      URL descriptionUrl = new URL(infoUrl.toString() + "/description.properties");
81      String desc = null;
82      try {
83        InputStream descriptionFileStream = descriptionUrl.openStream();
84        Properties descriptions = new Properties();
85        descriptions.load(descriptionFileStream);
86        // try to get the name in the user's default locale...
87        desc = descriptions.getProperty(Locale.getDefault().getLanguage());
88        if (desc == null) { // ...if no such name was defined, fall back to the English name (if it exists)
89  
90          desc = descriptions.getProperty("en");
91        }
92        if (desc == null) { // if there is even no English name, choose an arbitrary one
93          Collection<Object> values = descriptions.values();
94          if (values.size() > 0) {
95            desc = values.iterator().next().toString();
96          }
97        }
98        descriptionFileStream.close();
99      } catch (FileNotFoundException e) {
100       // no description file defined
101     }
102     // if no description was found, choose the archetype id as description
103     return (desc != null ? desc : id);
104   }
105 
106   /**
107    * Gets id.
108    *
109    * @return the id
110    */
111   public String getId() {
112     return id;
113   }
114 
115   /**
116    * Gets description.
117    *
118    * @return the description
119    */
120   public String getDescription() {
121     return description;
122   }
123 
124   /**
125    * Gets icon url.
126    *
127    * @return the icon url
128    */
129   public URL getIconURL() {
130     return iconUrl;
131   }
132 
133   /**
134    * Copy files to.
135    *
136    * @param targetFolder the target folder
137    * @param layout       the layout
138    * @throws IOException the io exception
139    */
140   public void copyFilesTo(File targetFolder, FolderLayout layout) throws IOException {
141     copyNonSourceFilesTo(targetFolder);
142     copySourceFilesTo(targetFolder, layout);
143   }
144 
145   private void copyNonSourceFilesTo(File targetFolder) throws IOException {
146     IOUtil.copyDirectory(url, targetFolder, candidate -> !candidate.contains("ARCHETYPE-INF") && !candidate.contains("/src/"));
147     copySchemaTo(targetFolder);
148   }
149 
150   private void copySourceFilesTo(File targetFolder, FolderLayout layout) throws IOException {
151     copySourceDirectory(targetFolder, "src/main/java", layout);
152     copySourceDirectory(targetFolder, "src/main/resources", layout);
153     copySourceDirectory(targetFolder, "src/test/java", layout);
154     copySourceDirectory(targetFolder, "src/test/resources", layout);
155     copySchemaTo(targetFolder);
156   }
157 
158   private void copySourceDirectory(File targetFolder, String subFolder, FolderLayout layout) throws IOException {
159     URL srcUrl = new URL(url.toString() + '/' + subFolder);
160     targetFolder = new File(targetFolder, layout.mapSubFolder(subFolder));
161     targetFolder.mkdir();
162     if (IOUtil.listResources(srcUrl).length > 0) {
163       IOUtil.copyDirectory(srcUrl, targetFolder, null);
164     }
165   }
166 
167   private void copySchemaTo(File targetFolder) throws IOException {
168     String xmlSchemaPath = BeneratorFactory.getSchemaPathForCurrentVersion();
169     URL schemaUrl = getClass().getClassLoader().getResource(xmlSchemaPath);
170     if (schemaUrl == null) {
171       throw new FileNotFoundException("File not found: " + xmlSchemaPath);
172     }
173     InputStream in = schemaUrl.openStream();
174     File file = new File(targetFolder, xmlSchemaPath.substring(xmlSchemaPath.lastIndexOf('/')));
175     OutputStream out = new FileOutputStream(file);
176     IOUtil.transfer(in, out);
177     in.close();
178   }
179 
180   @Override
181   public String toString() {
182     return description;
183   }
184 
185   @Override
186   public int hashCode() {
187     return id.hashCode();
188   }
189 
190   @Override
191   public boolean equals(Object obj) {
192     if (this == obj) {
193       return true;
194     }
195     if (obj == null || getClass() != obj.getClass()) {
196       return false;
197     }
198     Archetype./../../../com/rapiddweller/benerator/archetype/Archetype.html#Archetype">Archetype that = (Archetype) obj;
199     return (this.id.equals(that.id));
200   }
201 
202 }