[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;
|
---|
[41] | 10 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
[34] | 11 |
|
---|
[37] | 12 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
[41] | 13 | import de.ugoe.cs.swe.bnftools.ebnf.Import;
|
---|
[38] | 14 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 15 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
[37] | 16 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
[39] | 17 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
|
---|
[37] | 18 |
|
---|
[34] | 19 | public class EbnfHtmlFormatterVisitor extends EbnfFormatterVisitor {
|
---|
| 20 |
|
---|
[37] | 21 | private ArrayList<MetaTextEntry> metaTexts = new ArrayList<MetaTextEntry>();
|
---|
[39] | 22 | private ArrayList<MetaTextEntry> transformedTexts = new ArrayList<MetaTextEntry>();
|
---|
[40] | 23 | private int lastMetaTextBlock = 0;
|
---|
[41] | 24 | private boolean isParagraphOpen = false;
|
---|
[37] | 25 |
|
---|
[34] | 26 | public EbnfHtmlFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
| 27 | super(rootNode, config);
|
---|
| 28 | this.config = config;
|
---|
| 29 | buf = new StringBuffer();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[37] | 32 | protected void weaveComments() {
|
---|
| 33 | bufferPositionOriginalText = 0;
|
---|
| 34 | bufferPositionFormattedTextNoWhitespaces = 0;
|
---|
| 35 | bufferPositionFormattedText = 0;
|
---|
| 36 |
|
---|
| 37 | StringBuffer result = new StringBuffer();
|
---|
| 38 | formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
|
---|
| 39 | formattedText = buf.toString();
|
---|
[34] | 40 |
|
---|
[37] | 41 | MetaTextEntry currentMetaText = null;
|
---|
| 42 | if (metaTexts.iterator().hasNext())
|
---|
| 43 | currentMetaText = metaTexts.iterator().next();
|
---|
| 44 |
|
---|
[39] | 45 | MetaTextEntry currentTransformedText = null;
|
---|
| 46 | if (transformedTexts.iterator().hasNext())
|
---|
| 47 | currentTransformedText = transformedTexts.iterator().next();
|
---|
| 48 |
|
---|
[37] | 49 | while (bufferPositionFormattedTextNoWhitespaces <= formattedTextNoWhitespaces.length()) {
|
---|
| 50 | skipWhitespacesOriginalText();
|
---|
| 51 | skipWhitespacesFormattedText(result);
|
---|
| 52 |
|
---|
| 53 | if (!(bufferPositionOriginalText < originalText.length()))
|
---|
| 54 | break;
|
---|
| 55 |
|
---|
| 56 | char formattedPositionNoWhitespaces;
|
---|
| 57 | if (bufferPositionFormattedTextNoWhitespaces == formattedTextNoWhitespaces.length()) {
|
---|
| 58 | formattedPositionNoWhitespaces = ' ';
|
---|
| 59 | } else {
|
---|
| 60 | formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces);
|
---|
| 61 | }
|
---|
| 62 | char originalPosition = originalText.charAt(bufferPositionOriginalText);
|
---|
| 63 |
|
---|
| 64 | if (formattedPositionNoWhitespaces != originalPosition) {
|
---|
[39] | 65 | // if ((currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces == currentMetaText.getOffset())) {
|
---|
| 66 | // result.append(currentMetaText.getText());
|
---|
| 67 | // metaTexts.remove(currentMetaText);
|
---|
| 68 | // if (metaTexts.iterator().hasNext())
|
---|
| 69 | // currentMetaText = metaTexts.iterator().next();
|
---|
| 70 | // else
|
---|
| 71 | // currentMetaText = null;
|
---|
| 72 | // }
|
---|
| 73 |
|
---|
[37] | 74 | if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary
|
---|
| 75 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
| 76 | bufferPositionFormattedText++;
|
---|
| 77 | } else if (isCommentNext(originalText, bufferPositionOriginalText)) {
|
---|
| 78 | LeafNode currentComment = allComments.get(allCommentsPosition);
|
---|
| 79 | if (currentComment.getTotalOffset() == bufferPositionOriginalText) {
|
---|
| 80 | if (isMultiLineComment(currentComment.getText())) {
|
---|
| 81 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
| 82 | if (newLinesCount > 0) {
|
---|
| 83 | if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) {
|
---|
| 84 | result.append("\n\n");
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | result.append(currentComment.getText());
|
---|
| 88 | result.append("\n");
|
---|
| 89 | } else {
|
---|
| 90 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
| 91 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
| 92 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
| 93 | result.append(lastWhiteSpaces);
|
---|
| 94 | }
|
---|
| 95 | } else if (isSingleLineComment(currentComment.getText())) {
|
---|
| 96 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
| 97 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
| 98 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
| 99 | if (newLinesCount > 0) {
|
---|
| 100 | result.append("\n\n" + stripEndingNewline(currentComment.getText()));
|
---|
| 101 | } else {
|
---|
| 102 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
| 103 | }
|
---|
| 104 | result.append(lastWhiteSpaces);
|
---|
| 105 | }
|
---|
| 106 | bufferPositionOriginalText+=currentComment.getLength();
|
---|
| 107 | allCommentsPosition++;
|
---|
| 108 | }
|
---|
| 109 | } else { // disaster handling: return original unformatted text!
|
---|
| 110 | System.err.println("Disaster Recovery: returning original text!!");
|
---|
| 111 | buf = new StringBuffer();
|
---|
| 112 | buf.append(originalText);
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 | } else {
|
---|
[41] | 116 | while (lastMetaTextBlock == 0 && (currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces >= currentMetaText.getOffset())) {
|
---|
[37] | 117 | result.append(currentMetaText.getText());
|
---|
| 118 | metaTexts.remove(currentMetaText);
|
---|
| 119 | if (metaTexts.iterator().hasNext())
|
---|
| 120 | currentMetaText = metaTexts.iterator().next();
|
---|
| 121 | else
|
---|
| 122 | currentMetaText = null;
|
---|
[41] | 123 |
|
---|
| 124 | if ((currentMetaText == null) && (bufferPositionFormattedTextNoWhitespaces != currentMetaText.getOffset()))
|
---|
| 125 | lastMetaTextBlock = 1;
|
---|
[37] | 126 | }
|
---|
[39] | 127 |
|
---|
| 128 | if (currentTransformedText != null && currentTransformedText.getOffset() == bufferPositionFormattedTextNoWhitespaces) {
|
---|
| 129 | result.append(currentTransformedText.getTransformedText());
|
---|
| 130 | bufferPositionOriginalText += currentTransformedText.getText().length();
|
---|
| 131 | bufferPositionFormattedText += currentTransformedText.getText().length();
|
---|
| 132 | bufferPositionFormattedTextNoWhitespaces += currentTransformedText.getText().replaceAll("[ \t\r\n]", "").length();
|
---|
| 133 | transformedTexts.remove(currentTransformedText);
|
---|
| 134 | currentTransformedText = null;
|
---|
| 135 |
|
---|
| 136 | if (transformedTexts.iterator().hasNext())
|
---|
| 137 | currentTransformedText = transformedTexts.iterator().next();
|
---|
| 138 | } else {
|
---|
| 139 | String text = formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1);
|
---|
| 140 | result.append(text);
|
---|
| 141 | bufferPositionOriginalText++;
|
---|
| 142 | bufferPositionFormattedText++;
|
---|
| 143 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
[37] | 144 | }
|
---|
[39] | 145 |
|
---|
[41] | 146 | while ( lastMetaTextBlock == 1 && currentMetaText != null && bufferPositionFormattedTextNoWhitespaces >= currentMetaText.getOffset()) {
|
---|
[40] | 147 | result.append(currentMetaText.getText());
|
---|
| 148 | metaTexts.remove(currentMetaText);
|
---|
[41] | 149 | if (metaTexts.iterator().hasNext()) {
|
---|
[40] | 150 | currentMetaText = metaTexts.iterator().next();
|
---|
[41] | 151 | } else {
|
---|
[40] | 152 | currentMetaText = null;
|
---|
[41] | 153 | }
|
---|
| 154 |
|
---|
| 155 | if ((currentMetaText == null) || (bufferPositionFormattedTextNoWhitespaces != currentMetaText.getOffset()))
|
---|
| 156 | lastMetaTextBlock = 0;
|
---|
[40] | 157 | }
|
---|
[37] | 158 | }
|
---|
| 159 | }
|
---|
[41] | 160 | while ( bufferPositionFormattedTextNoWhitespaces >= formattedTextNoWhitespaces.length()-1 && currentMetaText != null) {
|
---|
| 161 | result.append(currentMetaText.getText());
|
---|
| 162 | metaTexts.remove(currentMetaText);
|
---|
| 163 | if (metaTexts.iterator().hasNext()) {
|
---|
| 164 | currentMetaText = metaTexts.iterator().next();
|
---|
| 165 | } else {
|
---|
| 166 | currentMetaText = null;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
[37] | 171 | buf = result;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | protected void metaText(String str) {
|
---|
[39] | 175 | MetaTextEntry metaTextEntry = new MetaTextEntry(str, buf.toString().replaceAll("[ \t\n\r]", "").length());
|
---|
[37] | 176 | metaTexts.add(metaTextEntry);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[39] | 179 | protected void textTransformed(String original, String transformed) {
|
---|
| 180 | MetaTextEntry metaText = new MetaTextEntry(original, transformed, buf.toString().replaceAll("[ \t\n\r]", "").length());
|
---|
| 181 | transformedTexts.add(metaText);
|
---|
| 182 | buf.append(original);
|
---|
| 183 | newLineOffsetCounter += original.length();
|
---|
| 184 | }
|
---|
[41] | 185 |
|
---|
| 186 | protected void newLine() {
|
---|
| 187 | buf.append("\n");
|
---|
| 188 | if (!isParagraphOpen) {
|
---|
| 189 | metaText("\n<p class=PL style='mso-pagination:widow-orphan lines-together;page-break-after: avoid'>\n");
|
---|
| 190 | isParagraphOpen = true;
|
---|
| 191 | } else {
|
---|
| 192 | metaText("\n</p>\n");
|
---|
| 193 | isParagraphOpen = false;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | if ((ruleSpacingStack != null) && (!ruleSpacingStack.empty())) {
|
---|
| 197 | newLineOffsetCounter = ruleSpacingStack.peek();
|
---|
| 198 | } else {
|
---|
| 199 | newLineOffsetCounter = 0;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[37] | 203 | // -----------------------------------------------------------------------------
|
---|
| 204 |
|
---|
[41] | 205 | protected void visitBefore(EtsiBnf node) {
|
---|
| 206 | parserEtsiBnfNode = NodeUtil.getNodeAdapter(node).getParserNode();
|
---|
| 207 | collectAllComments(parserEtsiBnfNode);
|
---|
| 208 | originalText = NodeUtil.getNodeAdapter(node).getParserNode().serialize();
|
---|
| 209 | originalTextNoWhitespaces = originalText.replaceAll("[ \t\n\r]", "");
|
---|
| 210 |
|
---|
| 211 | textTransformed("grammar " + node.getName(),"");
|
---|
| 212 | if (node.getType() != null)
|
---|
| 213 | textTransformed(node.getType(),"");
|
---|
| 214 | textTransformed(";","");
|
---|
| 215 | metaText("<body lang=EN-US link=blue vlink=purple style='tab-interval:14.15pt'>\n");
|
---|
| 216 | metaText("<div class=WordSection1>\n");
|
---|
| 217 |
|
---|
| 218 | // newLine();
|
---|
| 219 | // newLine();
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[38] | 222 | protected void visitAfter(EtsiBnf node) {
|
---|
[41] | 223 | metaText("</div>\n");
|
---|
| 224 | metaText("</body>\n");
|
---|
| 225 | metaText("</html>\n");
|
---|
[38] | 226 | weaveComments();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[41] | 229 | protected void visitBefore(Import node) {
|
---|
| 230 | textTransformed("import \"" + node.getImportURI() + "\"","");
|
---|
| 231 | if (node.getGrammarType() != null) {
|
---|
| 232 | textTransformed("/" + node.getGrammarType(), "");
|
---|
| 233 | }
|
---|
| 234 | if (node.getLabel() != null) {
|
---|
| 235 | space();
|
---|
| 236 | textTransformed("label: " + node.getLabel(), "");
|
---|
| 237 | }
|
---|
| 238 | textTransformed(";", "");
|
---|
| 239 | // text("\n");
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[37] | 242 | protected void visitBefore(SectionHeading node) {
|
---|
| 243 | if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
| 244 | newLine();
|
---|
| 245 |
|
---|
| 246 | lastWasSectionHeading=true;
|
---|
| 247 |
|
---|
[41] | 248 | metaText("<h3><span lang=EN-GB>\n");
|
---|
| 249 | String text = node.getSectionHeader().replaceAll("[\n\r]", "");
|
---|
| 250 | textTransformed(text, StringEscapeUtils.escapeHtml(text).replaceAll("[\n\r]", ""));
|
---|
| 251 | // textTransformed(text, text.replaceAll("[\t]", "\\<span style='mso-tab-count:1'\\>"));
|
---|
| 252 | metaText("\n</span></h3>\n");
|
---|
[37] | 253 | }
|
---|
| 254 |
|
---|
| 255 | protected void visitAfter(SectionHeading node) {
|
---|
| 256 | }
|
---|
[38] | 257 |
|
---|
| 258 | protected void visitBefore(RuleReference node) {
|
---|
| 259 | wrap();
|
---|
| 260 |
|
---|
[41] | 261 | metaText("<u><span style='color:blue'><a href=\"#T" + node.getRuleref().getName() + "\">");
|
---|
[38] | 262 | text(node.getRuleref().getName());
|
---|
[41] | 263 | metaText("</a></span></u>");
|
---|
[38] | 264 | }
|
---|
[37] | 265 |
|
---|
[38] | 266 | protected void visitAfter(RuleReference node) {
|
---|
[37] | 267 | }
|
---|
[38] | 268 |
|
---|
| 269 | protected void visitBefore(Rule node) {
|
---|
[41] | 270 | // if (lastWasSectionHeading)
|
---|
[38] | 271 | newLine();
|
---|
| 272 |
|
---|
| 273 | lastWasSectionHeading=false;
|
---|
| 274 |
|
---|
| 275 | newLineOffsetCounter = 0;
|
---|
| 276 |
|
---|
| 277 | if (node.getRulenumber() > 0)
|
---|
| 278 | text(node.getRulenumber() + ". ");
|
---|
[41] | 279 |
|
---|
| 280 | metaText("<span lang=EN-GB style='mso-no-proof:no'>\n<a name=\"#T" + node.getName() + "\">");
|
---|
| 281 | textTransformed(node.getName(), node.getName().replaceAll("[ \t\r\n]", ""));
|
---|
[38] | 282 | metaText("</a>");
|
---|
| 283 | text(" ::= ");
|
---|
| 284 |
|
---|
| 285 | rightHandSideRuleOffset = newLineOffsetCounter;
|
---|
| 286 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | protected void visitAfter(Rule node) {
|
---|
[41] | 290 | metaText("<o:p></o:p>\n</span>");
|
---|
[38] | 291 | text(";");
|
---|
| 292 | newLine();
|
---|
[41] | 293 | text("\n");
|
---|
[38] | 294 | ruleSpacingStack.pop();
|
---|
| 295 | }
|
---|
[39] | 296 |
|
---|
| 297 | protected void visitBefore(StringRule node) {
|
---|
| 298 | wrap();
|
---|
| 299 | if (node.getLiteral() != null) {
|
---|
| 300 | // text("\"" + node.getLiteral() + "\"");
|
---|
[41] | 301 | textTransformed("\"" + node.getLiteral() + "\"", StringEscapeUtils.escapeHtml("\"" + node.getLiteral() + "\"").replaceAll("[ \t\n\r]", ""));
|
---|
[39] | 302 | // textTransformed("\"" + node.getLiteral() + "\"", "gnabar");
|
---|
| 303 | } else if (node.getColon() != null) {
|
---|
| 304 | // text("\"\"\"");
|
---|
[41] | 305 | textTransformed("\"\"\"", StringEscapeUtils.escapeHtml("\"\"\"").replaceAll("[ \t\n\r]", ""));
|
---|
[39] | 306 | // textTransformed("\"\"\"", "gnabar");
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | protected void visitAfter(StringRule node) {
|
---|
| 311 | }
|
---|
[34] | 312 | }
|
---|