]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Node.java
7c91ceaa023e95f7a745f295362d6b77b03193be
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / markdown / nodes / Node.java
1 package org.simantics.scl.compiler.markdown.nodes;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.scl.compiler.markdown.internal.ExtensionNodeHandler;
7 import org.simantics.scl.compiler.markdown.internal.MarkdownParser;
8
9
10 public abstract class Node {
11     public Node next;
12     public Node prev;
13     public Node parent;
14     public Node firstChild;
15     public Node lastChild;
16     public boolean open = true;
17     public boolean lastLineBlank = false;
18     public StringBuilder stringContent;
19     public int lineNumber;
20     
21     public void setLastLineBlank(boolean value) {
22         if(MarkdownParser.DEBUG)
23             System.out.println("    " + getClass().getSimpleName() + "@" + hashCode() + " lastLineBlank: " +
24                     lastLineBlank + " -> " + value);
25         this.lastLineBlank = value;
26     }
27     
28     public boolean canContain(Node node) {
29         return false;
30     }
31
32     public boolean acceptLines() {
33         return false;
34     }
35     
36     public void toHtml(StringBuilder b) {
37         for(Node child = firstChild; child != null; child = child.next)
38             child.toHtml(b);
39     }
40     
41     public List<HeaderNode> extractHeaders() {
42         ArrayList<HeaderNode> result = new ArrayList<HeaderNode>();
43         for(Node child = firstChild; child != null; child = child.next)
44             if(child instanceof HeaderNode)
45                 result.add((HeaderNode)child);
46         return result;
47     }
48     
49     public String toHtml() {
50         StringBuilder b = new StringBuilder();
51         toHtml(b);
52         int len = b.length();
53         if(len > 0 && b.charAt(len-1) == '\n')
54             b.delete(len-1, len);
55         return b.toString();
56     }
57     
58     public void toPlainText(StringBuilder b) {
59         for(Node child = firstChild; child != null; child = child.next)
60             child.toPlainText(b);
61     }
62     
63     public void remove() {
64         if(prev == null)
65             parent.firstChild = next;
66         else
67             prev.next = next;
68         if(next == null)
69             parent.lastChild = prev;
70         else
71             next.prev = prev;
72     }
73     
74     public void processExtensionNodes(ExtensionNodeHandler handler) {
75         for(Node child = firstChild; child != null; child = child.next)
76             child.processExtensionNodes(handler);
77     }
78
79     public void addChild(Node child) {
80         child.parent = this;
81         if(lastChild == null)
82            firstChild = child; 
83         else {
84             lastChild.next = child;
85             child.prev = lastChild;
86         }
87         lastChild = child;
88     }
89 }