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    package org.crsh.command.pipeline;
020    
021    import org.crsh.command.CommandContext;
022    import org.crsh.shell.ScreenContext;
023    import org.crsh.text.Chunk;
024    import org.crsh.text.ChunkAdapter;
025    
026    import java.io.IOException;
027    
028    /** @author Julien Viet */
029    public class ToChunkPipe<C, CONS extends CommandContext<? super Chunk>> extends AbstractPipe<C, Chunk, CONS> {
030    
031      /** . */
032      private ChunkAdapter adapter;
033    
034      /** . */
035      private Class<C> consumedType;
036    
037      public ToChunkPipe(Class<C> consumedType, boolean piped) {
038        super(piped);
039    
040        //
041        this.consumedType = consumedType;
042      }
043    
044      @Override
045      public void open(final CONS consumer) {
046        super.open(consumer);
047    
048        //
049        adapter = new ChunkAdapter(new ScreenContext() {
050          public int getWidth() {
051            return consumer.getWidth();
052          }
053          public int getHeight() {
054            return consumer.getHeight();
055          }
056          public Class<Chunk> getConsumedType() {
057            return Chunk.class;
058          }
059          public void provide(Chunk element) throws IOException {
060            consumer.provide(element);
061          }
062          public void flush() throws IOException {
063            consumer.flush();
064          }
065          public void write(Chunk chunk) throws IOException {
066            consumer.write(chunk);
067          }
068        });
069      }
070    
071      public void provide(C element) throws IOException {
072        adapter.provide(element);
073      }
074    
075      @Override
076      public void flush() throws IOException {
077        adapter.flush();
078      }
079    
080      @Override
081      public void close() throws IOException {
082        adapter.flush();
083        super.close();
084      }
085    
086      public void write(Chunk chunk) throws IOException {
087        adapter.write(chunk);
088      }
089    
090      public Class<Chunk> getProducedType() {
091        return Chunk.class;
092      }
093    
094      public Class<C> getConsumedType() {
095        return consumedType;
096      }
097    }