Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AssetsStrings:loose implement of string resource getter from strings xml in assets directory (Android)
/*
* loose implement of string resource getter from strings xml in assets directory
* usage:
* AssetsStrings as = new AssetsStrings();
* String s = as.getString(mContext, "strings.xml", "message");
*
*/
package <your package here>;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.content.res.AssetManager;
public class AssetsStrings {
protected class ResourceStringHandler extends DefaultHandler {
class FOUNDException extends SAXException {
FOUNDException(String s) {
super(s);
}
}
boolean currentElement = false;
String currentValue = "";
final String name;
String resultValue = "";
public String getResultValue() {
return resultValue;
}
ResourceStringHandler(String name) {
super();
this.name = name;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = currentValue + new String(ch, start, length);
}
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("string")) {
for (int i = 0, l = attributes.getLength(); i < l; ++i) {
if (attributes.getQName(i).equals("name")
&& attributes.getValue(i).equalsIgnoreCase(this.name)) {
currentElement = true;
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (currentElement) {
resultValue = currentValue;
throw new FOUNDException("success");
}
currentElement = false;
}
}
static private InputStream getStringsISByDefaultLanguage(AssetManager am,
final String stringsxml) throws IOException {
String path = "values-" + Locale.getDefault().getLanguage() + "/"
+ stringsxml;
InputStream is = am.open(path);
return is;
}
static private InputStream getStringsIS(AssetManager am,
final String stringsxml) throws IOException {
String path = "values/" + stringsxml;
InputStream is = am.open(path);
return is;
}
public String getString(Context context, final String stringsxml,
final String name) {
AssetManager am = context.getResources().getAssets();
InputStream is = null;
ResourceStringHandler rshandler = new ResourceStringHandler(name);
try {
try {
is = getStringsISByDefaultLanguage(am, stringsxml);
} catch (IOException e) {
try {
is = getStringsIS(am, stringsxml);
} catch (IOException e1) {
e1.printStackTrace();
return name;
}
}
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(rshandler);
InputSource inStream = new InputSource(is);
xr.parse(inStream);
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (ResourceStringHandler.FOUNDException e) {
// success!
return rshandler.getResultValue();
} catch (SAXException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment