Render themes: support location prefixes in resources, closes #66
This commit is contained in:
parent
73bc26dd2d
commit
9b4cf470f9
@ -34,6 +34,9 @@ import java.io.InputStream;
|
|||||||
public abstract class CanvasAdapter {
|
public abstract class CanvasAdapter {
|
||||||
private static final Logger log = LoggerFactory.getLogger(CanvasAdapter.class);
|
private static final Logger log = LoggerFactory.getLogger(CanvasAdapter.class);
|
||||||
|
|
||||||
|
private static final String PREFIX_ASSETS = "assets:";
|
||||||
|
private static final String PREFIX_FILE = "file:";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The instance provided by backend
|
* The instance provided by backend
|
||||||
*/
|
*/
|
||||||
@ -116,19 +119,22 @@ public abstract class CanvasAdapter {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String pathName = (relativePathPrefix == null || relativePathPrefix.length() == 0 ? "" : relativePathPrefix) + File.separatorChar + src;
|
InputStream inputStream;
|
||||||
|
if (src.startsWith(PREFIX_ASSETS)) {
|
||||||
InputStream inputStream = null;
|
src = src.substring(PREFIX_ASSETS.length());
|
||||||
|
inputStream = inputStreamFromAssets(relativePathPrefix, src);
|
||||||
File file = new File(pathName);
|
} else if (src.startsWith(PREFIX_FILE)) {
|
||||||
if (file.exists() && file.isFile() && file.canRead())
|
src = src.substring(PREFIX_FILE.length());
|
||||||
inputStream = new FileInputStream(file);
|
inputStream = inputStreamFromFile(relativePathPrefix, src);
|
||||||
|
} else {
|
||||||
|
inputStream = inputStreamFromFile(relativePathPrefix, src);
|
||||||
|
|
||||||
if (inputStream == null)
|
if (inputStream == null)
|
||||||
inputStream = AssetAdapter.g.openFileAsStream(pathName);
|
inputStream = inputStreamFromAssets(relativePathPrefix, src);
|
||||||
|
}
|
||||||
|
|
||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
log.error("invalid resource: " + pathName);
|
log.error("invalid resource: " + src);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,6 +143,36 @@ public abstract class CanvasAdapter {
|
|||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static InputStream inputStreamFromAssets(String relativePathPrefix, String src) throws IOException {
|
||||||
|
String pathName = (relativePathPrefix == null || relativePathPrefix.length() == 0 ? "" : relativePathPrefix + File.separatorChar) + src;
|
||||||
|
return AssetAdapter.g.openFileAsStream(pathName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static InputStream inputStreamFromFile(String relativePathPrefix, String src) throws IOException {
|
||||||
|
File file = getFile(relativePathPrefix, src);
|
||||||
|
if (!file.exists()) {
|
||||||
|
if (src.length() > 0 && src.charAt(0) == File.separatorChar) {
|
||||||
|
file = getFile(relativePathPrefix, src.substring(1));
|
||||||
|
}
|
||||||
|
if (!file.exists()) {
|
||||||
|
file = null;
|
||||||
|
}
|
||||||
|
} else if (!file.isFile() || !file.canRead()) {
|
||||||
|
file = null;
|
||||||
|
}
|
||||||
|
if (file != null) {
|
||||||
|
return new FileInputStream(file);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static File getFile(String parentPath, String pathName) {
|
||||||
|
if (pathName.charAt(0) == File.separatorChar) {
|
||||||
|
return new File(pathName);
|
||||||
|
}
|
||||||
|
return new File(parentPath, pathName);
|
||||||
|
}
|
||||||
|
|
||||||
protected static void init(CanvasAdapter adapter) {
|
protected static void init(CanvasAdapter adapter) {
|
||||||
g = adapter;
|
g = adapter;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user