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 org.crsh.stream.Consumer;
023    
024    import java.io.IOException;
025    import java.util.LinkedList;
026    
027    /**
028     * A <code>Consumer&lt;Object&gt;</code> that renders the object stream to a {@link ScreenContext}.
029     */
030    public class ScreenContextConsumer implements Consumer<Object> {
031    
032      /** Buffers objects of the same kind. */
033      private final LinkedList<Object> buffer = new LinkedList<Object>();
034    
035      /** . */
036      private Renderer renderable = null;
037    
038      /** . */
039      private final RenderAppendable out;
040    
041      public ScreenContextConsumer(ScreenContext out) {
042        this.out = new RenderAppendable(out);
043      }
044    
045      public Class<Object> getConsumedType() {
046        return Object.class;
047      }
048    
049      public void provide(Object element) throws IOException {
050        Renderer current = Renderer.getRenderable(element.getClass());
051        if (current == null) {
052          send();
053          if (element instanceof CharSequence) {
054            out.append((CharSequence)element);
055          } else if (element instanceof CLS) {
056            out.cls();
057          } else if (element instanceof Style) {
058            out.append((Style)element);
059          } else {
060            out.append(element.toString());
061          }
062        } else {
063          if (renderable != null && !current.equals(renderable)) {
064            send();
065          }
066          buffer.addLast(element);
067          renderable = current;
068        }
069      }
070    
071      public void flush() throws IOException {
072        send();
073        out.flush();
074      }
075    
076      public void send() throws IOException {
077        if (buffer.size() > 0) {
078          LineRenderer renderer = renderable.renderer(buffer.iterator());
079          renderer.render(out);
080          buffer.clear();
081          renderable = null;
082        }
083      }
084    }