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.List;
024
025import org.xml.sax.Attributes;
026import org.xml.sax.SAXException;
027
028
029public final class ACLFactory extends S3Factory {
030
031        private boolean insideAccessControlList=false; 
032        private boolean insideGrant=false; 
033        private boolean insideGrantee=false; 
034        
035        private boolean insideOwners=false;
036
037        private AccessControl ac; 
038        private AccessControlPolicy acp=new AccessControlPolicy();
039        private List<AccessControl> acl=acp.getAccessControlList();
040        private String type;
041        
042
043        /**
044         * @param saxParser String Klassenpfad zum Sax Parser.
045         * @param file File Objekt auf die TLD.
046         * @throws IOException 
047         * @throws SAXException 
048         */
049        public ACLFactory(InputStream in,S3 s3) throws IOException, SAXException {
050                super();
051                init(in);
052        }
053
054        public void doStartElement(String uri, String name, String qName, Attributes atts) {
055                if(insideGrant) {
056                        if(qName.equals("Grantee")) {
057                                for(int i=atts.getLength()-1;i>=0;i--){
058                                        if("type".equalsIgnoreCase(atts.getLocalName(i)))
059                                                type=atts.getValue(i);
060                                }
061                                
062                                
063                                insideGrantee=true;
064                        }
065                }
066                else if(insideAccessControlList) {
067                        if(qName.equals("Grant")) startGrant();
068                }
069                else if(qName.equals("AccessControlList")) insideAccessControlList=true;
070                else if(qName.equals("Owner")) insideOwners=true;
071                
072        }
073    
074        public void doEndElement(String uri, String name, String qName) throws SAXException {
075                if(insideGrant) {
076                        if(qName.equals("Grant")) endGrant();
077                        else if(qName.equals("Grantee")) insideGrantee=false;
078                }
079                else if(qName.equals("AccessControlList")) insideAccessControlList=false;
080                else if(qName.equals("Owner")) insideOwners=false;
081        }
082        
083        
084        protected void setContent(String value) throws SAXException     {
085                if(insideGrant) {
086                        if(insideGrantee){
087                                if(inside.equals("ID"))                                         ac.setId(value);
088                                else if(inside.equals("DisplayName"))           ac.setDisplayName(value);
089                                else if(inside.equals("URI"))                           ac.setUri(value);
090                                else if(inside.equals("Email"))                         ac.setEmail(value);
091                                else if(inside.equals("EmailAddress"))          ac.setEmail(value);
092                                
093                        }
094                        else {
095                                if(inside.equals("Permission"))                         ac.setPermission(value);
096                        }
097        }
098                else if(insideOwners) {
099                        if(inside.equals("ID"))                                         acp.setId(value);
100                        else if(inside.equals("DisplayName"))           acp.setDisplayName(value);
101                }
102    }
103        
104        
105        /**
106         * Wird jedesmal wenn das Tag attribute beginnt aufgerufen, um intern in einen anderen Zustand zu gelangen.
107         */
108        private void startGrant()       {
109        insideGrant=true;
110        ac=new AccessControl(); 
111    }
112        
113        
114        /**
115         * Wird jedesmal wenn das Tag tag endet aufgerufen, um intern in einen anderen Zustand zu gelangen.
116         */
117        private void endGrant() {
118                ac.setType(AccessControl.toType(type,AccessControl.TYPE_CANONICAL_USER));
119                acl.add(ac);
120                insideGrant=false;
121    }
122
123        public AccessControlPolicy getAccessControlPolicy() {
124                return acp;
125        }
126
127
128}