1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
50
51 public class XmlMessageImpl extends AbstractMessage<Document> {
52
53
54 private Document myDocument;
55 private String myText;
56
57
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
76
77
78
79
80
81 protected XmlMessageDefinition exportConfig(XmlMessageDefinition theConfig) {
82 super.exportConfig(theConfig);
83 theConfig.setText(myText);
84
85 return theConfig;
86 }
87
88
89
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 }