Change BasicSupport to use jsoup
[fanfix.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 */
19class BasicSupportHelper {
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 */
28 public static Image getDefaultCover(String subject) {
29 if (subject != null && !subject.isEmpty()
30 && Instance.getCoverDir() != null) {
31 try {
32 File fileCover = new File(Instance.getCoverDir(), subject);
33 return getImage(null, fileCover.toURI().toURL(), subject);
34 } catch (MalformedURLException e) {
35 }
36 }
37
38 return null;
39 }
40
41 /**
42 * Return the list of supported image extensions.
43 *
44 * @param emptyAllowed
45 * TRUE to allow an empty extension on first place, which can be
46 * used when you may already have an extension in your input but
47 * are not sure about it
48 *
49 * @return the extensions
50 */
51 public static String[] getImageExt(boolean emptyAllowed) {
52 if (emptyAllowed) {
53 return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
54 }
55
56 return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
57 }
58
59 /**
60 * Check if the given resource can be a local image or a remote image, then
61 * refresh the cache with it if it is.
62 *
63 * @param support
64 * the linked {@link BasicSupport}
65 * @param source
66 * the story source
67 * @param line
68 * the resource to check
69 *
70 * @return the image if found, or NULL
71 *
72 */
73 public static Image getImage(BasicSupport support, URL source, String line) {
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 {
83 in = Instance.getCache().open(url,
84 BasicSupport.getSupport(url), true);
85 return new Image(in);
86 } catch (IOException e) {
87 } finally {
88 if (in != null) {
89 try {
90 in.close();
91 } catch (IOException e) {
92 }
93 }
94 }
95 }
96
97 return null;
98 }
99
100 /**
101 * Check if the given resource can be a local image or a remote image, then
102 * refresh the cache with it if it is.
103 *
104 * @param support
105 * the linked {@link BasicSupport}
106 * @param source
107 * the story source
108 * @param line
109 * the resource to check
110 *
111 * @return the image URL if found, or NULL
112 *
113 */
114 public static URL getImageUrl(BasicSupport support, URL source, String line) {
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)) {
158 if (Instance.getCache()
159 .check(new URL(line + ext), true)) {
160 url = new URL(line + ext);
161 break;
162 }
163 }
164
165 // try out of cache
166 if (url == null) {
167 for (String ext : getImageExt(true)) {
168 try {
169 url = new URL(line + ext);
170 Instance.getCache().refresh(url, support, true);
171 break;
172 } catch (IOException e) {
173 // no image with this ext
174 url = null;
175 }
176 }
177 }
178 } catch (MalformedURLException e) {
179 // Not an url
180 }
181 }
182
183 // refresh the cached file
184 if (url != null) {
185 try {
186 Instance.getCache().refresh(url, support, true);
187 } catch (IOException e) {
188 // woops, broken image
189 url = null;
190 }
191 }
192 }
193
194 return url;
195 }
196
197 /**
198 * Fix the author name if it is prefixed with some "by" {@link String}.
199 *
200 * @param author
201 * the author with a possible prefix
202 *
203 * @return the author without prefixes
204 */
205 public static String fixAuthor(String author) {
206 if (author != null) {
207 for (String suffix : new String[] { " ", ":" }) {
208 for (String byString : Instance.getConfig()
209 .getString(Config.BYS).split(",")) {
210 byString += suffix;
211 if (author.toUpperCase().startsWith(byString.toUpperCase())) {
212 author = author.substring(byString.length()).trim();
213 }
214 }
215 }
216
217 // Special case (without suffix):
218 if (author.startsWith("©")) {
219 author = author.substring(1);
220 }
221 }
222
223 return author;
224 }
225}