Skip to main content
summaryrefslogtreecommitdiffstats
blob: 13e6dd0e991c19ea41ba96d4d0f1a377b1bb8800 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.xpand2.output;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.internal.xpand2.ast.TextStatement;
import org.eclipse.internal.xtend.expression.ast.SyntaxElement;
import org.eclipse.internal.xtend.util.Pair;
import org.eclipse.xpand2.XpandExecutionContext;

/**
 * *
 * 
 * @author Sven Efftinge (http://www.efftinge.de) *
 */
public class OutputImpl implements Output {

	private boolean automaticHyphenation = false;

	public void setAutomaticHyphens(boolean automaticHyphenation) {
		this.automaticHyphenation = automaticHyphenation;
	}

	protected Stack<FileHandleImpl> fileHandles = new Stack<FileHandleImpl>();

	private final Map<String, Outlet> outlets = new HashMap<String, Outlet>();

	public void addOutlet(final Outlet outlet) {
		if (outlets.containsKey(outlet.getName())) {
			if (outlet.getName() == null)
				throw new IllegalArgumentException("A default outlet is already registered!");
			else
				throw new IllegalArgumentException("An outlet with name " + outlet.getName() + " is already registered!");
		}
		outlets.put(outlet.getName(), outlet);
	}

	public Outlet getOutlet (String name) {
	    return outlets.get (name);
	}
	
	protected FileHandle current() {
		return (fileHandles.isEmpty() ? null : fileHandles.peek());
	}
	
	/**
	 * DO NOT CALL THIS METHOD - FOR TESTS ONLY
	 */
	public FileHandle current__testONLY() {
		return current();
	}
	
	

	private boolean deleteLine = false;

	public void write(final String bytes) {
		if (current() != null) {
			if (deleteLine) {
				String temp = trimUntilNewline(bytes);
				removeWSAfterLastNewline(current().getBuffer());
				((StringBuffer) current().getBuffer()).append(temp);
			} else {
			    ((StringBuffer)current().getBuffer()).append(bytes);
			}
		}
		deleteLine = false;
	}

	public void removeWSAfterLastNewline(CharSequence cs) {
	    final StringBuffer buffer = (StringBuffer) cs;
		int i = buffer.length();
		boolean wsOnly = true;
		for (; i > 0 && wsOnly; i--) {
			char c = buffer.charAt(i - 1);
			wsOnly = Character.isWhitespace(c);
			if (wsOnly && isNewLine(c)) {
				buffer.delete(i, buffer.length());
				return;
			}
		}
		return;
	}

	private boolean isNewLine(char c) {
		return c == '\n' || c == '\r';
	}

	public String trimUntilNewline(String bytes) {
		int i = 0;
		boolean wsOnly = true;
		for (; i < bytes.length() && wsOnly; i++) {
			char c = bytes.charAt(i);
			wsOnly = Character.isWhitespace(c);
			if (wsOnly && isNewLine(c)) {
				if (c == '\r' && i + 1 < bytes.length() && bytes.charAt(i + 1) == '\n')
					i++;
				return bytes.substring(i + 1);
			}
		}
		return bytes;
	}

	private final static Pattern p = Pattern.compile("(.+)://(.+)");

	public static Pair<Outlet, String> resolveOutlet (Map<String, Outlet> allOutlets, String path, String outletName) {
        if (outletName == null) {
            final Matcher m = p.matcher(path);
            if (m.matches()) {
                outletName = m.group(1);
                path = m.group(2);
            }
        }
        final Outlet o = allOutlets.get(outletName);
        if (o == null) {
            if (outletName == null)
                throw new IllegalArgumentException ("No default outlet was configured!");
            else
                throw new IllegalArgumentException ("No outlet with the name " + outletName + " could be found!");
        }
        
        return new Pair<Outlet, String> (o, path);
	}
	
	public void openFile(String path, String outletName) {
	    final Pair<Outlet, String> raw = resolveOutlet(outlets, path, outletName);
	    
	    final Outlet actualOutlet = raw.getFirst();
	    final String actualPath = raw.getSecond();
	    
		fileHandles.push (actualOutlet.createFileHandle (actualPath));
	}

	public void closeFile() {
		final FileHandle fi = fileHandles.pop();
		fi.writeAndClose();
	}

	private final Stack<SyntaxElement> s = new Stack<SyntaxElement>();

	public void pushStatement(final SyntaxElement stmt, XpandExecutionContext ctx) {
		if (stmt instanceof TextStatement) {
			deleteLine = ((TextStatement) stmt).isDeleteLine();
			if (automaticHyphenation) {
				deleteLine = true;
			}
		}
		s.push(stmt);
	}

	public SyntaxElement popStatement() {
		final SyntaxElement se = s.pop();
		return se;
	}

}

Back to the top