r808 - in trunk: . src/main/java/org/nuiton/io src/test/java/org/nuiton/io
Author: fdesbois Date: 2011-05-04 00:29:44 +0200 (Wed, 04 May 2011) New Revision: 808 Url: http://nuiton.org/repositories/revision/maven-helper-plugin/808 Log: #1495 : Add methods to use specific encoding with File. The one from instance is renamed defaultEncoding. Keep same implementation as superclass for overriden methods (need only removeHeader management) Modified: trunk/ trunk/pom.xml trunk/src/main/java/org/nuiton/io/SortedProperties.java trunk/src/test/java/org/nuiton/io/SortedPropertiesTest.java Property changes on: trunk ___________________________________________________________________ Modified: svn:ignore - target *.iml *.ipr *.iws + *.ipr *.iws target Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2011-04-11 08:45:56 UTC (rev 807) +++ trunk/pom.xml 2011-05-03 22:29:44 UTC (rev 808) @@ -39,7 +39,7 @@ <artifactId>maven-helper-plugin</artifactId> - <version>1.2.12-SNAPSHOT</version> + <version>1.3-SNAPSHOT</version> <dependencies> Modified: trunk/src/main/java/org/nuiton/io/SortedProperties.java =================================================================== --- trunk/src/main/java/org/nuiton/io/SortedProperties.java 2011-04-11 08:45:56 UTC (rev 807) +++ trunk/src/main/java/org/nuiton/io/SortedProperties.java 2011-05-03 22:29:44 UTC (rev 808) @@ -36,6 +36,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.Writer; import java.util.Collections; import java.util.Date; import java.util.Enumeration; @@ -44,7 +45,8 @@ import java.util.Vector; /** - * Permet d'avoir les fichiers de proprietes tries. + * Extend {@link Properties} to allow alphabetical order. Encoding could also + * be defined but you must use this class only with Java 1.6. * * @author ruchaud <ruchaud@codelutin.com> * @author tchemit <chemit@codelutin.com> @@ -53,6 +55,12 @@ private static final long serialVersionUID = -1147150444452577558L; + public static final String ENCODING_DEFAULT = "8859_1"; + + public static final String ENCODING_LATIN1 = "iso-8859-1"; + + public static final String ENCODING_ASCII = "us-ascii"; + /** A table of hex digits in upper case */ private static final char[] hexDigitUpper = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' @@ -63,8 +71,8 @@ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - /** l'encoding a utiliser pour lire et ecrire le properties. */ - protected String encoding; + /** l'encoding par defaut a utiliser pour lire et ecrire le properties. */ + protected String defaultEncoding; /** un drapeau pour savoir s'il faut enlever l'entete generere */ protected boolean removeHeader; @@ -78,17 +86,20 @@ final protected char[] hexDigit; + public SortedProperties() { + this(ENCODING_DEFAULT); + } - public SortedProperties(String encoding) { - this(encoding, true, false); + public SortedProperties(String defaultEncoding) { + this(defaultEncoding, true, false); } - public SortedProperties(String encoding, boolean removeHeader) { - this(encoding, removeHeader, false); + public SortedProperties(String defaultEncoding, boolean removeHeader) { + this(defaultEncoding, removeHeader, false); } - public SortedProperties(String encoding, boolean removeHeader, boolean unicodeLower) { - this.encoding = encoding; + public SortedProperties(String defaultEncoding, boolean removeHeader, boolean unicodeLower) { + this.defaultEncoding = defaultEncoding; this.removeHeader = removeHeader; this.unicodeLower = unicodeLower; hexDigit = unicodeLower ? hexDigitLower : hexDigitUpper; @@ -96,6 +107,7 @@ public SortedProperties(Properties defaults) { super(defaults); + defaultEncoding = ENCODING_DEFAULT; unicodeLower = false; hexDigit = hexDigitUpper; } @@ -119,13 +131,31 @@ } /** - * Charge le properties a partir d'un fichier. + * Load all properties from given {@code src} file using defined {@code defaultEncoding}. * - * @param src le fichier src a charger en utilisant l'encoding declare - * @return l'instance du properties + * @param src source file + * @return this instance * @throws IOException if any io pb + * @see #load(File, String) */ public SortedProperties load(File src) throws IOException { + return load(src, defaultEncoding); + } + + /** + * Load Properties from {@code src} file using given {@code defaultEncoding}. + * If this {@code encoding} is different from default ones + * {@link #ENCODING_DEFAULT}, {@link #ENCODING_LATIN1} + * and {@link #ENCODING_ASCII}. A specific {@link Reader} will + * be used to read the file. + * + * @param src File where Properties will be loaded + * @param encoding Encoding to use + * @throws IOException for any file errors + * @since 1.3 + * @return this instance + */ + public SortedProperties load(File src, String encoding) throws IOException { Reader reader = new InputStreamReader(new FileInputStream(src), encoding); try { load(reader); @@ -136,25 +166,83 @@ } /** - * Sauvegarde le properties dans un fichier, sans commentaire et en utilisant l'encoding declare. + * Save properties in given {@code dst} file using defined {@code defaultEncoding}. * - * @param dst the fichier de destination + * @param dst output file * @throws IOException if any io pb + * @see #store(File, String) */ public void store(File dst) throws IOException { - OutputStream writer = new FileOutputStream(dst); + store(dst, defaultEncoding); + } + + /** + * Store Properties in {@code output} file using given {@code defaultEncoding}. + * If this {@code encoding} is different from default ones + * {@link #ENCODING_DEFAULT}, {@link #ENCODING_LATIN1} + * and {@link #ENCODING_ASCII}, a specific {@link Writer} will + * be used to save the file. Otherwise the default Properties behavior + * will escape unicode chars with <code>\u</code><i>xxxx</i>. + * + * @param dst File to save Properties + * @param encoding Encoding to use + * @throws IOException for any file errors + * @since 1.3 + */ + public void store(File dst, String encoding) throws IOException { + OutputStream stream = new FileOutputStream(dst); try { - store(writer, null); + storeEncode(stream, encoding); } finally { - writer.close(); + stream.close(); } } /** - * Sauvegarde le properties dans un fichier, sans commentaire en laissant - * java encode en unicode. + * If encoding is not the default Properties one, we will use a writer + * to use this custom encoding. + * <p/> + * We must call the right method depends on encoding, for a custom one + * the {@link #store(Writer, String)} method is used otherwise the + * default encoding 8859_1 must be used as superclass define it in + * {@link #store(OutputStream, String)}. + * <p/> + * Encoding {@link #ENCODING_DEFAULT}, + * {@link #ENCODING_LATIN1} and {@link #ENCODING_ASCII} + * are considered as default Properties one, so the old method will be use + * escaping unicode chars like <code>\u</code><i>xxxx</i>. * - * @param dst le fichier de destination + * @param stream OutputStream to save in + * @param encoding Encoding to use + * @throws IOException For any file errors + */ + protected void storeEncode(OutputStream stream, String encoding) throws IOException { + + // Specific encoding different from default ones + if (!ENCODING_LATIN1.equalsIgnoreCase(encoding) && + !ENCODING_DEFAULT.equals(encoding) && + !ENCODING_ASCII.equalsIgnoreCase(encoding)) { + + Writer writer = new OutputStreamWriter(stream, encoding); + try { + store(writer, null); + } finally { + writer.close(); + } + + } else { + + store(stream, null); + } + } + + /** + * Save properties in given {@code stream} file. {@code defaultEncoding} defined + * will be ignored to store the properties file. The default encoding is the + * basic Properties one (Latin1 ISO), unicode chars will be escaped + * like basic {@link #store(OutputStream, String)} method. + * + * @param dst output file * @throws IOException if any io pb */ public void store(OutputStream dst) throws IOException { @@ -164,26 +252,52 @@ @Override public void store(OutputStream out, String comments) throws IOException { - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoding)); + + // Keep same implementation as super Properties class. Even if the + // encoding is modified, the resulting file always be an ASCII one + // because of escUnicode flag in store0 method. This method is overriden + // because of custom store0 method that can removeHeader. + + BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(out, "8859_1")); try { - store0(writer, - // store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")), - comments, - true); + store0(writer, comments, true); } finally { writer.close(); } } + @Override + public void store(Writer writer, String comments) throws IOException { + + // Keep same implementation as super Properties class. This method + // is overriden because of custom store0 method that can removeHeader. + + BufferedWriter bufferedWriter; + if (writer instanceof BufferedWriter) { + bufferedWriter = (BufferedWriter) writer; + } else { + bufferedWriter = new BufferedWriter(writer); + } + try { + store0(bufferedWriter, comments, false); + } finally { + writer.close(); + } + } + protected void store0(BufferedWriter bw, String comments, boolean escUnicode) throws IOException { if (comments != null) { writeComments(bw, comments); } + + // Here is the modification wanted if (!removeHeader) { bw.write("#" + new Date().toString()); bw.newLine(); } + synchronized (this) { for (Enumeration e = keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); @@ -200,10 +314,8 @@ bw.flush(); } - /* - * Converts unicodes to encoded \uxxxx and escapes - * special characters with a preceding slash - */ + // --- Copy implementations from Properties superClass, the store0 method + // is overriden and all method calls are private, so we copy the code here. protected String saveConvert(String theString, boolean escapeSpace, Modified: trunk/src/test/java/org/nuiton/io/SortedPropertiesTest.java =================================================================== --- trunk/src/test/java/org/nuiton/io/SortedPropertiesTest.java 2011-04-11 08:45:56 UTC (rev 807) +++ trunk/src/test/java/org/nuiton/io/SortedPropertiesTest.java 2011-05-03 22:29:44 UTC (rev 808) @@ -26,14 +26,23 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.codehaus.plexus.util.IOUtil; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.nuiton.plugin.PluginHelper; import org.nuiton.plugin.TestHelper; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; import java.net.URL; +import java.util.Properties; /** * To test {@link SortedProperties}. @@ -71,7 +80,7 @@ File in = new File(resource.toURI()); log.info("Load file " + in); - SortedProperties p = new SortedProperties("utf-8", true); + SortedProperties p = new SortedProperties("iso-8859-1", true); p.load(in); @@ -81,8 +90,8 @@ p.store(out); - String inStr = PluginHelper.readAsString(in, "utf-8"); - String outStr = PluginHelper.readAsString(out, "utf-8"); + String inStr = PluginHelper.readAsString(in, "iso-8859-1"); + String outStr = PluginHelper.readAsString(out, "iso-8859-1"); Assert.assertEquals(inStr.trim(), outStr.trim()); @@ -102,7 +111,7 @@ File in = new File(resource.toURI()); log.info("Load file " + in); - SortedProperties p = new SortedProperties("utf-8", true, true); + SortedProperties p = new SortedProperties("iso-8859-1", true, true); p.load(in); @@ -112,10 +121,35 @@ p.store(out); + String inStr = PluginHelper.readAsString(in, "iso-8859-1"); + String outStr = PluginHelper.readAsString(out, "iso-8859-1"); + + Assert.assertEquals(inStr.trim(), outStr.trim()); + } + + @Test + public void testEncodingUTF8() throws Exception { + + URL resource = getClass().getResource("unicodeProperties.properties"); + File in = new File(resource.toURI()); + + log.info("Load file " + in); + SortedProperties p = new SortedProperties("utf-8"); + + p.load(in); + + Assert.assertEquals(p.get("key"), "àéçèéù 汉字"); + + File out = new File(testDir, "testEncodingUTF8.properties"); + + log.info("Store file to " + out); + + p.store(out); + String inStr = PluginHelper.readAsString(in, "utf-8"); String outStr = PluginHelper.readAsString(out, "utf-8"); Assert.assertEquals(inStr.trim(), outStr.trim()); + } - } }
participants (1)
-
fdesbois@users.nuiton.org