001    /*
002    // $Id: //open/mondrian/src/main/mondrian/xmla/impl/DefaultXmlaResponse.java#4 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2005-2007 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.xmla.impl;
011    
012    import java.io.OutputStream;
013    import java.io.UnsupportedEncodingException;
014    
015    import mondrian.olap.Util;
016    import mondrian.xmla.*;
017    
018    /**
019     * Default implementation of {@link mondrian.xmla.XmlaResponse}.
020     *
021     * @author Gang Chen
022     */
023    public class DefaultXmlaResponse implements XmlaResponse  {
024    
025        // TODO: add a msg to MondrianResource for this.
026        private static final String MSG_ENCODING_ERROR = "Encoding unsupported: ";
027    
028        private final SaxWriter writer;
029    
030        public DefaultXmlaResponse(OutputStream outputStream, String encoding) {
031            try {
032                writer = new DefaultSaxWriter(outputStream, encoding);
033            } catch (UnsupportedEncodingException uee) {
034                throw Util.newError(uee, MSG_ENCODING_ERROR + encoding);
035            }
036        }
037    
038        public SaxWriter getWriter() {
039            return writer;
040        }
041    
042        public void error(Throwable t) {
043            writer.completeBeforeElement("root");
044            Throwable throwable = XmlaUtil.rootThrowable(t);
045            writer.startElement("Messages");
046            writer.startElement("Error", new String[] {
047                    "ErrorCode", throwable.getClass().getName(),
048                    "Description", throwable.getMessage(),
049                    "Source", "Mondrian",
050                    "Help", "",
051            });
052            writer.endElement(); // </Messages>
053            writer.endElement(); // </Error>
054        }
055    
056    }
057    
058    // End DefaultXmlaResponse.java