/ws-tester-xor/ws-tester-xor-engine/src/main/java/com/googlecode/ws/tester/xor/engine/SAXEventHandler.java

http://ws-tester.googlecode.com/ · Java · 132 lines · 112 code · 14 blank · 6 comment · 14 complexity · ec235a5962a779bbfbdd83c38fd9ed3a MD5 · raw file

  1. package com.googlecode.ws.tester.xor.engine;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.xml.sax.helpers.DefaultHandler;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.Attributes;
  6. import com.googlecode.ws.tester.xor.engine.model.Namespace;
  7. import java.util.*;
  8. import java.lang.reflect.Method;
  9. import java.lang.reflect.InvocationTargetException;
  10. /**
  11. * Created by IntelliJ IDEA.
  12. * User: pablo
  13. * Date: 2009-06-08
  14. * Time: 20:25:54
  15. */
  16. public class SAXEventHandler extends DefaultHandler {
  17. private List<Namespace> namespaces = new ArrayList<Namespace>();
  18. private Config config;
  19. private Deque<Object> elements = new ArrayDeque<Object>();
  20. private Object root;
  21. public Config getConfig() {
  22. return config;
  23. }
  24. public void setConfig(Config config) {
  25. this.config = config;
  26. }
  27. @Override
  28. public void startPrefixMapping(String prefix, String uri) throws SAXException {
  29. Namespace namespace = new Namespace();
  30. namespace.setPrefix(prefix);
  31. namespace.setUri(uri);
  32. namespaces.add(namespace);
  33. }
  34. @Override
  35. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  36. if (config.getElements().containsKey(localName)) {
  37. Element element = config.getElements().get(localName);
  38. try {
  39. Class<?> aClass = Class.forName(element.getClassName());
  40. Object o = aClass.newInstance();
  41. Object parent = null;
  42. if (!elements.isEmpty()) {
  43. parent = elements.peek();
  44. }
  45. elements.push(o);
  46. if (Boolean.valueOf(element.getRoot())) {
  47. root = o;
  48. }
  49. if (Boolean.valueOf(element.getNamespaces())) {
  50. Method[] methods = aClass.getMethods();
  51. for (Method m : methods) {
  52. if (m.getName().equals("setNamespaces")) {
  53. m.invoke(o, namespaces);
  54. namespaces = new ArrayList<Namespace>();
  55. break;
  56. }
  57. }
  58. }
  59. if (!Boolean.valueOf(element.getRoot())) {
  60. String methodName = "";
  61. if (Boolean.valueOf(element.getCollection())) {
  62. methodName = "add" + StringUtils.capitalize(localName);
  63. } else {
  64. methodName = "set" + StringUtils.capitalize(localName);
  65. }
  66. try {
  67. Method method = parent.getClass().getMethod(methodName, o.getClass());
  68. method.invoke(parent, o);
  69. } catch (NoSuchMethodException nsme) {
  70. }
  71. }
  72. if (attributes.getLength() > 0) {
  73. int attrLength = attributes.getLength();
  74. for (int i = 0; i < attrLength; i++) {
  75. Method[] methods = o.getClass().getMethods();
  76. String methodName = "set" + StringUtils.capitalize(attributes.getLocalName(i));
  77. for (Method method : methods) {
  78. if (method.getName().equals(methodName)) {
  79. method.invoke(o, attributes.getValue(i));
  80. }
  81. }
  82. }
  83. }
  84. } catch (ClassNotFoundException e) {
  85. System.out.println(e.getMessage());
  86. } catch (IllegalAccessException e) {
  87. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  88. } catch (InvocationTargetException e) {
  89. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  90. } catch (InstantiationException e) {
  91. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  92. }
  93. }
  94. }
  95. @Override
  96. public void endElement(String uri, String localName, String qName) throws SAXException {
  97. if (config.getElements().containsKey(localName)) {
  98. elements.pop();
  99. }
  100. }
  101. @Override
  102. public void characters(char[] ch, int start, int length) throws SAXException {
  103. try {
  104. Object o = elements.peek();
  105. Method m = o.getClass().getMethod("setValue", String.class);
  106. char[] tmp = Arrays.copyOfRange(ch, start, start+length);
  107. m.invoke(o, new String(tmp));
  108. } catch (NoSuchMethodException e) {
  109. } catch (IllegalAccessException e) {
  110. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  111. } catch (InvocationTargetException e) {
  112. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  113. }
  114. }
  115. public Object getRoot() {
  116. return root;
  117. }
  118. }