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.commons.io.res.type.s3;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.ArrayList;
024import java.util.List;
025
026import lucee.runtime.exp.PageException;
027import lucee.runtime.op.Caster;
028
029import org.xml.sax.Attributes;
030import org.xml.sax.SAXException;
031
032public final class ContentFactory extends S3Factory {
033
034        private boolean insideContents=false;
035        private boolean insideOwners=false;
036
037        private Content content; 
038        private List contents=new ArrayList();
039        private final S3 s3;
040        private String bucketName;
041        private boolean isTruncated; 
042
043
044        /**
045         * @param saxParser String Klassenpfad zum Sax Parser.
046         * @param file File Objekt auf die TLD.
047         * @throws IOException 
048         * @throws SAXException 
049         */
050        public ContentFactory(InputStream in,S3 s3) throws IOException, SAXException {
051                super();
052                this.s3=s3;
053                init(in);
054        }
055
056        public void doStartElement(String uri, String name, String qName, Attributes atts) {
057                if(qName.equals("Contents")) startContents();
058                if(qName.equals("Owner")) insideOwners=true;
059                
060        }
061    
062        public void doEndElement(String uri, String name, String qName) throws SAXException {
063                if(qName.equals("Contents")) endContents();
064                if(qName.equals("Owner")) insideOwners=false;
065                
066        }
067        
068        
069        protected void setContent(String value) throws SAXException     {
070                if(insideContents)      {
071                        if(insideOwners){
072                                if(inside.equals("ID"))                                         content.setOwnerIdKey(value);
073                                else if(inside.equals("DisplayName"))           content.setOwnerDisplayName(value);
074                                
075                        }
076                        else {
077                                try {
078                                        if(inside.equals("Key"))                                {
079                                                content.setKey(value);
080                                        }
081                                        else if(inside.equals("LastModified"))  content.setLastModified(
082                                                        S3.toDate(value,s3.getTimeZone()).getTime());
083                                        else if(inside.equals("ETag"))                  content.setETag(value); // MUST HTML Encoder
084                                        else if(inside.equals("Size"))                  content.setSize(Caster.toLongValue(value,0L));
085                                        else if(inside.equals("StorageClass"))  content.setStorageClass(value);
086                                } 
087                                catch (PageException e) {
088                                        throw new SAXException(e.getMessage());
089                                }
090                        }
091                        
092        }
093                else {
094                        if(inside.equals("Name"))                               bucketName=value;
095                        else if(inside.equals("IsTruncated"))           isTruncated=Caster.toBooleanValue(value,false);
096                        
097                }
098                
099    }   
100        /*
101
102
103        <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
104                <Contents>
105                        <Owner>
106                                <ID>5f8fc1a3e7f09e6e1bf5f7d7d0dfadfd0efc2e0afbf7789836f3ab67b2dddfdf</ID>
107                                <DisplayName>michael.streit</DisplayName>
108                        </Owner>
109                        <StorageClass>STANDARD</StorageClass>
110                        </Contents>
111                        <Contents>
112                                <Key>susi2</Key>
113                                <LastModified>2008-03-05T14:10:43.000Z</LastModified>
114                                <ETag>&quot;f84bcc91f8fd1321a251311c7e9fbfce&quot;</ETag><Size>156169</Size><Owner><ID>5f8fc1a3e7f09e6e1bf5f7d7d0dfadfd0efc2e0afbf7789836f3ab67b2dddfdf</ID><DisplayName>michael.streit</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>
115
116         */
117        
118        
119        
120        /**
121         * Wird jedesmal wenn das Tag attribute beginnt aufgerufen, um intern in einen anderen Zustand zu gelangen.
122         */
123        private void startContents()    {
124        insideContents=true;
125        content=new Content(s3); 
126    }
127        
128        
129        /**
130         * Wird jedesmal wenn das Tag tag endet aufgerufen, um intern in einen anderen Zustand zu gelangen.
131         */
132        private void endContents()      {
133                content.setBucketName(bucketName);
134                content.setTruncated(isTruncated);
135                contents.add(content);
136        insideContents=false;
137    }
138
139        public Content[] getContents() {
140                return (Content[]) contents.toArray(new Content[contents.size()]);
141        }
142/*
143
144
145<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
146        <Name>halloWelt</Name>
147        <Prefix></Prefix>
148        <Marker></Marker>
149        <MaxKeys>1000</MaxKeys>
150        <IsTruncated>false</IsTruncated>
151        <Contents>
152                <Key>susi</Key>
153                <LastModified>2008-03-05T14:10:41.000Z</LastModified>
154                <ETag>&quot;f84bcc91f8fd1321a251311c7e9fbfce&quot;</ETag>
155                <Size>156169</Size>
156                <Owner>
157                        <ID>5f8fc1a3e7f09e6e1bf5f7d7d0dfadfd0efc2e0afbf7789836f3ab67b2dddfdf</ID>
158                        <DisplayName>michael.streit</DisplayName>
159                </Owner>
160                <StorageClass>STANDARD</StorageClass>
161                </Contents>
162                <Contents>
163                        <Key>susi2</Key>
164                        <LastModified>2008-03-05T14:10:43.000Z</LastModified>
165                        <ETag>&quot;f84bcc91f8fd1321a251311c7e9fbfce&quot;</ETag><Size>156169</Size><Owner><ID>5f8fc1a3e7f09e6e1bf5f7d7d0dfadfd0efc2e0afbf7789836f3ab67b2dddfdf</ID><DisplayName>michael.streit</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>
166
167 */
168
169        /**
170         * @return the isTruncated
171         */
172        public boolean isTruncated() {
173                return isTruncated;
174        }
175}