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