View Javadoc

1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  /*
27   * To change this template, choose Tools | Templates
28   * and open the template in the editor.
29   */
30  package ca.uhn.hl7v2.testpanel.model;
31  
32  import java.beans.PropertyVetoException;
33  import java.io.IOException;
34  import java.io.StringReader;
35  
36  import javax.xml.parsers.DocumentBuilder;
37  import javax.xml.parsers.DocumentBuilderFactory;
38  import javax.xml.parsers.ParserConfigurationException;
39  
40  import org.w3c.dom.Document;
41  import org.xml.sax.InputSource;
42  import org.xml.sax.SAXException;
43  
44  import ca.uhn.hl7v2.testpanel.ex.ConfigurationException;
45  import ca.uhn.hl7v2.testpanel.model.msg.AbstractMessage;
46  import ca.uhn.hl7v2.testpanel.xsd.XmlMessageDefinition;
47  
48  /**
49   * XML Message implementation
50   */
51  public class XmlMessageImpl extends AbstractMessage<Document> {
52      //~ Instance fields ------------------------------------------------------------------------------------------------
53  
54      private Document myDocument;
55      private String myText;
56  
57      //~ Constructors ---------------------------------------------------------------------------------------------------
58  
59      public XmlMessageImpl(String theId) {
60          super(theId);
61      }
62  
63      public XmlMessageImpl(XmlMessageDefinition theDefinition)
64                     throws ConfigurationException {
65          super(theDefinition);
66  
67          try {
68              final String text = theDefinition.getText();
69              setSourceMessage(text);
70          } catch (PropertyVetoException ex) {
71              throw new ConfigurationException(ex.getMessage(), ex);
72          }
73      }
74  
75      //~ Methods --------------------------------------------------------------------------------------------------------
76  
77      /**
78       * Subclasses should make use of this method to export AbstractInterface properties into
79       * the return value for {@link #exportConfigToXml()}
80       */
81      protected XmlMessageDefinition exportConfig(XmlMessageDefinition theConfig) {
82          super.exportConfig(theConfig);
83          theConfig.setText(myText);
84  
85          return theConfig;
86      }
87  
88      /**
89       * {@inheritDoc }
90       */
91      @Override
92      public XmlMessageDefinition exportConfigToXml() {
93          return exportConfig(new XmlMessageDefinition());
94      }
95  
96      @Override
97      public Class<Document> getMessageClass() {
98          return Document.class;
99      }
100 
101     @Override
102     public String getSourceMessage() {
103         return myText;
104     }
105 
106     @Override
107     public void setSourceMessage(final String text) throws PropertyVetoException {
108         String original = myText;
109 
110         try {
111             myText = text.trim();
112 
113             DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
114             parserFactory.setValidating(false);
115 
116             DocumentBuilder parser = parserFactory.newDocumentBuilder();
117             StringReader inputStream = new StringReader(myText);
118             myDocument = parser.parse(new InputSource(inputStream));
119         } catch (SAXException ex) {
120             throw new PropertyVetoException("Failed to parse XML message: " + ex.getMessage(), null);
121         } catch (IOException ex) {
122             throw new PropertyVetoException("Failed to parse XML message: " + ex.getMessage(), null);
123         } catch (ParserConfigurationException ex) {
124             throw new PropertyVetoException("Failed to parse XML message: " + ex.getMessage(), null);
125         }
126 
127         firePropertyChange(PARSED_MESSAGE_PROPERTY, original, myText);
128     }
129 
130 	@Override
131 	public Document getParsedMessage() {
132 		return myDocument;
133 	}
134 }