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;
027
028import org.xml.sax.Attributes;
029import org.xml.sax.SAXException;
030
031/**
032 * Die Klasse TagLibFactory liest die XML Repraesentation einer TLD ein 
033 * und laedt diese in eine Objektstruktur. 
034 * Sie tut dieses mithilfe eines Sax Parser.
035 * Die Klasse kann sowohl einzelne Files oder gar ganze Verzeichnisse von TLD laden.
036 */
037public final class BucketFactory extends S3Factory {
038        
039        private boolean insideBuckets=false;
040        private boolean insideBucket=false;
041        private final S3 s3;
042        
043        private Bucket bucket; 
044        private final List buckets=new ArrayList();
045        private boolean insideOwners;
046        private String ownerIdKey;
047        private String ownerDisplayName; 
048
049
050        /**
051         * Privater Konstruktor, der als Eingabe die TLD als File Objekt erhaelt.
052         * @param saxParser String Klassenpfad zum Sax Parser.
053         * @param file File Objekt auf die TLD.
054         * @throws IOException 
055         * @throws SAXException 
056         */
057        public BucketFactory(InputStream in, S3 s3) throws IOException, SAXException {
058                super();
059                this.s3=s3;
060                init(in);
061        }
062
063        @Override
064        public void doStartElement(String uri, String name, String qName, Attributes atts) {
065                if(qName.equals("Owner")) insideOwners=true;
066                if(qName.equals("Buckets")) insideBuckets=true;
067                if(qName.equals("Bucket")) startBucket();
068                
069        }
070    
071        @Override
072        public void doEndElement(String uri, String name, String qName) throws SAXException {
073                if(qName.equals("Owner")) insideOwners=false;
074                if(qName.equals("Buckets")) insideBuckets=false;
075                if(qName.equals("Bucket")) endBucket();
076        }
077        
078        
079        protected void setContent(String value) throws SAXException     {
080                if(insideOwners){
081                        if(inside.equals("ID"))                                         ownerIdKey=value;
082                        else if(inside.equals("DisplayName"))           ownerDisplayName=value;
083                        
084                }
085                if(insideBuckets && insideBucket)       {
086                        // Name
087                        if(inside.equals("Name")) bucket.setName(value);
088                        // CreationDate
089                        else if(inside.equals("CreationDate")) {
090                                try {
091                                        bucket.setCreation(S3.toDate(value,s3.getTimeZone()));
092                                } 
093                                catch (PageException e) {
094                                        throw new SAXException(e.getMessage());
095                                }       
096                        }
097        }
098    }   
099        
100        
101        
102        /**
103         * Wird jedesmal wenn das Tag attribute beginnt aufgerufen, um intern in einen anderen Zustand zu gelangen.
104         */
105        private void startBucket()      {
106        insideBucket=true;
107        bucket=new Bucket(s3); 
108    }
109        
110        
111        /**
112         * Wird jedesmal wenn das Tag tag endet aufgerufen, um intern in einen anderen Zustand zu gelangen.
113         */
114        private void endBucket()        {
115                bucket.setOwnerDisplayName(ownerDisplayName);
116                bucket.setOwnerIdKey(ownerIdKey);
117                buckets.add(bucket);
118        insideBucket=false;
119    }
120
121        public Bucket[] getBuckets() {
122                return (Bucket[]) buckets.toArray(new Bucket[buckets.size()]);
123        }
124
125}