From eecea0895ef007e2c05ed8b544fb0947f170bd39 Mon Sep 17 00:00:00 2001 From: Herve Boutemy Date: Tue, 16 Oct 2007 21:26:43 +0000 Subject: [PATCH] [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 --- .../settings/DefaultMavenSettingsBuilder.java | 9 +++--- .../apache/maven/embedder/MavenEmbedder.java | 17 ++++++----- .../maven/embedder/MavenEmbedderTest.java | 29 ++++++++++--------- .../profiles/DefaultMavenProfilesBuilder.java | 21 +++++++------- .../project/DefaultMavenProjectBuilder.java | 23 +++++---------- .../model/DefaultModelLineageBuilder.java | 7 +++-- .../model/DefaultModelLineageBuilderTest.java | 11 +++---- .../validation/DefaultModelValidatorTest.java | 4 +-- 8 files changed, 60 insertions(+), 61 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index c2704f3597..b814bdae63 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -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; @@ -49,7 +50,7 @@ public class DefaultMavenSettingsBuilder private SettingsValidator validator; /** @since 2.1 */ - public Settings buildSettings( MavenExecutionRequest request ) + public Settings buildSettings( MavenExecutionRequest request ) throws IOException, XmlPullParserException { File userSettingsFile = request.getUserSettingsFile(); @@ -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(); diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java index 89e313f4fc..bb95ca26cd 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -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 ) ); } diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java index 9df329cbfa..7bc22967b4 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -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; @@ -61,7 +62,7 @@ public class MavenEmbedderTest super.setUp(); basedir = System.getProperty( "basedir" ); - + if ( basedir == null ) { basedir = new File( "." ).getCanonicalPath(); @@ -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() ); diff --git a/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java b/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java index e355dcb34c..ec8b98e593 100644 --- a/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java +++ b/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java @@ -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,22 +49,22 @@ 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(); - + try { RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); interpolator.addValueSource( new EnvarBasedValueSource() ); - + rawInput = interpolator.interpolate( rawInput, "settings" ); } catch ( Exception e ) @@ -78,7 +79,7 @@ public class DefaultMavenProfilesBuilder } finally { - IOUtil.close( fileReader ); + IOUtil.close( profileReader ); } } diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 1a8438a336..073c6268fe 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -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( "4.0.0" ) < 0 ) + if ( modelSource.indexOf( "" + 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 ) diff --git a/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java b/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java index 14ae34b96d..c9e1730ea6 100644 --- a/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java @@ -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 ) diff --git a/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java b/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java index ebda3f09e8..5bffc04177 100644 --- a/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java @@ -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(); diff --git a/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java b/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java index 875aed7659..0b0cab64f1 100644 --- a/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java @@ -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();