1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
47
48
49
50
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
63
64
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
87 desc = descriptions.getProperty(Locale.getDefault().getLanguage());
88 if (desc == null) {
89
90 desc = descriptions.getProperty("en");
91 }
92 if (desc == null) {
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
101 }
102
103 return (desc != null ? desc : id);
104 }
105
106
107
108
109
110
111 public String getId() {
112 return id;
113 }
114
115
116
117
118
119
120 public String getDescription() {
121 return description;
122 }
123
124
125
126
127
128
129 public URL getIconURL() {
130 return iconUrl;
131 }
132
133
134
135
136
137
138
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 }