1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.org.usurper.handlers.defaults;
21
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.parsers.FactoryConfigurationError;
28 import javax.xml.parsers.ParserConfigurationException;
29
30 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
31 import org.org.usurper.handlers.exceptions.PropertyTypeHandlingException;
32 import org.org.usurper.model.HandledBeanProperty;
33 import org.org.usurper.model.HandledConstructorArg;
34 import org.org.usurper.model.PropertyTypeDefinition;
35 import org.org.usurper.utils.RandomUtils;
36 import org.w3c.dom.DOMException;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Node;
39
40
41
42
43
44 public class DocumentPropertyTypeHandler extends AbstractPropertyTypeHandler {
45 private static Set<PropertyTypeDefinition> handledTypes;
46 static {
47 handledTypes = new HashSet<PropertyTypeDefinition>();
48 handledTypes.add(new PropertyTypeDefinition(Document.class));
49 }
50
51
52
53
54 public DocumentPropertyTypeHandler() {
55
56 super(handledTypes);
57 }
58
59
60
61
62 public Object handle(HandledBeanProperty handledBeanProperty) {
63 Document document = null;
64 try {
65 document = doHandle();
66 } catch (ParserConfigurationException e) {
67 throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
68 } catch (FactoryConfigurationError e) {
69 throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
70 }
71 return document;
72 }
73
74
75
76
77 public Object handle(HandledConstructorArg handledConstructorArg) {
78 Document document = null;
79 try {
80 document = doHandle();
81 } catch (ParserConfigurationException e) {
82 throw new PropertyTypeHandlingException("Unable to handle constructor arg <#" + handledConstructorArg.getConstructorArgOrderingNumber() + "(" + handledConstructorArg.getConstructorArgClass().getName() + ")> from constructor " + handledConstructorArg.getTargetConstructor().getName(), e);
83 } catch (FactoryConfigurationError e) {
84 throw new PropertyTypeHandlingException("Unable to handle constructor arg <#" + handledConstructorArg.getConstructorArgOrderingNumber() + "(" + handledConstructorArg.getConstructorArgClass().getName() + ")> from constructor " + handledConstructorArg.getTargetConstructor().getName(), e);
85 }
86 return document;
87 }
88
89
90
91
92
93
94 private Document doHandle() throws ParserConfigurationException, DOMException {
95 Document document;
96 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
97 document = documentBuilder.newDocument();
98 Node node = document.appendChild(document.createElement(RandomUtils.nextString(5)));
99 node.appendChild(document.createElement(RandomUtils.nextString(5)));
100 return document;
101 }
102
103 }