Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of github.com:zanata/zanata

  • Loading branch information...
commit 681bb9832327eda581fc1ddfa6e47afb8b64a0e6 2 parents 89a0609 + eaae695
Alex Eng authored February 01, 2012
65  common/zanata-adapter-po/src/main/java/org/zanata/adapter/po/PoReader2.java
@@ -3,13 +3,11 @@
3 3
 import java.io.IOException;
4 4
 import java.net.URL;
5 5
 import java.nio.charset.Charset;
6  
-import java.security.MessageDigest;
7 6
 import java.util.ArrayList;
8 7
 import java.util.HashMap;
9 8
 import java.util.List;
10 9
 import java.util.Map;
11 10
 
12  
-import org.apache.commons.codec.binary.Hex;
13 11
 import org.apache.commons.lang.StringUtils;
14 12
 import org.fedorahosted.tennera.jgettext.HeaderFields;
15 13
 import org.fedorahosted.tennera.jgettext.Message;
@@ -27,6 +25,7 @@
27 25
 import org.zanata.rest.dto.resource.TextFlow;
28 26
 import org.zanata.rest.dto.resource.TextFlowTarget;
29 27
 import org.zanata.rest.dto.resource.TranslationsResource;
  28
+import org.zanata.util.HashUtil;
30 29
 import org.zanata.util.ShortString;
31 30
 
32 31
 import com.google.common.collect.ImmutableSet;
@@ -44,6 +43,16 @@ public PoReader2()
44 43
    {
45 44
    }
46 45
 
  46
+   /**
  47
+    * Extract contents of a PO file and convert to a TranslationsResource.
  48
+    * NB: If the file contains the gettext header Content-Type, it must be
  49
+    * set to ASCII, CHARSET, UTF8 or UTF-8, or an exception will occur.
  50
+    * @param inputSource PO file to be extracted
  51
+    * @param srcDoc source language document
  52
+    * @param useSourceOrder ensure that the TextFlowTargets have the
  53
+    * same order as the TextFlows in srcDoc
  54
+    * @return converted PO file as TranslationsResource
  55
+    */
47 56
    public TranslationsResource extractTarget(InputSource inputSource, Resource srcDoc, boolean useSourceOrder)
48 57
    {
49 58
       TranslationsResource document = new TranslationsResource();
@@ -126,11 +135,37 @@ else if (message.isPlural())
126 135
       return document;
127 136
    }
128 137
 
  138
+   /**
  139
+    * Checks that the file is safe to read as UTF-8.
  140
+    * @param hf
  141
+    */
  142
+   private static void checkContentType(HeaderFields hf)
  143
+   {
  144
+// @formatter:off
  145
+      String contentType = hf.getValue(HeaderFields.KEY_ContentType);
  146
+      if (contentType == null)
  147
+         return;
  148
+      String ct = contentType.toLowerCase();
  149
+      if (!ct.contains("charset="))
  150
+         return;
  151
+      if (ct.contains("charset=charset") || ct.contains("charset=ascii") ||
  152
+            ct.contains("charset=utf-8") || ct.contains("charset=utf8"))
  153
+      {
  154
+         return;
  155
+      }
  156
+      else
  157
+      {
  158
+         throw new RuntimeException("unsupported charset in " + HeaderFields.KEY_ContentType + ": " + contentType);
  159
+      }
  160
+// @formatter:on
  161
+   }
  162
+
129 163
    private static void extractPotHeader(Message message, PoHeader potHeader)
130 164
    {
131 165
       potHeader.setComment(StringUtils.join(message.getComments(), "\n"));
132 166
 
133 167
       HeaderFields hf = HeaderFields.wrap(message);
  168
+      checkContentType(hf);
134 169
       for (String key : hf.getKeys())
135 170
       {
136 171
          String val = hf.getValue(key);
@@ -148,6 +183,7 @@ private static void extractPoHeader(Message message, PoTargetHeader poHeader)
148 183
       poHeader.setComment(StringUtils.join(message.getComments(), "\n"));
149 184
 
150 185
       HeaderFields hf = HeaderFields.wrap(message);
  186
+      checkContentType(hf);
151 187
       for (String key : hf.getKeys())
152 188
       {
153 189
          String val = hf.getValue(key);
@@ -164,6 +200,15 @@ else if (!POT_HEADER_FIELDS.contains(key))
164 200
       }
165 201
    }
166 202
 
  203
+   /**
  204
+    * Extract contents of a POT file and convert to a Resource.
  205
+    * NB: If the file contains the gettext header Content-Type, it must be
  206
+    * set to ASCII, CHARSET, UTF8 or UTF-8, or an exception will occur.
  207
+    * @param inputSource POT file to be extracted
  208
+    * @param sourceLocaleId locale of POT, used to set metadata fields
  209
+    * @param docName name of POT file (minus .pot extension) used to set metadata fields
  210
+    * @return converted POT file as Resource
  211
+    */
167 212
    public Resource extractTemplate(InputSource inputSource, LocaleId sourceLocaleId, String docName)
168 213
    {
169 214
       Resource document = new Resource(docName);
@@ -283,21 +328,7 @@ static String createId(Message message)
283 328
    {
284 329
       String sep = "\u0000";
285 330
       String hashBase = message.getMsgctxt() == null ? message.getMsgid() : message.getMsgctxt() + sep + message.getMsgid();
286  
-      return generateHash(hashBase);
287  
-   }
288  
-
289  
-   public static String generateHash(String key)
290  
-   {
291  
-      try
292  
-      {
293  
-         MessageDigest md5 = MessageDigest.getInstance("MD5");
294  
-         md5.reset();
295  
-         return new String(Hex.encodeHex(md5.digest(key.getBytes("UTF-8"))));
296  
-      }
297  
-      catch (Exception exc)
298  
-      {
299  
-         throw new RuntimeException(exc);
300  
-      }
  331
+      return HashUtil.generateHash(hashBase);
301 332
    }
302 333
 
303 334
 }
28  common/zanata-adapter-po/src/main/java/org/zanata/adapter/po/PoWriter2.java
@@ -73,7 +73,7 @@ public void writePotToDir(File potDir, Resource doc) throws IOException
73 73
       File potFile = new File(potDir, doc.getName() + ".pot");
74 74
       PathUtil.makeParents(potFile);
75 75
       FileWriter fWriter = new FileWriter(potFile);
76  
-      write(fWriter, doc, null);
  76
+      write(fWriter, "UTF-8", doc, null);
77 77
    }
78 78
 
79 79
    /**
@@ -93,7 +93,7 @@ public void writePo(File baseDir, Resource doc, String locale, TranslationsResou
93 93
       File poFile = new File(localeDir, doc.getName() + ".po");
94 94
       mkdirs(poFile.getParentFile());
95 95
       FileWriter fWriter = new FileWriter(poFile);
96  
-      write(fWriter, doc, targetDoc);
  96
+      write(fWriter, "UTF-8", doc, targetDoc);
97 97
    }
98 98
    
99 99
    /**
@@ -108,7 +108,7 @@ public void writePo(File baseDir, Resource doc, String locale, TranslationsResou
108 108
    public void writePo(OutputStream stream, String charset, Resource doc, TranslationsResource targetDoc) throws IOException
109 109
    {
110 110
       OutputStreamWriter osWriter = new OutputStreamWriter(stream, charset);
111  
-      write(osWriter, doc, targetDoc);
  111
+      write(osWriter, charset, doc, targetDoc);
112 112
    }
113 113
 
114 114
    /**
@@ -122,19 +122,19 @@ public void writePo(OutputStream stream, String charset, Resource doc, Translati
122 122
     * @param targetDoc
123 123
     * @throws IOException
124 124
     */
125  
-   private void write(Writer writer, Resource document, TranslationsResource targetDoc) throws IOException
  125
+   private void write(Writer writer, String charset, Resource document, TranslationsResource targetDoc) throws IOException
126 126
    {
127 127
       PoHeader poHeader = document.getExtensions(true).findByType(PoHeader.class);
128 128
       HeaderFields hf = new HeaderFields();
129 129
       if (poHeader == null)
130 130
       {
131 131
          log.warn("No PO header in document named " + document.getName());
132  
-         setDefaultHeaderFields(hf);
133 132
       }
134 133
       else
135 134
       {
136 135
          copyToHeaderFields(hf, poHeader.getEntries());
137 136
       }
  137
+      setEncodingHeaderFields(hf, charset);
138 138
       Message headerMessage = null;
139 139
       if (targetDoc != null)
140 140
       {
@@ -225,11 +225,21 @@ private static void copyTargetHeaderComments(Message headerMessage, PoTargetHead
225 225
       }
226 226
    }
227 227
 
228  
-   static void setDefaultHeaderFields(HeaderFields hf)
  228
+   static void setEncodingHeaderFields(HeaderFields hf, String charset)
229 229
    {
230  
-      hf.setValue("MIME-Version", "1.0");
231  
-      hf.setValue("Content-Type", "text/plain; charset=UTF-8");
232  
-      hf.setValue("Content-Transfer-Encoding", "8bit");
  230
+      hf.setValue(HeaderFields.KEY_MimeVersion, "1.0");
  231
+      hf.setValue(HeaderFields.KEY_ContentTransferEncoding, "8bit");
  232
+
  233
+      String ct, contentType = hf.getValue(HeaderFields.KEY_ContentType);
  234
+      if (contentType == null)
  235
+      {
  236
+         ct = "text/plain; charset=" + charset;
  237
+      }
  238
+      else
  239
+      {
  240
+         ct = contentType.replaceFirst("charset=[^;]*", "charset=" + charset);
  241
+      }
  242
+      hf.setValue(HeaderFields.KEY_ContentType, ct);
233 243
    }
234 244
 
235 245
    static void copyToHeaderFields(HeaderFields hf, final List<HeaderEntry> entries)
0  ...a-war/src/main/java/org/zanata/util/HashUtil.java → ...-util/src/main/java/org/zanata/util/HashUtil.java
File renamed without changes

0 notes on commit 681bb98

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