Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:zanata/zanata
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Munoz committed Feb 21, 2012
2 parents 83f6a59 + 72859f2 commit b63cd0c
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 35 deletions.
@@ -1,9 +1,12 @@
package org.zanata.adapter.properties;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -38,7 +41,17 @@ private static void makeParentDirs(File f)
* @param baseDir
* @throws IOException
*/
public static void write(final Resource doc, final File baseDir, String charset) throws IOException
public static void write(final Resource doc, final File baseDir) throws IOException
{
write(doc, baseDir, false);
}

public static void writeUTF8(final Resource doc, final File baseDir) throws IOException
{
write(doc, baseDir, true);
}

private static void write(final Resource doc, final File baseDir, boolean utf8) throws IOException
{
File baseFile = new File(baseDir, doc.getName() + ".properties");
makeParentDirs(baseFile);
Expand All @@ -53,18 +66,20 @@ public static void write(final Resource doc, final File baseDir, String charset)
props.setComment(textFlow.getId(), simpleComment.getValue());
}
// props.store(System.out, null);
Writer out = new OutputStreamWriter(new FileOutputStream(baseFile), charset);
try
{
props.store(out, null);
}
finally
{
out.close();
}
storeProps(props, baseFile, utf8);
}

public static void write(Resource srcDoc, final TranslationsResource doc, final File baseDir, String bundleName, String locale, String charset) throws IOException
public static void write(Resource srcDoc, final TranslationsResource doc, final File baseDir, String bundleName, String locale) throws IOException
{
write(srcDoc, doc, baseDir, bundleName, locale, false);
}

public static void writeUTF8(Resource srcDoc, final TranslationsResource doc, final File baseDir, String bundleName, String locale) throws IOException
{
write(srcDoc, doc, baseDir, bundleName, locale, true);
}

private static void write(Resource srcDoc, final TranslationsResource doc, final File baseDir, String bundleName, String locale, boolean utf8) throws IOException
{
Properties targetProp = new Properties();

Expand Down Expand Up @@ -95,15 +110,27 @@ public static void write(Resource srcDoc, final TranslationsResource doc, final
File langFile = new File(baseDir, bundleName + "_" + locale + ".properties");
makeParentDirs(langFile);
logVerbose("Creating target file " + langFile);
// targetProp.store(System.out, null);
Writer out2 = new OutputStreamWriter(new FileOutputStream(langFile), charset);
storeProps(targetProp, langFile, utf8);
}

private static void storeProps(Properties props, File file, boolean utf8) throws UnsupportedEncodingException, FileNotFoundException, IOException
{
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try
{
targetProp.store(out2, null);
if (utf8)
{
Writer writer = new OutputStreamWriter(out, "UTF-8");
props.store(writer, null);
}
else
{
props.store(out, null);
}
}
finally
{
out2.close();
out.close();
}
}

Expand Down
@@ -1,6 +1,6 @@
package org.zanata.adapter.properties;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.*;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void roundtripSrcPropsToDocXmlToProps() throws Exception
Unmarshaller unmarshal = jc.createUnmarshaller();
Resource docIn = (Resource) unmarshal.unmarshal(new StringReader(sw.toString()));

PropWriter.write(docIn, TEST_OUTPUT_DIR, ISO_8859_1);
PropWriter.write(docIn, TEST_OUTPUT_DIR);

assertInputAndOutputDocContentSame(docName);
}
Expand All @@ -83,7 +83,7 @@ public void roundtripTransPropsToDocXmlToProps() throws Exception
Unmarshaller unmarshal = jc.createUnmarshaller();
TranslationsResource docIn = (TranslationsResource) unmarshal.unmarshal(new StringReader(sw.toString()));

PropWriter.write(null, docIn, TEST_OUTPUT_DIR, "test", locale, ISO_8859_1);
PropWriter.write(null, docIn, TEST_OUTPUT_DIR, "test", locale);

assertInputAndOutputDocContentSame(docName);
}
Expand Down
Expand Up @@ -35,27 +35,25 @@
*/
public class PropertiesStrategy implements PullStrategy
{
// "8859_1" is used in Properties.java...
private static final String ISO_8859_1 = "ISO-8859-1";

StringSet extensions = new StringSet("comment");
private PullOptions opts;
private final String charset;
private PullOptions pullOptions;

public PropertiesStrategy()
{
this(ISO_8859_1);
}

public PropertiesStrategy(String charset)
/**
* @return the opts
*/
protected PullOptions getPullOptions()
{
this.charset = charset;
return pullOptions;
}

@Override
public void setPullOptions(PullOptions opts)
{
this.opts = opts;
this.pullOptions = opts;
}

@Override
Expand All @@ -73,16 +71,16 @@ public boolean needsDocToWriteTrans()
@Override
public void writeSrcFile(Resource doc) throws IOException
{
PropWriter.write(doc, opts.getSrcDir(), charset);
PropWriter.write(doc, getPullOptions().getSrcDir());
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping localeMapping, TranslationsResource targetDoc) throws IOException
{
if (opts.getCreateSkeletons())
PropWriter.write(doc, targetDoc, opts.getTransDir(), docName, localeMapping.getJavaLocale(), charset);
if (getPullOptions().getCreateSkeletons())
PropWriter.write(doc, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale());
else
PropWriter.write(null, targetDoc, opts.getTransDir(), docName, localeMapping.getJavaLocale(), charset);
PropWriter.write(null, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale());
}

}
Expand Up @@ -32,12 +32,11 @@
public class PullCommand extends PushPullCommand<PullOptions>
{
private static final Logger log = LoggerFactory.getLogger(PullCommand.class);
private static final String UTF_8 = "UTF-8";

private static final Map<String, PullStrategy> strategies = new HashMap<String, PullStrategy>();

{
strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, new PropertiesStrategy(UTF_8));
strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, new UTF8PropertiesStrategy());
strategies.put(PROJECT_TYPE_PROPERTIES, new PropertiesStrategy());
strategies.put(PROJECT_TYPE_PUBLICAN, new GettextDirStrategy());
strategies.put(PROJECT_TYPE_XLIFF, new XliffStrategy());
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright 2011, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.zanata.client.commands.pull;

import java.io.IOException;

import org.zanata.adapter.properties.PropWriter;
import org.zanata.client.config.LocaleMapping;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TranslationsResource;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class UTF8PropertiesStrategy extends PropertiesStrategy
{

public UTF8PropertiesStrategy()
{
}

@Override
public void writeSrcFile(Resource doc) throws IOException
{
PropWriter.writeUTF8(doc, getPullOptions().getSrcDir());
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping localeMapping, TranslationsResource targetDoc) throws IOException
{
if (getPullOptions().getCreateSkeletons())
PropWriter.writeUTF8(doc, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale());
else
PropWriter.writeUTF8(null, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale());
}

}
Expand Up @@ -32,7 +32,7 @@
import org.zanata.rest.dto.resource.Resource;

/**
*
* NB: you must initialise this object with init() after setPushOptions()
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
Expand Down
Expand Up @@ -38,6 +38,11 @@
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TranslationsResource;

/**
* NB: you must initialise this object with init() after setPushOptions()
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
class PropertiesStrategy extends AbstractPushStrategy
{
// "8859_1" is used in Properties.java...
Expand Down
@@ -0,0 +1,83 @@
package org.zanata.client.commands.pull;

import static org.testng.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.fedorahosted.openprops.Properties;
import org.testng.annotations.Test;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;

@Test
public class PullPropertiesStrategyTest
{
IMocksControl control = EasyMock.createControl();

File outDir = new File("target/test-output/writeprops/");
Properties props = new Properties();
PullOptions opts;
Resource doc;

public PullPropertiesStrategyTest()
{
outDir.mkdirs();
opts = control.createMock(PullOptions.class);
EasyMock.expect(opts.getSrcDir()).andReturn(outDir).anyTimes();
control.replay();
doc = new Resource(null);
doc.getTextFlows().add(newTextFlow("key", "value"));
doc.getTextFlows().add(newTextFlow("unicode", "レス"));
}

@Test
public void utf8() throws Exception
{
PullStrategy strat = new UTF8PropertiesStrategy();
strat.setPullOptions(opts);

doc.setName("utf8");
strat.writeSrcFile(doc);

File f = new File(outDir, "utf8.properties");
InputStreamReader r = new InputStreamReader(new FileInputStream(f), "UTF-8");
props.load(r);
checkResults(props);
}


@Test
public void latin1() throws Exception
{
PullStrategy strat = new PropertiesStrategy();
strat.setPullOptions(opts);

doc.setName("latin1");
strat.writeSrcFile(doc);

File f = new File(outDir, "latin1.properties");
InputStream inStream = new FileInputStream(f);
props.load(inStream);
checkResults(props);
}

private TextFlow newTextFlow(String key, String value)
{
TextFlow tf = new TextFlow();
tf.setId(key);
tf.setContent(value);
return tf;
}

private void checkResults(Properties props)
{
assertEquals(props.getProperty("key"), "value");
assertEquals(props.getProperty("unicode"), "レス");
}

}

0 comments on commit b63cd0c

Please sign in to comment.