o Introduced component for metadata parsing to abstract from XPP3

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@807145 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-24 10:47:53 +00:00
parent c7142215c2
commit 29b30a3249
5 changed files with 305 additions and 90 deletions

View File

@ -0,0 +1,110 @@
package org.apache.maven.artifact.repository.metadata.io;
/*
* 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 java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* Handles deserialization of metadata from some kind of textual format like XML.
*
* @author Benjamin Bentmann
*/
@Component( role = MetadataReader.class )
public class DefaultMetadataReader
implements MetadataReader
{
public Metadata read( File input, Map<String, ?> options )
throws IOException
{
if ( input == null )
{
throw new IllegalArgumentException( "input file missing" );
}
Metadata metadata = read( ReaderFactory.newXmlReader( input ), options );
return metadata;
}
public Metadata read( Reader input, Map<String, ?> options )
throws IOException
{
if ( input == null )
{
throw new IllegalArgumentException( "input reader missing" );
}
try
{
MetadataXpp3Reader r = new MetadataXpp3Reader();
return r.read( input, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
public Metadata read( InputStream input, Map<String, ?> options )
throws IOException
{
if ( input == null )
{
throw new IllegalArgumentException( "input stream missing" );
}
try
{
MetadataXpp3Reader r = new MetadataXpp3Reader();
return r.read( input, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
private boolean isStrict( Map<String, ?> options )
{
Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
return value == null || Boolean.parseBoolean( value.toString() );
}
}

View File

@ -0,0 +1,93 @@
package org.apache.maven.artifact.repository.metadata.io;
/*
* 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 java.io.IOException;
/**
* Signals a failure to parse the metadata due to invalid syntax (e.g. non-wellformed XML or unknown elements).
*
* @author Benjamin Bentmann
*/
public class MetadataParseException
extends IOException
{
/**
* The one-based index of the line containing the error.
*/
private final int lineNumber;
/**
* The one-based index of the column containing the error.
*/
private final int columnNumber;
/**
* Creates a new parser exception with the specified details.
*
* @param message The error message, may be {@code null}.
* @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
* @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
*/
public MetadataParseException( String message, int lineNumber, int columnNumber )
{
super( message );
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
}
/**
* Creates a new parser exception with the specified details.
*
* @param message The error message, may be {@code null}.
* @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
* @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
* @param cause The nested cause of this error, may be {@code null}.
*/
public MetadataParseException( String message, int lineNumber, int columnNumber, Throwable cause )
{
super( message );
initCause( cause );
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
}
/**
* Gets the one-based index of the line containing the error.
*
* @return The one-based index of the line containing the error or a non-positive value if unknown.
*/
public int getLineNumber()
{
return lineNumber;
}
/**
* Gets the one-based index of the column containing the error.
*
* @return The one-based index of the column containing the error or non-positive value if unknown.
*/
public int getColumnNumber()
{
return columnNumber;
}
}

View File

@ -0,0 +1,82 @@
package org.apache.maven.artifact.repository.metadata.io;
/*
* 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 java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;
import org.apache.maven.artifact.repository.metadata.Metadata;
/**
* Handles deserialization of metadata from some kind of textual format like XML.
*
* @author Benjamin Bentmann
*/
public interface MetadataReader
{
/**
* The key for the option to enable strict parsing. This option is of type {@link Boolean} and defaults to {@code
* true}. If {@code false}, unknown elements will be ignored instead of causing a failure.
*/
static final String IS_STRICT = "org.apache.maven.artifact.repository.metadata.io.isStrict";
/**
* Reads the metadata from the specified file.
*
* @param input The file to deserialize the metadata from, must not be {@code null}.
* @param options The options to use for deserialization, may be {@code null} to use the default values.
* @return The deserialized metadata, never {@code null}.
* @throws IOException If the metadata could not be deserialized.
* @throws MetadataParseException If the input format could not be parsed.
*/
Metadata read( File input, Map<String, ?> options )
throws IOException, MetadataParseException;
/**
* Reads the metadata from the specified character reader. The reader will be automatically closed before the method
* returns.
*
* @param input The reader to deserialize the metadata from, must not be {@code null}.
* @param options The options to use for deserialization, may be {@code null} to use the default values.
* @return The deserialized metadata, never {@code null}.
* @throws IOException If the metadata could not be deserialized.
* @throws MetadataParseException If the input format could not be parsed.
*/
Metadata read( Reader input, Map<String, ?> options )
throws IOException, MetadataParseException;
/**
* Reads the metadata from the specified byte stream. The stream will be automatically closed before the method
* returns.
*
* @param input The stream to deserialize the metadata from, must not be {@code null}.
* @param options The options to use for deserialization, may be {@code null} to use the default values.
* @return The deserialized metadata, never {@code null}.
* @throws IOException If the metadata could not be deserialized.
* @throws MetadataParseException If the input format could not be parsed.
*/
Metadata read( InputStream input, Map<String, ?> options )
throws IOException, MetadataParseException;
}

View File

@ -20,15 +20,14 @@ package org.apache.maven.plugin.prefix.internal;
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataReadException;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.MetadataReader;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.BuildPluginManager;
@ -43,9 +42,6 @@ import org.apache.maven.wagon.TransferFailedException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* Resolves a plugin prefix.
@ -66,6 +62,9 @@ public class DefaultPluginPrefixResolver
@Requirement
private RepositorySystem repositorySystem;
@Requirement
private MetadataReader metadataReader;
public PluginPrefixResult resolve( PluginPrefixRequest request )
throws NoPluginFoundForPrefixException
{
@ -212,7 +211,9 @@ public class DefaultPluginPrefixResolver
{
try
{
Metadata pluginGroupMetadata = readMetadata( metadataFile );
Map<String, ?> options = Collections.singletonMap( MetadataReader.IS_STRICT, Boolean.FALSE );
Metadata pluginGroupMetadata = metadataReader.read( metadataFile, options );
List<org.apache.maven.artifact.repository.metadata.Plugin> plugins = pluginGroupMetadata.getPlugins();
@ -227,7 +228,7 @@ public class DefaultPluginPrefixResolver
}
}
}
catch ( RepositoryMetadataReadException e )
catch ( IOException e )
{
if ( logger.isDebugEnabled() )
{
@ -243,40 +244,4 @@ public class DefaultPluginPrefixResolver
return null;
}
private Metadata readMetadata( File mappingFile )
throws RepositoryMetadataReadException
{
Metadata result;
Reader reader = null;
try
{
reader = ReaderFactory.newXmlReader( mappingFile );
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
result = mappingReader.read( reader, false );
}
catch ( FileNotFoundException e )
{
throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "'", e );
}
catch ( IOException e )
{
throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "': "
+ e.getMessage(), e );
}
catch ( XmlPullParserException e )
{
throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "': "
+ e.getMessage(), e );
}
finally
{
IOUtil.close( reader );
}
return result;
}
}

View File

@ -20,14 +20,13 @@ package org.apache.maven.plugin.version.internal;
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.Collections;
import java.util.Map;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataReadException;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.MetadataReader;
import org.apache.maven.plugin.version.PluginVersionRequest;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.plugin.version.PluginVersionResolver;
@ -38,10 +37,7 @@ import org.apache.maven.wagon.TransferFailedException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* Resolves a version for a plugin.
@ -59,6 +55,9 @@ public class DefaultPluginVersionResolver
@Requirement
private RepositorySystem repositorySystem;
@Requirement
private MetadataReader metadataReader;
public PluginVersionResult resolve( PluginVersionRequest request )
throws PluginVersionResolutionException
{
@ -172,11 +171,13 @@ public class DefaultPluginVersionResolver
{
try
{
Metadata repoMetadata = readMetadata( metadataFile );
Map<String, ?> options = Collections.singletonMap( MetadataReader.IS_STRICT, Boolean.FALSE );
Metadata repoMetadata = metadataReader.read( metadataFile, options );
return mergeMetadata( target, repoMetadata );
}
catch ( RepositoryMetadataReadException e )
catch ( IOException e )
{
if ( logger.isDebugEnabled() )
{
@ -197,40 +198,4 @@ public class DefaultPluginVersionResolver
return target.merge( source );
}
private Metadata readMetadata( File mappingFile )
throws RepositoryMetadataReadException
{
Metadata result;
Reader reader = null;
try
{
reader = ReaderFactory.newXmlReader( mappingFile );
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
result = mappingReader.read( reader, false );
}
catch ( FileNotFoundException e )
{
throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "'", e );
}
catch ( IOException e )
{
throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "': "
+ e.getMessage(), e );
}
catch ( XmlPullParserException e )
{
throw new RepositoryMetadataReadException( "Cannot read metadata from '" + mappingFile + "': "
+ e.getMessage(), e );
}
finally
{
IOUtil.close( reader );
}
return result;
}
}