Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / BasicSupportHelper.java
CommitLineData
0ffa4754
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.MalformedURLException;
7import java.net.URL;
8
9import be.nikiroo.fanfix.Instance;
10import be.nikiroo.fanfix.bundles.Config;
11import be.nikiroo.utils.Image;
12
13/**
14 * Helper class for {@link BasicSupport}, mostly dedicated to text formating for
15 * the classes that implement {@link BasicSupport}.
16 *
17 * @author niki
18 */
8d59ce07 19public class BasicSupportHelper {
0ffa4754
NR
20 /**
21 * Get the default cover related to this subject (see <tt>.info</tt> files).
22 *
23 * @param subject
24 * the subject
25 *
26 * @return the cover if any, or NULL
27 */
8d59ce07 28 public Image getDefaultCover(String subject) {
d66deb8d 29 if (subject != null && !subject.isEmpty() && Instance.getInstance().getCoverDir() != null) {
0ffa4754 30 try {
d66deb8d 31 File fileCover = new File(Instance.getInstance().getCoverDir(), subject);
0ffa4754
NR
32 return getImage(null, fileCover.toURI().toURL(), subject);
33 } catch (MalformedURLException e) {
34 }
35 }
36
37 return null;
38 }
39
40 /**
41 * Return the list of supported image extensions.
42 *
43 * @param emptyAllowed
44 * TRUE to allow an empty extension on first place, which can be
45 * used when you may already have an extension in your input but
46 * are not sure about it
47 *
48 * @return the extensions
49 */
8d59ce07 50 public String[] getImageExt(boolean emptyAllowed) {
0ffa4754
NR
51 if (emptyAllowed) {
52 return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
53 }
54
55 return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
56 }
57
58 /**
59 * Check if the given resource can be a local image or a remote image, then
60 * refresh the cache with it if it is.
61 *
62 * @param support
8d59ce07 63 * the linked {@link BasicSupport} (can be NULL)
0ffa4754 64 * @param source
4642806a
NR
65 * the source of the story (for image lookup in the same path if
66 * the source is a file, can be NULL)
0ffa4754
NR
67 * @param line
68 * the resource to check
69 *
70 * @return the image if found, or NULL
71 *
72 */
8d59ce07 73 public Image getImage(BasicSupport support, URL source, String line) {
0ffa4754
NR
74 URL url = getImageUrl(support, source, line);
75 if (url != null) {
76 if ("file".equals(url.getProtocol())) {
77 if (new File(url.getPath()).isDirectory()) {
78 return null;
79 }
80 }
81 InputStream in = null;
82 try {
d66deb8d 83 in = Instance.getInstance().getCache().open(url, support, true);
0ffa4754
NR
84 return new Image(in);
85 } catch (IOException e) {
86 } finally {
87 if (in != null) {
88 try {
89 in.close();
90 } catch (IOException e) {
91 }
92 }
93 }
94 }
95
96 return null;
97 }
98
99 /**
100 * Check if the given resource can be a local image or a remote image, then
101 * refresh the cache with it if it is.
102 *
103 * @param support
8d59ce07 104 * the linked {@link BasicSupport} (can be NULL)
0ffa4754 105 * @param source
4642806a
NR
106 * the source of the story (for image lookup in the same path if
107 * the source is a file, can be NULL)
0ffa4754
NR
108 * @param line
109 * the resource to check
110 *
111 * @return the image URL if found, or NULL
112 *
113 */
8d59ce07 114 public URL getImageUrl(BasicSupport support, URL source, String line) {
0ffa4754
NR
115 URL url = null;
116
117 if (line != null) {
118 // try for files
119 if (source != null) {
120 try {
121
122 String relPath = null;
123 String absPath = null;
124 try {
125 String path = new File(source.getFile()).getParent();
126 relPath = new File(new File(path), line.trim())
127 .getAbsolutePath();
128 } catch (Exception e) {
129 // Cannot be converted to path (one possibility to take
130 // into account: absolute path on Windows)
131 }
132 try {
133 absPath = new File(line.trim()).getAbsolutePath();
134 } catch (Exception e) {
135 // Cannot be converted to path (at all)
136 }
137
138 for (String ext : getImageExt(true)) {
139 File absFile = new File(absPath + ext);
140 File relFile = new File(relPath + ext);
141 if (absPath != null && absFile.exists()
142 && absFile.isFile()) {
143 url = absFile.toURI().toURL();
144 } else if (relPath != null && relFile.exists()
145 && relFile.isFile()) {
146 url = relFile.toURI().toURL();
147 }
148 }
149 } catch (Exception e) {
150 // Should not happen since we control the correct arguments
151 }
152 }
153
154 if (url == null) {
155 // try for URLs
156 try {
157 for (String ext : getImageExt(true)) {
d66deb8d 158 if (Instance.getInstance().getCache().check(new URL(line + ext), true)) {
0ffa4754
NR
159 url = new URL(line + ext);
160 break;
161 }
162 }
163
164 // try out of cache
165 if (url == null) {
166 for (String ext : getImageExt(true)) {
167 try {
168 url = new URL(line + ext);
d66deb8d 169 Instance.getInstance().getCache().refresh(url, support, true);
0ffa4754
NR
170 break;
171 } catch (IOException e) {
172 // no image with this ext
173 url = null;
174 }
175 }
176 }
177 } catch (MalformedURLException e) {
178 // Not an url
179 }
180 }
181
182 // refresh the cached file
183 if (url != null) {
184 try {
d66deb8d 185 Instance.getInstance().getCache().refresh(url, support, true);
0ffa4754
NR
186 } catch (IOException e) {
187 // woops, broken image
188 url = null;
189 }
190 }
191 }
192
193 return url;
194 }
195
196 /**
197 * Fix the author name if it is prefixed with some "by" {@link String}.
198 *
199 * @param author
200 * the author with a possible prefix
201 *
202 * @return the author without prefixes
203 */
8d59ce07 204 public String fixAuthor(String author) {
0ffa4754
NR
205 if (author != null) {
206 for (String suffix : new String[] { " ", ":" }) {
d66deb8d 207 for (String byString : Instance.getInstance().getConfig().getList(Config.CONF_BYS)) {
0ffa4754
NR
208 byString += suffix;
209 if (author.toUpperCase().startsWith(byString.toUpperCase())) {
210 author = author.substring(byString.length()).trim();
211 }
212 }
213 }
214
215 // Special case (without suffix):
216 if (author.startsWith("©")) {
217 author = author.substring(1);
218 }
219 }
220
221 return author;
222 }
223}