001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.interpreter.ref.literal;
020
021import java.util.ArrayList;
022import java.util.Iterator;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.exp.PageException;
026import lucee.runtime.interpreter.ref.Ref;
027import lucee.runtime.interpreter.ref.RefSupport;
028import lucee.runtime.interpreter.ref.util.RefUtil;
029import lucee.runtime.op.Caster;
030
031/**
032 * Literal String
033 *
034 */
035public final class LStringBuffer extends RefSupport implements Literal {
036    
037    private ArrayList refs=new ArrayList();
038    private StringBuffer sb=new StringBuffer();
039
040    /**
041     * constructor of the class
042     * @param str 
043     */
044    public LStringBuffer(String str) {
045        sb.append(str);
046    }
047    /**
048     * constructor of the class
049     * @param str 
050     */
051    public LStringBuffer() {
052    }
053    
054
055    @Override
056        public Object getValue(PageContext pc) throws PageException {
057        if(refs.size()==0) return sb.toString();
058        
059        StringBuffer tmp=new StringBuffer();
060        Iterator it = refs.iterator();
061        while(it.hasNext()) {
062            tmp.append(Caster.toString(((Ref)it.next()).getValue(pc)));
063        }
064        if(sb.length()>0)tmp.append(sb);
065        
066        
067        return tmp.toString();
068    } 
069    
070    public void append(Ref ref) {
071        if(sb.length()>0) {
072            refs.add(new LString(sb.toString()));
073            sb=new StringBuffer();
074        }
075        refs.add(ref);
076    }
077
078    public void append(char c) {
079        sb.append(c);
080    }
081    public void append(String str) {
082        sb.append(str);
083    }
084    
085    public boolean isEmpty() {
086        return sb.length()+refs.size()==0;
087    }
088
089    @Override
090    public String getTypeName() {
091        return "literal";
092    }
093    
094    @Override
095    public String getString(PageContext pc) throws PageException {
096        return (String) getValue(pc);
097    }
098
099    @Override
100    public boolean eeq(PageContext pc,Ref other) throws PageException {
101                return RefUtil.eeq(pc,this,other);
102        }
103}