001    package railo.commons.io.res.type.s3;
002    
003    import java.io.IOException;
004    import java.io.InputStream;
005    import java.io.Reader;
006    
007    import org.xml.sax.Attributes;
008    import org.xml.sax.InputSource;
009    import org.xml.sax.SAXException;
010    import org.xml.sax.XMLReader;
011    import org.xml.sax.helpers.DefaultHandler;
012    
013    import railo.loader.util.Util;
014    import railo.runtime.text.xml.XMLUtil;
015    
016    /**
017     * Die Klasse TagLibFactory liest die XML Repraesentation einer TLD ein 
018     * und laedt diese in eine Objektstruktur. 
019     * Sie tut dieses mithilfe eines Sax Parser.
020     * Die Klasse kann sowohl einzelne Files oder gar ganze Verzeichnisse von TLD laden.
021     */
022    public abstract class S3Factory extends DefaultHandler {
023            
024            public final static String DEFAULT_SAX_PARSER="org.apache.xerces.parsers.SAXParser";
025                    
026            private XMLReader xmlReader;
027            
028            protected String inside;
029            protected StringBuffer content=new StringBuffer();
030    
031            private boolean insideError;
032            private boolean insideMessage;
033    
034    
035    
036            /**
037             * Privater Konstruktor, der als Eingabe die TLD als File Objekt erhaelt.
038             * @param saxParser String Klassenpfad zum Sax Parser.
039             * @param file File Objekt auf die TLD.
040             * @throws IOException 
041             * @throws SAXException 
042             */
043            public S3Factory() {
044                    
045            }
046            
047            /**
048             * Generelle Initialisierungsmetode der Konstruktoren.
049             * @param saxParser String Klassenpfad zum Sax Parser.
050             * @param  is InputStream auf die TLD.
051             * @throws SAXException 
052             * @throws IOException 
053             */
054            protected void init(InputStream in) throws IOException, SAXException    {
055                    Reader r=null;
056                    try {
057                            InputSource is=new InputSource(in);
058                            xmlReader=XMLUtil.createXMLReader(DEFAULT_SAX_PARSER);
059                            xmlReader.setContentHandler(this);
060                            xmlReader.setErrorHandler(this);
061                            xmlReader.parse(is);
062                            
063                    }
064                    finally {
065                            Util.closeEL(r);
066                    }
067        }
068    
069            @Override
070            public final void startElement(String uri, String name, String qName, Attributes atts) {
071                    inside=qName;
072    
073                    if(qName.equalsIgnoreCase("Error")) insideError=true;
074                    if(qName.equalsIgnoreCase("Message")) insideMessage=true;
075                    doStartElement(uri, name, qName, atts);
076            }
077            protected abstract void doStartElement(String uri, String name, String qName, Attributes atts);
078        
079            @Override
080            public final void endElement(String uri, String name, String qName) throws SAXException {
081                    _setContent(content.toString().trim());
082                    content=new StringBuffer();
083                    inside="";
084    
085                    if(qName.equalsIgnoreCase("Error")) insideError=false;
086                    if(qName.equalsIgnoreCase("Message")) insideMessage=false;
087                    doEndElement(uri, name, qName);
088            }
089            
090            public abstract void doEndElement(String uri, String name, String qName) throws SAXException;
091            
092            
093        private void _setContent(String value) throws SAXException {
094            
095            if(insideError && insideMessage)        {
096                    throw new SAXException(value);
097            }
098            setContent(value);
099            /*
100            <?xml version="1.0" encoding="UTF-8"?>
101                    <Error>
102                            <Code>SignatureDoesNotMatch</Code>
103                            <Message>The request signature we calculated does not match the signature you provided. 
104                                    Check your key and signing method.</Message>
105                            <RequestId>53DE01E3379AEF9F</RequestId>
106                            <SignatureProvided>CsJJe9qgVVxoOPyAZ48XhFd8VJs=</SignatureProvided>
107                            <StringToSignBytes>47 45 54 0a 0a 0a 57 65 64 2c 20 30 35 20 4d 61 72 20 32 30 30 38 20 31 31 3a 31 39 3a 34 33 20 47 4d 54 0a 2f</StringToSignBytes>
108                            <AWSAccessKeyId>03SG52G1QX3EVP5FEMG2</AWSAccessKeyId>
109                            <HostId>TFyQxYQuisdThJrENWZW7Q1yp5mbVabV8jGx6B0m9pB6dSG/AJhpCTEWnQpW/otb</HostId>
110                            <StringToSign>GET
111    
112            */
113        }
114    
115            protected abstract void setContent(String value) throws SAXException;
116    
117            @Override
118            public void characters (char ch[], int start, int length)       {
119                    content.append(new String(ch,start,length));
120            }
121    }