View Javadoc

1   //Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   //This file is part of MagicCodes.
3   //
4   //MagicCodes is free software; you can redistribute it and/or modify
5   //it under the terms of the GNU General Public License as published by
6   //the Free Software Foundation; either version 2 of the License, or
7   //(at your option) any later version.
8   //
9   //MagicCodes is distributed in the hope that it will be useful,
10  //but WITHOUT ANY WARRANTY; without even the implied warranty of
11  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  //GNU General Public License for more details.
13  //
14  //You should have received a copy of the GNU General Public License
15  //along with Foobar; if not, write to the Free Software
16  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  package org.ninjasoft.magiccodes.logic;
18  
19  import java.util.*;
20  import org.ninjasoft.magiccodes.plugins.*;
21  
22  /***
23   * @author enigma
24   *
25   * To change the template for this generated type comment go to
26   * Window - Preferences - Java - Code Generation - Code and Comments
27   */
28  public class Processor {
29      public static final int TYPE_BINARY = 1;
30      public static final int TYPE_HEX = 2;
31      public static final int TYPE_ASCII = 3;
32      public static final int TYPE_HTML = 4;
33      
34      private Vector pluginChain = new Vector();
35      private int inputType = -1;
36      private int outputType = -1;
37      private int keyType = -1;
38      
39      public void addPlugin(Plugin plugin) {
40          this.pluginChain.add(plugin);
41      }
42      
43      public void clearPlugins() {
44          this.pluginChain.removeAllElements();
45      }
46      
47      public void setInputType(int inputType) {
48          this.inputType = inputType;
49      }
50      public void setOutputType(int outputType) {
51          this.outputType = outputType;
52      }
53      public void setKeyType(int keyType) {
54          this.keyType = keyType;
55      }
56      public String doAction(String in, String key) throws RuntimeException, FormatException {
57          int[] data = null;
58          int[] keyData = null;
59          String result = null;
60          if (inputType == -1) 
61              throw new RuntimeException("Input type not set");
62          if (outputType == -1)
63              throw new RuntimeException("Output type not set");
64          switch(inputType) {
65              case TYPE_BINARY:
66                  data = Translator.binToByte(in);
67                  break;
68              case TYPE_HEX:
69                  data = Translator.hexToByte(in);
70                  break;
71              case TYPE_ASCII:
72                  data = Translator.asciiToByte(in);
73                  break;
74              default:
75                  throw new RuntimeException("Unknown input type");
76          }
77          switch(keyType) {
78              case TYPE_BINARY:
79                  keyData = Translator.binToByte(key);
80                  break;
81              case TYPE_HEX:
82                  keyData = Translator.hexToByte(key);
83                  break;
84              case TYPE_ASCII:
85                  keyData = Translator.asciiToByte(key);
86                  break;
87              default:
88                  throw new RuntimeException("Unknown input type");
89          }
90          // If no key given
91          if ((keyData == null) || (keyData.length == 0)) {
92              for (Iterator i = this.pluginChain.iterator(); i.hasNext(); ) {
93                  Plugin plugin = (Plugin) i.next(); 
94                  // If key required
95                  if (plugin.usesKey())
96                      throw new RuntimeException("Key required for plugin " + plugin.getName());
97              }
98          }
99          for (Iterator i = this.pluginChain.iterator(); i.hasNext(); ) {
100             data = ((Plugin) i.next()).doAction(data, keyData);
101         }
102         switch(outputType) {
103             case TYPE_BINARY:
104                 result = Translator.byteToBin(data);
105                 break;
106             case TYPE_HEX:
107                 result = Translator.byteToHex(data);
108                 break;
109             case TYPE_ASCII:
110                 result = Translator.byteToAscii(data);
111                 break;
112             case TYPE_HTML:
113                 result = Translator.byteToPrintableAscii(data);
114                 break;
115             default:
116                 throw new RuntimeException("Unknown input type");
117         }
118         return result;
119     }
120 }