You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2021/10/21 21:33:23 UTC
[royale-compiler] 04/04: formatter: refactor MXML script formatting
and format MXML metadata
This is an automated email from the ASF dual-hosted git repository.
joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git
commit f24ff86b7b711278a313f1ba76e09fab9a90e4ed
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Oct 21 14:14:49 2021 -0700
formatter: refactor MXML script formatting and format MXML metadata
---
.../org/apache/royale/formatter/FORMATTER.java | 102 +++++++++------
.../royale/formatter/BaseFormatterTests.java | 11 +-
.../apache/royale/formatter/TestMXMLMetadata.java | 137 +++++++++++++++++++++
3 files changed, 211 insertions(+), 39 deletions(-)
diff --git a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
index c4506cf..a55af2f 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -175,6 +175,7 @@ public class FORMATTER {
exitCode = ExitCode.PRINT_HELP;
}
} catch (Exception e) {
+ problems.add(new UnexpectedExceptionProblem(e));
System.err.println(e.getMessage());
exitCode = ExitCode.FAILED_WITH_EXCEPTIONS;
} finally {
@@ -375,6 +376,36 @@ public class FORMATTER {
}
private String formatMXMLTextInternal(String filePath, String text, Collection<ICompilerProblem> problems) {
+ StringBuilder builder = new StringBuilder();
+ Pattern scriptPattern = Pattern.compile(
+ "[\t ]*<((?:mx|fx):(?:Script|Metadata))>\\s*(?:<!\\[CDATA\\[)?(?:.|\\n)*?(?:\\]\\]>)?\\s*<\\/(?:mx|fx):(?:Script|Metadata)>");
+ Matcher scriptMatcher = scriptPattern.matcher(text);
+ if (problems == null) {
+ // we need to know if there were problems because it means that we
+ // need to return the original, unformatted text
+ problems = new ArrayList<ICompilerProblem>();
+ }
+ int lastIndex = 0;
+ while (scriptMatcher.find()) {
+ int start = scriptMatcher.start();
+ int end = scriptMatcher.end();
+ String scriptText = scriptMatcher.group().trim();
+
+ builder.append(text.substring(lastIndex, start));
+ String formattedText = formatMXMLScriptElement(scriptText, problems);
+ if (!ignoreProblems && hasErrors(problems)) {
+ return text;
+ }
+ builder.append(formattedText);
+ lastIndex = end;
+ }
+ if (lastIndex < text.length()) {
+ builder.append(text.substring(lastIndex));
+ }
+ return builder.toString();
+ }
+
+ private String formatMXMLScriptElement(String text, Collection<ICompilerProblem> problems) {
String indent = "\t";
if (insertSpaces) {
indent = "";
@@ -382,57 +413,58 @@ public class FORMATTER {
indent += " ";
}
}
- int lastIndex = 0;
StringBuilder builder = new StringBuilder();
Pattern scriptPattern = Pattern.compile(
- "[\t ]*<((?:mx|fx):Script)>\\s*(?:<!\\[CDATA\\[)?((?:.|\\n)*?)(?:\\]\\]>)?\\s*<\\/(?:mx|fx):Script>");
+ "^<((?:mx|fx):(\\w+))>\\s*(<!\\[CDATA\\[)?((?:.|\\n)*?)(?:\\]\\]>)?\\s*<\\/(?:mx|fx):(?:\\w+)>$");
Matcher scriptMatcher = scriptPattern.matcher(text);
+ if (!scriptMatcher.matches()) {
+ return text;
+ }
if (problems == null) {
// we need to know if there were problems because it means that we
// need to return the original, unformatted text
problems = new ArrayList<ICompilerProblem>();
}
- while (scriptMatcher.find()) {
- int start = scriptMatcher.start();
- int end = scriptMatcher.end();
- String scriptTagText = scriptMatcher.group(1);
- String scriptText = scriptMatcher.group(2);
- String formattedScriptText = formatAS3TextInternal(filePath, scriptText, problems);
- if (!ignoreProblems && hasErrors(problems)) {
- return text;
- }
- if (formattedScriptText.length() > 0) {
- String[] formattedLines = formattedScriptText.split("\n");
- String lineIndent = indent + indent + indent;
- for (int i = 0; i < formattedLines.length; i++) {
- formattedLines[i] = lineIndent + formattedLines[i];
- }
- formattedScriptText = String.join("\n", formattedLines);
+ String scriptTagText = scriptMatcher.group(1);
+ String scriptTagName = scriptMatcher.group(2);
+ String cdataText = scriptMatcher.group(3);
+ String scriptText = scriptMatcher.group(4);
+ boolean requireCdata = cdataText != null || "Script".equals(scriptTagName);
+ String formattedScriptText = formatAS3TextInternal("script.as", scriptText, problems);
+ if (!ignoreProblems && hasErrors(problems)) {
+ return text;
+ }
+ if (formattedScriptText.length() > 0) {
+ String[] formattedLines = formattedScriptText.split("\n");
+ String lineIndent = requireCdata ? (indent + indent + indent) : (indent + indent);
+ for (int i = 0; i < formattedLines.length; i++) {
+ formattedLines[i] = lineIndent + formattedLines[i];
}
- builder.append(text.substring(lastIndex, start));
- builder.append(indent);
- builder.append("<");
- builder.append(scriptTagText);
- builder.append(">\n");
+ formattedScriptText = String.join("\n", formattedLines);
+ }
+ builder.append(indent);
+ builder.append("<");
+ builder.append(scriptTagText);
+ builder.append(">\n");
+ if (requireCdata) {
builder.append(indent);
builder.append(indent);
builder.append("<![CDATA[\n");
- if (formattedScriptText.length() > 0) {
- builder.append(formattedScriptText);
- builder.append("\n");
- }
+ }
+ if (formattedScriptText.length() > 0) {
+ builder.append(formattedScriptText);
+ builder.append("\n");
+ }
+ if (requireCdata) {
builder.append(indent);
builder.append(indent);
builder.append("]]>\n");
- builder.append(indent);
- builder.append("</");
- builder.append(scriptTagText);
- builder.append(">");
- lastIndex = end;
- }
- if (lastIndex < text.length()) {
- builder.append(text.substring(lastIndex));
}
+ builder.append(indent);
+ builder.append("</");
+ builder.append(scriptTagText);
+ builder.append(">");
+
return builder.toString();
}
diff --git a/formatter/src/test/java/org/apache/royale/formatter/BaseFormatterTests.java b/formatter/src/test/java/org/apache/royale/formatter/BaseFormatterTests.java
index acc7792..db1cc7d 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/BaseFormatterTests.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/BaseFormatterTests.java
@@ -24,6 +24,8 @@ import static org.junit.Assert.fail;
import java.util.ArrayList;
+import org.apache.royale.compiler.clients.problems.CompilerProblemCategorizer;
+import org.apache.royale.compiler.problems.CompilerProblemSeverity;
import org.apache.royale.compiler.problems.ICompilerProblem;
import org.junit.After;
import org.junit.Before;
@@ -38,10 +40,11 @@ public class BaseFormatterTests {
@After
public void teardown() {
- int numProblems = problems.size();
- if (numProblems > 0) {
- for (ICompilerProblem problem : problems) {
- fail(problem.toString() + " (" + problem.getLine() +", " + problem.getColumn() + ")");
+ CompilerProblemCategorizer categorizer = new CompilerProblemCategorizer(null);
+ for (ICompilerProblem problem : problems) {
+ CompilerProblemSeverity severity = categorizer.getProblemSeverity(problem);
+ if(CompilerProblemSeverity.ERROR.equals(severity)) {
+ fail(problem.toString() + " (" + problem.getLine() + ", " + problem.getColumn() + ")");
}
}
problems = null;
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestMXMLMetadata.java b/formatter/src/test/java/org/apache/royale/formatter/TestMXMLMetadata.java
new file mode 100644
index 0000000..497f111
--- /dev/null
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestMXMLMetadata.java
@@ -0,0 +1,137 @@
+package org.apache.royale.formatter;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class TestMXMLMetadata extends BaseFormatterTests {
+ @Test
+ public void testEmptyMetadataNoCdata() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Metadata>\n" +
+ "</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Metadata>\n" +
+ "\t</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testEmptyMetadataWithCdata() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Metadata>\n" +
+ "<![CDATA[\n" +
+ "]]>\n" +
+ "</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Metadata>\n" +
+ "\t\t<![CDATA[\n" +
+ "\t\t]]>\n" +
+ "\t</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testSingleMetadata() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Metadata>\n" +
+ "[UnknownMetaTag]\n" +
+ "</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Metadata>\n" +
+ "\t\t[UnknownMetaTag]\n" +
+ "\t</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testMultipleMetadata() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Metadata>\n" +
+ "[UnknownMetaTag1]\n" +
+ "[UnknownMetaTag2]\n" +
+ "</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Metadata>\n" +
+ "\t\t[UnknownMetaTag1]\n" +
+ "\t\t[UnknownMetaTag2]\n" +
+ "\t</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testMultipleMetadataWithAttributes() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Metadata>\n" +
+ "[UnknownMetaTag1(attr1=\"one\", attr2=\"two\")]\n" +
+ "[UnknownMetaTag2(attr1=\"one\")]\n" +
+ "</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Metadata>\n" +
+ "\t\t[UnknownMetaTag1(attr1=\"one\", attr2=\"two\")]\n" +
+ "\t\t[UnknownMetaTag2(attr1=\"one\")]\n" +
+ "\t</fx:Metadata>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+}