- return null;
- }
-
- /**
- * Return the list of supported image extensions.
- *
- * @param emptyAllowed
- * TRUE to allow an empty extension on first place, which can be
- * used when you may already have an extension in your input but
- * are not sure about it
- *
- * @return the extensions
- */
- static String[] getImageExt(boolean emptyAllowed) {
- if (emptyAllowed) {
- return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
- } else {
- return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
- }
- }
-
- /**
- * Check if the given resource can be a local image or a remote image, then
- * refresh the cache with it if it is.
- *
- * @param source
- * the story source
- * @param line
- * the resource to check
- *
- * @return the image if found, or NULL
- *
- */
- static BufferedImage getImage(BasicSupport support, URL source, String line) {
- URL url = getImageUrl(support, source, line);
- if (url != null) {
- InputStream in = null;
- try {
- in = Instance.getCache().open(url, getSupport(url), true);
- return IOUtils.toImage(in);
- } catch (IOException e) {
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- }
- }
- }
- }
-
- return null;
- }
-
- /**
- * Check if the given resource can be a local image or a remote image, then
- * refresh the cache with it if it is.
- *
- * @param source
- * the story source
- * @param line
- * the resource to check
- *
- * @return the image URL if found, or NULL
- *
- */
- static URL getImageUrl(BasicSupport support, URL source, String line) {
- URL url = null;
-
- if (line != null) {
- // try for files
- String path = null;
- if (source != null) {
- path = new File(source.getFile()).getParent();
- try {
- String basePath = new File(new File(path), line.trim())
- .getAbsolutePath();
- for (String ext : getImageExt(true)) {
- if (new File(basePath + ext).exists()) {
- url = new File(basePath + ext).toURI().toURL();
- }
- }
- } catch (Exception e) {
- // Nothing to do here
- }
- }
-
- if (url == null) {
- // try for URLs
- try {
- for (String ext : getImageExt(true)) {
- if (Instance.getCache().check(new URL(line + ext))) {
- url = new URL(line + ext);
- break;
- }
- }
-
- // try out of cache
- if (url == null) {
- for (String ext : getImageExt(true)) {
- try {
- url = new URL(line + ext);
- Instance.getCache().refresh(url, support, true);
- break;
- } catch (IOException e) {
- // no image with this ext
- url = null;
- }
- }
- }
- } catch (MalformedURLException e) {
- // Not an url
- }
- }
-
- // refresh the cached file
- if (url != null) {
- try {
- Instance.getCache().refresh(url, support, true);
- } catch (IOException e) {
- // woops, broken image
- url = null;
- }
- }