[MNG-2254] support XML file encoding when reading/writing settings and pom files

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@585265 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Herve Boutemy 2007-10-16 21:26:43 +00:00
parent 89510079bb
commit eecea0895e
8 changed files with 60 additions and 61 deletions

View File

@ -26,14 +26,15 @@ import org.apache.maven.settings.validation.SettingsValidationResult;
import org.apache.maven.settings.validation.SettingsValidator;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.PropertiesBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
@ -148,11 +149,11 @@ public class DefaultMavenSettingsBuilder
Settings settings = null;
FileReader reader = null;
Reader reader = null;
try
{
reader = new FileReader( settingsFile );
reader = ReaderFactory.newXmlReader( settingsFile );
SettingsXpp3Reader modelReader = new SettingsXpp3Reader();

View File

@ -78,15 +78,16 @@ import org.codehaus.plexus.component.repository.exception.ComponentRepositoryExc
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
@ -211,7 +212,7 @@ public class MavenEmbedder
public Model readModel( File file )
throws XmlPullParserException, IOException
{
Reader reader = new FileReader( file );
Reader reader = ReaderFactory.newXmlReader( file );
try
{
@ -252,8 +253,6 @@ public class MavenEmbedder
Settings settings )
throws IOException
{
Writer fileWriter = new FileWriter( file );
SettingsValidator settingsValidator = new DefaultSettingsValidator();
SettingsValidationResult validationResult = settingsValidator.validate( settings );
@ -271,6 +270,8 @@ public class MavenEmbedder
String encoding = settings.getModelEncoding() != null ? settings.getModelEncoding() : "UTF-8";
Writer fileWriter = new OutputStreamWriter( new FileOutputStream( file ), encoding );
Format format = Format.getPrettyFormat().setEncoding( encoding );
try
@ -286,7 +287,7 @@ public class MavenEmbedder
public static Settings readSettings( File file )
throws IOException, SettingsConfigurationException
{
Reader fileReader = new FileReader( file );
Reader fileReader = ReaderFactory.newXmlReader( file );
SettingsValidator settingsValidator = new DefaultSettingsValidator();
@ -795,7 +796,7 @@ public class MavenEmbedder
{
try
{
fileReader = new FileReader( configuration.getUserSettingsFile() );
fileReader = ReaderFactory.newXmlReader( configuration.getUserSettingsFile() );
result.setUserSettings( new SettingsXpp3Reader().read( fileReader ) );
}
@ -819,7 +820,7 @@ public class MavenEmbedder
{
try
{
fileReader = new FileReader( configuration.getGlobalSettingsFile() );
fileReader = ReaderFactory.newXmlReader( configuration.getGlobalSettingsFile() );
result.setGlobalSettings( new SettingsXpp3Reader().read( fileReader ) );
}

View File

@ -36,12 +36,13 @@ import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Iterator;
@ -229,8 +230,8 @@ public class MavenEmbedderTest
model.setBuild( new Build() );
model.getBuild().addPlugin( plugin );
FileWriter writer = new FileWriter( pom );
maven.writeModel( new FileWriter( pom ), model );
Writer writer = WriterFactory.newXmlWriter( pom );
maven.writeModel( writer, model );
writer.close();
/* execute maven */
@ -248,8 +249,8 @@ public class MavenEmbedderTest
/* Add the surefire plugin 2.3 to the pom */
plugin.setVersion( "2.3" );
writer = new FileWriter( pom );
maven.writeModel( new FileWriter( pom ), model );
writer = WriterFactory.newXmlWriter( pom );
maven.writeModel( writer, model );
writer.close();
/* execute Maven */
@ -361,7 +362,7 @@ public class MavenEmbedderTest
File file = new File( basedir, "target/model.xml" );
Writer writer = new FileWriter( file );
Writer writer = WriterFactory.newXmlWriter( file );
maven.writeModel( writer, model );
@ -389,10 +390,10 @@ public class MavenEmbedderTest
File settingsFile = File.createTempFile( "embedder-test.settings.", "" );
settingsFile.deleteOnExit();
FileWriter writer = null;
Writer writer = null;
try
{
writer = new FileWriter( settingsFile );
writer = WriterFactory.newXmlWriter( settingsFile );
new SettingsXpp3Writer().write( writer, s );
}
finally
@ -422,10 +423,10 @@ public class MavenEmbedderTest
File settingsFile = File.createTempFile( "embedder-test.settings.", "" );
settingsFile.deleteOnExit();
FileWriter writer = null;
Writer writer = null;
try
{
writer = new FileWriter( settingsFile );
writer = WriterFactory.newXmlWriter( settingsFile );
new SettingsXpp3Writer().write( writer, s );
}
finally
@ -461,10 +462,10 @@ public class MavenEmbedderTest
MavenEmbedder.writeSettings( settingsFile, s );
FileReader reader = null;
Reader reader = null;
try
{
reader = new FileReader( settingsFile );
reader = ReaderFactory.newXmlReader( settingsFile );
Settings result = new SettingsXpp3Reader().read( reader );
assertEquals( localRepoPath, result.getLocalRepository() );

View File

@ -22,13 +22,14 @@ package org.apache.maven.profiles;
import org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
@ -48,14 +49,14 @@ public class DefaultMavenProfilesBuilder
if ( profilesXml.exists() )
{
ProfilesXpp3Reader reader = new ProfilesXpp3Reader();
FileReader fileReader = null;
Reader profileReader = null;
try
{
fileReader = new FileReader( profilesXml );
profileReader = ReaderFactory.newXmlReader( profilesXml );
StringWriter sWriter = new StringWriter();
IOUtil.copy( fileReader, sWriter );
IOUtil.copy( profileReader, sWriter );
String rawInput = sWriter.toString();
@ -78,7 +79,7 @@ public class DefaultMavenProfilesBuilder
}
finally
{
IOUtil.close( fileReader );
IOUtil.close( profileReader );
}
}

View File

@ -70,16 +70,14 @@ import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
@ -128,8 +126,7 @@ Notes
*/
/**
* @version $Id: DefaultMavenProjectBuilder.java,v 1.37 2005/03/08 01:55:22
* trygvis Exp $
* @version $Id$
*/
public class DefaultMavenProjectBuilder
extends AbstractLogEnabled
@ -995,7 +992,7 @@ public class DefaultMavenProjectBuilder
Reader reader = null;
try
{
reader = new FileReader( file );
reader = ReaderFactory.newXmlReader( file );
return readModel( projectId, file.getAbsolutePath(), reader, strict );
}
catch ( FileNotFoundException e )
@ -1020,15 +1017,11 @@ public class DefaultMavenProjectBuilder
boolean strict )
throws IOException, InvalidProjectModelException
{
StringWriter sw = new StringWriter();
String modelSource = IOUtil.toString( reader );
IOUtil.copy( reader, sw );
String modelSource = sw.toString();
if ( modelSource.indexOf( "<modelVersion>4.0.0" ) < 0 )
if ( modelSource.indexOf( "<modelVersion>" + MAVEN_MODEL_VERSION ) < 0 )
{
throw new InvalidProjectModelException( projectId, pomLocation, "Not a v4.0.0 POM." );
throw new InvalidProjectModelException( projectId, pomLocation, "Not a v" + MAVEN_MODEL_VERSION + " POM." );
}
StringReader sReader = new StringReader( modelSource );
@ -1049,10 +1042,10 @@ public class DefaultMavenProjectBuilder
boolean strict )
throws ProjectBuildingException
{
InputStreamReader reader = null;
Reader reader = null;
try
{
reader = new InputStreamReader( url.openStream() );
reader = ReaderFactory.newXmlReader( url.openStream() );
return readModel( projectId, url.toExternalForm(), reader, strict );
}
catch ( IOException e )

View File

@ -39,12 +39,13 @@ import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
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;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
@ -188,11 +189,11 @@ public class DefaultModelLineageBuilder
}
Model model;
FileReader reader = null;
Reader reader = null;
try
{
reader = new FileReader( pomFile );
reader = ReaderFactory.newXmlReader( pomFile );
model = new MavenXpp3Reader().read( reader );
}
catch ( IOException e )

View File

@ -36,10 +36,11 @@ import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.WriterFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
@ -103,10 +104,10 @@ public class DefaultModelLineageBuilderTest
File pomFile = File.createTempFile( "DefaultModelLineageBuilder.test.", ".pom" );
pomFile.deleteOnExit();
FileWriter writer = null;
Writer writer = null;
try
{
writer = new FileWriter( pomFile );
writer = WriterFactory.newXmlWriter( pomFile );
new MavenXpp3Writer().write( writer, model );
}
@ -475,12 +476,12 @@ public class DefaultModelLineageBuilderTest
File file )
throws IOException
{
FileWriter writer = null;
Writer writer = null;
try
{
file.getParentFile().mkdirs();
writer = new FileWriter( file );
writer = WriterFactory.newXmlWriter( file );
new MavenXpp3Writer().write( writer, model );
writer.flush();

View File

@ -22,8 +22,8 @@ package org.apache.maven.project.validation;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.AbstractMavenProjectTestCase;
import org.codehaus.plexus.util.ReaderFactory;
import java.io.FileReader;
import java.io.Reader;
import java.util.List;
@ -219,7 +219,7 @@ public class DefaultModelValidatorTest
private ModelValidationResult validate( String testName )
throws Exception
{
Reader input = new FileReader( getFileForClasspathResource( "validation/" + testName ) );
Reader input = ReaderFactory.newXmlReader( getFileForClasspathResource( "validation/" + testName ) );
MavenXpp3Reader reader = new MavenXpp3Reader();