[34] | 1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
| 2 |
|
---|
[37] | 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Iterator;
|
---|
| 5 |
|
---|
[39] | 6 | import org.apache.commons.lang.StringEscapeUtils;
|
---|
| 7 | import org.apache.commons.lang.StringUtils;
|
---|
[34] | 8 | import org.eclipse.emf.ecore.EObject;
|
---|
[37] | 9 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
[34] | 10 |
|
---|
[37] | 11 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
[38] | 12 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 13 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
[37] | 14 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
[39] | 15 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
|
---|
[37] | 16 |
|
---|
[34] | 17 | public class EbnfHtmlFormatterVisitor extends EbnfFormatterVisitor {
|
---|
| 18 |
|
---|
[37] | 19 | private ArrayList<MetaTextEntry> metaTexts = new ArrayList<MetaTextEntry>();
|
---|
[39] | 20 | private ArrayList<MetaTextEntry> transformedTexts = new ArrayList<MetaTextEntry>();
|
---|
[40] | 21 | private int lastMetaTextBlock = 0;
|
---|
[37] | 22 |
|
---|
[34] | 23 | public EbnfHtmlFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
| 24 | super(rootNode, config);
|
---|
| 25 | this.config = config;
|
---|
| 26 | buf = new StringBuffer();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[37] | 29 | protected void weaveComments() {
|
---|
| 30 | bufferPositionOriginalText = 0;
|
---|
| 31 | bufferPositionFormattedTextNoWhitespaces = 0;
|
---|
| 32 | bufferPositionFormattedText = 0;
|
---|
| 33 |
|
---|
| 34 | StringBuffer result = new StringBuffer();
|
---|
| 35 | formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
|
---|
| 36 | formattedText = buf.toString();
|
---|
[34] | 37 |
|
---|
[37] | 38 | MetaTextEntry currentMetaText = null;
|
---|
| 39 | if (metaTexts.iterator().hasNext())
|
---|
| 40 | currentMetaText = metaTexts.iterator().next();
|
---|
| 41 |
|
---|
[39] | 42 | MetaTextEntry currentTransformedText = null;
|
---|
| 43 | if (transformedTexts.iterator().hasNext())
|
---|
| 44 | currentTransformedText = transformedTexts.iterator().next();
|
---|
| 45 |
|
---|
[37] | 46 | while (bufferPositionFormattedTextNoWhitespaces <= formattedTextNoWhitespaces.length()) {
|
---|
| 47 | skipWhitespacesOriginalText();
|
---|
| 48 | skipWhitespacesFormattedText(result);
|
---|
| 49 |
|
---|
| 50 | if (!(bufferPositionOriginalText < originalText.length()))
|
---|
| 51 | break;
|
---|
| 52 |
|
---|
| 53 | char formattedPositionNoWhitespaces;
|
---|
| 54 | if (bufferPositionFormattedTextNoWhitespaces == formattedTextNoWhitespaces.length()) {
|
---|
| 55 | formattedPositionNoWhitespaces = ' ';
|
---|
| 56 | } else {
|
---|
| 57 | formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces);
|
---|
| 58 | }
|
---|
| 59 | char originalPosition = originalText.charAt(bufferPositionOriginalText);
|
---|
| 60 |
|
---|
| 61 | if (formattedPositionNoWhitespaces != originalPosition) {
|
---|
[39] | 62 | // if ((currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces == currentMetaText.getOffset())) {
|
---|
| 63 | // result.append(currentMetaText.getText());
|
---|
| 64 | // metaTexts.remove(currentMetaText);
|
---|
| 65 | // if (metaTexts.iterator().hasNext())
|
---|
| 66 | // currentMetaText = metaTexts.iterator().next();
|
---|
| 67 | // else
|
---|
| 68 | // currentMetaText = null;
|
---|
| 69 | // }
|
---|
| 70 |
|
---|
[37] | 71 | if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary
|
---|
| 72 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
| 73 | bufferPositionFormattedText++;
|
---|
| 74 | } else if (isCommentNext(originalText, bufferPositionOriginalText)) {
|
---|
| 75 | LeafNode currentComment = allComments.get(allCommentsPosition);
|
---|
| 76 | if (currentComment.getTotalOffset() == bufferPositionOriginalText) {
|
---|
| 77 | if (isMultiLineComment(currentComment.getText())) {
|
---|
| 78 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
| 79 | if (newLinesCount > 0) {
|
---|
| 80 | if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) {
|
---|
| 81 | result.append("\n\n");
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | result.append(currentComment.getText());
|
---|
| 85 | result.append("\n");
|
---|
| 86 | } else {
|
---|
| 87 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
| 88 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
| 89 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
| 90 | result.append(lastWhiteSpaces);
|
---|
| 91 | }
|
---|
| 92 | } else if (isSingleLineComment(currentComment.getText())) {
|
---|
| 93 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
| 94 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
| 95 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
| 96 | if (newLinesCount > 0) {
|
---|
| 97 | result.append("\n\n" + stripEndingNewline(currentComment.getText()));
|
---|
| 98 | } else {
|
---|
| 99 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
| 100 | }
|
---|
| 101 | result.append(lastWhiteSpaces);
|
---|
| 102 | }
|
---|
| 103 | bufferPositionOriginalText+=currentComment.getLength();
|
---|
| 104 | allCommentsPosition++;
|
---|
| 105 | }
|
---|
| 106 | } else { // disaster handling: return original unformatted text!
|
---|
| 107 | System.err.println("Disaster Recovery: returning original text!!");
|
---|
| 108 | buf = new StringBuffer();
|
---|
| 109 | buf.append(originalText);
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 | } else {
|
---|
[40] | 113 | if (lastMetaTextBlock == 0 && (currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces == currentMetaText.getOffset())) {
|
---|
[37] | 114 | result.append(currentMetaText.getText());
|
---|
| 115 | metaTexts.remove(currentMetaText);
|
---|
| 116 | if (metaTexts.iterator().hasNext())
|
---|
| 117 | currentMetaText = metaTexts.iterator().next();
|
---|
| 118 | else
|
---|
| 119 | currentMetaText = null;
|
---|
[40] | 120 | lastMetaTextBlock = 1;
|
---|
[37] | 121 | }
|
---|
[39] | 122 |
|
---|
| 123 | if (currentTransformedText != null && currentTransformedText.getOffset() == bufferPositionFormattedTextNoWhitespaces) {
|
---|
| 124 | result.append(currentTransformedText.getTransformedText());
|
---|
| 125 | bufferPositionOriginalText += currentTransformedText.getText().length();
|
---|
| 126 | bufferPositionFormattedText += currentTransformedText.getText().length();
|
---|
| 127 | bufferPositionFormattedTextNoWhitespaces += currentTransformedText.getText().replaceAll("[ \t\r\n]", "").length();
|
---|
| 128 | transformedTexts.remove(currentTransformedText);
|
---|
| 129 | currentTransformedText = null;
|
---|
| 130 |
|
---|
| 131 | if (transformedTexts.iterator().hasNext())
|
---|
| 132 | currentTransformedText = transformedTexts.iterator().next();
|
---|
| 133 | } else {
|
---|
| 134 | String text = formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1);
|
---|
| 135 | result.append(text);
|
---|
| 136 | bufferPositionOriginalText++;
|
---|
| 137 | bufferPositionFormattedText++;
|
---|
| 138 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
[37] | 139 | }
|
---|
[39] | 140 |
|
---|
[40] | 141 | if (lastMetaTextBlock == 1 && (currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces == currentMetaText.getOffset())) {
|
---|
| 142 | result.append(currentMetaText.getText());
|
---|
| 143 | metaTexts.remove(currentMetaText);
|
---|
| 144 | if (metaTexts.iterator().hasNext())
|
---|
| 145 | currentMetaText = metaTexts.iterator().next();
|
---|
| 146 | else
|
---|
| 147 | currentMetaText = null;
|
---|
| 148 | lastMetaTextBlock = 0;
|
---|
| 149 | }
|
---|
[37] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | buf = result;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | protected void metaText(String str) {
|
---|
[39] | 156 | MetaTextEntry metaTextEntry = new MetaTextEntry(str, buf.toString().replaceAll("[ \t\n\r]", "").length());
|
---|
[37] | 157 | metaTexts.add(metaTextEntry);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[39] | 160 | protected void textTransformed(String original, String transformed) {
|
---|
| 161 | MetaTextEntry metaText = new MetaTextEntry(original, transformed, buf.toString().replaceAll("[ \t\n\r]", "").length());
|
---|
| 162 | transformedTexts.add(metaText);
|
---|
| 163 | buf.append(original);
|
---|
| 164 | newLineOffsetCounter += original.length();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[37] | 167 | // -----------------------------------------------------------------------------
|
---|
| 168 |
|
---|
[38] | 169 | protected void visitAfter(EtsiBnf node) {
|
---|
| 170 | weaveComments();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[37] | 173 | protected void visitBefore(SectionHeading node) {
|
---|
| 174 | if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
| 175 | newLine();
|
---|
| 176 |
|
---|
| 177 | lastWasSectionHeading=true;
|
---|
| 178 |
|
---|
| 179 | metaText("<h1>");
|
---|
| 180 | text(node.getSectionHeader().replaceAll("[\n\r]", ""));
|
---|
| 181 | metaText("</h1>");
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | protected void visitAfter(SectionHeading node) {
|
---|
| 185 | }
|
---|
[38] | 186 |
|
---|
| 187 | protected void visitBefore(RuleReference node) {
|
---|
| 188 | wrap();
|
---|
| 189 |
|
---|
| 190 | metaText("<a href=\"#" + node.getRuleref().getName() + "\">");
|
---|
| 191 | text(node.getRuleref().getName());
|
---|
| 192 | metaText("</a>");
|
---|
| 193 | }
|
---|
[37] | 194 |
|
---|
[38] | 195 | protected void visitAfter(RuleReference node) {
|
---|
[37] | 196 | }
|
---|
[38] | 197 |
|
---|
| 198 | protected void visitBefore(Rule node) {
|
---|
| 199 | if (lastWasSectionHeading)
|
---|
| 200 | newLine();
|
---|
| 201 |
|
---|
| 202 | lastWasSectionHeading=false;
|
---|
| 203 |
|
---|
| 204 | newLineOffsetCounter = 0;
|
---|
| 205 |
|
---|
| 206 | if (node.getRulenumber() > 0)
|
---|
| 207 | text(node.getRulenumber() + ". ");
|
---|
| 208 |
|
---|
| 209 | metaText("<a name=\"#" + node.getName() + "\">");
|
---|
| 210 | text(node.getName());
|
---|
| 211 | metaText("</a>");
|
---|
| 212 | text(" ::= ");
|
---|
| 213 |
|
---|
| 214 | rightHandSideRuleOffset = newLineOffsetCounter;
|
---|
| 215 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | protected void visitAfter(Rule node) {
|
---|
| 219 | metaText("<br/>");
|
---|
| 220 | text(";");
|
---|
| 221 | newLine();
|
---|
| 222 | ruleSpacingStack.pop();
|
---|
| 223 | }
|
---|
[39] | 224 |
|
---|
| 225 | protected void visitBefore(StringRule node) {
|
---|
| 226 | wrap();
|
---|
| 227 | if (node.getLiteral() != null) {
|
---|
| 228 | // text("\"" + node.getLiteral() + "\"");
|
---|
| 229 | textTransformed("\"" + node.getLiteral() + "\"", StringEscapeUtils.escapeHtml("\"" + node.getLiteral() + "\""));
|
---|
| 230 | // textTransformed("\"" + node.getLiteral() + "\"", "gnabar");
|
---|
| 231 | } else if (node.getColon() != null) {
|
---|
| 232 | // text("\"\"\"");
|
---|
| 233 | textTransformed("\"\"\"", StringEscapeUtils.escapeHtml("\"\"\""));
|
---|
| 234 | // textTransformed("\"\"\"", "gnabar");
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | protected void visitAfter(StringRule node) {
|
---|
| 239 | }
|
---|
[34] | 240 | }
|
---|