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.shell.impl.command;
021
022 import org.crsh.io.ScreenContext;
023 import org.crsh.command.ScriptException;
024 import org.crsh.io.Filter;
025 import org.crsh.io.ProducerContext;
026 import org.crsh.text.Chunk;
027 import org.crsh.text.ChunkAdapter;
028
029 import java.io.IOException;
030
031 class ToChunkPipeFilter implements Filter<Object, Chunk> {
032
033 /** . */
034 private Filter<Chunk, ?> context;
035
036 /** . */
037 private ChunkAdapter ca;
038
039 public Class<Chunk> getProducedType() {
040 return Chunk.class;
041 }
042
043 public Class<Object> getConsumedType() {
044 return Object.class;
045 }
046
047 public void setPiped(boolean piped) {
048 context.setPiped(piped);
049 }
050
051 public void open(final ProducerContext<Chunk> context) {
052 ca = new ChunkAdapter(new ScreenContext<Chunk>() {
053 public int getWidth() {
054 return context.getWidth();
055 }
056 public int getHeight() {
057 return context.getHeight();
058 }
059 public void provide(Chunk element) throws IOException {
060 ToChunkPipeFilter.this.context.provide(element);
061 }
062 public void flush() throws IOException {
063 ToChunkPipeFilter.this.context.flush();
064 }
065 });
066
067 //
068 this.context = (Filter<Chunk, ?>)context;
069 }
070
071 public boolean takeAlternateBuffer() {
072 return context.takeAlternateBuffer();
073 }
074
075 public boolean releaseAlternateBuffer() {
076 return context.releaseAlternateBuffer();
077 }
078
079 public void provide(Object element) throws ScriptException, IOException {
080 ca.provide(element);
081 }
082
083 public void flush() throws ScriptException, IOException {
084 ca.flush();
085 }
086
087 public void close() throws ScriptException {
088 context.close();
089 }
090
091 public String getProperty(String propertyName) {
092 return context.getProperty(propertyName);
093 }
094
095 public String readLine(String msg, boolean echo) {
096 return context.readLine(msg, echo);
097 }
098
099 public int getWidth() {
100 return context.getWidth();
101 }
102
103 public int getHeight() {
104 return context.getHeight();
105 }
106 }