View Javadoc

1   package net.sf.crispy.impl.rest;
2   
3   import java.util.Hashtable;
4   import java.util.Vector;
5   
6   import net.sf.crispy.util.Converter;
7   
8   import org.xml.sax.Attributes;
9   import org.xml.sax.SAXException;
10  import org.xml.sax.helpers.DefaultHandler;
11  
12  public class RestContentHandler extends DefaultHandler {
13  
14  	private Hashtable map = new Hashtable();
15  	private String name = null;
16  	private String type = null;
17  	private Integer parentRef = null;
18  
19  	
20  	
21  	public Object getResult() {
22  		Object ret = map.get(Integer.valueOf("0"));
23  		return  ret;
24  	}
25  	
26  	public void startElement(String pvUri, String pvLocalName, String pvName, Attributes pvAttributes) throws SAXException {
27  		name = pvName;
28  		type = null;
29  		parentRef = null;
30  		if (name.equals("CLASS")) {
31  			name = null;
32  			Integer id = Integer.valueOf(pvAttributes.getValue("id"));
33  			String className = pvAttributes.getValue("type");
34  			Integer parentRef = Integer.valueOf(pvAttributes.getValue("parentRef"));
35  			String lvName = pvAttributes.getValue("name");
36  			try {
37  				Object classObject = Class.forName(className).newInstance();
38  				map.put(id, classObject);
39  				Object parent = map.get(parentRef);
40  				if (parent != null) {
41  					if (parent instanceof Hashtable) {
42  						if (lvName != null) { ((Hashtable) parent).put(lvName, classObject); }
43  					}
44  					else if (parent instanceof Vector) {
45  						((Vector) parent).add(classObject);
46  					}
47  				}
48  			} catch (Exception e) {
49  				e.printStackTrace();
50  			}
51  
52  		}
53  		
54  		else if (name.equals("property")) { 
55  			name = pvAttributes.getValue("name"); 
56  			type = pvAttributes.getValue("type");
57  			parentRef = Integer.valueOf(pvAttributes.getValue("parentRef"));
58  		}
59  	}
60  	
61  	public void endElement(String pvUri, String pvLocalName, String pvName) throws SAXException {
62  		name = null;
63  		type = null;
64  		parentRef = null;
65  	}
66  	
67  	
68  	public void characters(char[] pvCh, int pvStart, int pvLength) throws SAXException {
69  		StringBuffer sb = new StringBuffer();
70  		sb.append(pvCh, pvStart, pvLength);
71  //		String temp = (String) map.get(name);
72  //		if (temp != null) { sb.append(temp); }
73  //		map.put(name, sb.toString());			
74  
75  		if (name != null) {
76  			Object lvClassObject = map.get(parentRef);
77  			if (name.equals("class")) {
78  				if (lvClassObject instanceof Hashtable) {
79  					((Hashtable) lvClassObject).put(name, sb.toString());
80  				}
81  				else if (lvClassObject instanceof Vector) {
82  					System.out.println("--- Darf NICHT sein ---");
83  //					((Vector) lvClassObject).add (name, sb.toString());
84  				}
85  			} else {
86  				try {
87  					Object lvValue = Converter.convertString2Value(sb.toString(), type);
88  					if (lvClassObject instanceof Hashtable) {
89  						if (lvValue != null) {
90  							((Hashtable) lvClassObject).put(name, lvValue);
91  						}
92  					}
93  					else if (lvClassObject instanceof Vector) {
94  //						System.out.println("--- Darf NICHT sein: Vector ---");
95  						((Vector) lvClassObject).add (sb.toString());
96  					}
97  				} catch (Exception e) {
98  					e.printStackTrace();
99  				}
100 			}
101 		}
102 		// simple datatype
103 		else if ((name == null) && (parentRef.equals(Integer.valueOf("-1")))) {
104 			try {
105 				Object lvValue = Converter.convertString2Value(sb.toString(), type);
106 				map.put(Integer.valueOf("0"), lvValue);
107 			} catch (Exception e) {
108 				e.printStackTrace();
109 			}
110 		}
111 	}
112 
113 	public void startDocument() throws SAXException {
114 		map = new Hashtable();
115 		name = null;
116 	}
117 	
118 	public void endDocument() throws SAXException {	}
119 	
120 }