| 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 | 0 | handledTypes = new HashSet<PropertyTypeDefinition>(); |
| 48 | 0 | handledTypes.add(new PropertyTypeDefinition(Document.class)); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public DocumentPropertyTypeHandler() { |
| 55 | |
|
| 56 | 0 | super(handledTypes); |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public Object handle(HandledBeanProperty handledBeanProperty) { |
| 63 | 0 | Document document = null; |
| 64 | |
try { |
| 65 | 0 | document = doHandle(); |
| 66 | 0 | } catch (ParserConfigurationException e) { |
| 67 | 0 | throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e); |
| 68 | 0 | } catch (FactoryConfigurationError e) { |
| 69 | 0 | throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e); |
| 70 | 0 | } |
| 71 | 0 | return document; |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public Object handle(HandledConstructorArg handledConstructorArg) { |
| 78 | 0 | Document document = null; |
| 79 | |
try { |
| 80 | 0 | document = doHandle(); |
| 81 | 0 | } catch (ParserConfigurationException e) { |
| 82 | 0 | throw new PropertyTypeHandlingException("Unable to handle constructor arg <#" + handledConstructorArg.getConstructorArgOrderingNumber() + "(" + handledConstructorArg.getConstructorArgClass().getName() + ")> from constructor " + handledConstructorArg.getTargetConstructor().getName(), e); |
| 83 | 0 | } catch (FactoryConfigurationError e) { |
| 84 | 0 | throw new PropertyTypeHandlingException("Unable to handle constructor arg <#" + handledConstructorArg.getConstructorArgOrderingNumber() + "(" + handledConstructorArg.getConstructorArgClass().getName() + ")> from constructor " + handledConstructorArg.getTargetConstructor().getName(), e); |
| 85 | 0 | } |
| 86 | 0 | return document; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
private Document doHandle() throws ParserConfigurationException, DOMException { |
| 95 | |
Document document; |
| 96 | 0 | DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 97 | 0 | document = documentBuilder.newDocument(); |
| 98 | 0 | Node node = document.appendChild(document.createElement(RandomUtils.nextString(5))); |
| 99 | 0 | node.appendChild(document.createElement(RandomUtils.nextString(5))); |
| 100 | 0 | return document; |
| 101 | |
} |
| 102 | |
|
| 103 | |
} |