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.transformer.library.tag;
020
021import java.util.Iterator;
022
023import lucee.commons.lang.StringUtil;
024
025public final class TagLibTagScript {
026
027        public static final short TYPE_NONE = 0;
028        public static final short TYPE_SINGLE = 1;
029        public static final short TYPE_MULTIPLE = 2;
030        
031        public static final short CTX_OTHER = -1;
032        public static final short CTX_NONE = 0;
033        public static final short CTX_IF = 1;
034        public static final short CTX_ELSE_IF = 2;
035        public static final short CTX_ELSE = 3;
036        public static final short CTX_FOR = 4;
037        public static final short CTX_WHILE = 5;
038        public static final short CTX_DO_WHILE = 6;
039        public static final short CTX_CFC = 7;
040        public static final short CTX_INTERFACE = 8;
041        public static final short CTX_FUNCTION = 9;
042        public static final short CTX_BLOCK = 10;
043        public static final short CTX_FINALLY = 11;
044        public static final short CTX_SWITCH = 12;
045        public static final short CTX_TRY = 13;
046        public static final short CTX_CATCH = 14;
047        public static final short CTX_TRANSACTION = 15;
048        public static final short CTX_THREAD = 16;
049        public static final short CTX_SAVECONTENT = 17;
050        public static final short CTX_LOCK = 18;
051        public static final short CTX_LOOP = 19;
052        public static final short CTX_QUERY = 20;
053        public static final short CTX_ZIP = 21;
054        
055        
056        
057
058        private final static TagLibTagAttr UNDEFINED=new TagLibTagAttr(null);
059        
060        private TagLibTag tag;
061        private boolean rtexpr;
062        private short type=TYPE_NONE;
063        private TagLibTagAttr singleAttr=UNDEFINED;
064        private short context=CTX_OTHER;
065
066
067        public TagLibTagScript(TagLibTag tag) {
068                this.tag=tag;
069        }
070
071        public void setType(String type) {
072                if(!StringUtil.isEmpty(type,true))  {
073                        type=type.trim().toLowerCase();
074                        if("single".equals(type)) this.type=TYPE_SINGLE;
075                        else if("multiple".equals(type)) this.type=TYPE_MULTIPLE;
076                }
077        }
078
079        public void setRtexpr(boolean rtexpr) {
080                this.rtexpr=rtexpr;
081        }
082
083        /**
084         * @return the tag
085         */
086        public TagLibTag getTag() {
087                return tag;
088        }
089
090        /**
091         * @return the rtexpr
092         */
093        public boolean getRtexpr() {
094                return rtexpr;
095        }
096
097        /**
098         * @return the type
099         */
100        public short getType() {
101                return type;
102        }
103
104
105        public String getTypeAsString() {
106                if(type==TYPE_MULTIPLE) return "multiple";
107                if(type==TYPE_SINGLE) return "single";
108                return "none";
109        }
110        
111        public TagLibTagAttr getSingleAttr() {
112                if(singleAttr==UNDEFINED) {
113                        singleAttr=null;
114                        Iterator<TagLibTagAttr> it = tag.getAttributes().values().iterator();
115                        TagLibTagAttr attr;
116                        while(it.hasNext()){
117                                attr=it.next();
118                                if(attr.getScriptSupport()!=TagLibTagAttr.SCRIPT_SUPPORT_NONE){
119                                        singleAttr=attr;
120                                        break;
121                                }       
122                        }
123                }
124                return singleAttr;
125        }
126
127        public void setContext(String str) {
128                if(!StringUtil.isEmpty(str,true))  {
129                        str=str.trim().toLowerCase();
130                        if("none".equals(str)) this.context=CTX_NONE;
131                        else if("if".equals(str)) this.context=CTX_IF;
132                        else if("elseif".equals(str)) this.context=CTX_ELSE_IF;
133                        else if("else".equals(str)) this.context=CTX_ELSE;
134                        else if("for".equals(str)) this.context=CTX_FOR;
135                        else if("while".equals(str)) this.context=CTX_WHILE;
136                        else if("dowhile".equals(str)) this.context=CTX_DO_WHILE;
137                        else if("cfc".equals(str)) this.context=CTX_CFC;
138                        else if("component".equals(str)) this.context=CTX_CFC;
139                        else if("interface".equals(str)) this.context=CTX_INTERFACE;
140                        else if("function".equals(str)) this.context=CTX_FUNCTION;
141                        else if("block".equals(str)) this.context=CTX_BLOCK;
142                        else if("finally".equals(str)) this.context=CTX_FINALLY;
143                        else if("switch".equals(str)) this.context=CTX_SWITCH;
144                        else if("try".equals(str)) this.context=CTX_TRY;
145                        else if("catch".equals(str)) this.context=CTX_CATCH;
146                        else if("transaction".equals(str)) this.context=CTX_TRANSACTION;
147                        else if("thread".equals(str)) this.context=CTX_THREAD;
148                        else if("savecontent".equals(str)) this.context=CTX_SAVECONTENT;
149                        else if("lock".equals(str)) this.context=CTX_LOCK;
150                        else if("loop".equals(str)) this.context=CTX_LOOP;
151                        else if("query".equals(str)) this.context=CTX_QUERY;
152                        else if("zip".equals(str)) this.context=CTX_ZIP;
153                }
154        }
155        
156        /**
157         * @return the context
158         */
159        public short getContext() {
160                return context;
161        }
162
163        
164}