001    package railo.runtime.tag;
002    
003    import javax.servlet.jsp.tagext.Tag;
004    
005    import org.apache.oro.text.regex.MalformedPatternException;
006    
007    import railo.commons.io.res.Resource;
008    import railo.commons.io.res.util.ResourceUtil;
009    import railo.commons.io.res.util.WildCardFilter;
010    import railo.runtime.exp.ApplicationException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.ext.tag.TagImpl;
013    import railo.runtime.op.Caster;
014    
015    public final class ZipParam extends TagImpl {
016            
017            private String charset;
018            private Object content;
019            private String entryPath;
020            private String filter;
021            private String prefix;
022            private railo.commons.io.res.Resource source;
023            private Boolean recurse=null;
024            private Zip zip;
025            
026    
027    
028            /**
029            * @see javax.servlet.jsp.tagext.Tag#release()
030            */
031            public void release()   {
032                    super.release();
033                    charset=null;
034                    content=null;
035                    entryPath=null;
036                    filter=null;
037                    prefix=null;
038                    source=null;
039                    recurse=null;
040            }
041            
042            
043            /**
044             * @param charset the charset to set
045             */
046            public void setCharset(String charset) {
047                    this.charset=charset;
048            }
049    
050            /**
051             * @param content the content to set
052             */
053            public void setContent(Object content) {
054                    this.content=content;
055            }
056    
057            /**
058             * @param entryPath the entryPath to set
059             */
060            public void setEntrypath(String entryPath) {
061                    this.entryPath=entryPath;
062            }
063    
064            /**
065             * @param filter the filter to set
066             */
067            public void setFilter(String filter) {
068                    this.filter=filter;
069            }
070    
071            /**
072             * @param prefix the prefix to set
073             */
074            public void setPrefix(String prefix) {
075                    this.prefix=prefix;
076            }
077    
078            /**
079             * @param strSource the source to set
080             * @throws PageException 
081             */
082            public void setSource(String strSource) throws PageException {
083                    Resource zipSrc = getZip().getSource();
084                    if(zipSrc!=null)source=zipSrc.getRealResource(strSource);
085                    if(source==null || !source.exists())
086                            source=ResourceUtil.toResourceExisting(pageContext, strSource);
087            }
088    
089            /**
090             * @param recurse the recurse to set
091             */
092            public void setRecurse(boolean recurse) {
093                    this.recurse=Caster.toBoolean(recurse);
094            }
095    
096            /**
097             * @see javax.servlet.jsp.tagext.Tag#doStartTag()
098            */
099            public int doStartTag() throws PageException    {
100                    
101                    if(source!=null) {
102                            notAllowed("source","charset", charset);
103                            notAllowed("source","content", content);
104                            try {
105                                    WildCardFilter f = filter==null?null:new WildCardFilter(filter);
106                                    getZip().setParam(new ZipParamSource(source,entryPath,f,prefix,recurse()));
107                            } catch (MalformedPatternException e) {
108                                    throw Caster.toPageException(e);
109                            }
110                    }
111                    else if(content!=null) {
112                            required("content","entrypath",entryPath);
113                            notAllowed("content,entrypath","filter", filter);
114                            notAllowed("content,entrypath","prefix", prefix);
115                            notAllowed("content,entrypath","source", source);
116                            notAllowed("content,entrypath","recurse", recurse);
117                            
118                            getZip().setParam(new ZipParamContent(content,entryPath,charset));
119                    }
120                    /*else if(filter!=null) {
121                            notAllowed("filter","charset", charset);
122                            notAllowed("filter","content", content);
123                            notAllowed("filter","prefix", prefix);
124                            notAllowed("filter","source", source);
125                            getZip().setParam(new ZipParamFilter(filter,entryPath,recurse()));
126                    }
127                    else if(entryPath!=null) {
128                            notAllowed("entryPath","charset", charset);
129                            notAllowed("entryPath","content", content);
130                            notAllowed("entryPath","prefix", prefix);
131                            notAllowed("entryPath","source", source);
132                            getZip().setParam(new ZipParamFilter(filter,entryPath,recurse()));
133                    }*/
134                    else 
135                            throw new ApplicationException("invalid attribute combination");
136                            
137    
138                    
139                    
140                    return SKIP_BODY;
141            }
142    
143            private boolean recurse() {
144                    return recurse==null?true:recurse.booleanValue();
145            }
146    
147    
148            private Zip getZip() throws ApplicationException {
149                    if(zip!=null) return zip;
150                    Tag parent=getParent();
151                    while(parent!=null && !(parent instanceof Zip)) {
152                            parent=parent.getParent();
153                    }
154                    
155                    if(parent instanceof Zip) {
156                            return zip=(Zip)parent;
157                    }
158                    throw new ApplicationException("Wrong Context, tag ZipParam must be inside a Zip tag"); 
159            }
160    
161    
162            private void notAllowed(String combi, String name, Object value) throws ApplicationException {
163                    if(value!=null)
164                            throw new ApplicationException("attribute ["+name+"] is not allowed in combination with attribute(s) ["+combi+"]");     
165            }
166            public void required(String combi, String name, Object value) throws ApplicationException {
167                    if(value==null)
168                            throw new ApplicationException("attribute ["+name+"] is required in combination with attribute(s) ["+combi+"]");        
169            }
170    
171            /**
172            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
173            */
174            public int doEndTag()   {
175                    return EVAL_PAGE;
176            }
177    }