MATSIM
MatsimResource.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MatsimResource.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.gbl;
22 
23 import java.awt.Image;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 
32 import javax.imageio.ImageIO;
33 
34 import org.apache.logging.log4j.LogManager;
35 import org.apache.logging.log4j.Logger;
36 
55 public abstract class MatsimResource {
56 
58  private static final String RES_PATH_JARFILE = "/res/";
59 
61  private static final String RES_PATH_LOCAL = "./res/"; //NOPMD // this line should be ignored for PMD analysis
62  private static final String RES_PATH_LOCAL2 = "./src/main/resources/res/"; //NOPMD // this line should be ignored for PMD analysis
63 
64  private static final Logger log = LogManager.getLogger(MatsimResource.class);
65 
70  public final static URL getAsURL(final String filename) {
71  // look for the file locally
72  {
73  File file = new File(RES_PATH_LOCAL + filename);
74  if (file.exists()) {
75  try {
76  return file.toURI().toURL();
77  } catch (MalformedURLException e) {
78  log.warn("Found resource-file, but could not return URL for it.", e); // just continue, maybe we have more luck in the classpath
79  }
80  }
81  }
82  {
83  File file = new File(RES_PATH_LOCAL2 + filename);
84  if (file.exists()) {
85  try {
86  return file.toURI().toURL();
87  } catch (MalformedURLException e) {
88  log.warn("Found resource-file, but could not return URL for it.", e); // just continue, maybe we have more luck in the classpath
89  }
90  }
91  }
92  // maybe we find the file in the classpath, possibly inside a jar-file
93  URL url = MatsimResource.class.getResource(RES_PATH_JARFILE + filename);
94  if (url == null) {
95  log.warn("Resource '" + filename + "' not found!");
96  }
97  return url;
98  }
99 
104  public final static InputStream getAsInputStream(final String filename) {
105  // look for the file locally
106  try {
107  return new FileInputStream(RES_PATH_LOCAL + filename);
108  } catch (FileNotFoundException e) {
109  log.info("Resource '" + filename + "' not found locally. May not be fatal.");
110  // just continue, maybe we have more luck in the classpath
111  }
112  // maybe we find the file in the classpath, possibly inside a jar-file
113  InputStream stream = MatsimResource.class.getResourceAsStream(RES_PATH_JARFILE + filename);
114  if (stream == null) {
115  log.warn("Resource '" + filename + "' not found!");
116  }
117  return stream;
118  }
119 
124  public final static Image getAsImage(final String filename) {
125  final URL url = getAsURL(filename);
126  if (url == null) {
127  return null;
128  }
129  try {
130  return ImageIO.read(url);
131  } catch (IOException e) {
132  log.error("Could not load requested image", e);
133  return null;
134  }
135  }
136 }
static final URL getAsURL(final String filename)
static final InputStream getAsInputStream(final String filename)
static final Image getAsImage(final String filename)