blob: 3118bbd516747cb13865ffb53d78f4e8be2dc05a [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* 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:
* Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.codegen;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.eclipse.viatra2.framework.IFramework;
import org.eclipse.viatra2.logger.Logger;
/**
* @author Andras Balogh
*
*/
public class FileCodeFormatter implements CodeOutputPlugin {
Logger log;
PrintStream codeout = System.out;
boolean inFile = false;
public void init(IFramework fw) {
log = fw.getLogger();
log.debug("File-based code formatter loaded.");
String s = fw.getProperties().getRuntimeProperty("codeout",
"file.autolinefeed");
if (s != null) {
printLN = s.toLowerCase().equals("yes");
}
}
boolean printLN = true;
/*
* (non-Javadoc)
*
* @see org.eclipse.viatra2.codegen.CodeOutputPlugin#beginWork()
*/
public void beginWork() {
// TODO Auto-generated method stub
inFile = false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.viatra2.codegen.CodeOutputPlugin#endWork()
*/
public void endWork() {
if (inFile) {
codeout.flush();
codeout.close();
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.viatra2.codegen.CodeOutputPlugin#codeOut(java.lang.String)
*/
public void codeOut(String s) {
// TODO Auto-generated method stub
if (inFile) {
if (s.startsWith("//!!ENDFILE")) {
inFile = false;
codeout.close();
} else {
if (printLN)
codeout.println(s);
else
codeout.print(s.replaceAll("\\\\n", "\n"));
}
} else {
if (s.startsWith("//!!FILE=")) {
String s2 = s.substring(9);
try {
codeout = new PrintStream(new FileOutputStream(s2, false),
true);
inFile = true;
} catch (Exception e) {
log.error("File " + s2
+ " cannot be created for code output. Reason: "
+ e.getMessage());
}
}
}
}
}