More jDoc, a new BasicElementProcessor
authorNiki Roo <niki@nikiroo.be>
Wed, 6 Sep 2017 12:51:08 +0000 (14:51 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 6 Sep 2017 12:51:08 +0000 (14:51 +0200)
src/be/nikiroo/gofetch/support/BasicSupport.java
src/be/nikiroo/gofetch/support/LWN.java
src/be/nikiroo/gofetch/support/LeMonde.java
src/be/nikiroo/gofetch/support/Pipedot.java
src/be/nikiroo/gofetch/support/Slashdot.java

index 102023eb051a02b6c13a41e5ddc0d64e98743156..f3348e366f1ceeece3c6bec39ea01f031ffe8c95 100644 (file)
@@ -23,25 +23,86 @@ public abstract class BasicSupport {
                SLASHDOT, PIPEDOT, LWN, LEMONDE,
        }
 
-       public interface QuoteProcessor {
+       /**
+        * Used to process an element into lines.
+        * 
+        * @author niki
+        */
+       public interface ElementProcessor {
+               /**
+                * Detect if this node is a quote and should be trated as such.
+                * 
+                * @param node
+                *            the node to check
+                * @return TRUE if it is
+                */
                public boolean detectQuote(Node node);
 
+               /**
+                * Process text content (will be called on each text element, allowing
+                * you to modify it if needed).
+                * 
+                * @param text
+                *            the text to process
+                * @return
+                */
                public String processText(String text);
 
+               /**
+                * Ignore this node.
+                * 
+                * @param node
+                *            the node to ignore
+                * @return TRUE if it has to be ignored
+                */
                public boolean ignoreNode(Node node);
 
                /**
-                * Manually process this node if so desired.
+                * Manually process this node (and return the manual processing value)
+                * if so desired.
+                * <p>
+                * If the node is manually processed, it and its children will not be
+                * automatically processed.
                 * 
                 * @param node
                 *            the node to optionally process
                 * 
-                * @return NULL if not processed, a {@link String} (may be empty) if we
-                *         must not process it any further
+                * @return NULL if not processed (will thus be automatically processed
+                *         as usual), a {@link String} (may be empty) if we process it
+                *         manually -- the given {@link String} will be used instead of
+                *         the usual automatic processing if not NULL
                 */
                public String manualProcessing(Node node);
        }
 
+       /**
+        * A default {@link ElementProcessor} (will not detect or process anything
+        * manually).
+        * 
+        * @author niki
+        */
+       public class BasicElementProcessor implements ElementProcessor {
+               @Override
+               public boolean detectQuote(Node node) {
+                       return false;
+               }
+
+               @Override
+               public String processText(String text) {
+                       return text;
+               }
+
+               @Override
+               public boolean ignoreNode(Node node) {
+                       return false;
+               }
+
+               @Override
+               public String manualProcessing(Node node) {
+                       return null;
+               }
+       }
+
        static private String preselector;
 
        private Type type;
@@ -92,6 +153,15 @@ public abstract class BasicSupport {
                BasicSupport.preselector = preselector;
        }
 
+       /**
+        * Return a {@link BasicSupport} that is compatible with the given
+        * {@link Type} if it exists (or NULL if not).
+        * 
+        * @param type
+        *            the type
+        * 
+        * @return a compatible {@link BasicSupport} if it exists (or NULL if not)
+        */
        static public BasicSupport getSupport(Type type) {
                BasicSupport support = null;
 
@@ -175,8 +245,20 @@ public abstract class BasicSupport {
                return new Element("span");
        }
 
+       /**
+        * Process the given element into text (each line is a text paragraph and
+        * can be prepended with ">" signs to indicate a quote or sub-quote or
+        * sub-sub-quote...).
+        * 
+        * @param element
+        *            the element to process
+        * @param elementProcessor
+        *            the element processor, must not be NULL
+        * 
+        * @return text lines, each line is a paragraph
+        */
        static protected List<String> toLines(Element element,
-                       final QuoteProcessor quoteProcessor) {
+                       final ElementProcessor elementProcessor) {
                final List<String> lines = new ArrayList<String>();
                final StringBuilder currentLine = new StringBuilder();
                final List<Integer> quoted = new ArrayList<Integer>();
@@ -187,10 +269,10 @@ public abstract class BasicSupport {
                                @Override
                                public void head(Node node, int depth) {
                                        String manual = null;
-                                       boolean ignore = quoteProcessor.ignoreNode(node)
+                                       boolean ignore = elementProcessor.ignoreNode(node)
                                                        || ignoredNodes.contains(node.parentNode());
                                        if (!ignore) {
-                                               manual = quoteProcessor.manualProcessing(node);
+                                               manual = elementProcessor.manualProcessing(node);
                                                if (manual != null) {
                                                        currentLine.append(manual);
                                                        ignore = true;
@@ -208,7 +290,7 @@ public abstract class BasicSupport {
                                        }
                                        prep += " ";
 
-                                       boolean enterQuote = quoteProcessor.detectQuote(node);
+                                       boolean enterQuote = elementProcessor.detectQuote(node);
                                        boolean leaveQuote = quoted.contains(depth);
 
                                        if (enterQuote) {
@@ -243,7 +325,7 @@ public abstract class BasicSupport {
                                                String line = StringUtil.normaliseWhitespace(textNode
                                                                .getWholeText());
 
-                                               currentLine.append(quoteProcessor.processText(line));
+                                               currentLine.append(elementProcessor.processText(line));
                                                currentLine.append(" ");
                                        }
                                }
index c492d10d21cde68e48f1390db8efcf7b0a10c470..c48ed2a2130b63b5bc81ed3389612e4efdb72ec0 100644 (file)
@@ -167,7 +167,7 @@ public class LWN extends BasicSupport {
        }
 
        private List<String> toLines(Element element) {
-               return toLines(element, new QuoteProcessor() {
+               return toLines(element, new BasicElementProcessor() {
                        @Override
                        public String processText(String text) {
                                while (text.startsWith(">")) { // comments
@@ -201,11 +201,6 @@ public class LWN extends BasicSupport {
 
                                return false;
                        }
-
-                       @Override
-                       public String manualProcessing(Node node) {
-                               return null;
-                       }
                });
        }
 }
index 454b7e36a82d35a8674c86f186230ebe4f88ae1f..c83dc14b8d0198c89ac46bf5ac7980c86eaf2da6 100644 (file)
@@ -78,12 +78,7 @@ public class LeMonde extends BasicSupport {
                Document doc = DataUtil.load(in, "UTF-8", url.toString());
                Element article = doc.getElementById("articleBody");
                if (article != null) {
-                       for (String line : toLines(article, new QuoteProcessor() {
-                               @Override
-                               public String processText(String text) {
-                                       return text;
-                               }
-
+                       for (String line : toLines(article, new BasicElementProcessor() {
                                @Override
                                public boolean ignoreNode(Node node) {
                                        if (node instanceof Element) {
@@ -96,11 +91,6 @@ public class LeMonde extends BasicSupport {
                                        return false;
                                }
 
-                               @Override
-                               public boolean detectQuote(Node node) {
-                                       return false;
-                               }
-
                                @Override
                                public String manualProcessing(Node node) {
                                        if (node instanceof Element) {
index 89932f7636c170abc3b34b7c71542880b255984c..8db4749d71855287d644936ab716c1ae2b19159e 100644 (file)
@@ -142,12 +142,7 @@ public class Pipedot extends BasicSupport {
        }
 
        private List<String> toLines(Element element) {
-               return toLines(element, new QuoteProcessor() {
-                       @Override
-                       public String processText(String text) {
-                               return text;
-                       }
-
+               return toLines(element, new BasicElementProcessor() {
                        @Override
                        public boolean detectQuote(Node node) {
                                if (node instanceof Element) {
@@ -160,16 +155,6 @@ public class Pipedot extends BasicSupport {
 
                                return false;
                        }
-
-                       @Override
-                       public boolean ignoreNode(Node node) {
-                               return false;
-                       }
-
-                       @Override
-                       public String manualProcessing(Node node) {
-                               return null;
-                       }
                });
        }
 }
index 378b3a4bfdb0cf0c7277397075ccf13d4c33aff5..1581d23cb2361f8fa55c912ff1e2516c8fc91d9d 100644 (file)
@@ -138,7 +138,7 @@ public class Slashdot extends BasicSupport {
        }
 
        private List<String> toLines(Element element) {
-               return toLines(element, new QuoteProcessor() {
+               return toLines(element, new BasicElementProcessor() {
                        @Override
                        public String processText(String text) {
                                while (text.startsWith(">")) { // comment in one-liners
@@ -164,16 +164,6 @@ public class Slashdot extends BasicSupport {
 
                                return false;
                        }
-
-                       @Override
-                       public boolean ignoreNode(Node node) {
-                               return false;
-                       }
-
-                       @Override
-                       public String manualProcessing(Node node) {
-                               return null;
-                       }
                });
        }
 }