EventMappingDAO.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.udapsoft.waf.system.handler.dao;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.MalformedURLException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.Node;
  12. import org.w3c.dom.NodeList;
  13. import org.xml.sax.InputSource;
  14. import org.xml.sax.SAXException;
  15. import org.xml.sax.SAXParseException;
  16. public class EventMappingDAO {
  17. public static final String EVENT_MAPPING = "EVENT-MAPPING";
  18. public static final String SCREEN = "SCREEN";
  19. public static final String EVENT = "EVENT";
  20. public static final String BIZ = "BIZ";
  21. public EventMappingDAO() {
  22. }
  23. private static Element loadDocument(Class<?> pclass) {
  24. Document doc = null;
  25. Element root = null;
  26. try {
  27. InputStream is = null;
  28. String encoding = "";
  29. try {
  30. byte[] bytes = new byte[100];
  31. is = pclass.getResourceAsStream("event_mapping.xml");
  32. if (is.read(bytes) != -1) {
  33. encoding = new String(bytes);
  34. if (encoding.indexOf("encoding=\"") > 0) {
  35. encoding = encoding.substring(encoding.indexOf("encoding=\"") + 10, encoding.length());
  36. } else if (encoding.indexOf("'encoding='") > 0) {
  37. encoding = encoding.substring(encoding.indexOf("'encoding='"), encoding.length());
  38. }
  39. if (encoding.indexOf("\"") > 0) {
  40. encoding = encoding.substring(0, encoding.indexOf("\""));
  41. } else if (encoding.indexOf("'") > 0) {
  42. encoding = encoding.substring(0, encoding.indexOf("\""));
  43. }
  44. }
  45. } catch (Exception e) {
  46. System.err.println("EventMappingDAO encoding extract error: " + e);
  47. } finally {
  48. if (is != null)
  49. is.close();
  50. }
  51. is = pclass.getResourceAsStream("event_mapping.xml");
  52. InputSource xmlInp = new InputSource(is);
  53. xmlInp.setEncoding(encoding);
  54. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  55. DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
  56. doc = parser.parse(xmlInp);
  57. root = doc.getDocumentElement();
  58. root.normalize();
  59. } catch (SAXParseException err) {
  60. System.err.println("EventMappingDAO ** Parsing error, line " + err.getLineNumber() + ", uri " + err.getSystemId());
  61. System.err.println("EventMappingDAO error: " + err.getMessage());
  62. } catch (SAXException e) {
  63. System.err.println("EventMappingDAO error: " + e);
  64. } catch (MalformedURLException mfx) {
  65. System.err.println("EventMappingDAO error: " + mfx);
  66. } catch (IOException e) {
  67. System.err.println("EventMappingDAO error: " + e);
  68. } catch (Exception pce) {
  69. System.err.println("EventMappingDAO error: " + pce);
  70. }
  71. return root;
  72. }
  73. public static EventMappings loadEventMappings(Class<?> pclass) {
  74. Element root = loadDocument(pclass);
  75. if (root != null)
  76. return getEventMappings(root);
  77. else
  78. return null;
  79. }
  80. private static EventMappings getEventMappings(Element root) {
  81. EventMappings eventMappings = new EventMappings();
  82. NodeList screenNodeList = root.getElementsByTagName(SCREEN);
  83. for (int i = 0; i < screenNodeList.getLength(); i++) {
  84. Node screenNode = screenNodeList.item(i);
  85. if (screenNode != null) {
  86. String screenName = null;
  87. String eventName = null;
  88. List<String> bizList = null;
  89. screenName = ((Element) screenNode).getAttribute("name");
  90. NodeList eventNodeList = ((Element) screenNode).getElementsByTagName(EVENT);
  91. for (int j = 0; j < eventNodeList.getLength(); j++) {
  92. bizList = new ArrayList<String>();
  93. Node eventNode = eventNodeList.item(j);
  94. if (screenNode != null) {
  95. eventName = ((Element) eventNode).getAttribute("name");
  96. NodeList bizNodeList = ((Element) eventNode).getElementsByTagName(BIZ);
  97. for (int k = 0; k < bizNodeList.getLength(); k++) {
  98. Node bizNode = bizNodeList.item(k);
  99. if (bizNode != null) {
  100. String className = ((Element) bizNode).getAttribute("class");
  101. bizList.add(className);
  102. }
  103. }
  104. EventMapping eventMapping = new EventMapping(screenName, eventName, bizList);
  105. eventMappings.put(screenName, eventName, eventMapping);
  106. }
  107. }
  108. }
  109. }
  110. return eventMappings;
  111. }
  112. }