00001 package riverrat;
00002
00003 import org.xml.sax.*;
00004 import org.xml.sax.helpers.*;
00005
00006 import java.net.*;
00007 import java.io.*;
00008 import java.util.*;
00009 import javax.comm.*;
00010
00014 public class BasicServer extends DefaultHandler implements Runnable {
00016 protected int port = 8080;
00017
00019 protected ServerSocket serverSocket;
00020
00022 protected Socket clientSocket;
00023
00025 protected OutputStream networkOutputStream;
00026
00028 protected InputStream networkInputStream;
00029
00031 protected int fElementDepth;
00032
00034 protected Locator fLocator;
00035
00037 protected PrintWriter sout;
00038
00039 protected static final boolean DEFAULT_CANONICAL = true;
00040
00043 public BasicServer() {
00044 serverSocket = null;
00045 sout = null;
00046 }
00047
00054 public BasicServer(int newport) {
00055 try {
00056 serverSocket = new ServerSocket(newport);
00057 } catch(IOException e) {
00058 System.err.println("Could not listen on port: "+newport);
00059 System.exit(1);
00060 }
00061 sout = null;
00062 }
00063
00068 protected void openConnection() {
00069 if(serverSocket == null) {
00070 try {
00071 serverSocket = new ServerSocket(port);
00072 } catch(IOException e) {
00073 System.err.println("Could not listen on port: "+port);
00074 }
00075 }
00076 try {
00077 System.out.println("Waiting for a connection.");
00078 clientSocket = serverSocket.accept();
00079 } catch(IOException e) {
00080 System.err.println("Error trying to accept connection.");
00081 System.exit(1);
00082 }
00083 setPrintWriter();
00084 }
00085
00089 protected void setPrintWriter() {
00090 try {
00091 System.out.println("Client connected "+clientSocket);
00092 networkOutputStream = clientSocket.getOutputStream();
00093 networkInputStream = clientSocket.getInputStream();
00094 } catch(IOException e) {
00095 System.err.println("Error trying to get I/O stream from socket.");
00096 }
00097 sout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(networkOutputStream)));
00098 }
00099
00103 protected void closeConnection() {
00104 sout.close();
00105 try {
00106 networkInputStream.close();
00107 networkOutputStream.close();
00108 clientSocket.close();
00109 } catch(IOException e) {
00110 System.err.println("Error closing connection.");
00111 System.exit(1);
00112 }
00113 }
00114
00116 protected void normalizeAndPrint(String s, boolean isAttValue) {
00117
00118 int length = (s != null) ? s.length() : 0;
00119 for(int i = 0; i < length; i++) {
00120 char c = s.charAt(i);
00121 normalizeAndPrint(c, isAttValue);
00122 }
00123 }
00124
00126 protected void normalizeAndPrint(char[] ch, int offset, int length, boolean isAttValue) {
00127 for(int i = 0; i < length; i++) {
00128 normalizeAndPrint(ch[offset + i], isAttValue);
00129 }
00130 }
00131
00133 protected void normalizeAndPrint(char c, boolean isAttValue) {
00134 switch (c) {
00135 case '<': { sout.print("<"); break; }
00136 case '>': { sout.print(">"); break; }
00137 case '&': { sout.print("&"); break; }
00138 case '"': {
00139 if (isAttValue) {
00140 sout.print(""");
00141 } else {
00142 sout.print("\"");
00143 }
00144 break;
00145 }
00146 case '\r': { sout.print("
"); break; }
00147 case '\n': {
00148 if (DEFAULT_CANONICAL) {
00149 sout.print("
");
00150 break;
00151 }
00152
00153 }
00154 default: {
00155 if (((c >= 0x01 && c <= 0x1F && c != 0x09 && c != 0x0A)
00156 || (c >= 0x7F && c <= 0x9F) || c == 0x2028)
00157 || isAttValue && (c == 0x09 || c == 0x0A)) {
00158 sout.print("&#x");
00159 sout.print(Integer.toHexString(c).toUpperCase());
00160 sout.print(";");
00161 } else {
00162 sout.print(c);
00163 }
00164 }
00165 }
00166 }
00167
00171 public void startDocument() {
00172
00173 fElementDepth = 0;
00174 }
00175
00179 public void startElement(String uri, String local, String raw, Attributes attrs){
00180
00181
00182 fElementDepth++;
00183 sout.print('<');
00184 sout.print(raw);
00185 if(attrs != null) {
00186
00187 for(int i = 0; i < attrs.getLength(); i++) {
00188 sout.print(' ');
00189 sout.print(attrs.getQName(i));
00190 sout.print("=\"");
00191 normalizeAndPrint(attrs.getValue(i), true);
00192 sout.print('"');
00193 }
00194 }
00195 sout.print('>');
00196 sout.flush();
00197 }
00198
00202 public void endElement(String uri, String local, String raw) {
00203
00204
00205 fElementDepth--;
00206 sout.print("</");
00207 sout.print(raw);
00208 sout.print('>');
00209 sout.flush();
00210 }
00211
00215 public void endDocument() {
00216
00217 sout.print("\n");
00218 }
00219
00221 public void warning(SAXParseException ex) throws SAXException {
00222 System.err.println("Warning: "+ex);
00223 }
00224
00226 public void error(SAXParseException ex) throws SAXException {
00227 System.err.println("Error: "+ex);
00228 }
00229
00231 public void fatalError(SAXParseException ex) throws SAXException {
00232 System.err.println("Fatal Error: "+ex);
00233 }
00234
00236 public void setSocket(Socket s) {
00237 clientSocket = s;
00238 setPrintWriter();
00239 }
00240
00242 public void run() {
00243 if(clientSocket == null) {
00244 openConnection();
00245 }
00246 mainServer();
00247 closeConnection();
00248 }
00249
00253 public void mainServer() {
00254 sout.println("wheeee... connected!");
00255 while(true) {
00256 try {
00257 Thread.sleep(1000);
00258 } catch(InterruptedException e) {
00259 }
00260 sout.print("badger-");
00261 }
00262 }
00263
00272 public static void main(String args[]) {
00273
00274
00275 BasicServer testserver = new BasicServer();
00276 Thread serverthread = new Thread(testserver);
00277 serverthread.start();
00278
00279 while(true) {
00280 System.out.println("This is the main loop.");
00281 try {
00282 Thread.sleep(10000);
00283 } catch(InterruptedException e) {
00284 System.err.println("Interrupted.");
00285 }
00286 }
00287 }
00288 }