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    
021    package org.granite.generator.gsp;
022    
023    import java.util.List;
024    
025    import org.granite.generator.gsp.token.Comment;
026    import org.granite.generator.gsp.token.Expression;
027    import org.granite.generator.gsp.token.Scriplet;
028    import org.granite.generator.gsp.token.TemplateText;
029    import org.granite.generator.gsp.token.Token;
030    
031    /**
032     * @author Franck WOLFF
033     */
034    public class GroovyRenderer implements Renderer {
035    
036        private String source = null;
037    
038        public String renderSource(List<Token> tokens) {
039            return renderSource(tokens, null);
040        }
041    
042        public String renderSource(List<Token> tokens, String out) {
043            StringBuilder sb = new StringBuilder(1024);
044    
045            for (Token token : tokens) {
046                if (token instanceof TemplateText) {
047                    String content = token.getContent();
048    
049                    if (content.length() > 0) {
050                        int iLast = content.length() - 1;
051                        sb.append("print(\"");
052    
053                        for (int i = 0; i < content.length(); i++) {
054                            char c = content.charAt(i);
055    
056                            switch (c) {
057                            case '\\':
058                                sb.append("\\\\");
059                                break;
060                            case '"' :
061                                sb.append("\\\"");
062                                break;
063                            case '\n':
064                                sb.append("\\n\");\n");
065                                if (i < iLast)
066                                    sb.append("print(\"");
067                                break;
068                            case '\f':
069                                sb.append("\\f");
070                                break;
071                            default  :
072                                sb.append(c);
073                                break;
074                            }
075                        }
076    
077                        if (content.charAt(iLast) != '\n')
078                            sb.append("\");\n");
079                    }
080                }
081                else if (token instanceof Expression)
082                    sb.append("print(").append(token.getContent()).append(");\n");
083                else if (token instanceof Scriplet)
084                    sb.append(token.getContent()).append('\n');
085                else if (!(token instanceof Comment))
086                    throw new UnsupportedOperationException("Unsupported token (not implemented): " + token);
087            }
088    
089            source = sb.toString();
090    
091            return source;
092        }
093    
094        public String getSource() {
095            return source;
096        }
097    }