View Javadoc

1   /*
2    * The MIT License
3    *
4    * Original work sponsored and donated by National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
5    *
6    * Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   *
26   * $HeadURL: https://svn.softwareborsen.dk/sosi/trunk/modules/seal/src/main/java/dk/sosi/seal/vault/renewal/modelbuilders/ResponseModelBuilder.java $
27   * $Id: ResponseModelBuilder.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28   */
29  
30  package dk.sosi.seal.vault.renewal.modelbuilders;
31  
32  import dk.sosi.seal.modelbuilders.ModelBuildException;
33  import dk.sosi.seal.vault.renewal.model.Response;
34  import dk.sosi.seal.vault.renewal.model.dombuilders.Constants;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Node;
37  import org.w3c.dom.NodeList;
38  
39  import java.lang.reflect.Constructor;
40  import java.lang.reflect.InvocationTargetException;
41  
42  
43  /**
44   * Class to build renewal response model objects from DOM representations.
45   * 
46   * @author thomas@signaturgruppen.dk
47   * @author $LastChangedBy: chg@lakeside.dk $
48   * @version $Revision: 8697 $
49   * @since 1.0
50   */
51  public class ResponseModelBuilder extends MessageModelBuilder { //NOPMD
52  	
53  	/**
54  	 * Build a message model object
55  	 * @param doc
56  	 * 		DOM representation of the renewal response message object
57  	 * @return The constructed <code>Response</code> object.
58  	 * @throws ModelBuildException
59  	 */
60  	public Response buildModel(Document doc) throws ModelBuildException {
61  
62  		NodeList results = doc.getElementsByTagName("result");
63  		if(results.getLength() != 1) {
64  			throw new ModelBuildException("Wrong number of result elements in response.");
65  		}
66  			
67  		Node result = results.item(0);
68  		String rType = getResponseType(result);
69  		
70  		try {
71  			
72  			Class<?> responseType =  Class.forName(rType);
73  			Constructor<?> constructor =  responseType.getConstructor(new Class[]{});
74  			Response response = (Response) constructor.newInstance(new Object[]{});
75  			
76  			NodeList fields = result.getChildNodes();
77  			for(int i = 0; i < fields.getLength(); i++) {
78  				if(fields.item(i).getAttributes() != null) {
79  					setField(response, fields.item(i));
80  				}
81  			}
82  
83  			
84  			return response;
85  			
86  		} catch (ClassNotFoundException e) {
87  			throw new ModelBuildException("Failed to construct response", e);
88  		} catch (SecurityException e) {
89  			throw new ModelBuildException("Failed to construct response", e);
90  		} catch (NoSuchMethodException e) {
91  			throw new ModelBuildException("Failed to construct response", e);		
92  		} catch (IllegalArgumentException e) {
93  			throw new ModelBuildException("Failed to construct response", e);
94  		} catch (InstantiationException e) {
95  			throw new ModelBuildException("Failed to construct response", e);
96  		} catch (IllegalAccessException e) {
97  			throw new ModelBuildException("Failed to construct response", e);
98  		} catch (InvocationTargetException e) {
99  			throw new ModelBuildException("Failed to construct response", e);
100 		}
101 
102 		
103 		
104 	}
105 	
106 	
107 
108 	/**
109 	 * Compute the name of the response model class to instantiate
110 	 * @param result
111 	 * 		The result DOM element 
112 	 * @return
113 	 * 		Name of response class
114 	 */
115 	private String getResponseType(Node result) {
116 		String usedPackage = Response.class.getPackage().getName();
117 		String responseType = result.getAttributes().getNamedItem(Constants.XSI_TYPE).getNodeValue();
118 		
119 		responseType = responseType.split(":")[1];
120 		return usedPackage + "." + responseType;
121 	}
122 
123 	
124 }