Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

rhbz760390 Add support for GWT's UTF-8 Properties files; handle entri…

…es where value is the empty string
  • Loading branch information...
commit f072bb3814fcb3f1680d98b0e2028c8a32dd84f8 1 parent 60006fc
Sean Flanigan authored December 06, 2011
30  client/zanata-adapter-properties/src/main/java/org/zanata/adapter/properties/PropWriter.java
@@ -3,7 +3,8 @@
3 3
 import java.io.File;
4 4
 import java.io.FileOutputStream;
5 5
 import java.io.IOException;
6  
-import java.io.PrintStream;
  6
+import java.io.OutputStreamWriter;
  7
+import java.io.Writer;
7 8
 
8 9
 import org.fedorahosted.openprops.Properties;
9 10
 import org.zanata.rest.dto.extensions.comment.SimpleComment;
@@ -27,7 +28,7 @@ private static void makeParentDirs(File f)
27 28
          parentFile.mkdirs();
28 29
    }
29 30
 
30  
-   public static void write(final Resource doc, final File baseDir) throws IOException
  31
+   public static void write(final Resource doc, final File baseDir, String charset) throws IOException
31 32
    {
32 33
       File baseFile = new File(baseDir, doc.getName() + ".properties");
33 34
       makeParentDirs(baseFile);
@@ -42,13 +43,19 @@ public static void write(final Resource doc, final File baseDir) throws IOExcept
42 43
             props.setComment(textFlow.getId(), simpleComment.getValue());
43 44
       }
44 45
       // props.store(System.out, null);
45  
-      PrintStream out = new PrintStream(new FileOutputStream(baseFile));
46  
-      props.store(out, null);
  46
+      Writer out = new OutputStreamWriter(new FileOutputStream(baseFile), charset);
  47
+      try
  48
+      {
  49
+         props.store(out, null);
  50
+      }
  51
+      finally
  52
+      {
  53
+         out.close();
  54
+      }
47 55
    }
48 56
 
49  
-   public static void write(final TranslationsResource doc, final File baseDir, String bundleName, String locale) throws IOException
  57
+   public static void write(final TranslationsResource doc, final File baseDir, String bundleName, String locale, String charset) throws IOException
50 58
    {
51  
-
52 59
       Properties targetProp = new Properties();
53 60
       for (TextFlowTarget target : doc.getTextFlowTargets())
54 61
       {
@@ -62,8 +69,15 @@ public static void write(final TranslationsResource doc, final File baseDir, Str
62 69
       makeParentDirs(langFile);
63 70
       logVerbose("Creating target file " + langFile);
64 71
       // targetProp.store(System.out, null);
65  
-      PrintStream out2 = new PrintStream(new FileOutputStream(langFile));
66  
-      targetProp.store(out2, null);
  72
+      Writer out2 = new OutputStreamWriter(new FileOutputStream(langFile), charset);
  73
+      try
  74
+      {
  75
+         targetProp.store(out2, null);
  76
+      }
  77
+      finally
  78
+      {
  79
+         out2.close();
  80
+      }
67 81
    }
68 82
 
69 83
 }
9  client/zanata-adapter-properties/src/test/java/org/zanata/adapter/properties/PropReaderTests.java
@@ -21,6 +21,7 @@
21 21
 public class PropReaderTests
22 22
 {
23 23
    private static final Logger log = LoggerFactory.getLogger(PropReaderTests.class);
  24
+   static final String ISO_8859_1 = "ISO-8859-1";
24 25
 
25 26
    @SuppressWarnings("deprecation")
26 27
    PropReader propReader = new PropReader();
@@ -42,9 +43,9 @@ public void roundtripSrcPropsToDocXmlToProps() throws Exception
42 43
       Unmarshaller unmarshal = jc.createUnmarshaller();
43 44
       Resource docIn = (Resource) unmarshal.unmarshal(new StringReader(sw.toString()));
44 45
 
45  
-      PropWriter.write(docIn, new File("target/test-output"));
  46
+      PropWriter.write(docIn, new File("target/test-output"), ISO_8859_1);
46 47
 
47  
-      // TODO check output files against input
  48
+      // FIXME check output files against input
48 49
    }
49 50
 
50 51
    @Test
@@ -65,9 +66,9 @@ public void roundtripTransPropsToDocXmlToProps() throws Exception
65 66
       Unmarshaller unmarshal = jc.createUnmarshaller();
66 67
       TranslationsResource docIn = (TranslationsResource) unmarshal.unmarshal(new StringReader(sw.toString()));
67 68
 
68  
-      PropWriter.write(docIn, new File("target/test-output"), "test", locale);
  69
+      PropWriter.write(docIn, new File("target/test-output"), "test", locale, ISO_8859_1);
69 70
 
70  
-      // TODO check output files against input
  71
+      // FIXME check output files against input
71 72
    }
72 73
 
73 74
    private InputStream getResourceAsStream(String relativeResourceName) throws FileNotFoundException
3  client/zanata-client-commands/src/main/java/org/zanata/client/commands/ConfigurableProjectCommand.java
@@ -34,8 +34,9 @@
34 34
 public abstract class ConfigurableProjectCommand<O extends ConfigurableProjectOptions> extends ConfigurableCommand<O>
35 35
 {
36 36
 
37  
-   protected static final String PROJECT_TYPE_PUBLICAN = "podir";
  37
+   protected static final String PROJECT_TYPE_UTF8_PROPERTIES = "utf8properties";
38 38
    protected static final String PROJECT_TYPE_PROPERTIES = "properties";
  39
+   protected static final String PROJECT_TYPE_PUBLICAN = "podir";
39 40
    protected static final String PROJECT_TYPE_XLIFF = "xliff";
40 41
    protected static final String PROJECT_TYPE_XML = "xml";
41 42
 
18  client/zanata-client-commands/src/main/java/org/zanata/client/commands/pull/PropertiesStrategy.java
@@ -35,8 +35,22 @@
35 35
  */
36 36
 public class PropertiesStrategy implements PullStrategy
37 37
 {
  38
+   // "8859_1" is used in Properties.java...
  39
+   private static final String ISO_8859_1 = "ISO-8859-1";
  40
+
38 41
    StringSet extensions = new StringSet("comment");
39 42
    private PullOptions opts;
  43
+   private final String charset;
  44
+
  45
+   public PropertiesStrategy()
  46
+   {
  47
+      this(ISO_8859_1);
  48
+   }
  49
+
  50
+   public PropertiesStrategy(String charset)
  51
+   {
  52
+      this.charset = charset;
  53
+   }
40 54
 
41 55
    @Override
42 56
    public void setPullOptions(PullOptions opts)
@@ -59,13 +73,13 @@ public boolean needsDocToWriteTrans()
59 73
    @Override
60 74
    public void writeSrcFile(Resource doc) throws IOException
61 75
    {
62  
-      PropWriter.write(doc, opts.getSrcDir());
  76
+      PropWriter.write(doc, opts.getSrcDir(), charset);
63 77
    }
64 78
 
65 79
    @Override
66 80
    public void writeTransFile(Resource doc, String docName, LocaleMapping localeMapping, TranslationsResource targetDoc) throws IOException
67 81
    {
68  
-      PropWriter.write(targetDoc, opts.getTransDir(), docName, localeMapping.getJavaLocale());
  82
+      PropWriter.write(targetDoc, opts.getTransDir(), docName, localeMapping.getJavaLocale(), charset);
69 83
    }
70 84
 
71 85
 }
2  client/zanata-client-commands/src/main/java/org/zanata/client/commands/pull/PullCommand.java
@@ -38,10 +38,12 @@
38 38
 public class PullCommand extends ConfigurableProjectCommand
39 39
 {
40 40
    private static final Logger log = LoggerFactory.getLogger(PullCommand.class);
  41
+   private static final String UTF_8 = "UTF-8";
41 42
 
42 43
    private static final Map<String, PullStrategy> strategies = new HashMap<String, PullStrategy>();
43 44
 
44 45
    {
  46
+      strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, new PropertiesStrategy(UTF_8));
45 47
       strategies.put(PROJECT_TYPE_PROPERTIES, new PropertiesStrategy());
46 48
       strategies.put(PROJECT_TYPE_PUBLICAN, new GettextDirStrategy());
47 49
       strategies.put(PROJECT_TYPE_XLIFF, new XliffStrategy());
30  client/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PropertiesStrategy.java
@@ -21,12 +21,12 @@
21 21
 
22 22
 package org.zanata.client.commands.push;
23 23
 
24  
-import java.io.BufferedInputStream;
25 24
 import java.io.File;
26 25
 import java.io.FileInputStream;
27 26
 import java.io.FileNotFoundException;
28 27
 import java.io.IOException;
29  
-import java.io.InputStream;
  28
+import java.io.InputStreamReader;
  29
+import java.io.Reader;
30 30
 import java.util.HashSet;
31 31
 import java.util.List;
32 32
 import java.util.Set;
@@ -46,9 +46,20 @@
46 46
 
47 47
 class PropertiesStrategy extends AbstractPushStrategy
48 48
 {
  49
+   // "8859_1" is used in Properties.java...
  50
+   private static final String ISO_8859_1 = "ISO-8859-1";
  51
+
  52
+   private final String charset;
  53
+
49 54
    public PropertiesStrategy()
50 55
    {
  56
+      this(ISO_8859_1);
  57
+   }
  58
+
  59
+   public PropertiesStrategy(String charset)
  60
+   {
51 61
       super(new StringSet("comment"), ".properties");
  62
+      this.charset = charset;
52 63
    }
53 64
 
54 65
    @Override
@@ -68,16 +79,16 @@ public PropertiesStrategy()
68 79
 
69 80
    private Properties loadPropFile(File propFile) throws FileNotFoundException, IOException
70 81
    {
71  
-      InputStream is = new BufferedInputStream(new FileInputStream(propFile));
  82
+      Reader reader = new InputStreamReader(new FileInputStream(propFile), charset);
72 83
       try
73 84
       {
74 85
          Properties props = new Properties();
75  
-         props.load(is);
  86
+         props.load(reader);
76 87
          return props;
77 88
       }
78 89
       finally
79 90
       {
80  
-         is.close();
  91
+         reader.close();
81 92
       }
82 93
    }
83 94
 
@@ -147,7 +158,14 @@ private void addPropEntryToDoc(TranslationsResource targetDoc, Properties props,
147 158
          return;
148 159
       TextFlowTarget textFlowTarget = new TextFlowTarget(key);
149 160
       textFlowTarget.setContent(content);
150  
-      textFlowTarget.setState(ContentState.Approved);
  161
+      if (!content.isEmpty())
  162
+      {
  163
+         textFlowTarget.setState(ContentState.Approved);
  164
+      }
  165
+      else
  166
+      {
  167
+         textFlowTarget.setState(ContentState.New);
  168
+      }
151 169
       String comment = props.getComment(key);
152 170
       if (comment != null)
153 171
       {
2  client/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushCommand.java
@@ -39,6 +39,7 @@
39 39
 public class PushCommand extends ConfigurableProjectCommand
40 40
 {
41 41
    private static final Logger log = LoggerFactory.getLogger(PushCommand.class);
  42
+   private static final String UTF_8 = "UTF-8";
42 43
 
43 44
    private static final Map<String, AbstractPushStrategy> strategies = new HashMap<String, AbstractPushStrategy>();
44 45
 
@@ -48,6 +49,7 @@
48 49
    }
49 50
 
50 51
    {
  52
+      strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, new PropertiesStrategy(UTF_8));
51 53
       strategies.put(PROJECT_TYPE_PROPERTIES, new PropertiesStrategy());
52 54
       strategies.put(PROJECT_TYPE_PUBLICAN, new GettextDirStrategy());
53 55
       strategies.put(PROJECT_TYPE_XLIFF, new XliffStrategy());

0 notes on commit f072bb3

Please sign in to comment.
Something went wrong with that request. Please try again.