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    import org.xml.sax.helpers.XMLReaderFactory;
013    
014    import railo.loader.util.Util;
015    
016    /**
017     * Die Klasse TagLibFactory liest die XML Repr�sentation einer TLD ein 
018     * und l�dt 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 erh�lt.
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                            
059                            xmlReader=XMLReaderFactory.createXMLReader(DEFAULT_SAX_PARSER);
060                            xmlReader.setContentHandler(this);
061                            xmlReader.setErrorHandler(this);
062                            xmlReader.parse(is);
063                            
064                    }
065                    finally {
066                            Util.closeEL(r);
067                    }
068        }
069    
070            /**
071             * Geerbte Methode von org.xml.sax.ContentHandler, 
072             * wird bei durchparsen des XML, beim Auftreten eines Start-Tag aufgerufen.
073             *  
074             * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
075             */
076            public final void startElement(String uri, String name, String qName, Attributes atts) {
077                    inside=qName;
078    
079                    if(qName.equalsIgnoreCase("Error")) insideError=true;
080                    if(qName.equalsIgnoreCase("Message")) insideMessage=true;
081                    doStartElement(uri, name, qName, atts);
082            }
083            protected abstract void doStartElement(String uri, String name, String qName, Attributes atts);
084        
085            /**
086             * Geerbte Methode von org.xml.sax.ContentHandler, 
087             * wird bei durchparsen des XML, beim auftreten eines End-Tag aufgerufen.
088             *  
089             * @see org.xml.sax.ContentHandler#endElement(String, String, String)
090             */
091            public final void endElement(String uri, String name, String qName) throws SAXException {
092                    _setContent(content.toString().trim());
093                    content=new StringBuffer();
094                    inside="";
095    
096                    if(qName.equalsIgnoreCase("Error")) insideError=false;
097                    if(qName.equalsIgnoreCase("Message")) insideMessage=false;
098                    doEndElement(uri, name, qName);
099            }
100            
101            public abstract void doEndElement(String uri, String name, String qName) throws SAXException;
102            
103            
104        private void _setContent(String value) throws SAXException {
105            
106            if(insideError && insideMessage)        {
107                    throw new SAXException(value);
108            }
109            setContent(value);
110            /*
111            <?xml version="1.0" encoding="UTF-8"?>
112                    <Error>
113                            <Code>SignatureDoesNotMatch</Code>
114                            <Message>The request signature we calculated does not match the signature you provided. 
115                                    Check your key and signing method.</Message>
116                            <RequestId>53DE01E3379AEF9F</RequestId>
117                            <SignatureProvided>CsJJe9qgVVxoOPyAZ48XhFd8VJs=</SignatureProvided>
118                            <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>
119                            <AWSAccessKeyId>03SG52G1QX3EVP5FEMG2</AWSAccessKeyId>
120                            <HostId>TFyQxYQuisdThJrENWZW7Q1yp5mbVabV8jGx6B0m9pB6dSG/AJhpCTEWnQpW/otb</HostId>
121                            <StringToSign>GET
122    
123            */
124        }
125    
126            protected abstract void setContent(String value) throws SAXException;
127    
128            /** 
129         * Geerbte Methode von org.xml.sax.ContentHandler, 
130             * wird bei durchparsen des XML, zum einlesen des Content eines Body Element aufgerufen.
131             * 
132             * @see org.xml.sax.ContentHandler#characters(char[], int, int)
133             */
134            public void characters (char ch[], int start, int length)       {
135                    content.append(new String(ch,start,length));
136            }
137    }