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