001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.tapedeck;
021:
022: import java.nio.charset.Charset;
023: import java.util.LinkedList;
024:
025: import org.apache.mina.common.IoBuffer;
026: import org.apache.mina.common.IoSession;
027: import org.apache.mina.filter.codec.ProtocolDecoder;
028: import org.apache.mina.filter.codec.ProtocolDecoderOutput;
029: import org.apache.mina.filter.codec.textline.LineDelimiter;
030: import org.apache.mina.filter.codec.textline.TextLineDecoder;
031:
032: /**
033: * MINA {@link ProtocolDecoder} which decodes bytes into {@link Command}
034: * objects.
035: *
036: * @author The Apache MINA Project (dev@mina.apache.org)
037: * @version $Rev: 587133 $, $Date: 2007-10-22 09:54:15 -0600 (Mon, 22 Oct 2007) $
038: */
039: public class CommandDecoder extends TextLineDecoder {
040:
041: public CommandDecoder() {
042: super (Charset.forName("UTF8"), LineDelimiter.WINDOWS);
043: }
044:
045: private Object parseCommand(String line)
046: throws CommandSyntaxException {
047: String[] temp = line.split(" +", 2);
048: String cmd = temp[0].toLowerCase();
049: String arg = temp.length > 1 ? temp[1] : null;
050:
051: if (LoadCommand.NAME.equals(cmd)) {
052: if (arg == null) {
053: throw new CommandSyntaxException(
054: "No tape number specified");
055: }
056: try {
057: return new LoadCommand(Integer.parseInt(arg));
058: } catch (NumberFormatException nfe) {
059: throw new CommandSyntaxException(
060: "Illegal tape number: " + arg);
061: }
062: } else if (PlayCommand.NAME.equals(cmd)) {
063: return new PlayCommand();
064: } else if (PauseCommand.NAME.equals(cmd)) {
065: return new PauseCommand();
066: } else if (StopCommand.NAME.equals(cmd)) {
067: return new StopCommand();
068: } else if (ListCommand.NAME.equals(cmd)) {
069: return new ListCommand();
070: } else if (EjectCommand.NAME.equals(cmd)) {
071: return new EjectCommand();
072: } else if (QuitCommand.NAME.equals(cmd)) {
073: return new QuitCommand();
074: } else if (InfoCommand.NAME.equals(cmd)) {
075: return new InfoCommand();
076: }
077:
078: throw new CommandSyntaxException("Unknown command: " + cmd);
079: }
080:
081: @Override
082: public void decode(IoSession session, IoBuffer in,
083: final ProtocolDecoderOutput out) throws Exception {
084:
085: final LinkedList<String> lines = new LinkedList<String>();
086: super .decode(session, in, new ProtocolDecoderOutput() {
087: public void write(Object message) {
088: lines.add((String) message);
089: }
090:
091: public void flush() {
092: }
093: });
094:
095: for (String s : lines) {
096: out.write(parseCommand(s));
097: }
098: }
099:
100: }
|