|
13 | 13 |
|
14 | 14 | package org.eclipse.esmf.aspectmodel.loader;
|
15 | 15 |
|
16 |
| -import java.io.ByteArrayInputStream; |
17 |
| -import java.io.ByteArrayOutputStream; |
18 | 16 | import java.io.File;
|
19 | 17 | import java.io.FileInputStream;
|
20 | 18 | import java.io.IOException;
|
|
33 | 31 | import java.util.function.Supplier;
|
34 | 32 | import java.util.stream.Collectors;
|
35 | 33 | import java.util.stream.Stream;
|
36 |
| -import java.util.zip.ZipEntry; |
37 |
| -import java.util.zip.ZipInputStream; |
38 | 34 |
|
39 | 35 | import org.eclipse.esmf.aspectmodel.AspectLoadingException;
|
40 | 36 | import org.eclipse.esmf.aspectmodel.AspectModelFile;
|
|
43 | 39 | import org.eclipse.esmf.aspectmodel.resolver.EitherStrategy;
|
44 | 40 | import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
|
45 | 41 | import org.eclipse.esmf.aspectmodel.resolver.ModelSource;
|
| 42 | +import org.eclipse.esmf.aspectmodel.resolver.NamespacePackage; |
46 | 43 | import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
|
47 | 44 | import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategySupport;
|
48 | 45 | import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
|
@@ -176,7 +173,8 @@ public AspectModel loadUrns( final Collection<AspectModelUrn> urns ) {
|
176 | 173 | }
|
177 | 174 |
|
178 | 175 | /**
|
179 |
| - * Load an Aspect Model from an input stream and optionally set the source location for this input |
| 176 | + * Load an Aspect Model (.ttl) from an input stream and optionally set the source location for this input. For loading |
| 177 | + * an Aspect Model Namespace Package (.zip), use {@link #loadNamespacePackage(InputStream, URI)} instead. |
180 | 178 | *
|
181 | 179 | * @param inputStream the input stream
|
182 | 180 | * @param sourceLocation the source location for the model
|
@@ -212,70 +210,54 @@ public AspectModel loadNamespacePackage( final File namespacePackage ) {
|
212 | 210 | }
|
213 | 211 |
|
214 | 212 | try ( final InputStream inputStream = new FileInputStream( namespacePackage ) ) {
|
215 |
| - return loadNamespacePackage( inputStream ); |
| 213 | + return loadNamespacePackage( inputStream, namespacePackage.toURI() ); |
216 | 214 | } catch ( final IOException exception ) {
|
217 | 215 | LOG.error( "Error reading the file: {}", namespacePackage.getAbsolutePath(), exception );
|
218 | 216 | throw new AspectLoadingException( "Error reading the file: " + namespacePackage.getAbsolutePath(), exception );
|
219 | 217 | }
|
220 | 218 | }
|
221 | 219 |
|
222 | 220 | /**
|
223 |
| - * Load a Namespace Package (Archive) from an InputStream |
| 221 | + * Load a namespace package from binary with a given location. The location is not resolved or loaded from, but is only attached |
| 222 | + * to the files loaded from the input stream to indicate their original source, e.g., file system location or URL. |
224 | 223 | *
|
225 |
| - * @param inputStream the input stream |
226 |
| - * @return the Aspect Model |
| 224 | + * @param location the source location |
| 225 | + * @param binaryContent the ZIP content |
| 226 | + * @return the loaded and resolved Aspect Model |
227 | 227 | */
|
228 |
| - public AspectModel loadNamespacePackage( final InputStream inputStream ) { |
229 |
| - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
230 |
| - final boolean hasAspectModelsFolder; |
231 |
| - try { |
232 |
| - inputStream.transferTo( baos ); |
233 |
| - hasAspectModelsFolder = containsFolderInNamespacePackage( new ByteArrayInputStream( baos.toByteArray() ) ); |
234 |
| - } catch ( final IOException exception ) { |
235 |
| - throw new AspectLoadingException( "Could not read from input", exception ); |
236 |
| - } |
237 |
| - return loadNamespacePackageFromStream( new ByteArrayInputStream( baos.toByteArray() ), hasAspectModelsFolder ); |
| 228 | + public AspectModel loadNamespacePackage( final byte[] binaryContent, final URI location ) { |
| 229 | + final List<AspectModelFile> packageFiles = new NamespacePackage( binaryContent, location ).loadContents().toList(); |
| 230 | + final LoaderContext loaderContext = new LoaderContext(); |
| 231 | + resolve( packageFiles, loaderContext ); |
| 232 | + return loadAspectModelFiles( loaderContext.loadedFiles() ); |
238 | 233 | }
|
239 | 234 |
|
240 |
| - private AspectModel loadNamespacePackageFromStream( final InputStream inputStream, final boolean hasAspectModelsFolder ) { |
241 |
| - final List<AspectModelFile> aspectModelFiles = new ArrayList<>(); |
242 |
| - |
243 |
| - try ( final ZipInputStream zis = new ZipInputStream( inputStream ) ) { |
244 |
| - ZipEntry entry; |
245 |
| - |
246 |
| - while ( ( entry = zis.getNextEntry() ) != null ) { |
247 |
| - final boolean isRelevantEntry = |
248 |
| - ( hasAspectModelsFolder && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) |
249 |
| - && entry.getName().endsWith( ".ttl" ) ) |
250 |
| - || ( !hasAspectModelsFolder && entry.getName().endsWith( ".ttl" ) ); |
251 |
| - |
252 |
| - if ( isRelevantEntry ) { |
253 |
| - final AspectModelFile aspectModelFile = migrate( AspectModelFileLoader.load( zis ) ); |
254 |
| - aspectModelFiles.add( aspectModelFile ); |
255 |
| - } |
256 |
| - } |
257 |
| - |
258 |
| - zis.closeEntry(); |
259 |
| - } catch ( final IOException exception ) { |
260 |
| - LOG.error( "Error reading the Archive input stream", exception ); |
261 |
| - throw new AspectLoadingException( "Error reading the Archive input stream", exception ); |
262 |
| - } |
263 |
| - |
| 235 | + /** |
| 236 | + * Load a namespace package from an input stream with a given location. The location is not resolved or loaded from, but is only attached |
| 237 | + * to the files loaded from the input stream to indicate their original source, e.g., file system location or URL of the |
| 238 | + * namespace package. |
| 239 | + * |
| 240 | + * @param location the source location |
| 241 | + * @param inputStream the input stream to load the ZIP content from |
| 242 | + * @return the loaded and resolved Aspect Model |
| 243 | + */ |
| 244 | + public AspectModel loadNamespacePackage( final InputStream inputStream, final URI location ) { |
| 245 | + final List<AspectModelFile> packageFiles = new NamespacePackage( inputStream, location ).loadContents().toList(); |
264 | 246 | final LoaderContext loaderContext = new LoaderContext();
|
265 |
| - resolve( aspectModelFiles, loaderContext ); |
| 247 | + resolve( packageFiles, loaderContext ); |
266 | 248 | return loadAspectModelFiles( loaderContext.loadedFiles() );
|
267 | 249 | }
|
268 | 250 |
|
269 |
| - private boolean containsFolderInNamespacePackage( final InputStream inputStream ) throws IOException { |
270 |
| - try ( final ZipInputStream zis = new ZipInputStream( inputStream ) ) { |
271 |
| - ZipEntry entry; |
272 |
| - while ( ( entry = zis.getNextEntry() ) != null ) { |
273 |
| - if ( entry.isDirectory() && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) ) { |
274 |
| - return true; |
275 |
| - } |
276 |
| - } |
277 |
| - } |
278 |
| - return false; |
| 251 | + /** |
| 252 | + * Load a Namespace Package (Archive) from an InputStream |
| 253 | + * |
| 254 | + * @param inputStream the input stream |
| 255 | + * @return the Aspect Model |
| 256 | + * @deprecated Use {@link #loadNamespacePackage(InputStream, URI)} instead |
| 257 | + */ |
| 258 | + @Deprecated |
| 259 | + public AspectModel loadNamespacePackage( final InputStream inputStream ) { |
| 260 | + return loadNamespacePackage( inputStream, URI.create( "file:unknown" ) ); |
279 | 261 | }
|
280 | 262 |
|
281 | 263 | private AspectModelFile migrate( final AspectModelFile file ) {
|
|
0 commit comments