Coverage Report - org.webslinger.ext.wiki.Optimizer
 
Classes in this File Line Coverage Branch Coverage Complexity
Optimizer
80%
149/186
78%
42/54
0
 
 1  
 package org.webslinger.ext.wiki;
 2  
 
 3  
 import java.util.LinkedList;
 4  
 import java.util.Iterator;
 5  
 import java.util.ListIterator;
 6  
 
 7  
 import org.webslinger.ext.wiki.parser.*;
 8  
 
 9  3
 public class Optimizer extends AbstractWikiVisitor {
 10  3
     protected boolean removeInterblockSpacing = true;
 11  3
     protected boolean mergeText = true;
 12  3
     protected boolean convertNewlinesToText = true;
 13  3
     protected boolean makeParagraphs = true;
 14  3
     protected boolean supportPlugins = false;
 15  
 
 16  
     public boolean getRemoveInterblockSpacing() {
 17  0
         return removeInterblockSpacing;
 18  
     }
 19  
 
 20  
     public void setRemoveInterblockSpacing(boolean removeInterblockSpacing) {
 21  0
         this.removeInterblockSpacing = removeInterblockSpacing;
 22  0
     }
 23  
 
 24  
     public boolean getMergeText() {
 25  0
         return mergeText;
 26  
     }
 27  
 
 28  
     public void setMergeText(boolean mergeText) {
 29  0
         this.mergeText = mergeText;
 30  0
     }
 31  
 
 32  
     public boolean getConvertNewlinesToText() {
 33  0
         return convertNewlinesToText;
 34  
     }
 35  
 
 36  
     public void setConvertNewlinesToText(boolean convertNewlinesToText) {
 37  3
         this.convertNewlinesToText = convertNewlinesToText;
 38  3
     }
 39  
 
 40  
     public boolean getMakeParagraphs() {
 41  0
         return makeParagraphs;
 42  
     }
 43  
 
 44  
     public void setMakeParagraphs(boolean makeParagraphs) {
 45  0
         this.makeParagraphs = makeParagraphs;
 46  0
     }
 47  
 
 48  
     public boolean getSupportPlugins() {
 49  0
         return supportPlugins;
 50  
     }
 51  
 
 52  
     public void setSupportPlugins(boolean supportPlugins) {
 53  0
         this.supportPlugins = supportPlugins;
 54  0
     }
 55  
 
 56  
     protected Node appendChild(Node parent, Node child) {
 57  28
         parent.jjtAddChild(child, parent.jjtGetNumChildren());
 58  28
         child.jjtSetParent(parent);
 59  28
         return child;
 60  
     }
 61  
 
 62  
     protected Object visitChildren(SimpleNode node, Object data) {
 63  177
         Object result = super.visitChildren(node, data);
 64  177
         if (node instanceof InlineContainer) optimizeInlineContainer((InlineContainer) node);
 65  177
         if (node instanceof WikiString) mergeText(node);
 66  177
         return result;
 67  
     }
 68  
 
 69  
     protected void optimizeInlineContainer(InlineContainer node) {
 70  33
         convertNewlinesToText(node);
 71  33
         fixXml(node);
 72  33
         mergeText(node);
 73  33
         makeTableRows(node);
 74  33
         makeParagraphs(node);
 75  33
         removeInterblockSpacing(node);
 76  33
     }
 77  
 
 78  
     protected void visitChild(Node parent, Node child, ListIterator<Node> it, Object data) {
 79  174
         super.visitChild(parent, child, it, data);
 80  174
         if (child instanceof WikiList) {
 81  1
             it.remove();
 82  1
             optimizeList((WikiList) child);
 83  1
             ListIterator<Node> childIterator = child.getChildrenIterator();
 84  2
             while (childIterator.hasNext()) {
 85  1
                 it.add(childIterator.next());
 86  1
                 childIterator.remove();
 87  
             }
 88  1
         } else if (child instanceof WikiPre) {
 89  1
             mergeText(child);
 90  
         }
 91  174
     }
 92  
 
 93  
     protected void optimizeList(WikiList node) {
 94  1
         ListIterator<Node> it = node.getChildrenIterator();
 95  1
         LinkedList<Node> allChildren = new LinkedList<Node>();
 96  7
         while (it.hasNext()) {
 97  6
             allChildren.add(it.next());
 98  6
             it.remove();
 99  
         }
 100  1
         it = allChildren.listIterator();
 101  1
         int depth = 0;
 102  1
         Node parent = node;
 103  7
         while (it.hasNext()) {
 104  6
             ListItem item = (ListItem) it.next();
 105  
 
 106  6
             boolean isOrdered = item instanceof WikiOrderedListItem;
 107  8
             while (depth < item.getDepth()) {
 108  2
                 AbstractList list = isOrdered ? (AbstractList) node.getEmptyOrderedList() : node.getEmptyUnorderedList();
 109  2
                 parent = appendChild(parent, list);
 110  2
                 depth++;
 111  2
             }
 112  7
             while (depth > item.getDepth()) {
 113  1
                 parent = parent.jjtGetParent();
 114  1
                 depth--;
 115  
             }
 116  6
             if (   (isOrdered && parent instanceof WikiUnorderedList)
 117  
                 || (!isOrdered && parent instanceof WikiOrderedList)) {
 118  0
                 AbstractList list = isOrdered ? (AbstractList) node.getEmptyOrderedList() : node.getEmptyUnorderedList();
 119  0
                 parent = appendChild(parent.jjtGetParent(), list);
 120  
             }
 121  6
             Node listItem = appendChild(parent, node.getEmptyListItem());
 122  6
             ListIterator<Node> childIterator = item.getChildrenIterator();
 123  12
             while (childIterator.hasNext()) {
 124  6
                 appendChild(listItem, childIterator.next());
 125  6
                 childIterator.remove();
 126  
             }
 127  6
         }
 128  1
         it = node.getChildrenIterator();
 129  1
     }
 130  
 
 131  
     protected void convertNewlinesToText(InlineContainer node) {
 132  33
         if (!convertNewlinesToText) return;
 133  0
         ListIterator<Node> it = node.getChildrenIterator();
 134  0
         while (it.hasNext()) {
 135  0
             Node child = it.next();
 136  0
             if (!(child instanceof WikiNewline)) continue;
 137  0
             WikiNewline newline = (WikiNewline) child;
 138  0
             if (newline.getCount() == 1) it.set(newline.convertToText());
 139  0
         }
 140  0
     }
 141  
 
 142  
     protected void mergeText(Node node) {
 143  34
         if (!mergeText) return;
 144  34
         ListIterator<Node> it = node.getChildrenIterator();
 145  34
         Node last = null;
 146  168
         while (it.hasNext()) {
 147  134
             Node next = it.next();
 148  134
             if (last instanceof WikiText && next instanceof WikiText) {
 149  22
                 ((WikiText) last).addText(((WikiText) next).getText());
 150  22
                 it.remove();
 151  22
                 last.setLastToken(next.getLastToken());
 152  
             } else {
 153  112
                 last = next;
 154  
             }
 155  134
         }
 156  34
     }
 157  
 
 158  
     protected void fixXml(InlineContainer node) {
 159  33
         ListIterator<Node> it = node.getChildrenIterator();
 160  144
         while (it.hasNext()) {
 161  111
             Node child = it.next();
 162  111
             if (!(child instanceof AbstractWikiXml)) continue;
 163  0
             if (supportPlugins) {
 164  0
                 if (child instanceof WikiXml) {
 165  0
                 } else if (child instanceof WikiXmlEnd) {
 166  
                 } else {
 167  
                     continue;
 168  
                 }
 169  
             } else {
 170  0
                 it.set(((AbstractWikiXml) child).convertToText());
 171  
             }
 172  0
         }
 173  33
     }
 174  
 
 175  
     protected void makeParagraphs(InlineContainer node) {
 176  33
         if (!makeParagraphs) return;
 177  33
         ListIterator<Node> it = node.getChildrenIterator();
 178  33
         int paragraphCount = 0;
 179  144
         while (it.hasNext()) {
 180  111
             Node child = it.next();
 181  111
             if (child instanceof WikiNewline) {
 182  34
                 WikiNewline newline = (WikiNewline) child;
 183  34
                 int count = newline.getCount();
 184  34
                 if (count > 1) {
 185  5
                     newline.setCount(count - 1);
 186  5
                     paragraphCount += makeParagraph(node, it);
 187  
                 }
 188  
             }
 189  111
         }
 190  33
         if (paragraphCount != 0) {
 191  
             //System.err.println("checking for trailing text for makeParagraph");
 192  5
             while (it.hasPrevious()) {
 193  5
                 Node previous = it.previous();
 194  5
                 if (previous instanceof WikiParagraph) break;
 195  4
             }
 196  6
             while (it.hasNext()) it.next();
 197  1
             paragraphCount += makeParagraph(node, it);
 198  
         }
 199  33
     }
 200  
 
 201  
     protected int findFirstNonSpace(String value) {
 202  
         int i;
 203  0
         for (i = 0; i < value.length(); i++) {
 204  0
             if (!Character.isSpace(value.charAt(i))) break;
 205  
         }
 206  0
         return i;
 207  
     }
 208  
 
 209  
     protected int findLastNonSpace(String value) {
 210  
         int i;
 211  0
         for (i = value.length() - 1; i >= 0; i--) {
 212  0
             if (!Character.isSpace(value.charAt(i))) break;
 213  
         }
 214  0
         return i;
 215  
     }
 216  
 
 217  
     protected void removeInterblockSpacing(Node node) {
 218  33
         if (!removeInterblockSpacing) return;
 219  33
         ListIterator it = node.getChildrenIterator();
 220  33
         if (!it.hasNext()) return;
 221  29
         Node last = null;
 222  29
         Node next = (Node) it.next();
 223  29
         boolean isFirst = true;
 224  
         do {
 225  93
             boolean setLast = true;
 226  93
             if (next instanceof BlockNode) {
 227  9
                 it.previous();
 228  9
                 while (last instanceof WikiSpace) {
 229  0
                     it.remove();
 230  0
                     if (!it.hasPrevious()) break;
 231  0
                     last = (Node) it.previous();
 232  
                 }
 233  9
                 it.next();
 234  84
             } else if ((last instanceof BlockNode || isFirst)) {
 235  41
                 while (next instanceof WikiSpace) {
 236  6
                     it.remove();
 237  6
                     setLast = false;
 238  6
                     if (!it.hasNext()) break;
 239  6
                     next = (Node) it.next();
 240  
                 }
 241  
             }
 242  93
             isFirst = false;
 243  93
             if (setLast) last = next;
 244  93
             if (!it.hasNext()) break;
 245  64
             next = (Node) it.next();
 246  64
         } while (true);
 247  29
     }
 248  
 
 249  
     protected int makeParagraph(InlineContainer parent, ListIterator<Node> it) {
 250  6
         LinkedList<Node> removedChildren = new LinkedList<Node>();
 251  
         //System.err.println("\tscanning");
 252  24
         while (it.hasPrevious()) {
 253  24
             Node previous = it.previous();
 254  
             //System.err.println("\t\tprevious=" + previous);
 255  24
             if (
 256  
                 previous instanceof WikiParagraph || previous instanceof BlockNode) {
 257  
                 //|| previous instanceof WikiDivStyle
 258  
                 //|| previous instanceof WikiDivClass) {
 259  6
                 it.next();
 260  6
                 break;
 261  
             }
 262  18
             it.remove();
 263  18
             removedChildren.addFirst(previous);
 264  18
         }
 265  
         //System.err.println("\tremovedChildren=" + removedChildren);
 266  6
         if (removedChildren.isEmpty()) return 0;
 267  6
         if (removedChildren.size() == 1) {
 268  4
             Node singleton = removedChildren.getFirst();
 269  4
             if (!(singleton instanceof InlineNode) || singleton instanceof WikiNewline) {
 270  4
                 it.add(singleton);
 271  4
                 return 0;
 272  
             }
 273  
         }
 274  2
         WikiParagraph paragraph = parent.getEmptyParagraph();
 275  2
         int childrenSize = removedChildren.size();
 276  16
         for (int i = 0; i < childrenSize; i++) {
 277  14
             appendChild(paragraph, removedChildren.removeFirst());
 278  
         }
 279  2
         optimizeInlineContainer(paragraph);
 280  2
         int numChildren = paragraph.jjtGetNumChildren();
 281  2
         if (numChildren > 0) {
 282  2
             paragraph.setFirstToken(paragraph.jjtGetChild(0).getFirstToken());
 283  2
             paragraph.setLastToken(paragraph.jjtGetChild(numChildren - 1).getLastToken());
 284  
         }
 285  2
         it.add(paragraph);
 286  2
         return 1;
 287  
     }
 288  
 
 289  
     protected void makeTableRows(InlineContainer parent) {
 290  
         /*
 291  
         ListIterator it = parent.getChildrenIterator();
 292  
         while (it.hasNext()) {
 293  
             Node child = (Node) it.next();
 294  
             if (child instanceof WikiTableRow) {
 295  
                 WikiTableRow row = (WikiTableRow) child;
 296  
                 while (it.hasNext()) {
 297  
                     child = (Node) it.next();
 298  
                     it.remove();
 299  
                     if (child instanceof WikiTableRowEnd) break;
 300  
                     appendChild(row, child);
 301  
                 }
 302  
             }
 303  
         }
 304  
          *
 305  
         */
 306  33
     }
 307  
 }