001    /*
002     * Copyright (C) 2012 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.text;
021    
022    import java.io.Closeable;
023    import java.io.IOException;
024    import java.io.Writer;
025    
026    public class RenderWriter extends Writer implements Screenable {
027    
028      /** . */
029      final ScreenContext out;
030    
031      /** . */
032      private final Closeable closeable;
033    
034      /** . */
035      private boolean closed;
036    
037      /** . */
038      private boolean empty;
039    
040      public RenderWriter(ScreenContext out) throws NullPointerException {
041        this(out, null);
042      }
043    
044      public RenderWriter(ScreenContext out, Closeable closeable) throws NullPointerException {
045        if (out == null) {
046          throw new NullPointerException("No null appendable expected");
047        }
048    
049        //
050        this.out = out;
051        this.empty = true;
052        this.closeable = closeable;
053      }
054    
055      public boolean isEmpty() {
056        return empty;
057      }
058    
059      public RenderWriter append(CharSequence s) throws IOException {
060        empty &= s.length() == 0;
061        out.append(s);
062        return this;
063      }
064    
065      public Screenable append(Style style) throws IOException {
066        out.append(style);
067        return this;
068      }
069    
070      public Screenable cls() throws IOException {
071        out.cls();
072        return this;
073      }
074    
075      @Override
076      public void write(char[] cbuf, int off, int len) throws IOException {
077        if (closed) {
078          throw new IOException("Already closed");
079        }
080        if (len > 0) {
081          out.append(new String(cbuf, off, len));
082        }
083      }
084    
085      @Override
086      public void flush() throws IOException {
087        if (closed) {
088          throw new IOException("Already closed");
089        }
090        try {
091          out.flush();
092        }
093        catch (IOException e) {
094          throw e;
095        }
096        catch (Exception e) {
097          // e.printStackTrace();
098          // just swallow ?
099        }
100      }
101    
102      @Override
103      public void close() throws IOException {
104        if (!closed) {
105          closed = true;
106          if (closeable != null) {
107            closeable.close();
108          }
109        }
110      }
111    }