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