001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.shell.io;
021    
022    import org.crsh.command.InvocationContext;
023    import org.crsh.shell.ui.Element;
024    import org.crsh.text.ShellPrintWriter;
025    import org.crsh.shell.ui.UIBuilder;
026    
027    import java.io.IOException;
028    
029    /**
030     * Extends the {@link org.crsh.text.ShellPrintWriter} to provides support for easy padding.
031     *
032     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
033     * @version $Revision$
034     */
035    public class ShellPrinter extends ShellPrintWriter {
036    
037      /** . */
038      private final ShellWriter out;
039    
040      /** . */
041      private final InvocationContext context;
042    
043      public ShellPrinter(ShellWriter out, InvocationContext context) {
044        super(out);
045    
046        //
047        this.out = out;
048        this.context = context;
049      }
050    
051      @Override
052      public void println(Object x) {
053        print(x);
054        println();
055      }
056    
057      @Override
058      public void print(Object obj) {
059        if (obj instanceof UIBuilder) {
060          for (Element element : ((UIBuilder)obj).getElements()) {
061            print(element);
062          }
063        } else if (obj instanceof Element) {
064          try {
065            ((Element)obj).print(out, context);
066          } catch (IOException e) {
067            setError();
068          }
069        } else {
070          super.print(obj);
071        }
072      }
073    }