1 package be
.nikiroo
.gofetch
.support
;
3 import java
.io
.IOException
;
4 import java
.text
.ParseException
;
5 import java
.text
.SimpleDateFormat
;
6 import java
.util
.ArrayList
;
10 import org
.jsoup
.helper
.StringUtil
;
11 import org
.jsoup
.nodes
.Element
;
12 import org
.jsoup
.nodes
.Node
;
13 import org
.jsoup
.nodes
.TextNode
;
14 import org
.jsoup
.select
.Elements
;
15 import org
.jsoup
.select
.NodeTraversor
;
16 import org
.jsoup
.select
.NodeVisitor
;
18 import be
.nikiroo
.gofetch
.data
.Story
;
19 import be
.nikiroo
.utils
.Downloader
;
22 * Base class for website support.
26 public abstract class BasicSupport
{
27 /** The downloader to use for all websites. */
28 protected static Downloader downloader
= new Downloader("gofetcher");
31 * The support type (each website we support has a single type).
36 /** EN: Any, but mostly IT/Sci */
38 /** EN: Clone of Slashdot, mostly abandoned */
53 * Used to process an element into lines.
57 public interface ElementProcessor
{
59 * Detect if this node is a quote and should be trated as such.
63 * @return TRUE if it is
65 public boolean detectQuote(Node node
);
68 * Process text content (will be called on each text element, allowing
69 * you to modify it if needed).
74 * @return the resulting text
76 public String
processText(String text
);
83 * @return TRUE if it has to be ignored
85 public boolean ignoreNode(Node node
);
88 * Manually process this node (and return the manual processing value)
91 * If the node is manually processed, it and its children will not be
92 * automatically processed.
95 * the node to optionally process
97 * @return NULL if not processed (will thus be automatically processed
98 * as usual), a {@link String} (may be empty) if we process it
99 * manually -- the given {@link String} will be used instead of
100 * the usual automatic processing if not NULL
102 public String
manualProcessing(Node node
);
106 * A default {@link ElementProcessor} (will not detect or process anything
111 public class BasicElementProcessor
implements ElementProcessor
{
113 public boolean detectQuote(Node node
) {
118 public String
processText(String text
) {
123 public boolean ignoreNode(Node node
) {
128 public String
manualProcessing(Node node
) {
133 static private String preselector
;
138 * List all the recent items, but only assure the ID and internal URL to
139 * fetch it later on (until it has been fetched, the rest of the
140 * {@link Story} is not confirmed).
142 * @return the list of new stories
144 * @throws IOException
147 abstract public List
<Story
> list() throws IOException
;
150 * Fetch the full article content as well as all the comments associated to
151 * this {@link Story}, if any (can be empty, but not NULL).
154 * the story to fetch the comments of
156 * @throws IOException
157 * in case of I/O error
159 abstract public void fetch(Story story
) throws IOException
;
162 * The website textual description, to add in the dispatcher page.
166 * @return the description
168 abstract public String
getDescription();
171 * The gopher "selector" to use for output.
173 * A kind of "URL path", like "/news/" or "/misc/news/" or...
175 * @return the selector
177 public String
getSelector() {
178 return getSelector(type
);
186 public Type
getType() {
196 protected void setType(Type type
) {
201 * The {@link String} to append to the selector (the selector will be
202 * constructed as "this string" then "/type/".
205 * the preselector to set
207 static public void setPreselector(String preselector
) {
208 BasicSupport
.preselector
= preselector
;
212 * Return a {@link BasicSupport} that is compatible with the given
213 * {@link Type} if it exists (or NULL if not).
218 * @return a compatible {@link BasicSupport} if it exists (or NULL if not)
220 static public BasicSupport
getSupport(Type type
) {
221 BasicSupport support
= null;
226 support
= new Slashdot();
229 support
= new Pipedot();
235 support
= new LeMonde();
238 support
= new TheRegister();
241 support
= new TooLinux();
244 support
= new EreNumerique();
248 if (support
!= null) {
249 support
.setType(type
);
257 * The gopher "selector" to use for output for this type, using the
260 * A kind of "URL path", like "/news/" or "/misc/news/" or...
263 * the type to get the selector of
265 * @return the selector
267 static public String
getSelector(Type type
) {
268 return preselector
+ "/" + type
+ "/";
272 * Get the first {@link Element} of the given class, or an empty span
273 * {@link Element} if none found.
276 * the element to look in
278 * the class to look for
280 * @return the value or an empty span {@link Element}
282 static protected Element
firstOrEmpty(Element element
, String className
) {
283 Elements subElements
= element
.getElementsByClass(className
);
284 if (subElements
.size() > 0) {
285 return subElements
.get(0);
288 return new Element("span");
292 * Get the first {@link Element} of the given tag, or an empty span
293 * {@link Element} if none found.
296 * the element to look in
298 * the tag to look for
300 * @return the value or an empty span {@link Element}
302 static protected Element
firstOrEmptyTag(Element element
, String tagName
) {
303 Elements subElements
= element
.getElementsByTag(tagName
);
304 if (subElements
.size() > 0) {
305 return subElements
.get(0);
308 return new Element("span");
312 * Process the given element into text (each line is a text paragraph and
313 * can be prepended with ">" signs to indicate a quote or sub-quote or
317 * the element to process
318 * @param elementProcessor
319 * the element processor, must not be NULL
321 * @return text lines, each line is a paragraph
323 static protected List
<String
> toLines(Element element
,
324 final ElementProcessor elementProcessor
) {
325 final List
<String
> lines
= new ArrayList
<String
>();
326 final StringBuilder currentLine
= new StringBuilder();
327 final List
<Integer
> quoted
= new ArrayList
<Integer
>();
328 final List
<Node
> ignoredNodes
= new ArrayList
<Node
>();
330 if (element
!= null) {
331 new NodeTraversor(new NodeVisitor() {
333 public void head(Node node
, int depth
) {
334 String manual
= null;
335 boolean ignore
= elementProcessor
.ignoreNode(node
)
336 || ignoredNodes
.contains(node
.parentNode());
338 manual
= elementProcessor
.manualProcessing(node
);
339 if (manual
!= null) {
340 currentLine
.append(manual
);
346 ignoredNodes
.add(node
);
351 for (int i
= 0; i
< quoted
.size(); i
++) {
356 boolean enterQuote
= elementProcessor
.detectQuote(node
);
357 boolean leaveQuote
= quoted
.contains(depth
);
364 quoted
.remove(Integer
.valueOf(depth
));
367 if (enterQuote
|| leaveQuote
) {
368 if (currentLine
.length() > 0) {
369 if (currentLine
.charAt(currentLine
.length() - 1) == '\n') {
370 currentLine
.setLength(currentLine
.length() - 1);
372 for (String l
: currentLine
.toString().split("\n")) {
376 currentLine
.setLength(0);
379 if (node
instanceof Element
) {
380 Element element
= (Element
) node
;
381 boolean block
= element
.isBlock()
382 || element
.tagName().equalsIgnoreCase("br");
383 if (block
&& currentLine
.length() > 0) {
384 currentLine
.append("\n");
386 } else if (node
instanceof TextNode
) {
387 TextNode textNode
= (TextNode
) node
;
388 String line
= StringUtil
.normaliseWhitespace(textNode
391 currentLine
.append(elementProcessor
.processText(line
));
392 currentLine
.append(" ");
397 public void tail(Node node
, int depth
) {
399 }).traverse(element
);
402 if (currentLine
.length() > 0) {
404 for (int i
= 0; i
< quoted
.size(); i
++) {
408 if (currentLine
.length() > 0) {
409 if (currentLine
.charAt(currentLine
.length() - 1) == '\n') {
410 currentLine
.setLength(currentLine
.length() - 1);
412 for (String l
: currentLine
.toString().split("\n")) {
418 for (int i
= 0; i
< lines
.size(); i
++) {
419 lines
.set(i
, lines
.get(i
).replace(" ", " ").trim());
426 * Reformat the date if possible.
431 * @return the reformated date, or the same value if it was not parsable
433 static protected String
date(String date
) {
434 SimpleDateFormat out
= new SimpleDateFormat("yyyy/MM/dd");
438 epoch
= Long
.parseLong(date
.trim());
439 } catch (Exception e
) {
444 return out
.format(new Date(1000 * epoch
));
448 Date dat
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
450 return out
.format(dat
);
451 } catch (ParseException e
) {