package com.ericsson.tic.vi; import org.w3c.dom.Node; import org.w3c.dom.Text; import org.w3c.dom.NodeList; import org.w3c.dom.Element; import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Reads and parses the XML file used during startup. * * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class XMLInterface { // explain naming convention here... // it's hierarchical - and follows the XML tree structure. nuff said? // data // DataSet // SetId // = dataDataSetSetId // etc... public static int totalData; public static int totalCategories; public static int totalNodeTypes; public static int[] totalCategoryItems; public static int totalCommands; public static int[] dataDataSetId; public static String[] dataDataSetName; public static String[] dataDataSetPName; public static int[] dataDataSetSet; public static int[] dataDataSetCat; public static int[] dataDataSetParseStrat; public static int[] dataDataSetDimension; public static int[] dataDataSetNodeType; public static int[] dataDataSetMax; public static int[] dataDataSetMin; public static float[][][] dataDataSetColor; public static int[] categoriesCategoryId; public static String[] categoriesCategoryName; public static int[] categoriesCategoryDatabaseTableID; public static String[][] categoriesCategoryCategoryItemName; public static String[][] categoriesCategoryCategoryItemParseName; public static float[][][][] categoriesCategoryCategoryItemColor; public static int[] commandsCommandId; public static String[] commandsCommandName; public static String[] commandsCommandMessagePrefix; public static String[] commandsCommandMessage; public static String windowTitle; /* public static String databaseHost; public static String databasePort; public static String databaseSchema; public static String databaseUser; public static String databasePassword; */ public static String masterHost; public static String masterPort; public static String keepAliveHost; public static String keepAlivePort; public static String inputPort; public static int timeOut; public static int defaultValue; public static int defaultBarType; public static boolean betterLightingOn = false; public static boolean fogOn = false; public static boolean barShadingOn = false; public static boolean nodeShadingOn = false; public static boolean lineAAOn = false; public static boolean skyboxOn = false; /// note: hardcoded for TIC!! public static int TICC; public static int TICA; public static String topologyFileName; public static int topologyNodeTypeId[]; public static String topologyNodeTypeName[]; public static int topologyNodeTypeLevel[]; public static float topologyNodeTypeNullColor[][]; /** * Reads the xml file. */ public static void readConfiguration() { String xmlFile = "vi_interface.xml"; File file = new File(xmlFile); if (file.exists()){ try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); Document doc = factory.newDocumentBuilder().parse(xmlFile); readDocument(doc); } catch(Exception e) { e.printStackTrace(); } } else { System.out.print("XMLFile not found! (vi_interface.xml)"); System.exit(1); } } private static void readDocument(Document doc) { Node[] root = stripTextNodes(doc.getChildNodes()); Node[] rootChildren = getChildren(root[0]); Node[] dn = getChildrenWithName(rootChildren, "Initialization"); readInit(dn); dn = getChildrenWithName(rootChildren, "Topology"); readTopology(dn); dn = getChildrenWithName(rootChildren, "Data"); readData(dn); dn = getChildrenWithName(rootChildren, "Categories"); readCategories(dn); dn = getChildrenWithName(rootChildren, "Commands"); readCommands(dn); // continue... } private static void readTopology(Node[] node) { Node[] fetch = getChildrenWithName(node[0], "File"); topologyFileName = getValue(fetch[0]); Node[] nodeTypes = getChildrenWithName(node[0], "NodeTypes"); Node[] nodeType = getChildrenWithName(nodeTypes[0], "NodeType"); totalNodeTypes = nodeType.length; topologyNodeTypeId = new int[totalNodeTypes]; topologyNodeTypeName = new String[totalNodeTypes]; topologyNodeTypeLevel = new int[totalNodeTypes]; topologyNodeTypeNullColor = new float[totalNodeTypes][3]; for (int i = 0; i < nodeType.length; i++) { Node[] id = getChildrenWithName(nodeType[i], "ID"); topologyNodeTypeId[i] = Integer.parseInt(getValue(id[0])); Node[] name = getChildrenWithName(nodeType[i], "Name"); topologyNodeTypeName[i] = getValue(name[0]); Node[] level = getChildrenWithName(nodeType[i], "Level"); topologyNodeTypeLevel[i] = Integer.parseInt(getValue(level[0])); Node[] colors = getChildrenWithName(nodeType[i], "Colors"); topologyNodeTypeNullColor[i] = getColor(colors[0]); } totalNodeTypes++; // zero is reserved } private static void readInit(Node[] node) { Node[] wtitle = getChildrenWithName(node[0], "WindowTitle"); windowTitle = getValue(wtitle[0]); /* Node[] dhost = getChildrenWithName(node[0], "DatabaseHost"); databaseHost = getValue(dhost[0]); Node[] dport = getChildrenWithName(node[0], "DatabasePort"); databasePort = getValue(dport[0]); Node[] dschema = getChildrenWithName(node[0], "DatabaseSchema"); databaseSchema = getValue(dschema[0]); Node[] duser = getChildrenWithName(node[0], "DatabaseUser"); databaseUser = getValue(duser[0]); Node[] dpass = getChildrenWithName(node[0], "DatabasePassword"); databasePassword = getValue(dpass[0]); */ Node[] mhost = getChildrenWithName(node[0], "MasterHost"); masterHost = getValue(mhost[0]); Node[] mport = getChildrenWithName(node[0], "MasterPort"); masterPort = getValue(mport[0]); Node[] sockBuf = getChildrenWithName(node[0], "SocketBufferSize"); VI.socketBufferSize = Integer.parseInt(getValue(sockBuf[0])); Node[] kahost = getChildrenWithName(node[0], "KeepAliveHost"); keepAliveHost = getValue(kahost[0]); Node[] kaport = getChildrenWithName(node[0], "KeepAlivePort"); keepAlivePort = getValue(kaport[0]); Node[] iport = getChildrenWithName(node[0], "InputPort"); inputPort = getValue(iport[0]); Node[] timeo = getChildrenWithName(node[0], "TimeOut"); timeOut = 1000*Integer.parseInt(getValue(timeo[0])); Node[] dval = getChildrenWithName(node[0], "DefaultValue"); defaultValue = Integer.parseInt(getValue(dval[0])); Node[] dtype = getChildrenWithName(node[0], "DefaultBarType"); defaultBarType = Integer.parseInt(getValue(dtype[0])); Node[] lighting = getChildrenWithName(node[0], "AdvancedLighting"); VI.betterLightingOn = Boolean.parseBoolean(getValue(lighting[0])); Node[] fog = getChildrenWithName(node[0], "Fog"); VI.fogOn = Boolean.parseBoolean(getValue(fog[0])); Node[] barShading = getChildrenWithName(node[0], "BarShading"); VI.barShadingOn = Boolean.parseBoolean(getValue(barShading[0])); Node[] nodeShading = getChildrenWithName(node[0], "NodeShading"); VI.nodeShadingOn = Boolean.parseBoolean(getValue(nodeShading[0])); Node[] lineAA = getChildrenWithName(node[0], "AntiAliasing"); VI.lineAAOn = Boolean.parseBoolean(getValue(lineAA[0])); Node[] skybox = getChildrenWithName(node[0], "Skybox"); VI.skyboxOn = Boolean.parseBoolean(getValue(skybox[0])); } private static void readCommands(Node[] node) { for (int i = 0; i < node.length; i++) { Node[] dn = getChildrenWithName(node[i], "Command"); totalCommands = dn.length; commandsCommandId = new int[totalCommands]; commandsCommandName = new String[totalCommands]; commandsCommandMessagePrefix = new String[totalCommands]; commandsCommandMessage = new String[totalCommands]; for (int j = 0; j < dn.length; j++) { Node[] id = getChildrenWithName(dn[j], "ID"); commandsCommandId[j] = Integer.parseInt(getValue(id[0])); Node[] name = getChildrenWithName(dn[j], "Name"); commandsCommandName[j] = getValue(name[0]); Node[] mpre = getChildrenWithName(dn[j], "MessagePrefix"); commandsCommandMessagePrefix[j] = getValue(mpre[0]); Node[] m = getChildrenWithName(dn[j], "Message"); commandsCommandMessage[j] = getValue(m[0]); } } } private static void readCategories(Node[] node) { for (int i = 0; i < node.length; i++) { Node[] dn = getChildrenWithName(node[i], "Category"); totalCategories = dn.length; categoriesCategoryId = new int[totalCategories]; categoriesCategoryName = new String[totalCategories]; totalCategoryItems = new int[totalCategories]; for (int j = 0; j < dn.length; j++) { Node[] id = getChildrenWithName(dn[j], "ID"); categoriesCategoryId[j] = Integer.parseInt(getValue(id[0])); Node[] name = getChildrenWithName(dn[j], "Name"); categoriesCategoryName[j] = getValue(name[0]); Node[] catItems = getChildrenWithName(dn[j], "CategoryItem"); totalCategoryItems[j] = catItems.length; categoriesCategoryCategoryItemName = new String[totalCategories][totalCategoryItems[j]]; categoriesCategoryCategoryItemParseName = new String[totalCategories][totalCategoryItems[j]]; categoriesCategoryCategoryItemColor = new float[totalCategories][totalCategoryItems[j]][1][3]; for (int k = 0; k < totalCategoryItems[j]; k++) { Node[] iname = getChildrenWithName(catItems[k], "Name"); categoriesCategoryCategoryItemName[j][k] = getValue(iname[0]); Node[] ipname = getChildrenWithName(catItems[k], "ParseName"); categoriesCategoryCategoryItemParseName[j][k] = getValue(ipname[0]); Node[] colors = getChildrenWithName(catItems[k], "Colors"); categoriesCategoryCategoryItemColor[j][k] = getColors(colors[0]); } } totalCategories++; // zero is reserved } } private static void readData(Node[] node) { for (int i = 0; i < node.length; i++) { Node[] dn = getChildrenWithName(node[i], "DataSet"); totalData = dn.length; dataDataSetId = new int[totalData]; dataDataSetName = new String[totalData]; dataDataSetPName = new String[totalData]; dataDataSetSet = new int[totalData]; dataDataSetCat = new int[totalData]; dataDataSetParseStrat = new int[totalData]; dataDataSetDimension = new int[totalData]; dataDataSetMax = new int[totalData]; dataDataSetMin = new int[totalData]; dataDataSetNodeType = new int[totalData]; dataDataSetColor = new float[totalData][2][3]; for (int j = 0; j < dn.length; j++) { Node[] id = getChildrenWithName(dn[j], "ID"); dataDataSetId[j] = Integer.parseInt(getValue(id[0])); Node[] name = getChildrenWithName(dn[j], "Name"); dataDataSetName[j] = getValue(name[0]); Node[] pname = getChildrenWithName(dn[j], "ParseName"); dataDataSetPName[j] = getValue(pname[0]); Node[] set = getChildrenWithName(dn[j], "Set"); dataDataSetSet[j] = Integer.parseInt(getValue(set[0])); Node[] cat = getChildrenWithName(dn[j], "Category"); dataDataSetCat[j] = Integer.parseInt(getValue(cat[0])); Node[] pstrat = getChildrenWithName(dn[j], "ParseStrat"); dataDataSetParseStrat[j] = Integer.parseInt(getValue(pstrat[0])); Node[] dim = getChildrenWithName(dn[j], "Dimension"); dataDataSetDimension[j] = Integer.parseInt(getValue(dim[0])); Node[] type = getChildrenWithName(dn[j], "NodeType"); dataDataSetNodeType[j] = Integer.parseInt(getValue(type[0])); Node[] max = getChildrenWithName(dn[j], "Max"); dataDataSetMax[j] = Integer.parseInt(getValue(max[0])); Node[] min = getChildrenWithName(dn[j], "Min"); dataDataSetMin[j] = Integer.parseInt(getValue(min[0])); Node[] colors = getChildrenWithName(dn[j], "Colors"); dataDataSetColor[j] = getColors(colors[0]); } } } private static float[] getColor(Node colors) { float[] colorArr; if (colors == null) { colorArr = new float[3]; colorArr[0] = 0.0f; colorArr[1] = 0.0f; colorArr[2] = 0.0f; } else { Node[] color = getChildrenWithName(colors, "Color"); colorArr = new float[3]; Node[] red = getChildrenWithName(color[0], "Red"); Node[] green = getChildrenWithName(color[0], "Green"); Node[] blue = getChildrenWithName(color[0], "Blue"); colorArr[0] = Float.parseFloat(getValue(red[0])); colorArr[1] = Float.parseFloat(getValue(green[0])); colorArr[2] = Float.parseFloat(getValue(blue[0])); } return colorArr; } private static float[][] getColors(Node colors) { float[][] colorArr; if (colors == null) { colorArr = new float[2][3]; colorArr[0][0] = 0.0f; colorArr[0][1] = 0.0f; colorArr[0][2] = 0.0f; colorArr[1][0] = 0.0f; colorArr[1][1] = 0.0f; colorArr[1][2] = 0.0f; } else { Node[] color = getChildrenWithName(colors, "Color"); colorArr = new float[color.length][3]; for (int i = 0; i < color.length; i++) { Node[] red = getChildrenWithName(color[i], "Red"); Node[] green = getChildrenWithName(color[i], "Green"); Node[] blue = getChildrenWithName(color[i], "Blue"); colorArr[i][0] = Float.parseFloat(getValue(red[0])); colorArr[i][1] = Float.parseFloat(getValue(green[0])); colorArr[i][2] = Float.parseFloat(getValue(blue[0])); } } return colorArr; } private static void printChildren(Node n) { if (n instanceof Text) { System.out.print("text: "); printElement(n.getParentNode().getFirstChild()); } if (n instanceof Element) { System.out.print("element: "); printElement(n); } if (n.hasChildNodes()) { NodeList nList = n.getChildNodes(); for (int i = 0; i < nList.getLength(); i++) { printChildren(nList.item(i)); } } } private static void printElement(Node n) { System.out.print("name: " + n.getNodeName()); if (n.hasChildNodes()) { System.out.println(" value: " + n.getNodeValue()); } else { System.out.println(" value: " + n.getNodeValue()); } } private static String getValue(Node n) { if (n instanceof Element) { Node p = n.getFirstChild(); if (p instanceof Text) { return p.getNodeValue(); } } return null; } private static Node[] getChildren(Node n) { NodeList l; if (n.hasChildNodes()) { l = n.getChildNodes(); } else { return null; } int c = 0; for (int i = 0; i < l.getLength(); i++) { if (l.item(i) instanceof Element) { c++; } } if (c == 0) { return null; } Node[] ret = new Node[c]; c = 0; for (int i = 0; i < l.getLength(); i++) { if (l.item(i) instanceof Element) { ret[c] = l.item(i); c++; } } return ret; } private static Node[] getChildrenWithName(Node n, String name) { NodeList l; if (n.hasChildNodes()) { l = n.getChildNodes(); } else { return null; } int c = 0; for (int i = 0; i < l.getLength(); i++) { if (l.item(i) instanceof Element && l.item(i).getNodeName().equals(name)) { c++; } } if (c == 0) { return null; } Node[] ret = new Node[c]; c = 0; for (int i = 0; i < l.getLength(); i++) { if (l.item(i) instanceof Element && l.item(i).getNodeName().equals(name)) { ret[c] = l.item(i); c++; } } return ret; } private static Node[] getChildrenWithName(Node[] l, String name) { int c = 0; for (int i = 0; i < l.length; i++) { if (l[i] instanceof Element && l[i].getNodeName().equals(name)) { c++; } } if (c == 0) { return null; } Node[] ret = new Node[c]; c = 0; for (int i = 0; i < l.length; i++) { if (l[i] instanceof Element && l[i].getNodeName().equals(name)) { ret[c] = l[i]; c++; } } return ret; } private static Node[] stripTextNodes(NodeList l) { int c = 0; for (int i = 0; i < l.getLength(); i++) { if (l.item(i) instanceof Element) { c++; } } if (c == 0) { return null; } Node[] ret = new Node[c]; c = 0; for (int i = 0; i < l.getLength(); i++) { if (l.item(i) instanceof Element) { ret[c] = l.item(i); c++; } } return ret; } private static void readDatabase(Node[] db) { String tableName = ""; String[] columnName; String[] columnType; String[] columnMap; for (int i = 0; i < db.length; i++) { Node[] db_tn = getChildrenWithName(db[0], "TableName"); tableName = getValue(db_tn[0]); Node[] db_co = getChildrenWithName(db[0], "Column"); columnName = new String[db_co.length]; columnType = new String[db_co.length]; columnMap = new String[db_co.length]; for (int j = 0; j < db_co.length; j++) { Node[] db_co_nm = getChildrenWithName(db_co[j], "Name"); columnName[j] = getValue(db_co_nm[0]); Node[] db_co_ty = getChildrenWithName(db_co[j], "Type"); columnType[j] = getValue(db_co_ty[0]); Node[] db_co_ma = getChildrenWithName(db_co[j], "Map"); columnMap[j] = getValue(db_co_ma[0]); } } } }