/NuSCRtoFBD/src/parselink/XmlParsing.java

http://fbdtoverilog.googlecode.com/ · Java · 521 lines · 397 code · 107 blank · 17 comment · 72 complexity · ca02221093432897f5ed38e3eaec4c39 MD5 · raw file

  1. package parselink;
  2. import java.io.FileInputStream;
  3. import java.util.List;
  4. import java.util.Stack;
  5. import main.MainWindow;
  6. import org.jdom.Document;
  7. import org.jdom.Element;
  8. import org.jdom.input.SAXBuilder;
  9. import sdt.ActionInformation;
  10. import sdt.ConditionInformation;
  11. import tts.StateForTTS;
  12. import tts.TransitionForTTS;
  13. import variableinformation.FODInformation;
  14. import variableinformation.FSMInformation;
  15. import variableinformation.GenericInformation;
  16. import variableinformation.InputInformation;
  17. import variableinformation.OutputInformation;
  18. import variableinformation.SDTInformation;
  19. import variableinformation.TTSInformation;
  20. import fod.TransitionForFOD;
  21. import fsm.StateForFSM;
  22. import fsm.TransitionForFSM;
  23. public class XmlParsing {
  24. MainWindow mainWindow;
  25. Stack<Object> xmlStack = new Stack<Object>(); //xml?? ??? stack
  26. Stack<String> stateStack = new Stack<String>(); //state? stack
  27. FODInformation fodInformation = new FODInformation();
  28. public int nodeCount=0;
  29. public XmlParsing(MainWindow mainWindow)
  30. {
  31. this.mainWindow = mainWindow;
  32. }
  33. public FODInformation LoadNuSCRXml(String filePath)
  34. {
  35. Document document = null;
  36. try{
  37. document = new SAXBuilder().build(new FileInputStream(filePath));
  38. }catch(Exception e)
  39. {
  40. e.printStackTrace();
  41. }
  42. //root element ??
  43. Element element = document.getRootElement();
  44. //root element? ?? ??
  45. System.out.println("Root Element Name :"+element.getName()+"\n");
  46. //child element? ??? ????
  47. List childElementList = element.getChildren();
  48. //root FOD??
  49. CreateRootFOD((Element)childElementList.get(0));
  50. HandlingElement(element);
  51. return this.fodInformation;
  52. }
  53. public void HandlingElement(Element element)
  54. {
  55. List childElementList = element.getChildren();
  56. if(childElementList.size()==0)
  57. return;
  58. for(int i=0;i<childElementList.size();i++)
  59. {
  60. Element childElement =(Element)childElementList.get(i);
  61. String elementName = ((Element)childElementList.get(i)).getName();
  62. System.out.println("Child element Name:"+elementName);
  63. if(elementName=="FOD")
  64. { CreateFOD(childElement); }
  65. else if(elementName=="SDT")
  66. { CreateSDT(childElement); }
  67. else if(elementName=="condition")
  68. { CreateCondition(childElement); }
  69. else if(elementName=="action")
  70. { CreateAction(childElement); }
  71. else if(elementName=="FSM")
  72. { CreateFSM(childElement); }
  73. else if(elementName=="TTS")
  74. { CreateTTS(childElement); }
  75. else if(elementName=="states")
  76. { CreateStates(childElement); }
  77. else if(elementName=="transitions")
  78. { CreateTransition(childElement); }
  79. else if(elementName=="input")
  80. { CreateInput(childElement); }
  81. else if(elementName=="output")
  82. { CreateOutput(childElement); }
  83. //else if(elementName=="nodes")
  84. //{}
  85. //else if(elementName=="gml")
  86. //{}
  87. if(((Element)childElementList.get(i)).getChildren().size()!=0)
  88. HandlingElement((Element)childElementList.get(i));
  89. System.out.println();
  90. }
  91. }
  92. public void CreateRootFOD(Element element)
  93. {
  94. nodeCount++;
  95. String[] ss;
  96. ss = element.getAttribute("clock").getValue().toString().split(";");
  97. for(String s:ss)
  98. this.fodInformation.localClockVarialbles.add(s); //????
  99. ss = element.getAttribute("constants").getValue().toString().split(";");
  100. for(String s:ss)
  101. this.fodInformation.constants.add(s);
  102. ss = element.getAttribute("description").getValue().toString().split(";");
  103. for(String s:ss)
  104. this.fodInformation.description.add(s);
  105. this.fodInformation.id=element.getAttribute("id").toString();
  106. ss = element.getAttribute("memoVar").getValue().toString().split(";");
  107. for(String s:ss)
  108. this.fodInformation.momoriazbleVariableofExternalinputs.add(s);
  109. this.fodInformation.name = element.getAttribute("name").getValue().toString();
  110. //(6) nodecount
  111. this.fodInformation.previousStateVariable = element.getAttribute("prevStateVar").getValue().toString();
  112. ss = element.getAttribute("templateNum").getValue().toString().split(";");
  113. for(String s:ss)
  114. this.fodInformation.templeteNumber.add(s);
  115. //(9) transitioncount
  116. xmlStack.push(fodInformation);
  117. }
  118. public void CreateFOD(Element element)
  119. {
  120. nodeCount++;
  121. Object o = xmlStack.peek();
  122. FODInformation parent = (FODInformation)o; //fod? ??? ??fod??.
  123. FODInformation fodInformation = new FODInformation();
  124. String[] ss;
  125. ss = element.getAttribute("clock").getValue().toString().split(";");
  126. for(String s:ss)
  127. fodInformation.localClockVarialbles.add(s); //????
  128. ss = element.getAttribute("constants").getValue().toString().split(";");
  129. for(String s:ss)
  130. fodInformation.constants.add(s);
  131. ss = element.getAttribute("description").getValue().toString().split(";");
  132. for(String s:ss)
  133. fodInformation.description.add(s);
  134. fodInformation.id=element.getAttribute("id").getValue().toString();
  135. ss = element.getAttribute("memoVar").getValue().toString().split(";");
  136. for(String s:ss)
  137. fodInformation.momoriazbleVariableofExternalinputs.add(s);
  138. fodInformation.name = element.getAttribute("name").getValue().toString();
  139. //(6) nodecount
  140. fodInformation.previousStateVariable = element.getAttribute("prevStateVar").getValue().toString();
  141. ss = element.getAttribute("templateNum").getValue().toString().split(";");
  142. for(String s:ss)
  143. fodInformation.templeteNumber.add(s);
  144. //(9) transitioncount
  145. parent.fods.add(fodInformation);
  146. //xmlStack.pop();
  147. xmlStack.push(fodInformation);
  148. }
  149. public void CreateSDT(Element element)
  150. {
  151. nodeCount++;
  152. Object o = xmlStack.peek();
  153. FODInformation parent = (FODInformation)o; //sdt? ??? ?? fod??.
  154. SDTInformation sdtInformation = new SDTInformation();
  155. String[] ss;
  156. ss = element.getAttribute("clock").getValue().toString().split(";");
  157. for(String s:ss)
  158. sdtInformation.localClockVarialbles.add(s); //????
  159. ss = element.getAttribute("constants").getValue().toString().split(";");
  160. for(String s:ss)
  161. sdtInformation.constants.add(s);
  162. ss = element.getAttribute("description").getValue().toString().split(";");
  163. for(String s:ss)
  164. sdtInformation.description.add(s);
  165. sdtInformation.id = element.getAttribute("id").getValue().toString();
  166. ss = element.getAttribute("memoVar").getValue().toString().split(";");
  167. for(String s:ss)
  168. sdtInformation.momoriazbleVariableofExternalinputs.add(s);
  169. sdtInformation.name = element.getAttribute("name").getValue().toString();
  170. sdtInformation.previousStateVariable = element.getAttribute("prevStateVar").getValue().toString();
  171. ss = element.getAttribute("templateNum").getValue().toString().split(";");
  172. for(String s:ss)
  173. sdtInformation.templeteNumber.add(s);
  174. //sdtInformation.yindex = element.GetAttribute(8);
  175. parent.sdts.add(sdtInformation);
  176. xmlStack.push(sdtInformation);
  177. }
  178. public void CreateCondition(Element element) //for SDT
  179. {
  180. Object o = xmlStack.peek();
  181. SDTInformation sdtInformation = (SDTInformation)o;
  182. int nCol = Integer.parseInt(element.getAttribute("nCol").getValue().toString());
  183. int nRow = Integer.parseInt(element.getAttribute("nRow").getValue().toString());
  184. int conditionCount = -1;
  185. List childElementList = element.getChildren();
  186. for(int i=0;i<childElementList.size();i++)
  187. {
  188. String s = ((Element)childElementList.get(i)).getAttribute("col").getValue().toString();
  189. if (s.equals("0"))
  190. { //??? condition??
  191. ConditionInformation conditionInformation = new ConditionInformation();
  192. conditionInformation.condition = ((Element)childElementList.get(i)).getAttribute("value").getValue().toString();
  193. if (conditionInformation.condition != "")
  194. {
  195. sdtInformation.conditions.add(conditionInformation);
  196. conditionCount++;
  197. }
  198. else
  199. {
  200. for (int j = 0; j < nCol-1; j++)
  201. {
  202. i++;
  203. }
  204. }
  205. }
  206. else
  207. { //???? ??? condition? content??
  208. ((ConditionInformation)(sdtInformation.conditions.get(conditionCount))).contents.add
  209. (((Element)childElementList.get(i)).getAttribute("value").getValue().toString());
  210. }
  211. }
  212. }
  213. public void CreateAction(Element element)
  214. {
  215. Object o = xmlStack.peek();
  216. SDTInformation sdtInformation = (SDTInformation)o;
  217. int nCol = Integer.parseInt(element.getAttribute("nCol").getValue().toString());
  218. int nRow = Integer.parseInt(element.getAttribute("nRow").getValue().toString());
  219. int actionCount = -1;
  220. List childElementList = element.getChildren();
  221. for(int i=0;i<childElementList.size();i++)
  222. {
  223. String s = ((Element)childElementList.get(i)).getAttribute("col").getValue().toString();
  224. if (s.equals("0"))
  225. { //??? action??
  226. ActionInformation actionInformation = new ActionInformation();
  227. actionInformation.action = ((Element)childElementList.get(i)).getAttribute("value").getValue().toString();
  228. if(actionInformation.action!="")
  229. {
  230. sdtInformation.actions.add(actionInformation);
  231. actionCount++;
  232. }
  233. }
  234. else
  235. { //???? ??? action? content??
  236. ((ActionInformation)(sdtInformation.actions.get(actionCount))).contents.add
  237. (((Element)childElementList.get(i)).getAttribute("value").getValue().toString());
  238. }
  239. }
  240. xmlStack.pop();
  241. }
  242. public void CreateFSM(Element element)
  243. {
  244. nodeCount++;
  245. Object o = xmlStack.peek();
  246. FODInformation parent = (FODInformation)o; //sdt? ??? ?? fod??.
  247. FSMInformation fsmInformation = new FSMInformation();
  248. String[] ss;
  249. ss = element.getAttribute("clock").getValue().toString().split(";");
  250. for(String s:ss)
  251. fsmInformation.localClockVarialbles.add(s); //????
  252. ss = element.getAttribute("constants").getValue().toString().split(";");
  253. for(String s:ss)
  254. fsmInformation.constants.add(s);
  255. ss = element.getAttribute("description").getValue().toString().split(";");
  256. for(String s:ss)
  257. fsmInformation.description.add(s);
  258. fsmInformation.id = element.getAttribute("id").getValue().toString();
  259. ss = element.getAttribute("memoVar").getValue().toString().split(";");
  260. for(String s:ss)
  261. fsmInformation.momoriazbleVariableofExternalinputs.add(s);
  262. fsmInformation.initReference = element.getAttribute("initialRef").getValue().toString();
  263. fsmInformation.name = element.getAttribute("name").getValue().toString();
  264. fsmInformation.previousStateVariable = element.getAttribute("prevStateVar").getValue().toString();
  265. ss = element.getAttribute("templateNum").getValue().toString().split(";");
  266. for(String s:ss)
  267. fsmInformation.templeteNumber.add(s);
  268. //sdtInformation.yindex = element.GetAttribute(8);
  269. parent.fsms.add(fsmInformation);
  270. xmlStack.push(fsmInformation);
  271. }
  272. public void CreateTTS(Element element)
  273. {
  274. nodeCount++;
  275. Object o = xmlStack.peek();
  276. FODInformation parent = (FODInformation)o; //sdt? ??? ?? fod??.
  277. TTSInformation ttsInformation = new TTSInformation();
  278. String[] ss;
  279. ss = element.getAttribute("clock").getValue().toString().split(";");
  280. for(String s:ss)
  281. ttsInformation.localClockVarialbles.add(s); //????
  282. ss = element.getAttribute("constants").getValue().toString().split(";");
  283. for(String s:ss)
  284. ttsInformation.constants.add(s);
  285. ss = element.getAttribute("description").getValue().toString().split(";");
  286. for(String s:ss)
  287. ttsInformation.description.add(s);
  288. ttsInformation.id = element.getAttribute("id").getValue().toString();
  289. ss = element.getAttribute("memoVar").getValue().toString().split(";");
  290. for(String s:ss)
  291. ttsInformation.momoriazbleVariableofExternalinputs.add(s);
  292. ttsInformation.initReference = element.getAttribute("initialRef").getValue().toString();
  293. ttsInformation.name = element.getAttribute("name").getValue().toString();
  294. ttsInformation.previousStateVariable = element.getAttribute("prevStateVar").getValue().toString();
  295. ss = element.getAttribute("templateNum").getValue().toString().split(";");
  296. for(String s:ss)
  297. ttsInformation.templeteNumber.add(s);
  298. //sdtInformation.yindex = element.GetAttribute(8);
  299. parent.ttss.add(ttsInformation);
  300. xmlStack.push(ttsInformation);
  301. }
  302. public void CreateStates(Element element)
  303. {
  304. List childElementList = element.getChildren();
  305. if (xmlStack.peek() instanceof FSMInformation)
  306. {
  307. FSMInformation fsmInformation = (FSMInformation)xmlStack.peek();
  308. for(int i=0;i<childElementList.size();i++)
  309. {
  310. StateForFSM state = new StateForFSM();
  311. state.id = (((Element)childElementList.get(i)).getAttribute("id").getValue().toString());
  312. state.name = (((Element)childElementList.get(i)).getAttribute("name").getValue().toString());
  313. if(state.name.charAt(state.name.length()-1)=='_')
  314. continue;
  315. fsmInformation.states.add(state);
  316. }
  317. }
  318. else if (xmlStack.peek() instanceof TTSInformation)
  319. {
  320. TTSInformation ttsInformation = (TTSInformation)xmlStack.peek();
  321. for(int i=0;i<childElementList.size();i++)
  322. {
  323. StateForTTS state = new StateForTTS();
  324. state.id = (((Element)childElementList.get(i)).getAttribute("id").getValue().toString());
  325. state.name = (((Element)childElementList.get(i)).getAttribute("name").getValue().toString());
  326. ttsInformation.states.add(state);
  327. }
  328. }
  329. }
  330. public void CreateTransition(Element element)
  331. {
  332. nodeCount++;
  333. // Element && states ?? element ??
  334. List childElementList = element.getChildren();
  335. if (xmlStack.peek() instanceof FSMInformation)
  336. {
  337. FSMInformation fsmInformation = (FSMInformation)xmlStack.peek();
  338. for(int i=0;i<childElementList.size();i++)
  339. {
  340. TransitionForFSM transition = new TransitionForFSM();
  341. transition.id = (((Element)childElementList.get(i)).getAttribute("id").getValue().toString());
  342. List subChildElementList =((Element)childElementList.get(i)).getChildren();
  343. transition.sourceid = ((Element)subChildElementList.get(0)).getAttribute("refId").getValue().toString();
  344. transition.sourcename = ((Element)subChildElementList.get(0)).getAttribute("refName").getValue().toString();
  345. transition.targetid = ((Element)subChildElementList.get(1)).getAttribute("refId").getValue().toString();
  346. transition.targetname = ((Element)subChildElementList.get(1)).getAttribute("refName").getValue().toString();
  347. transition.condition = ((Element)subChildElementList.get(2)).getValue().toString().replace("\n", "");
  348. transition.assignment = ((Element)subChildElementList.get(3)).getValue().toString().replace("\n", "");
  349. fsmInformation.transitions.add(transition);
  350. }
  351. }
  352. else if (xmlStack.peek() instanceof TTSInformation)
  353. {
  354. TTSInformation ttsInformation = (TTSInformation)xmlStack.peek();
  355. for(int i=0;i<childElementList.size();i++)
  356. {
  357. TransitionForTTS transition = new TransitionForTTS();
  358. transition.id = (((Element)childElementList.get(i)).getAttribute("id").getValue().toString());
  359. List subChildElementList =((Element)childElementList.get(i)).getChildren();
  360. transition.sourceid = ((Element)subChildElementList.get(0)).getAttribute("refId").getValue().toString();
  361. transition.sourcename = ((Element)subChildElementList.get(0)).getAttribute("refName").getValue().toString();
  362. transition.targetid = ((Element)subChildElementList.get(1)).getAttribute("refId").getValue().toString();
  363. transition.targetname = ((Element)subChildElementList.get(1)).getAttribute("refName").getValue().toString();
  364. if(subChildElementList.size()==4)
  365. {
  366. transition.condition = ((Element)subChildElementList.get(2)).getValue().toString();
  367. transition.assignment = ((Element)subChildElementList.get(3)).getValue().toString();
  368. }
  369. else if(subChildElementList.size()==5)
  370. {
  371. transition.condition = "["+((Element)subChildElementList.get(2)).getAttribute("start").getValue().toString()+",";
  372. transition.condition += ((Element)subChildElementList.get(2)).getAttribute("end").getValue().toString()+"]";
  373. transition.condition += "("+((Element)subChildElementList.get(3)).getValue().toString()+")";
  374. transition.assignment = ((Element)subChildElementList.get(4)).getValue().toString().replace("\n", "");;
  375. }
  376. ttsInformation.transitions.add(transition);
  377. }
  378. }
  379. else if (xmlStack.peek() instanceof FODInformation)
  380. {
  381. FODInformation fodInformation = (FODInformation)xmlStack.peek();
  382. for(int i=0;i<childElementList.size();i++)
  383. {
  384. TransitionForFOD transition = new TransitionForFOD();
  385. transition.sourceid = transition.id = (((Element)childElementList.get(i)).getAttribute("id").getValue().toString());
  386. List subChildelementList = ((Element)childElementList.get(i)).getChildren();
  387. transition.sourceid = ((Element)subChildelementList.get(0)).getAttribute("refId").getValue().toString();
  388. transition.sourcename = ((Element)subChildelementList.get(0)).getAttribute("refName").getValue().toString();
  389. transition.targetid = ((Element)subChildelementList.get(1)).getAttribute("refId").getValue().toString();
  390. transition.targetname = ((Element)subChildelementList.get(1)).getAttribute("refName").getValue().toString();
  391. fodInformation.transitions.add(transition);
  392. }
  393. }
  394. xmlStack.pop();
  395. }
  396. public void CreateInput(Element element)
  397. {
  398. Object o = xmlStack.peek();
  399. GenericInformation genericInformation = (GenericInformation)o;
  400. InputInformation inputInformation = new InputInformation();
  401. inputInformation.id = (element.getAttribute("id").getValue().toString());
  402. inputInformation.name = (element.getAttribute("name").getValue().toString());
  403. genericInformation.inputs.add(inputInformation);
  404. }
  405. public void CreateOutput(Element element)
  406. {
  407. Object o = xmlStack.peek();
  408. GenericInformation genericInformation = (GenericInformation)o;
  409. OutputInformation inputInformation = new OutputInformation();
  410. inputInformation.id = (element.getAttribute("id").getValue().toString());
  411. inputInformation.name = (element.getAttribute("name").getValue().toString());
  412. genericInformation.outputs.add(inputInformation);
  413. }
  414. }