package com.ericsson.tic.vi; import java.util.ListIterator; import java.net.SocketException; /** * Handles all parsing and encoding. * * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class Parser { /** * This method parses messages from the master system. It assumes that * messages are structured according to one of several predefined * "strategies". What strategy to use is defined in the XMLfile. *

*

	strategy 0: (nodes)
	
	dataParseName		host		value
	token[0]			token[1]	token[2]
	
	
	strategy 1: (nodes)
	
	dataParseName		host		category	value
	token[0]			token[1]	token[2]	token[3]
	
	
	strategy 2: (nodes)
	
	dataParseName		host		category	value1		value2		...		valueN
	token[0]			token[1]	token[2]	token[3]	token[4]	...		token[N+2]
	
					
	strategy 3: (relations)
	
	dataParseName		host_host	value
	token[0]			token[1]	token[2]
	
	parsing of token[1]: (split by "_")
	
	host1			host2
	token[0]		token[1]
	
	
	strategy 4: (relations)
	
	dataParseName		host_host	value1		value2
	token[0]			token[1]	token[2]	token[3]
	
	parsing of token[1]: (split by "_")
	
	host1			host2
	token[0]		token[1]
	 * 
* @param s The server input message which needs to be parsed. */ public static void parseMessageInput(String s) { if (s == null || s.equals("")) { return; } if (VI.printServerMessages) { dbrec("> server says: " + s); } String[] clean = s.split(VI.NULL_CHAR); String[] token = clean[0].split(VI.SPACE); String[] hostToken; String temp = ""; int nr = 0; // id of node or relation int cat = 0; // id of category float v = 0; int dID = (VI.dataID.get(token[0])).intValue(); if (VI.dataArr[dID].parseStrat == VI.STRAT_NODE_HOST_VALUE) { // strategy 0: (nodes) // // dataParseName host value // token[0] token[1] token[2] if (token.length > 1 && VI.dataArr[dID].category.id == VI.NULL_CATEGORY) { try { nr = (VI.nodeID.get(token[1])).intValue(); } catch (NumberFormatException e) { return; } try { v = Float.parseFloat(token[2]); } catch (NumberFormatException e) { return; } VI.dataArr[dID].value[nr-1][0][0] = v; VI.dataArr[dID].timeStamp[nr-1][0] = VI.currentTimeStamp; VI.dataArr[dID].normalizeValue(nr-1, 0); } } else if (VI.dataArr[dID].parseStrat == VI.STRAT_NODE_HOST_CAT_VALUE) { // strategy 1: (nodes) // // dataParseName host category value // token[0] token[1] token[2] token[3] if (token.length > 1 && VI.dataArr[dID].category.id > VI.NULL_CATEGORY) { // todo: get() may return null! try { nr = (VI.nodeID.get(token[1])).intValue(); } catch (NumberFormatException e) { return; } try { cat = (VI.catID.get(token[2])).intValue(); } catch (NumberFormatException e) { return; } try { v = Float.parseFloat(token[3]); } catch (NumberFormatException e) { return; } VI.dataArr[dID].value[nr-1][cat-1][0] = v; VI.dataArr[dID].timeStamp[nr-1][cat-1] = VI.currentTimeStamp; VI.dataArr[dID].normalizeValue(nr-1, cat-1); } } else if (VI.dataArr[dID].parseStrat == VI.STRAT_NODE_HOST_CAT_VALUE_N) { // strategy 2: (nodes) // // dataParseName host category value1 value2 ... valueN // token[0] token[1] token[2] token[3] token[4] ... token[N+2] if (token.length > 1 && VI.dataArr[dID].category.id > VI.NULL_CATEGORY) { try { nr = (VI.nodeID.get(token[1])).intValue(); } catch (NumberFormatException e) { return; } try { cat = (VI.catID.get(token[2])).intValue(); } catch (NumberFormatException e) { return; } for (int i = 0; i < VI.dataArr[dID].dimension; i++) { try { v = Float.parseFloat(token[3+i]); } catch (NumberFormatException e) { return; } VI.dataArr[dID].value[nr-1][cat-1][i] = v; VI.dataArr[dID].timeStamp[nr-1][cat-1] = VI.currentTimeStamp; } VI.dataArr[dID].normalizeValue(nr-1, cat-1); } } else if (VI.dataArr[dID].parseStrat == VI.STRAT_REL_HOST_VALUE) { // strategy 3: (relations) // // dataParseName host1_host2 value // token[0] token[1] token[2] // // parsing of token[1]: (split by "_") // // host1 host2 // token[0] token[1] if (token.length > 1 && VI.dataArr[dID].category.id == VI.NULL_CATEGORY) { if (VI.relID.containsKey(token[1])) { try { nr = (VI.relID.get(token[1])).intValue(); } catch (NumberFormatException e) { return; } } else { hostToken = token[1].split(VI.UNDERSCORE); if (VI.relID.containsKey(hostToken[1] + "_" + hostToken[0])) { try { nr = (VI.relID.get(hostToken[1] + "_" + hostToken[0])).intValue(); } catch (NumberFormatException e) { return; } } } try { v = Float.parseFloat(token[2]); } catch (NumberFormatException e) { return; } VI.dataArr[dID].value[nr-1][0][0] = v; VI.dataArr[dID].timeStamp[nr-1][0] = VI.currentTimeStamp; VI.dataArr[dID].normalizeValue(nr-1, 0); } } else if (VI.dataArr[dID].parseStrat == VI.STRAT_REL_HOST_VALUE_2) { // strategy 4: (relations) // // dataParseName host_host value1 value2 // token[0] token[1] token[2] token[3] // // parsing of token[1]: (split by "_") // // host1 host2 // token[0] token[1] if (token.length > 1 && VI.dataArr[dID].category.id == VI.NULL_CATEGORY) { if (VI.relID.containsKey(token[1])) { try { nr = (VI.relID.get(token[1])).intValue(); } catch (NumberFormatException e) { return; } } else { hostToken = token[1].split(VI.UNDERSCORE); if (VI.relID.containsKey(hostToken[1] + "_" + hostToken[0])) { try { nr = (VI.relID.get(hostToken[1] + "_" + hostToken[0])).intValue(); } catch (NumberFormatException e) { return; } } } try { v = Float.parseFloat(token[2]); } catch (NumberFormatException e) { return; } VI.dataArr[dID].value[nr-1][0][0] = v; try { v = Float.parseFloat(token[3]); } catch (NumberFormatException e) { return; } VI.dataArr[dID].value[nr-1][0][1] = v; VI.dataArr[dID].timeStamp[nr-1][0] = VI.currentTimeStamp; VI.dataArr[dID].normalizeValue(nr-1, 0); } } else { // do nothing } } /** * Prints a message to all ConsolePanels. * @param s The message */ private static void dbrec(String s) { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.dbrec(s); } } /** * Prints a message to all ConsolePanels. * @param s The message */ private static void udpsent(String s) { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.udpsent(s); } } /** * Prints a message to all ConsolePanels. * @param s The message */ private static void chat(String s) { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.chat(s); } } /** * Prints a message to all ConsolePanels. * @param s The message */ private static void def(String s) { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.def(s); } } /** * Prints a message to all ConsolePanels. * @param s The message */ private static void d(String s) { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.d(s); } } /** * Prints a message to all ConsolePanels. * @param s The message */ private static void user(String s) { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.user(s); } } /** * Prints an array of strings. * @param s The message */ public static void printArr(String[] s) { for (int i = 0; i < s.length; i++) { System.out.print(s[i] + "|"); } System.out.println(""); } /** * Prints a help text message to all ConsolePanels. */ private static void help() { ListIterator iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); con.helpTitle(" Keyboard Shortcuts:"); con.help(""); con.help(" Note: you need to click in the main view to recieve keyboard focus."); con.help(""); con.help(" PageUp Zoom in. (or use the scroll wheel)"); con.help(" PageDown Zoom out. (or use the scroll wheel)"); con.help(" ArrowKeys Pan/tilt. (or click and drag with mouse)"); con.help(" Shift Hold down while using the arrow keys to move a selected label."); con.help(" Q Press and hold to tilt the view up in the globe view."); con.help(" A Press and hold to tilt the view down in the globe view."); con.help(" P Print server messages."); con.help(" F Toggle between fixed and infinite panning (makes the globe spin)."); con.help(" J If you have infinite panning on this option will make the globe break and eventually stop."); con.help(" 0-9 Toggle between the various bar-modalities."); con.help(""); con.helpTitle(" Commands:"); con.help(""); con.help(" quit Exit the software."); con.help(" help Print this help message."); } } /** * Parse user input. * @param s The message which is to be parsed. */ public static void parseUserInput(String s) { if (s == null || s.equals("")) { return; } String[] clean = s.split(VI.NULL_CHAR); String[] token = clean[0].split(VI.SPACE); String temp = ""; if (token[0].equals("chat")) { if (token.length > 1) { temp = token[1]; for (int i = 2; i < token.length; i++) { temp = temp + " " + token[i]; } user("> say: " + temp); try { NetCom.sendMessage("chat " + temp, VI.udpOutHost, VI.udpOutPort); } catch (SocketException e) { e.printStackTrace(); } } else { def(s); } } else if (token[0].equals("toggle")) { if (token.length > 1) { if (token[1].equals("shiftPressed")) { VI.shiftPressed = !VI.shiftPressed; user("> shiftPressed: " + VI.shiftPressed); } else if (token[1].equals("panMoveDelayOn")) { VI.panMoveDelayOn = !VI.panMoveDelayOn; user("> panMoveDelayOn: " + VI.panMoveDelayOn); } else if (token[1].equals("tiltMoveDelayOn")) { VI.tiltMoveDelayOn = !VI.tiltMoveDelayOn; user("> tiltMoveDelayOn: " + VI.tiltMoveDelayOn); } else if (token[1].equals("ignoreShift")) { VI.ignoreShift = !VI.ignoreShift; user("> ignoreShift: " + VI.ignoreShift); } else if (token[1].equals("infiniteMoveDelayOn")) { VI.infiniteMoveDelayOn = !VI.infiniteMoveDelayOn; user("> infiniteMoveDelayOn: " + VI.infiniteMoveDelayOn); } else if (token[1].equals("globeRotationOn")) { VI.globeRotationOn = !VI.globeRotationOn; user("> globeRotationOn: " + VI.globeRotationOn); } else if (token[1].equals("skyboxRotationOn")) { VI.skyboxRotationOn = !VI.skyboxRotationOn; user("> skyboxRotationOn: " + VI.skyboxRotationOn); } else if (token[1].equals("printServerMessages")) { VI.printServerMessages = !VI.printServerMessages; user("> printServerMessages: " + VI.printServerMessages); // add more... } else { def(s); } } else { def(s); } } else if (token[0].equals("param")) { if (token.length > 1) { if (token[1].equals("shiftPressed")) { VI.shiftPressed = !VI.shiftPressed; user("> shiftPressed: " + VI.shiftPressed); // add more... } else { def(s); } } else { def(s); } } else if (token[0].equals("brush")) { int c = 0; if (token.length > 1) { try { c = Integer.parseInt(token[1]); } catch (NumberFormatException e) { return; } VI.brushedCategory = c; VI.brushingOn = true; user("> brushingOn: " + VI.brushingOn + " category: " + VI.brushedCategory); } else if (token.length == 1) { VI.brushingOn = !VI.brushingOn; user("> brushingOn: " + VI.brushingOn + " category: " + VI.brushedCategory); } else { def(s); } // add more... } else if (token[0].equals("help")) { help(); } else if (token[0].equals("quit")) { System.exit(0); } else { def(s); } } }