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.tag;
020
021import java.io.IOException;
022import java.util.Iterator;
023import java.util.Map.Entry;
024
025import javax.servlet.jsp.JspException;
026import javax.servlet.jsp.tagext.BodyContent;
027import javax.servlet.jsp.tagext.BodyTag;
028
029import lucee.commons.lang.StringUtil;
030import lucee.runtime.exp.ApplicationException;
031import lucee.runtime.exp.ExpressionException;
032import lucee.runtime.exp.PageException;
033import lucee.runtime.op.Caster;
034import lucee.runtime.type.Collection.Key;
035import lucee.runtime.type.util.KeyConstants;
036
037// TODO tag textarea
038// attribute html macht irgendwie keinen sinn, aber auch unter neo nicht
039
040
041
042public final class Textarea extends Input  implements BodyTag {
043        private static final String BASE_PATH = null; // TODO
044        private static final String STYLE_XML = null;
045        private static final String TEMPLATE_XML = null;
046        private static final String SKIN = "default";
047        private static final String TOOLBAR = "default";
048        
049        private static final int WRAP_OFF = 0;
050        private static final int WRAP_HARD = 1;
051        private static final int WRAP_SOFT = 2;
052        private static final int WRAP_PHYSICAL = 3;
053        private static final int WRAP_VIRTUAL = 4;
054        
055        private BodyContent bodyContent=null;
056
057        private String basepath=BASE_PATH;
058        private String fontFormats=null;
059        private String fontNames=null;
060        private String fontSizes=null;
061        
062        private boolean html=false;
063        private boolean richText=false;
064        private String skin=SKIN;
065        private String stylesXML=STYLE_XML;
066        private String templatesXML=TEMPLATE_XML;
067        private String toolbar=TOOLBAR;
068        private boolean toolbarOnFocus=false;
069        private int wrap=WRAP_OFF;
070        
071        @Override
072        public void release() {
073                super.release();
074                bodyContent=null;
075                
076
077                basepath=BASE_PATH;
078                fontFormats=null;
079                fontNames=null;
080                fontSizes=null;
081                
082                html=false;
083                richText=false;
084                skin=SKIN;
085                stylesXML=STYLE_XML;
086                templatesXML=TEMPLATE_XML;
087                toolbar=TOOLBAR;
088                toolbarOnFocus=false;
089                wrap=WRAP_OFF;
090        }
091
092        public void setCols(double cols) throws PageException {
093                attributes.set("cols", Caster.toString(cols));
094        }
095        public void setRows(double rows) throws PageException {
096                attributes.set("rows", Caster.toString(rows));
097        }
098        public void setBasepath(String basepath) {
099                this.basepath=basepath;
100        }
101        public void setFontFormats(String fontFormats) {
102                this.fontFormats=fontFormats;
103        }
104        public void setFontNames(String fontNames) {
105                this.fontNames=fontNames;
106        }
107        public void setFontSizes(String fontSizes) {
108                this.fontSizes=fontSizes;
109        }
110        public void setHtml(boolean html) {
111                this.html=html;
112        }
113        public void setRichtext(boolean richText) {
114                this.richText = richText;
115        }
116        public void setSkin(String skin) {
117                this.skin = skin;
118        }
119        public void setStylesxml(String stylesXML) {
120                this.stylesXML = stylesXML;
121        }
122        public void setTemplatesxml(String templatesXML) {
123                this.templatesXML = templatesXML;
124        }
125        public void setToolbar(String toolbar) {
126                this.toolbar = toolbar;
127        }
128        public void setToolbaronfocus(boolean toolbarOnFocus) {
129                this.toolbarOnFocus = toolbarOnFocus;
130        }
131        public void setWrap(String strWrap) throws ExpressionException {
132                strWrap=strWrap.trim().toLowerCase();
133                if("hard".equals(strWrap))                      wrap=WRAP_HARD;
134                else if("soft".equals(strWrap))         wrap=WRAP_SOFT;
135                else if("off".equals(strWrap))          wrap=WRAP_OFF;
136                else if("physical".equals(strWrap))     wrap=WRAP_PHYSICAL;
137                else if("virtual".equals(strWrap))      wrap=WRAP_VIRTUAL;
138                else throw new ExpressionException("invalid value ["+strWrap+"] for attribute wrap, valid values are [hard,soft,off,physical,virtual]");                
139        }
140
141        @Override
142        void draw() throws IOException, PageException {
143                
144                // value
145                String attrValue=null;
146                String bodyValue=null;
147                String value="";
148                if(bodyContent!=null)bodyValue=bodyContent.getString();
149                if(attributes.containsKey(KeyConstants._value))attrValue=Caster.toString(attributes.get(KeyConstants._value,null));
150                
151                // check values
152        if(!StringUtil.isEmpty(bodyValue) && !StringUtil.isEmpty(attrValue)) {
153                throw new ApplicationException("the value of tag can't be set twice (tag body and attribute value)");
154        }
155        else if(!StringUtil.isEmpty(bodyValue)){
156                value=enc(bodyValue);
157        }
158        else if(!StringUtil.isEmpty(attrValue)){
159                value=enc(attrValue);
160        }
161        // id
162                if(StringUtil.isEmpty(attributes.get(KeyConstants._id,null)))
163                        attributes.set(KeyConstants._id,StringUtil.toVariableName((String)attributes.get(KeyConstants._name)));
164                
165                // start output
166        pageContext.forceWrite("<textarea");
167        
168        Iterator<Entry<Key, Object>> it = attributes.entryIterator();
169        Entry<Key, Object> e;
170        while(it.hasNext()) {
171            e = it.next();
172            pageContext.forceWrite(" ");
173            pageContext.forceWrite(e.getKey().getString());
174            pageContext.forceWrite("=\"");
175            pageContext.forceWrite(enc(Caster.toString(e.getValue())));
176            pageContext.forceWrite("\"");
177        }
178        
179        if(passthrough!=null) {
180            pageContext.forceWrite(" ");
181            pageContext.forceWrite(passthrough);
182        }
183        pageContext.forceWrite(">");
184        pageContext.forceWrite(value);
185        pageContext.forceWrite("</textarea>");
186        }
187
188        @Override
189        public int doStartTag() {
190                return EVAL_BODY_BUFFERED;
191        }
192        
193        @Override
194        public void setBodyContent(BodyContent bodyContent) {
195                this.bodyContent=bodyContent;
196        }
197
198        @Override
199        public void doInitBody() throws JspException {}
200
201        @Override
202        public int doAfterBody() throws JspException {
203                return SKIP_BODY;
204        }
205        public void hasBody(boolean hasBody) {
206            
207        }
208}