Changeset 721:f72b7bb18930
- Timestamp:
- 10/09/09 11:11:37 (4 years ago)
- Author:
- tgambet
- Branch:
- default
- convert_revision:
- svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@722
- Message:
-
updated
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r719
|
r721
|
|
| | 1 | // $Id: LanguageAction.java,v 1.8 2009-10-09 11:11:37 tgambet Exp $ |
| | 2 | // Author: Thomas Gambet |
| | 3 | // (c) COPYRIGHT MIT, ERCIM and Keio, 2009. |
| | 4 | // Please first read the full copyright statement in file COPYRIGHT.html |
| 1 | 5 | package org.w3c.unicorn.action; |
| 2 | 6 | |
| 3 | | import java.io.CharArrayWriter; |
| 4 | | import java.io.File; |
| 5 | | import java.io.FileOutputStream; |
| 6 | | import java.io.FileWriter; |
| | 7 | import java.io.ByteArrayOutputStream; |
| 7 | 8 | import java.io.IOException; |
| 8 | 9 | import java.io.OutputStreamWriter; |
| 9 | 10 | import java.io.PrintWriter; |
| 10 | | import java.io.Writer; |
| 11 | | import java.net.URLDecoder; |
| 12 | | import java.nio.ByteBuffer; |
| 13 | | import java.nio.CharBuffer; |
| 14 | | import java.nio.charset.CharacterCodingException; |
| 15 | | import java.nio.charset.Charset; |
| 16 | | import java.nio.charset.CharsetDecoder; |
| 17 | | import java.nio.charset.CharsetEncoder; |
| 18 | 11 | import java.util.ArrayList; |
| 19 | 12 | import java.util.Hashtable; |
| … |
… |
|
| 28 | 21 | import javax.servlet.http.HttpServletResponse; |
| 29 | 22 | |
| 30 | | import org.apache.commons.io.output.ByteArrayOutputStream; |
| | 23 | import org.apache.commons.logging.Log; |
| | 24 | import org.apache.commons.logging.LogFactory; |
| 31 | 25 | import org.apache.velocity.VelocityContext; |
| 32 | 26 | import org.w3c.unicorn.Framework; |
| 33 | 27 | import org.w3c.unicorn.exceptions.UnicornException; |
| 34 | | import org.w3c.unicorn.output.AttachmentOutputFormater; |
| | 28 | import org.w3c.unicorn.output.FileOutputFormater; |
| 35 | 29 | import org.w3c.unicorn.output.OutputFormater; |
| 36 | 30 | import org.w3c.unicorn.output.SimpleOutputFormater; |
| … |
… |
|
| 49 | 43 | private static final long serialVersionUID = 1L; |
| 50 | 44 | |
| | 45 | private static Log logger = LogFactory.getLog(LanguageAction.class); |
| | 46 | |
| 51 | 47 | private static TreeMap<String, Properties> languageProperties; |
| 52 | 48 | |
| … |
… |
|
| 96 | 92 | else { |
| 97 | 93 | if (Framework.getLanguageProperties().containsKey(langParameter)) { |
| 98 | | messages.add(new Message(Message.INFO, "This translation lacks " + (defaultProperties.size() - languageProperties.get(langParameter).size()) + " properties. Help us to improve it.")); |
| | 94 | if (langParameter.equals(Property.get("DEFAULT_LANGUAGE"))) { |
| | 95 | messages.add(new Message(Message.INFO, "You cannot edit the default language")); |
| | 96 | Templates.write("language.vm", velocityContext, writer); |
| | 97 | writer.close(); return; |
| | 98 | } else { |
| | 99 | int missings = defaultProperties.size() - languageProperties.get(langParameter).size(); |
| | 100 | |
| | 101 | if (missings > 0) |
| | 102 | messages.add(new Message(Message.INFO, "This translation lacks " + (defaultProperties.size() - languageProperties.get(langParameter).size()) + " properties. Help us to improve it.")); |
| | 103 | else |
| | 104 | messages.add(new Message(Message.INFO, "This translation is complete but you can help us to improve it if needed.")); |
| | 105 | } |
| 99 | 106 | velocityContext.put("prop", languageProperties.get(langParameter)); |
| 100 | 107 | } else if (Language.isISOLanguageCode(langParameter)) { |
| … |
… |
|
| 135 | 142 | req.setCharacterEncoding("UTF-8"); |
| 136 | 143 | resp.setContentType("text/html; charset=UTF-8"); |
| 137 | | |
| 138 | | //resp.getWriter().println(new String(req.getParameter("ja_universal_checker").getBytes("UTF-8"), "UTF-8")); |
| 139 | | |
| 140 | | /*File file = new File(Property.get("UPLOADED_FILES_REPOSITORY") + "/test.txt"); |
| 141 | | file.createNewFile(); |
| 142 | | FileOutputStream fileStream = new FileOutputStream(file); |
| 143 | | OutputStreamWriter os = new OutputStreamWriter(fileStream, "UTF-8"); |
| 144 | | os.write(req.getParameter("ja_universal_checker")); |
| 145 | | os.close();*/ |
| 146 | 144 | |
| 147 | 145 | String languageParameter = req.getParameter("translation_language"); |
| … |
… |
|
| 152 | 150 | return; |
| 153 | 151 | } else { |
| 154 | | Properties langProps = languageProperties.get(languageParameter); |
| 155 | | if (langProps == null) { |
| | 152 | Properties langProps; |
| | 153 | if (languageProperties.get(languageParameter) == null) { |
| 156 | 154 | langProps = createProperties(languageParameter); |
| | 155 | contextObjects.put("new_translation", true); |
| 157 | 156 | if (langProps == null) { |
| 158 | 157 | doGet(req, resp); |
| 159 | 158 | return; |
| 160 | 159 | } |
| 161 | | } |
| | 160 | } else |
| | 161 | langProps = (Properties) languageProperties.get(languageParameter).clone(); |
| 162 | 162 | |
| 163 | 163 | StringBuilder changeLog = new StringBuilder(); |
| 164 | | |
| 165 | 164 | for (Object obj : req.getParameterMap().keySet()) { |
| 166 | 165 | String paramKey = (String) obj; |
| … |
… |
|
| 172 | 171 | |
| 173 | 172 | if (!req.getParameter(paramKey).equals("") && !req.getParameter(paramKey).equals(langProps.getProperty(key))) { |
| 174 | | changeLog.append(key + ":\n"); |
| | 173 | changeLog.append("\n" + key + ":\n"); |
| 175 | 174 | changeLog.append("\t + " + req.getParameter(paramKey) + "\n"); |
| 176 | 175 | if (langProps.getProperty(key) != null) |
| … |
… |
|
| 179 | 178 | langProps.put(key, req.getParameter(paramKey)); |
| 180 | 179 | } |
| 181 | | |
| 182 | | |
| 183 | | } |
| 184 | | |
| 185 | | |
| 186 | | |
| 187 | | //contextObjects.put("changeLog", changeLog); |
| 188 | | |
| 189 | | //Writer os = new CharArrayWriter(); |
| 190 | | |
| 191 | | |
| 192 | | //langProps.store(new OutputStreamWriter(new ByteArrayOutputStream(), "UTF-8"), ""); |
| 193 | | |
| 194 | | //contextObjects.put("properties", os.toString()); |
| | 180 | } |
| | 181 | |
| | 182 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | 183 | OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8"); |
| | 184 | langProps.store(osw, "Submitted by " + req.getParameter("translator_name") + " (" + req.getParameter("translator_mail") +")"); |
| | 185 | osw.close(); |
| | 186 | baos.close(); |
| | 187 | contextObjects.put("properties", baos.toString("UTF-8")); |
| | 188 | contextObjects.put("changeLog", changeLog); |
| | 189 | contextObjects.put("translator_name", req.getParameter("translator_name")); |
| | 190 | contextObjects.put("translator_mail", req.getParameter("translator_mail")); |
| | 191 | contextObjects.put("translator_comments", req.getParameter("translator_comments")); |
| | 192 | contextObjects.put("language", Language.getLocale(languageParameter).getDisplayLanguage(Locale.ENGLISH)); |
| 195 | 193 | |
| 196 | 194 | MessageList messages = new MessageList(); |
| … |
… |
|
| 202 | 200 | // From now on the response is committed, careful |
| 203 | 201 | |
| 204 | | String[] recipients = {"thomas.gambet@orange.fr"}; |
| | 202 | String[] recipients = {Property.getProps("mail.properties").getProperty("unicorn.mail.language.to"), |
| | 203 | req.getParameter("translator_mail")}; |
| | 204 | String subject = "Unicorn - Translation in " + contextObjects.get("language") + " (submitted by " + req.getParameter("translator_name") + ")"; |
| 205 | 205 | |
| 206 | 206 | OutputFormater mainOutputFormater = new SimpleOutputFormater("language.mail", Property.get("DEFAULT_LANGUAGE"), "text/plain"); |
| 207 | | OutputFormater attachmentOutputFormater = new AttachmentOutputFormater("language.properties", Property.get("DEFAULT_LANGUAGE"), "text/plain", "test.txt"); |
| | 207 | OutputFormater fileOutputFormater = new FileOutputFormater("language.properties", Property.get("DEFAULT_LANGUAGE"), "text/plain", languageParameter + ".properties"); |
| 208 | 208 | |
| 209 | 209 | List<OutputFormater> outputFormaters = new ArrayList<OutputFormater>(); |
| 210 | 210 | outputFormaters.add(mainOutputFormater); |
| 211 | | //outputFormaters.add(attachmentOutputFormater); |
| | 211 | outputFormaters.add(fileOutputFormater); |
| 212 | 212 | |
| 213 | 213 | Mail mailer = new Mail(); |
| 214 | 214 | try { |
| 215 | | mailer.sendMail(recipients, "test subject", outputFormaters, contextObjects); |
| | 215 | mailer.sendMail(recipients, subject, outputFormaters, contextObjects); |
| 216 | 216 | } catch (UnicornException e) { |
| 217 | | // TODO Auto-generated catch block |
| 218 | | e.printStackTrace(); |
| 219 | | } |
| 220 | | |
| 221 | | //checkParameters(req); |
| | 217 | logger.error(e.getMessage(), e); |
| | 218 | } |
| | 219 | |
| 222 | 220 | } |
| 223 | 221 | |