1 package be
.nikiroo
.fanfix
.reader
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
7 import java
.text
.ParseException
;
8 import java
.text
.SimpleDateFormat
;
11 import java
.util
.TreeMap
;
13 import be
.nikiroo
.fanfix
.Instance
;
14 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
15 import be
.nikiroo
.fanfix
.data
.MetaData
;
16 import be
.nikiroo
.fanfix
.data
.Story
;
17 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
18 import be
.nikiroo
.utils
.StringUtils
;
21 * The class that handles the different {@link Story} readers you can use.
25 public abstract class BasicReader
{
27 * Return an {@link URL} from this {@link String}, be it a file path or an
33 * @return the corresponding {@link URL}
35 * @throws MalformedURLException
36 * if this is neither a file nor a conventional {@link URL}
38 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
39 if (sourceString
== null || sourceString
.isEmpty()) {
40 throw new MalformedURLException("Empty url");
45 source
= new URL(sourceString
);
46 } catch (MalformedURLException e
) {
47 File sourceFile
= new File(sourceString
);
48 source
= sourceFile
.toURI().toURL();
55 * Describe a {@link Story} from its {@link MetaData} and return a list of
56 * title/value that represent this {@link Story}.
59 * the {@link MetaData} to represent
61 * @return the information
63 public static Map
<String
, String
> getMetaDesc(MetaData meta
) {
64 Map
<String
, String
> metaDesc
= new TreeMap
<String
, String
>();
68 StringBuilder tags
= new StringBuilder();
69 for (String tag
: meta
.getTags()) {
70 if (tags
.length() > 0) {
77 metaDesc
.put("Author", meta
.getAuthor());
78 metaDesc
.put("Publication date", formatDate(meta
.getDate()));
79 metaDesc
.put("Published on", meta
.getPublisher());
80 metaDesc
.put("URL", meta
.getUrl());
82 if (meta
.getWords() > 0) {
83 count
= StringUtils
.formatNumber(meta
.getWords());
85 if (meta
.isImageDocument()) {
86 metaDesc
.put("Number of images", count
);
88 metaDesc
.put("Number of words", count
);
90 metaDesc
.put("Source", meta
.getSource());
91 metaDesc
.put("Subject", meta
.getSubject());
92 metaDesc
.put("Language", meta
.getLang());
93 metaDesc
.put("Tags", tags
.toString());
99 * Open the {@link Story} with an external reader (the program will be
100 * passed the main file associated with this {@link Story}).
103 * the {@link BasicLibrary} to select the {@link Story} from
105 * the {@link Story} LUID
107 * execute the process synchronously (wait until it is terminated
110 * @throws IOException
111 * in case of I/O error
113 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
115 MetaData meta
= lib
.getInfo(luid
);
116 File target
= lib
.getFile(luid
, null);
118 openExternal(meta
, target
, sync
);
122 * Open the {@link Story} with an external reader (the program will be
123 * passed the given target file).
126 * the {@link Story} to load
128 * the target {@link File}
130 * execute the process synchronously (wait until it is terminated
133 * @throws IOException
134 * in case of I/O error
136 protected void openExternal(MetaData meta
, File target
, boolean sync
)
138 String program
= null;
139 if (meta
.isImageDocument()) {
140 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.IMAGES_DOCUMENT_READER
);
142 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.NON_IMAGES_DOCUMENT_READER
);
145 if (program
!= null && program
.trim().isEmpty()) {
149 start(target
, program
, sync
);
153 * Start a file and open it with the given program if given or the first
154 * default system starter we can find.
159 * the program to use or NULL for the default system starter
161 * execute the process synchronously (wait until it is terminated
164 * @throws IOException
165 * in case of I/O error
167 protected void start(File target
, String program
, boolean sync
)
171 if (program
== null) {
173 for (String starter
: new String
[] { "xdg-open", "open", "see",
176 Instance
.getInstance().getTraceHandler().trace("starting external program");
177 proc
= Runtime
.getRuntime().exec(new String
[] { starter
, target
.getAbsolutePath() });
180 } catch (IOException e
) {
184 throw new IOException("Cannot find a program to start the file");
187 Instance
.getInstance().getTraceHandler().trace("starting external program");
188 proc
= Runtime
.getRuntime().exec(
189 new String
[] { program
, target
.getAbsolutePath() });
192 if (proc
!= null && sync
) {
195 } catch (InterruptedException e
) {
200 static private String
formatDate(String date
) {
203 if (date
!= null && !date
.isEmpty()) {
205 ms
= StringUtils
.toTime(date
);
206 } catch (ParseException e
) {
210 SimpleDateFormat sdf
= new SimpleDateFormat(
211 "yyyy-MM-dd'T'HH:mm:ssSSS");
213 ms
= sdf
.parse(date
).getTime();
214 } catch (ParseException e
) {
219 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
220 return sdf
.format(new Date(ms
));