1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import java.io.*; |
5 |
import java.net.*; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import org.xml.sax.*; |
9 |
import javax.xml.parsers.SAXParserFactory; |
10 |
import javax.xml.parsers.ParserConfigurationException; |
11 |
import javax.xml.parsers.SAXParser; |
12 |
|
13 |
/** |
14 |
* XMLStreamReaderThread. |
15 |
* |
16 |
* @author $Author: $ |
17 |
* @version $Id: $ |
18 |
*/ |
19 |
public class XMLStreamReaderThread extends Thread { |
20 |
|
21 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: $"; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
// Constructor |
33 |
public XMLStreamReaderThread (String serverName, int serverPort, TerminalScreen screen) { |
34 |
_serverName = serverName; |
35 |
_serverPort = serverPort; |
36 |
_screen = screen; |
37 |
} |
38 |
|
39 |
//---PUBLIC METHODS--- |
40 |
|
41 |
public void run() { |
42 |
|
43 |
final int maxPacketSize = 8196; |
44 |
final int startByte = 0; |
45 |
final int endByte = 1; |
46 |
|
47 |
BufferedReader br = null; |
48 |
try { |
49 |
Socket conn = new Socket(_serverName, _serverPort); |
50 |
InputStreamReader isr = new InputStreamReader(conn.getInputStream()); |
51 |
br = new BufferedReader(isr); |
52 |
|
53 |
} |
54 |
catch(Exception e) { |
55 |
_screen.clear(); |
56 |
System.out.println("Cannot connect to "+_serverName+":"+_serverPort); |
57 |
System.exit(1); |
58 |
} |
59 |
|
60 |
DataLayout layout = new DataLayout(_screen, JavaCLIParameters.showData); |
61 |
|
62 |
// Use the default (non-validating) parser |
63 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
64 |
while (1 == 1) { |
65 |
try { |
66 |
String xml = br.readLine(); |
67 |
|
68 |
// Parse the input |
69 |
XMLPacket packet = new XMLPacket(); |
70 |
SAXParser saxParser = factory.newSAXParser(); |
71 |
saxParser.parse(new InputSource(new StringReader(xml)), new XMLStreamParser(packet)); |
72 |
|
73 |
|
74 |
// For out information |
75 |
//_screen.gotoxy(4, 5); |
76 |
//_screen.print(packet.printAll(), TerminalScreen.RED); |
77 |
|
78 |
layout.updateScreen(packet); |
79 |
|
80 |
} |
81 |
catch (IOException e) { |
82 |
System.out.println("Connection to server broken."); |
83 |
break; |
84 |
} |
85 |
catch (Exception e) { |
86 |
System.out.println("Error caught in the Thread: "+e); |
87 |
break; |
88 |
} |
89 |
} |
90 |
|
91 |
_screen.clear(); |
92 |
|
93 |
} |
94 |
|
95 |
|
96 |
//---PRIVATE METHODS--- |
97 |
|
98 |
//---ACCESSOR/MUTATOR METHODS--- |
99 |
|
100 |
//---ATTRIBUTES--- |
101 |
|
102 |
private String _serverName; |
103 |
private int _serverPort; |
104 |
private TerminalScreen _screen; |
105 |
|
106 |
//---STATIC ATTRIBUTES--- |
107 |
|
108 |
} |