1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Iterator;
|
---|
5 |
|
---|
6 | import org.eclipse.emf.ecore.EObject;
|
---|
7 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
10 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
11 |
|
---|
12 | public class EbnfHtmlFormatterVisitor extends EbnfFormatterVisitor {
|
---|
13 |
|
---|
14 | private ArrayList<MetaTextEntry> metaTexts = new ArrayList<MetaTextEntry>();
|
---|
15 |
|
---|
16 | public EbnfHtmlFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
17 | super(rootNode, config);
|
---|
18 | this.config = config;
|
---|
19 | buf = new StringBuffer();
|
---|
20 | }
|
---|
21 |
|
---|
22 | protected void weaveComments() {
|
---|
23 | bufferPositionOriginalText = 0;
|
---|
24 | bufferPositionFormattedTextNoWhitespaces = 0;
|
---|
25 | bufferPositionFormattedText = 0;
|
---|
26 |
|
---|
27 | StringBuffer result = new StringBuffer();
|
---|
28 | formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
|
---|
29 | formattedText = buf.toString();
|
---|
30 |
|
---|
31 | MetaTextEntry currentMetaText = null;
|
---|
32 | if (metaTexts.iterator().hasNext())
|
---|
33 | currentMetaText = metaTexts.iterator().next();
|
---|
34 |
|
---|
35 | while (bufferPositionFormattedTextNoWhitespaces <= formattedTextNoWhitespaces.length()) {
|
---|
36 | skipWhitespacesOriginalText();
|
---|
37 | skipWhitespacesFormattedText(result);
|
---|
38 |
|
---|
39 | if (!(bufferPositionOriginalText < originalText.length()))
|
---|
40 | break;
|
---|
41 |
|
---|
42 | char formattedPositionNoWhitespaces;
|
---|
43 | if (bufferPositionFormattedTextNoWhitespaces == formattedTextNoWhitespaces.length()) {
|
---|
44 | formattedPositionNoWhitespaces = ' ';
|
---|
45 | } else {
|
---|
46 | formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces);
|
---|
47 | }
|
---|
48 | char originalPosition = originalText.charAt(bufferPositionOriginalText);
|
---|
49 |
|
---|
50 | if (formattedPositionNoWhitespaces != originalPosition) {
|
---|
51 | if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary
|
---|
52 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
53 | bufferPositionFormattedText++;
|
---|
54 | } else if (isCommentNext(originalText, bufferPositionOriginalText)) {
|
---|
55 | LeafNode currentComment = allComments.get(allCommentsPosition);
|
---|
56 | if (currentComment.getTotalOffset() == bufferPositionOriginalText) {
|
---|
57 | if (isMultiLineComment(currentComment.getText())) {
|
---|
58 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
59 | if (newLinesCount > 0) {
|
---|
60 | if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) {
|
---|
61 | result.append("\n\n");
|
---|
62 | }
|
---|
63 |
|
---|
64 | result.append(currentComment.getText());
|
---|
65 | result.append("\n");
|
---|
66 | } else {
|
---|
67 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
68 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
69 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
70 | result.append(lastWhiteSpaces);
|
---|
71 | }
|
---|
72 | } else if (isSingleLineComment(currentComment.getText())) {
|
---|
73 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
74 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
75 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
76 | if (newLinesCount > 0) {
|
---|
77 | result.append("\n\n" + stripEndingNewline(currentComment.getText()));
|
---|
78 | } else {
|
---|
79 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
80 | }
|
---|
81 | result.append(lastWhiteSpaces);
|
---|
82 | }
|
---|
83 | bufferPositionOriginalText+=currentComment.getLength();
|
---|
84 | allCommentsPosition++;
|
---|
85 | }
|
---|
86 | } else { // disaster handling: return original unformatted text!
|
---|
87 | System.err.println("Disaster Recovery: returning original text!!");
|
---|
88 | buf = new StringBuffer();
|
---|
89 | buf.append(originalText);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | if ((currentMetaText != null) && (bufferPositionFormattedText >= currentMetaText.getOffset())) {
|
---|
94 | result.append(currentMetaText.getText());
|
---|
95 | metaTexts.remove(currentMetaText);
|
---|
96 | if (metaTexts.iterator().hasNext())
|
---|
97 | currentMetaText = metaTexts.iterator().next();
|
---|
98 | else
|
---|
99 | currentMetaText = null;
|
---|
100 | }
|
---|
101 |
|
---|
102 | result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1));
|
---|
103 | bufferPositionOriginalText++;
|
---|
104 | bufferPositionFormattedText++;
|
---|
105 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
106 |
|
---|
107 | if ((currentMetaText != null) && (bufferPositionFormattedText >= currentMetaText.getOffset())) {
|
---|
108 | result.append(currentMetaText.getText());
|
---|
109 | metaTexts.remove(currentMetaText);
|
---|
110 | if (metaTexts.iterator().hasNext())
|
---|
111 | currentMetaText = metaTexts.iterator().next();
|
---|
112 | else
|
---|
113 | currentMetaText = null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | buf = result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected void metaText(String str) {
|
---|
121 | MetaTextEntry metaTextEntry = new MetaTextEntry(str, buf.length());
|
---|
122 | metaTexts.add(metaTextEntry);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // -----------------------------------------------------------------------------
|
---|
126 |
|
---|
127 | protected void visitBefore(SectionHeading node) {
|
---|
128 | if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
129 | newLine();
|
---|
130 |
|
---|
131 | lastWasSectionHeading=true;
|
---|
132 |
|
---|
133 | metaText("<h1>");
|
---|
134 | text(node.getSectionHeader().replaceAll("[\n\r]", ""));
|
---|
135 | metaText("</h1>");
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected void visitAfter(SectionHeading node) {
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected void visitAfter(EtsiBnf node) {
|
---|
142 | weaveComments();
|
---|
143 | }
|
---|
144 | }
|
---|