Skip to content

Commit

Permalink
Added support for localization (resource bundles)
Browse files Browse the repository at this point in the history
  • Loading branch information
syvaidya committed Dec 17, 2007
1 parent 0862ffe commit ac442b4
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 37 deletions.
9 changes: 5 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
</target>

<target name="compile" depends="init">
<javac srcdir="${src}"
destdir="${build.classesdir}"
classpath="${build.classpath}"/>
<jar jarfile="./lib/openstego.jar" basedir="${build.classesdir}" manifest="./src/META-INF/MANIFEST.MF"/>
<javac srcdir="${src}" destdir="${build.classesdir}" classpath="${build.classpath}"/>
<jar destfile="./lib/openstego.jar" manifest="./src/META-INF/MANIFEST.MF">
<fileset dir="${build.classesdir}"/>
<fileset dir="${src}" includes="net/sourceforge/openstego/resource/**/*.properties"/>
</jar>
</target>

<target name="clean">
Expand Down
Binary file modified lib/openstego.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion src/net/sourceforge/openstego/DataHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package net.sourceforge.openstego;

import net.sourceforge.openstego.util.LabelUtil;

import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -69,7 +71,7 @@ public DataHeader(InputStream dataInStream, StegoConfig config) throws IOExcepti

if(!(new String(stamp)).equals(new String(DATA_STAMP)))
{
throw new IOException("Wrong Header: Image does not contain embedded data");
throw new IOException(LabelUtil.getString("err.invalidHeader"));
}

dataLength = (byteToInt(header[stampLen]) + (byteToInt(header[stampLen + 1]) << 8)
Expand Down
27 changes: 5 additions & 22 deletions src/net/sourceforge/openstego/OpenStego.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package net.sourceforge.openstego;

import net.sourceforge.openstego.util.LabelUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -178,7 +180,7 @@ private byte[] getFileBytes(File file) throws IOException

if(offset < data.length)
{
throw new IOException("Error loading file: " + file.getName());
throw new IOException(LabelUtil.getString("err.fileLoadError", new Object[] { file.getName() }));
}

is.close();
Expand Down Expand Up @@ -302,26 +304,7 @@ else if(option.equals("-extract"))
*/
private static void displayUsage()
{
System.err.println(VERSION_STRING + ". Usage:");
System.err.println(" java -jar <path>" + File.separator + "openstego.jar -embed [options] <data_file> <image_file>");
System.err.println(" OR java -jar <path>" + File.separator + "openstego.jar -extract <image_file>");
System.err.println();
System.err.println(" For '-embed' option, openstego will embed the data into the given image file");
System.err.println(" and save the file as PNG after appending '_out' to the file name.");
System.err.println();
System.err.println(" [options] can be specified for '-embed' and should be of the format ");
System.err.println(" '--name=value' pairs. Supported options are:");
System.err.println();
System.err.println(" maxBitsUsedPerChannel - Max number of bits to use per color channel in");
System.err.println(" the image for embedding data. This value can be");
System.err.println(" increased at the expense of image quality, in");
System.err.println(" case size of image is not able to accommodate");
System.err.println(" the data (Default = " + (new StegoConfig()).getMaxBitsUsedPerChannel() + ")");
System.err.println();
System.err.println(" useCompression - Flag to indicate whether compression should be");
System.err.println(" used on the data or not (Default = " + (new StegoConfig()).isUseCompression() + ")");
System.err.println();
System.err.println(" For '-extract' option, openstego will output the extracted data on the");
System.err.println(" standard OUT stream, so make sure that output is redirected to required file.");
System.err.print(LabelUtil.getString("versionString"));
System.err.println(LabelUtil.getString("cmd.usage"));
}
}
12 changes: 7 additions & 5 deletions src/net/sourceforge/openstego/StegoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package net.sourceforge.openstego;

import net.sourceforge.openstego.util.LabelUtil;

import java.util.Iterator;
import java.util.Map;

Expand Down Expand Up @@ -44,7 +46,7 @@ public class StegoConfig
/**
* Flag to indicate whether compression should be used or not
*/
private boolean useCompression = false;
private boolean useCompression = true;


/**
Expand Down Expand Up @@ -78,12 +80,12 @@ public StegoConfig(Map propMap)
}
catch(NumberFormatException ex)
{
throw new IllegalArgumentException("Invalid value for configuration item 'maxBitsUsedPerChannel': " + value);
throw new IllegalArgumentException(LabelUtil.getString("err.config.maxBitsUsedPerChannel.notNumber", new Object[] { value }));
}

if(maxBitsUsedPerChannel < 1 || maxBitsUsedPerChannel > 8)
{
throw new IllegalArgumentException("Configuration item 'maxBitsUsedPerChannel' must be between 1 and 8. Value: " + value);
throw new IllegalArgumentException(LabelUtil.getString("err.config.maxBitsUsedPerChannel.notInRange", new Object[] { value }));
}
}
else if(key.equals(USE_COMPRESSION))
Expand All @@ -99,12 +101,12 @@ else if(value.equalsIgnoreCase("false") || value.equalsIgnoreCase("n"))
}
else
{
throw new IllegalArgumentException("Invalid value for configuration item 'useCompression': " + value);
throw new IllegalArgumentException(LabelUtil.getString("err.config.useCompression.invalid", new Object[] { value }));
}
}
else
{
throw new IllegalArgumentException("Invalid configuration item provided: " + key);
throw new IllegalArgumentException(LabelUtil.getString("err.config.invalidKey", new Object[] { key }));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/net/sourceforge/openstego/StegoInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package net.sourceforge.openstego;

import net.sourceforge.openstego.util.LabelUtil;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -75,7 +77,7 @@ public StegoInputStream(BufferedImage image, StegoConfig config) throws IOExcept

if(image.getColorModel() instanceof java.awt.image.IndexColorModel)
{
throw new IllegalArgumentException("Images with indexed color model (e.g. GIF) not supported");
throw new IllegalArgumentException(LabelUtil.getString("err.image.indexed"));
}

this.image = image;
Expand Down
10 changes: 6 additions & 4 deletions src/net/sourceforge/openstego/StegoOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package net.sourceforge.openstego;

import net.sourceforge.openstego.util.LabelUtil;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -76,12 +78,12 @@ public StegoOutputStream(BufferedImage image, int dataLength, StegoConfig config
{
if(image == null)
{
throw new IllegalArgumentException("Null value provided for image");
throw new IllegalArgumentException(LabelUtil.getString("err.image.arg.nullValue"));
}

if(image.getColorModel() instanceof java.awt.image.IndexColorModel)
{
throw new IllegalArgumentException("Images with indexed color model (e.g. GIF) not supported");
throw new IllegalArgumentException(LabelUtil.getString("err.image.indexed"));
}

this.image = image;
Expand Down Expand Up @@ -112,7 +114,7 @@ private void writeHeader() throws IOException
channelBits++;
if(channelBits > config.getMaxBitsUsedPerChannel())
{
throw new IOException("Image size not enough to embed the data");
throw new IOException(LabelUtil.getString("err.image.insufficientSize"));
}
}
else
Expand Down Expand Up @@ -225,7 +227,7 @@ private void writeCurrentBitSet() throws IOException

if(y == imgHeight)
{
throw new IOException("Image size not enough to embed the data");
throw new IOException(LabelUtil.getString("err.image.insufficientSize"));
}

maskPerByte = (int) (Math.pow(2, channelBitsUsed) - 1);
Expand Down
38 changes: 38 additions & 0 deletions src/net/sourceforge/openstego/resource/LabelBundle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
######## Resource bundle for labels to be displayed in OpenStego ########

# Master labels
versionString = OpenStego v0.1.2

# Labels for exceptions
err.fileLoadError = Error loading file\: {0}
err.invalidHeader = Wrong Header\: Image does not contain embedded data
err.config.maxBitsUsedPerChannel.notNumber = Invalid value for configuration item 'maxBitsUsedPerChannel'\: {0}
err.config.maxBitsUsedPerChannel.notInRange = Configuration item 'maxBitsUsedPerChannel' must be between 1 and 8. Value given\: {0}
err.config.useCompression.invalid = Invalid value for configuration item 'useCompression'\: {0}
err.config.invalidKey = Invalid configuration item provided\: {0}
err.image.indexed = Images with indexed color model (e.g. GIF) not supported
err.image.arg.nullValue = Null value provided for image
err.image.insufficientSize = Image size not enough to embed the data

# Labels for command line interface
cmd.usage = . Usage\: \n\
\ java -jar <path>/openstego.jar -embed [options] <data_file> <image_file>\n\
\ OR java -jar <path>/openstego.jar -extract <image_file>\n\
\n\
\ For '-embed' option, openstego will embed the data into the given image file\n\
\ and save the file as PNG after appending '_out' to the file name.\n\
\n\
\ [options] can be specified for '-embed' and should be of the format\n\
\ '--name\=value' pairs. Supported options are\:\n\
\n\
\ maxBitsUsedPerChannel - Max number of bits to use per color channel in\n\
\ the image for embedding data. This value can be\n\
\ increased at the expense of image quality, in\n\
\ case size of image is not able to accommodate\n\
\ the data (Default \= 3)\n\
\n\
\ useCompression - Flag to indicate whether compression should be\n\
\ used on the data or not (Default \= true)\n\
\n\
\ For '-extract' option, openstego will output the extracted data on the\n\
\ standard OUT stream, so make sure that output is redirected to required file.
48 changes: 48 additions & 0 deletions src/net/sourceforge/openstego/util/LabelUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Utility to embed data into images
* Author: Samir Vaidya (mailto:[email protected])
* Copyright (c) 2007 Samir Vaidya
*/

package net.sourceforge.openstego.util;

import java.util.Locale;
import java.util.ResourceBundle;
import java.text.MessageFormat;

/**
* Localized label handler for OpenStego
*/
public class LabelUtil
{
/**
* Static variable to hold the labels loaded from resource file
*/
private static ResourceBundle labels = null;

static
{
labels = ResourceBundle.getBundle("net.sourceforge.openstego.resource.LabelBundle", Locale.getDefault());
}

/**
* Method to get label value for the given label key
* @param key Key for the label
* @return Display value for the label
*/
public static String getString(String key)
{
return labels.getString(key);
}

/**
* Method to get label value for the given label key (using optional parameters)
* @param key Key for the label
* @param parameters Parameters to pass for a parameterized label
* @return Display value for the label
*/
public static String getString(String key, Object[] parameters)
{
return MessageFormat.format(labels.getString(key), parameters);
}
}

0 comments on commit ac442b4

Please sign in to comment.