PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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