001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.generator.gsp;
022
023import java.util.List;
024
025import org.granite.generator.gsp.token.Comment;
026import org.granite.generator.gsp.token.Expression;
027import org.granite.generator.gsp.token.Scriplet;
028import org.granite.generator.gsp.token.TemplateText;
029import org.granite.generator.gsp.token.Token;
030
031/**
032 * @author Franck WOLFF
033 */
034public class GroovyRenderer implements Renderer {
035
036    private String source = null;
037
038    @Override
039        public String renderSource(List<Token> tokens) {
040        return renderSource(tokens, null);
041    }
042
043    @Override
044        public String renderSource(List<Token> tokens, String out) {
045        StringBuilder sb = new StringBuilder(1024);
046
047        for (Token token : tokens) {
048            if (token instanceof TemplateText) {
049                String content = token.getContent();
050
051                if (content.length() > 0) {
052                    int iLast = content.length() - 1;
053                    sb.append("print(\"");
054
055                    for (int i = 0; i < content.length(); i++) {
056                        char c = content.charAt(i);
057
058                        switch (c) {
059                        case '\\':
060                            sb.append("\\\\");
061                            break;
062                        case '"' :
063                            sb.append("\\\"");
064                            break;
065                        case '\n':
066                            sb.append("\\n\");\n");
067                            if (i < iLast)
068                                sb.append("print(\"");
069                            break;
070                        case '\f':
071                            sb.append("\\f");
072                            break;
073                        default  :
074                            sb.append(c);
075                            break;
076                        }
077                    }
078
079                    if (content.charAt(iLast) != '\n')
080                        sb.append("\");\n");
081                }
082            }
083            else if (token instanceof Expression)
084                sb.append("print(").append(token.getContent()).append(");\n");
085            else if (token instanceof Scriplet)
086                sb.append(token.getContent()).append('\n');
087            else if (!(token instanceof Comment))
088                throw new UnsupportedOperationException("Unsupported token (not implemented): " + token);
089        }
090
091        source = sb.toString();
092
093        return source;
094    }
095
096    @Override
097        public String getSource() {
098        return source;
099    }
100}