001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.messaging.jmf;
023    
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.PrintStream;
027    
028    import org.granite.messaging.jmf.codec.StandardCodec;
029    
030    /**
031     * @author Franck WOLFF
032     */
033    public class JMFDumper extends JMFDeserializer implements DumpContext {
034            
035            protected static final int DEFAULT_MAX_ARRAY_ELEMENTS = 32;
036            
037            protected final PrintStream ps;
038            protected final int maxArrayElements;
039    
040            protected int indentCount = 0;
041            
042            public JMFDumper(InputStream is, SharedContext context, PrintStream ps) {
043                    this(is, context, ps, DEFAULT_MAX_ARRAY_ELEMENTS);
044            }
045            
046            public JMFDumper(InputStream is, SharedContext context, PrintStream ps, int maxArrayElements) {
047                    super(is, context);
048                    this.ps = ps;
049                    this.maxArrayElements = maxArrayElements;
050            }
051    
052            public int getMaxArrayElements() {
053                    return maxArrayElements;
054            }
055    
056            public void dump() throws IOException {
057                    int parameterizedJmfType;
058                    
059                    while ((parameterizedJmfType = inputStream.read()) != -1) {
060                            int jmfType = codecRegistry.extractJmfType(parameterizedJmfType);
061                            
062                            StandardCodec<?> codec = codecRegistry.getCodec(jmfType);
063                            if (codec == null)
064                                    throw new JMFEncodingException("Unsupported type: " + jmfType);
065                            
066                            codec.dump(this, parameterizedJmfType);
067                    }
068            }
069    
070            public void incrIndent(int off) {
071                    this.indentCount += off;
072                    if (indentCount < 0)
073                            indentCount = 0;
074            }
075    
076            public void indentPrint(String message) throws IOException {
077                    for (int i = 0; i < indentCount; i++)
078                            ps.print("    ");
079                    ps.print(message);
080            }
081    
082            public void print(String message) throws IOException {
083                    ps.print(message);
084            }
085    
086            public void noIndentPrintLn(String message) throws IOException {
087                    ps.println(message);
088            }
089    
090            public void indentPrintLn(String message) throws IOException {
091                    for (int i = 0; i < indentCount; i++)
092                            ps.print("    ");
093                    ps.println(message);
094            }
095    }