View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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   * @author pagregoire
42   * @deprecated this is a poor handler...please create your own one. ;)
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       * @param handledType
53       */
54      public DocumentPropertyTypeHandler() {
55          // FIXME implement a Document generator based on a Schema or a DTD
56          super(handledTypes);
57      }
58  
59      /**
60       * @see org.org.zepag.vogenerator.handlers.AbstractPropertyTypeHandler#handle()
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       * @see org.org.zepag.vogenerator.handlers.AbstractPropertyTypeHandler#handle()
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       * @return
91       * @throws ParserConfigurationException
92       * @throws DOMException
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 }