Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoratikhomirov2006-11-02 17:35:04 +0000
committeratikhomirov2006-11-02 17:35:04 +0000
commit0d0823451bca9fea936ffa6a2654638d977f77b3 (patch)
tree982bf834a859ee78527c7c0992f16bd51fae2571
parentc528ee568808998e7e00829e83ab3295559ba44c (diff)
downloadorg.eclipse.gmf-tooling-0d0823451bca9fea936ffa6a2654638d977f77b3.tar.gz
org.eclipse.gmf-tooling-0d0823451bca9fea936ffa6a2654638d977f77b3.tar.xz
org.eclipse.gmf-tooling-0d0823451bca9fea936ffa6a2654638d977f77b3.zip
buffer-based output implementation
-rw-r--r--plugins/org.eclipse.gmf.xpand/src/org/eclipse/gmf/internal/xpand/BufferOutput.java81
1 files changed, 81 insertions, 0 deletions
diff --git a/plugins/org.eclipse.gmf.xpand/src/org/eclipse/gmf/internal/xpand/BufferOutput.java b/plugins/org.eclipse.gmf.xpand/src/org/eclipse/gmf/internal/xpand/BufferOutput.java
new file mode 100644
index 000000000..a21a819f2
--- /dev/null
+++ b/plugins/org.eclipse.gmf.xpand/src/org/eclipse/gmf/internal/xpand/BufferOutput.java
@@ -0,0 +1,81 @@
+package org.eclipse.gmf.internal.xpand;
+
+import java.util.Map;
+import java.util.Stack;
+
+import org.eclipse.gmf.internal.xpand.ast.TextStatement;
+import org.eclipse.gmf.internal.xpand.expression.ast.SyntaxElement;
+import org.eclipse.gmf.internal.xpand.model.Output;
+
+public class BufferOutput implements Output {
+ /**
+ * INV: size > 0
+ */
+ private final Stack<StringBuilder> outletStack;
+
+ private boolean deleteLine = false;
+
+ private final Map<String, StringBuilder> namedSlots;
+
+ // XXX not map but config to show whether to append/overwrite content
+ public BufferOutput(StringBuilder buffer, Map<String, StringBuilder> namedSlots) {
+ assert buffer != null;
+ assert namedSlots != null;
+ outletStack = new Stack<StringBuilder>();
+ outletStack.push(buffer);
+ this.namedSlots = namedSlots;
+ }
+
+ public void closeFile() {
+ if (outletStack.size() == 1) {
+ String msg = "CLOSE FILE without previous openFile";
+ System.err.println("<<<" + msg);
+ throw new UnsupportedOperationException(msg);
+ }
+ outletStack.pop();
+ }
+
+ public void openFile(String path, String outletName) {
+ if (!namedSlots.containsKey(outletName)) {
+ String msg = "OPEN FILE ('" + path + "', " + outletName + ")";
+ System.err.println(">>>" + msg);
+ throw new UnsupportedOperationException(msg);
+ }
+ outletStack.push(namedSlots.get(outletName));
+ assert outletStack.peek() != null;
+ }
+
+ public void enterStatement(SyntaxElement stmt) {
+ if (stmt instanceof TextStatement) {
+ deleteLine = ((TextStatement) stmt).isDeleteLine();
+ }
+ }
+
+ public void exitStatement(SyntaxElement stmt) {
+ deleteLine = false;
+ }
+
+ public void write(String text) {
+ if (deleteLine) {
+ int i = 0;
+ while (i < text.length()) {
+ char charAt = text.charAt(i);
+ if (Character.isWhitespace(charAt)) {
+ if (charAt == '\r' || charAt == '\n') {
+ char nextToLF = text.charAt(++i);
+ if (nextToLF != charAt && (nextToLF == '\n' || nextToLF == '\r')) {
+ i++;
+ }
+ break;
+ }
+ }
+ i++;
+ }
+ outletStack.peek().append(text.substring(i));
+ deleteLine = false;
+ } else {
+ outletStack.peek().append(text);
+ }
+ }
+
+}

Back to the top