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 javax.servlet.jsp.tagext.Tag;
022
023import lucee.commons.io.res.Resource;
024import lucee.commons.io.res.filter.ResourceFilter;
025import lucee.commons.io.res.util.ResourceUtil;
026import lucee.commons.io.res.util.UDFFilter;
027import lucee.commons.io.res.util.WildcardPatternFilter;
028import lucee.commons.lang.StringUtil;
029import lucee.runtime.exp.ApplicationException;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.ext.tag.TagImpl;
032import lucee.runtime.op.Caster;
033import lucee.runtime.type.UDF;
034
035public final class ZipParam extends TagImpl {
036        
037        private String charset;
038        private Object content;
039        private String entryPath;
040        private ResourceFilter filter;
041        private String pattern;
042        private String patternDelimiters;
043        private String prefix;
044        private lucee.commons.io.res.Resource source;
045        private Boolean recurse=null;
046        private Zip zip;
047        
048
049
050        @Override
051        public void release()   {
052                super.release();
053                charset=null;
054                content=null;
055                entryPath=null;
056                filter=null;
057                prefix=null;
058                source=null;
059                recurse=null;
060                zip=null;
061                pattern = null;
062                patternDelimiters = null;
063        }
064        
065        
066        /**
067         * @param charset the charset to set
068         */
069        public void setCharset(String charset) {
070                this.charset=charset;
071        }
072
073        /**
074         * @param content the content to set
075         */
076        public void setContent(Object content) {
077                this.content=content;
078        }
079
080        /**
081         * @param entryPath the entryPath to set
082         */
083        public void setEntrypath(String entryPath) {
084                this.entryPath=entryPath;
085        }
086
087        /**
088         * @param filter the filter to set
089         */
090        public void setFilter(Object filter) throws PageException {
091
092                if (filter instanceof UDF)
093                        this.setFilter((UDF)filter);
094                else if (filter instanceof String)
095                        this.setFilter((String)filter);
096        }
097
098        public void setFilter(UDF filter) throws PageException  {
099
100                this.filter = UDFFilter.createResourceAndResourceNameFilter(filter);
101        }
102
103        public void setFilter(String pattern) {
104
105                this.pattern = pattern;
106        }
107
108        public void setFilterdelimiters(String patternDelimiters) {
109
110                this.patternDelimiters = patternDelimiters;
111        }
112
113        /**
114         * @param prefix the prefix to set
115         */
116        public void setPrefix(String prefix) {
117                this.prefix=prefix;
118        }
119
120        /**
121         * @param strSource the source to set
122         * @throws PageException 
123         */
124        public void setSource(String strSource) throws PageException {
125                Resource zipSrc = getZip().getSource();
126                if(zipSrc!=null)source=zipSrc.getRealResource(strSource);
127                if(source==null || !source.exists())
128                        source=ResourceUtil.toResourceExisting(pageContext, strSource);
129        }
130
131        /**
132         * @param recurse the recurse to set
133         */
134        public void setRecurse(boolean recurse) {
135                this.recurse=Caster.toBoolean(recurse);
136        }
137
138        @Override
139        public int doStartTag() throws PageException    {
140
141                if (this.filter == null && !StringUtil.isEmpty(this.pattern))
142                        this.filter = new WildcardPatternFilter(pattern, patternDelimiters);
143                
144                if(source!=null) {
145                        notAllowed("source","charset", charset);
146                        notAllowed("source","content", content);
147                
148                        getZip().setParam( new ZipParamSource( source, entryPath, filter, prefix, recurse() ) );
149                }
150                else if(content!=null) {
151                        required("content","entrypath",entryPath);
152                        notAllowed("content,entrypath","filter", filter);
153                        notAllowed("content,entrypath","prefix", prefix);
154                        notAllowed("content,entrypath","source", source);
155                        notAllowed("content,entrypath","recurse", recurse);
156                        
157                        getZip().setParam(new ZipParamContent(content,entryPath,charset));
158                }
159                /*else if(filter!=null) {
160                        notAllowed("filter","charset", charset);
161                        notAllowed("filter","content", content);
162                        notAllowed("filter","prefix", prefix);
163                        notAllowed("filter","source", source);
164                        getZip().setParam(new ZipParamFilter(filter,entryPath,recurse()));
165                }
166                else if(entryPath!=null) {
167                        notAllowed("entryPath","charset", charset);
168                        notAllowed("entryPath","content", content);
169                        notAllowed("entryPath","prefix", prefix);
170                        notAllowed("entryPath","source", source);
171                        getZip().setParam(new ZipParamFilter(filter,entryPath,recurse()));
172                }*/
173                else 
174                        throw new ApplicationException("invalid attribute combination");
175                        
176
177                
178                
179                return SKIP_BODY;
180        }
181
182        private boolean recurse() {
183                return recurse==null?true:recurse.booleanValue();
184        }
185
186
187        private Zip getZip() throws ApplicationException {
188                if(zip!=null) return zip;
189                Tag parent=getParent();
190                while(parent!=null && !(parent instanceof Zip)) {
191                        parent=parent.getParent();
192                }
193                
194                if(parent instanceof Zip) {
195                        return zip=(Zip)parent;
196                }
197                throw new ApplicationException("Wrong Context, tag ZipParam must be inside a Zip tag"); 
198        }
199
200
201        private void notAllowed(String combi, String name, Object value) throws ApplicationException {
202                if(value!=null)
203                        throw new ApplicationException("attribute ["+name+"] is not allowed in combination with attribute(s) ["+combi+"]");     
204        }
205        public void required(String combi, String name, Object value) throws ApplicationException {
206                if(value==null)
207                        throw new ApplicationException("attribute ["+name+"] is required in combination with attribute(s) ["+combi+"]");        
208        }
209
210        @Override
211        public int doEndTag()   {
212                return EVAL_PAGE;
213        }
214}