|
15 | 15 |
|
16 | 16 | import static java.util.stream.Collectors.toSet;
|
17 | 17 |
|
| 18 | +import java.io.ByteArrayInputStream; |
| 19 | +import java.io.ByteArrayOutputStream; |
18 | 20 | import java.io.File;
|
| 21 | +import java.io.FileInputStream; |
19 | 22 | import java.io.FileNotFoundException;
|
20 | 23 | import java.io.IOException;
|
21 | 24 | import java.io.InputStream;
|
|
24 | 27 | import java.util.ArrayList;
|
25 | 28 | import java.util.Collection;
|
26 | 29 | import java.util.Deque;
|
27 |
| -import java.util.Enumeration; |
28 | 30 | import java.util.HashSet;
|
29 | 31 | import java.util.List;
|
30 | 32 | import java.util.Map;
|
|
34 | 36 | import java.util.function.Supplier;
|
35 | 37 | import java.util.stream.Stream;
|
36 | 38 | import java.util.zip.ZipEntry;
|
37 |
| -import java.util.zip.ZipFile; |
| 39 | +import java.util.zip.ZipInputStream; |
38 | 40 |
|
39 | 41 | import org.eclipse.esmf.aspectmodel.AspectModelFile;
|
40 | 42 | import org.eclipse.esmf.aspectmodel.resolver.AspectModelFileLoader;
|
|
71 | 73 | */
|
72 | 74 | public class AspectModelLoader implements ResolutionStrategySupport {
|
73 | 75 | private static final Logger LOG = LoggerFactory.getLogger( AspectModelLoader.class );
|
| 76 | + private static final String ASPECT_MODELS_FOLDER = "aspect-models"; |
74 | 77 |
|
75 | 78 | public static final Supplier<ResolutionStrategy> DEFAULT_STRATEGY = () -> {
|
76 | 79 | final Path currentDirectory = Path.of( System.getProperty( "user.dir" ) );
|
@@ -178,37 +181,89 @@ public AspectModel load( final InputStream inputStream ) {
|
178 | 181 | return buildAspectModel( loaderContext.loadedFiles() );
|
179 | 182 | }
|
180 | 183 |
|
181 |
| - public AspectModel loadFromArchive( final String pathToArchive ) { |
182 |
| - File zipFile = new File( pathToArchive ); |
| 184 | + @FunctionalInterface |
| 185 | + public interface InputStreamProvider { |
| 186 | + InputStream get() throws IOException; |
| 187 | + } |
183 | 188 |
|
184 |
| - if ( !zipFile.exists() || !zipFile.isFile() ) { |
| 189 | + /** |
| 190 | + * Load Namespace Package (Archive) an Aspect Model from a File |
| 191 | + * |
| 192 | + * @param namespacePackage the archive file |
| 193 | + * @return the Aspect Model |
| 194 | + */ |
| 195 | + public AspectModel loadNamespacePackage( final File namespacePackage ) { |
| 196 | + if ( !namespacePackage.exists() || !namespacePackage.isFile() ) { |
185 | 197 | throw new RuntimeException( new FileNotFoundException( "The specified file does not exist or is not a file." ) );
|
186 | 198 | }
|
187 | 199 |
|
| 200 | + try ( InputStream inputStream = new FileInputStream( namespacePackage ) ) { |
| 201 | + return loadNamespacePackage( inputStream ); |
| 202 | + } catch ( IOException e ) { |
| 203 | + LOG.error( "Error reading the file: {}", namespacePackage.getAbsolutePath(), e ); |
| 204 | + throw new RuntimeException( "Error reading the file: " + namespacePackage.getAbsolutePath(), e ); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Load Namespace Package (Archive) an Aspect Model from an InputStream |
| 210 | + * |
| 211 | + * @param inputStream the input stream |
| 212 | + * @return the Aspect Model |
| 213 | + */ |
| 214 | + public AspectModel loadNamespacePackage( final InputStream inputStream ) { |
| 215 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 216 | + try { |
| 217 | + inputStream.transferTo( baos ); |
| 218 | + } catch ( IOException e ) { |
| 219 | + throw new RuntimeException( e ); |
| 220 | + } |
| 221 | + final boolean hasAspectModelsFolder = containsFolderInNamespacePackage( new ByteArrayInputStream( baos.toByteArray() ) ); |
| 222 | + return loadNamespacePackageFromStream( new ByteArrayInputStream( baos.toByteArray() ), hasAspectModelsFolder ); |
| 223 | + } |
| 224 | + |
| 225 | + private AspectModel loadNamespacePackageFromStream( final InputStream inputStream, final boolean hasAspectModelsFolder ) { |
188 | 226 | List<AspectModelFile> aspectModelFiles = new ArrayList<>();
|
189 | 227 |
|
190 |
| - try ( ZipFile zip = new ZipFile( zipFile ) ) { |
191 |
| - Enumeration<? extends ZipEntry> entries = zip.entries(); |
| 228 | + try ( ZipInputStream zis = new ZipInputStream( inputStream ) ) { |
| 229 | + ZipEntry entry; |
192 | 230 |
|
193 |
| - while ( entries.hasMoreElements() ) { |
194 |
| - ZipEntry entry = entries.nextElement(); |
| 231 | + while ( (entry = zis.getNextEntry()) != null ) { |
| 232 | + boolean isRelevantEntry = |
| 233 | + (hasAspectModelsFolder && entry.getName().startsWith( ASPECT_MODELS_FOLDER ) && entry.getName().endsWith( ".ttl" )) || |
| 234 | + (!hasAspectModelsFolder && entry.getName().endsWith( ".ttl" )); |
195 | 235 |
|
196 |
| - if ( entry.getName().endsWith( ".ttl" ) ) { |
197 |
| - try ( InputStream inputStream = zip.getInputStream( entry ) ) { |
198 |
| - AspectModelFile aspectModelFile = AspectModelFileLoader.load( inputStream ); |
199 |
| - aspectModelFiles.add( aspectModelFile ); |
200 |
| - } catch ( IOException e ) { |
201 |
| - LOG.error( String.format( "Error reading entry: %s - %s", entry.getName(), e.getMessage() ) ); |
202 |
| - } |
| 236 | + if ( isRelevantEntry ) { |
| 237 | + AspectModelFile aspectModelFile = migrate( AspectModelFileLoader.load( zis ) ); |
| 238 | + aspectModelFiles.add( aspectModelFile ); |
203 | 239 | }
|
204 | 240 | }
|
| 241 | + |
| 242 | + zis.closeEntry(); |
205 | 243 | } catch ( IOException e ) {
|
206 |
| - LOG.error( String.format( "Error reading the ZIP file: %s", e.getMessage() ) ); |
| 244 | + LOG.error( "Error reading the Archive input stream", e ); |
| 245 | + throw new RuntimeException( "Error reading the Archive input stream", e ); |
207 | 246 | }
|
208 | 247 |
|
| 248 | + LoaderContext loaderContext = new LoaderContext(); |
| 249 | + resolve( aspectModelFiles, loaderContext ); |
209 | 250 | return buildAspectModel( aspectModelFiles );
|
210 | 251 | }
|
211 | 252 |
|
| 253 | + private boolean containsFolderInNamespacePackage( final InputStream inputStream ) { |
| 254 | + try ( ZipInputStream zis = new ZipInputStream( inputStream ) ) { |
| 255 | + ZipEntry entry; |
| 256 | + while ( (entry = zis.getNextEntry()) != null ) { |
| 257 | + if ( entry.isDirectory() && entry.getName().equals( ASPECT_MODELS_FOLDER ) ) { |
| 258 | + return true; |
| 259 | + } |
| 260 | + } |
| 261 | + } catch ( IOException e ) { |
| 262 | + throw new RuntimeException( e ); |
| 263 | + } |
| 264 | + return false; |
| 265 | + } |
| 266 | + |
212 | 267 | private AspectModelFile migrate( final AspectModelFile file ) {
|
213 | 268 | return MetaModelVersionMigrator.INSTANCE.apply( file );
|
214 | 269 | }
|
|
0 commit comments