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