diff options
author | Martin Stockhammer <martin_s@apache.org> | 2020-02-01 00:41:34 +0100 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2020-02-01 00:41:34 +0100 |
commit | 865e3fef2449f093ddfb6c133563de0a0b5d4659 (patch) | |
tree | 38975ae0f82f587a49df840346012dc87f650283 /archiva-modules/archiva-base | |
parent | 5fe962ccce28d60c2745343e0174fed42bd5e2b5 (diff) | |
download | archiva-865e3fef2449f093ddfb6c133563de0a0b5d4659.tar.gz archiva-865e3fef2449f093ddfb6c133563de0a0b5d4659.zip |
Adding metadatareader interface
Diffstat (limited to 'archiva-modules/archiva-base')
10 files changed, 279 insertions, 112 deletions
diff --git a/archiva-modules/archiva-base/archiva-configuration/pom.xml b/archiva-modules/archiva-base/archiva-configuration/pom.xml index ec79c4386..1275ca1e0 100644 --- a/archiva-modules/archiva-base/archiva-configuration/pom.xml +++ b/archiva-modules/archiva-base/archiva-configuration/pom.xml @@ -96,13 +96,7 @@ </exclusion> </exclusions> </dependency> - <!-- - <dependency> - <groupId>commons-collections</groupId> - <artifactId>commons-collections</artifactId> - <version>3.2.2</version> - </dependency> - --> + <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> @@ -115,17 +109,10 @@ <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> </dependency> - <!-- - <dependency> - <groupId>commons-beanutils</groupId> - <artifactId>commons-beanutils</artifactId> - </dependency> - --> <!-- Test scope --> - <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> diff --git a/archiva-modules/archiva-base/archiva-repository-api/pom.xml b/archiva-modules/archiva-base/archiva-repository-api/pom.xml index 1c7675b95..584375efd 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/pom.xml +++ b/archiva-modules/archiva-base/archiva-repository-api/pom.xml @@ -64,6 +64,10 @@ <artifactId>javax.inject</artifactId> </dependency> <dependency> + <groupId>javax.annotation</groupId> + <artifactId>javax.annotation-api</artifactId> + </dependency> + <dependency> <groupId>org.apache.archiva.components.registry</groupId> <artifactId>archiva-components-spring-registry-api</artifactId> </dependency> diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java index fec96f6c7..bb6bbc98b 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java @@ -27,6 +27,8 @@ import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.event.EventSource; import org.apache.archiva.indexer.ArchivaIndexManager; import org.apache.archiva.indexer.IndexUpdateFailedException; +import org.apache.archiva.repository.metadata.MetadataReader; +import org.apache.archiva.repository.storage.StorageAsset; import java.util.Collection; @@ -41,12 +43,36 @@ import java.util.Collection; */ public interface RepositoryRegistry extends EventSource { + /** + * Set the configuration for the registry + * @param archivaConfiguration + */ void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration ); + /** + * Return the index manager for the given repository type + * @param type the repository type + * @return the index manager, if it exists + */ ArchivaIndexManager getIndexManager( RepositoryType type ); + /** + * Returns the metadatareader for the given repository type + * @param type the repository type + * @return the metadata reader instance + */ + MetadataReader getMetadataReader(RepositoryType type) throws UnsupportedRepositoryTypeException; + + /** + * Returns all registered repositories + * @return the list of repositories + */ Collection<Repository> getRepositories( ); + /** + * Returns all managed repositories + * @return the list of managed repositories + */ Collection<ManagedRepository> getManagedRepositories( ); Collection<RemoteRepository> getRemoteRepositories( ); @@ -106,4 +132,11 @@ public interface RepositoryRegistry extends EventSource <T extends Repository> Repository clone( T repo, String newId ) throws RepositoryException; RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException; + + /** + * Return the repository that stores the given asset. + * @param asset the asset + * @return the repository or <code>null</code> if no matching repository is found + */ + Repository getRepositoryOfAsset( StorageAsset asset ); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/metadata/MetadataReader.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/metadata/MetadataReader.java new file mode 100644 index 000000000..74bacb7d6 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/metadata/MetadataReader.java @@ -0,0 +1,55 @@ +package org.apache.archiva.repository.metadata; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.model.ArchivaRepositoryMetadata; +import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.storage.StorageAsset; + +/** + * Interface for reading metadata from a given file + */ +public interface MetadataReader +{ + /** + * Reads the given metadata file and returns the corresponding metadata object. + * @param asset The asset where the metadata should be read from + * @return The parsed metadata + * @throws RepositoryMetadataException if the metadata could not be read + */ + ArchivaRepositoryMetadata read( StorageAsset asset ) throws RepositoryMetadataException; + + /** + * Returns <code>true</code>, if the given path is a valid path for a metadata file, otherwise <code>false</code> + * The implementation should not access the file directly, just use the path for validation. + * @param path the path to the metadata file / asset + * @return <code>true</code>, if the path is valid for a metadata file otherwise <code>false</code> + */ + boolean isValidMetadataPath(String path); + + /** + * Returns <code>true</code>, if this metadata reader instance can be used to read metadata for the + * given repository type, otherwise <code>false</code>. + * + * @param repositoryType the repository type to check for + * @return <code>true</code>, if this is a implementation for the given type, otherwise <code>false</code> + */ + boolean isValidForType(RepositoryType repositoryType); +} diff --git a/archiva-modules/archiva-base/archiva-repository-layer/pom.xml b/archiva-modules/archiva-base/archiva-repository-layer/pom.xml index bc031014e..a5f22a383 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/pom.xml +++ b/archiva-modules/archiva-base/archiva-repository-layer/pom.xml @@ -83,10 +83,16 @@ <version>${project.version}</version> <scope>test</scope> </dependency> + <!-- <dependency> <groupId>org.apache.archiva</groupId> <artifactId>metadata-model</artifactId> </dependency> + --> + <dependency> + <groupId>org.apache.archiva.components.registry</groupId> + <artifactId>archiva-components-spring-registry-api</artifactId> + </dependency> <dependency> <groupId>com.cronutils</groupId> <artifactId>cron-utils</artifactId> @@ -97,6 +103,10 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>javax.annotation</groupId> + <artifactId>javax.annotation-api</artifactId> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> @@ -111,21 +121,25 @@ <!-- Test scope --> + <!-- <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <scope>test</scope> </dependency> + --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <scope>test</scope> </dependency> + <!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <scope>test</scope> </dependency> + --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java index 0f0d4bfea..1c10a2a49 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java @@ -38,10 +38,13 @@ import org.apache.archiva.repository.RepositoryGroup; import org.apache.archiva.repository.RepositoryProvider; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.UnsupportedRepositoryTypeException; import org.apache.archiva.repository.event.*; import org.apache.archiva.event.EventHandler; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.StagingRepositoryFeature; +import org.apache.archiva.repository.metadata.MetadataReader; +import org.apache.archiva.repository.storage.StorageAsset; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -89,6 +92,9 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa ArchivaConfiguration archivaConfiguration; @Inject + List<MetadataReader> metadataReaderList; + + @Inject @Named("repositoryContentFactory#default") RepositoryContentFactory repositoryContentFactory; @@ -254,6 +260,16 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa return indexManagerFactory.getIndexManager(type); } + @Override + public MetadataReader getMetadataReader( final RepositoryType type ) throws UnsupportedRepositoryTypeException + { + if (metadataReaderList!=null) { + return metadataReaderList.stream( ).filter( mr -> mr.isValidForType( type ) ).findFirst( ).orElseThrow( ( ) -> new UnsupportedRepositoryTypeException( type ) ); + } else { + throw new UnsupportedRepositoryTypeException( type ); + } + } + private void createIndexingContext( EditableRepository editableRepo) throws RepositoryException { if (editableRepo.supportsFeature(IndexCreationFeature.class)) { ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType()); @@ -1261,6 +1277,13 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa return cloned; } + @Override + public Repository getRepositoryOfAsset( StorageAsset asset ) + { + return getManagedRepositories( ).stream( ).filter( r -> r.getAsset( "" ) + .getStorage( ).equals( asset.getStorage( ) ) ).findFirst( ).orElse( null ); + } + @Override public void configurationEvent(ConfigurationEvent event) { diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/base/MetadataTools.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/base/MetadataTools.java index 53786b1f3..d85b3a320 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/base/MetadataTools.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/base/MetadataTools.java @@ -30,7 +30,7 @@ import org.apache.archiva.configuration.ConfigurationListener; import org.apache.archiva.configuration.ConfigurationNames; import org.apache.archiva.configuration.FileTypes; import org.apache.archiva.configuration.ProxyConnectorConfiguration; -import org.apache.archiva.maven2.metadata.MavenMetadataReader; +// import org.apache.archiva.maven2.metadata.MavenMetadataReader; import org.apache.archiva.model.ArchivaRepositoryMetadata; import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.model.Plugin; @@ -43,6 +43,10 @@ import org.apache.archiva.repository.ContentNotFoundException; import org.apache.archiva.repository.LayoutException; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.RemoteRepositoryContent; +import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.metadata.MetadataReader; import org.apache.archiva.repository.metadata.RepositoryMetadataException; import org.apache.archiva.repository.storage.StorageAsset; import org.apache.archiva.xml.XMLException; @@ -75,7 +79,7 @@ import java.util.stream.Stream; public class MetadataTools implements RegistryListener, ConfigurationListener { - private Logger log = LoggerFactory.getLogger( getClass() ); + private static final Logger log = LoggerFactory.getLogger( MetadataTools.class ); public static final String MAVEN_METADATA = "maven-metadata.xml"; @@ -85,6 +89,9 @@ public class MetadataTools private static final char GROUP_SEPARATOR = '.'; + @Inject + private RepositoryRegistry repositoryRegistry; + /** * */ @@ -359,26 +366,12 @@ public class MetadataTools public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference, String proxyId ) { + MetadataReader reader = getMetadataReader( managedRepository ); + String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) ); StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath ); - if ( !metadataFile.exists() || metadataFile.isContainer()) - { - // Nothing to do. return null. - return null; - } - - try - { - return MavenMetadataReader.read( metadataFile ); - } - catch (XMLException | IOException e ) - { - // TODO: [monitor] consider a monitor for this event. - // TODO: consider a read-redo on monitor return code? - log.warn( "Unable to read metadata: {}", metadataFile.getPath(), e ); - return null; - } + return readMetadataFile( managedRepository, metadataFile ); } public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository, @@ -386,24 +379,7 @@ public class MetadataTools { String metadataPath = getRepositorySpecificName( proxyId, logicalResource ); StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath ); - - if ( !metadataFile.exists() || metadataFile.isContainer()) - { - // Nothing to do. return null. - return null; - } - - try - { - return MavenMetadataReader.read( metadataFile ); - } - catch (XMLException | IOException e ) - { - // TODO: [monitor] consider a monitor for this event. - // TODO: consider a read-redo on monitor return code? - log.warn( "Unable to read metadata: {}", metadataFile.getPath(), e ); - return null; - } + return readMetadataFile( managedRepository, metadataFile ); } public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository, @@ -411,24 +387,7 @@ public class MetadataTools { String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) ); StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath ); - - if ( !metadataFile.exists() || metadataFile.isContainer()) - { - // Nothing to do. return null. - return null; - } - - try - { - return MavenMetadataReader.read( metadataFile ); - } - catch (XMLException | IOException e ) - { - // TODO: [monitor] consider a monitor for this event. - // TODO: consider a read-redo on monitor return code? - log.warn( "Unable to read metadata: {}", metadataFile.getPath(), e ); - return null; - } + return readMetadataFile( managedRepository, metadataFile ); } public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource ) @@ -504,22 +463,10 @@ public class MetadataTools if ( file.exists() ) { - try + ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, file ); + if ( existingMetadata != null ) { - ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file ); - if ( existingMetadata != null ) - { - metadatas.add( existingMetadata ); - } - } - catch (XMLException | IOException e ) - { - log.debug( "Could not read metadata at {}. Metadata will be removed.", file.getPath() ); - try { - file.getStorage().removeAsset(file); - } catch (IOException ex) { - log.error("Could not remove asset {}", file.getPath()); - } + metadatas.add( existingMetadata ); } } @@ -563,8 +510,9 @@ public class MetadataTools { StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) ); + ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, metadataFile ); - long lastUpdated = getExistingLastUpdated( metadataFile ); + long lastUpdated = getExistingLastUpdated( existingMetadata ); ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata(); metadata.setGroupId( reference.getGroupId() ); @@ -577,16 +525,9 @@ public class MetadataTools // TODO: do we know this information instead? // Set<Plugin> allPlugins = managedRepository.getPlugins( reference ); Set<Plugin> allPlugins; - if ( metadataFile.exists()) + if ( existingMetadata!=null) { - try - { - allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile ).getPlugins() ); - } - catch ( XMLException e ) - { - throw new RepositoryMetadataException( e.getMessage(), e ); - } + allPlugins = new LinkedHashSet<Plugin>( existingMetadata.getPlugins() ); } else { @@ -641,6 +582,16 @@ public class MetadataTools checksum.fixChecksums( algorithms ); } + public MetadataReader getMetadataReader( ManagedRepositoryContent managedRepository ) + { + if (managedRepository!=null) + { + return repositoryRegistry.getMetadataReader( managedRepository.getRepository( ).getType( ) ); + } else { + return repositoryRegistry.getMetadataReader( RepositoryType.MAVEN ); + } + } + private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata ) { // Sort the versions @@ -732,25 +683,34 @@ public class MetadataTools } } - private long getExistingLastUpdated( StorageAsset metadataFile ) - { - if ( !metadataFile.exists() ) + ArchivaRepositoryMetadata readMetadataFile( ManagedRepositoryContent repository, StorageAsset asset) { + MetadataReader reader = getMetadataReader( repository ); + try { - // Doesn't exist. - return 0; + if (asset.exists() && !asset.isContainer()) + { + return reader.read( asset ); + } else { + log.error( "Trying to read metadata from container: {}", asset.getPath( ) ); + return null; + } } - - try + catch ( RepositoryMetadataException e ) { - ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile ); - - return getLastUpdated( metadata ); + log.error( "Could not read metadata file {}", asset, e ); + return null; } - catch (XMLException | IOException e ) + } + + private long getExistingLastUpdated( ArchivaRepositoryMetadata metadata ) + { + if ( metadata==null ) { - // Error. + // Doesn't exist. return 0; } + + return getLastUpdated( metadata ); } /** @@ -773,8 +733,9 @@ public class MetadataTools throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException { StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) ); + ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile ); - long lastUpdated = getExistingLastUpdated( metadataFile ); + long lastUpdated = getExistingLastUpdated( existingMetadata ); ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata(); metadata.setGroupId( reference.getGroupId() ); @@ -988,4 +949,6 @@ public class MetadataTools { log.debug( "Configuration event {}", event ); } + + } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/MetadataReaderMock.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/MetadataReaderMock.java new file mode 100644 index 000000000..c38276f20 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/MetadataReaderMock.java @@ -0,0 +1,49 @@ +package org.apache.archiva.repository.mock; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.model.ArchivaRepositoryMetadata; +import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.metadata.MetadataReader; +import org.apache.archiva.repository.metadata.RepositoryMetadataException; +import org.apache.archiva.repository.storage.StorageAsset; +import org.springframework.stereotype.Service; + +@Service("metadataReader#test") +public class MetadataReaderMock implements MetadataReader +{ + @Override + public ArchivaRepositoryMetadata read( StorageAsset asset ) throws RepositoryMetadataException + { + return new ArchivaRepositoryMetadata(); + } + + @Override + public boolean isValidMetadataPath( String path ) + { + return true; + } + + @Override + public boolean isValidForType( RepositoryType repositoryType ) + { + return true; + } +} diff --git a/archiva-modules/archiva-base/archiva-xml-tools/pom.xml b/archiva-modules/archiva-base/archiva-xml-tools/pom.xml index 6423d268e..99ff8973e 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/pom.xml +++ b/archiva-modules/archiva-base/archiva-xml-tools/pom.xml @@ -38,6 +38,10 @@ <artifactId>archiva-common</artifactId> </dependency> <dependency> + <groupId>org.apache.archiva</groupId> + <artifactId>archiva-storage-api</artifactId> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> </dependency> diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java index 49073d930..2180dafe9 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java @@ -19,6 +19,7 @@ package org.apache.archiva.xml; * under the License. */ +import org.apache.archiva.repository.storage.StorageAsset; import org.apache.commons.lang3.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -37,6 +38,7 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.*; import java.io.*; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -78,6 +80,10 @@ public class XMLReader public XMLReader( String type, Path file ) throws XMLException { + initWithFile( type, file ); + } + + private void initWithFile( String type, Path file) throws XMLException { if ( !Files.exists(file) ) { throw new XMLException( "file does not exist: " + file.toAbsolutePath() ); @@ -95,7 +101,7 @@ public class XMLReader try { - init( type, file.toUri().toURL() ); + initWithUrl( type, file.toUri().toURL() ); } catch ( MalformedURLException e ) { @@ -103,23 +109,52 @@ public class XMLReader } } + public XMLReader( String type, StorageAsset asset) throws XMLException + { + if (asset.isFileBased()) { + initWithFile( type, asset.getFilePath( ) ); + } else { + URI uri = asset.getStorage( ).getLocation( ).resolve( asset.getPath( ) ); + try(InputStream in = asset.getReadStream()) { + initWithStream( type, uri.toURL( ), in ); + } + catch ( IOException e ) + { + throw new XMLException( "Could not open asset stream of " + uri + ": " + e.getMessage( ), e ); + } + } + + + } + + public XMLReader( String type, URL url ) throws XMLException { - init( type, url ); + initWithUrl( type, url ); + } + + + private void initWithUrl( String type, URL url ) throws XMLException { + try(InputStream in = url.openStream()) { + initWithStream( type, url, in ); + } + catch ( IOException e ) + { + throw new XMLException( "Could not open url " + url + ": " + e.getMessage( ), e ); + } } - private void init( String type, URL url ) + private void initWithStream( String type, URL url, InputStream in ) throws XMLException { this.documentType = type; this.xmlUrl = url; - // SAXReader reader = new SAXReader(); - try (InputStream in = url.openStream(); Reader reader = new LatinEntityResolutionReader(new BufferedReader(new InputStreamReader(in, "UTF-8")))) + try (Reader reader = new LatinEntityResolutionReader(new BufferedReader(new InputStreamReader(in, "UTF-8")))) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |