/*
This file is part of JLoom
Copyright (C) 2006 Gereon Fassbender
Homepage: jloom.sourceforge.net
JLoom is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You can find a copy of the GNU General Public License along with this program
in a file called COPYING. Information can also be found at www.fsf.org or
www.gnu.org or write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
created: 03.03.2006 Gereon Fassbender
$Revision$
$Date$
$Log$
*/
package net.gereon.jloom.semantics;
import java.io.*;
import net.gereon.jloom.core.*;
import net.gereon.jloom.syntax.Scriptlet;
import net.gereon.jloom.translation.translator.SimpleTranslationMapping;
import net.gereon.jloom.util.*;
public class ScriptletSemantic implements Semantic
{
public void translate(TranslationContext tc, GenerationContext gc, Scriptlet scriptlet) throws IOException
{
String s = scriptlet.getScriptletPart().getText(); //.trim();
if (s.equals("")) { return; }
int start = scriptlet.getScriptletPart().getInputRange().getStart();
CharArrayWriter w = (CharArrayWriter) gc.getOut();
SimpleTranslationMapping tm = (SimpleTranslationMapping) tc.getTranslationMapping();
LinedString l = new LinedString(s);
int firstLine = 0;
int lastLine = l.getLineCount() - 1;
if (l.getLine(firstLine).trim().equals("")) {
firstLine++;
}
if (l.getLine(lastLine).trim().equals("")) {
lastLine--;
}
if (firstLine > lastLine) {
return;
}
String[] lines = l.getLinesArray(firstLine, lastLine);
int ws = TranslationTools.getCommonLeadingWhitespace(lines).length();
for (int i=firstLine; i<=lastLine; i++) {
GenerationTools.print(gc, ""); // for indention
String line = l.getLine(i).substring(ws);
int templatePos = start + l.getPosition(i) + ws;
int generatorPos = w.size();
tm.addCheckpoint(templatePos, generatorPos, true);
GenerationTools.println(gc, line);
}
}
}