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