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 package ca.uhn.hl7v2.testpanel.model.msg;
27
28 import java.beans.PropertyVetoException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import ca.uhn.hl7v2.HL7Exception;
36 import ca.uhn.hl7v2.model.Segment;
37 import ca.uhn.hl7v2.parser.PipeParser;
38 import ca.uhn.hl7v2.testpanel.util.Range;
39 import ca.uhn.hl7v2.testpanel.util.SegmentAndComponentPath;
40 import ca.uhn.hl7v2.testpanel.util.XmlScanner;
41 import ca.uhn.hl7v2.testpanel.xsd.Hl7V2EncodingTypeEnum;
42
43 public class Hl7V2MessageXml extends Hl7V2MessageBase {
44
45 private static final Logger ourLog = LoggerFactory.getLogger(Hl7V2MessageXml.class);
46
47 private String myHilightedPath;
48
49 @Override
50 protected void recalculateIndexes() {
51
52 }
53
54 @Override
55 public void setHighlitedPathBasedOnRange(Range theAdd) {
56 if (theAdd == null) {
57 myHilightedPath = null;
58 return;
59 }
60
61 List<String> pathElements = new ArrayList<String>();
62
63 XmlScanner scanner = new XmlScanner(getSourceMessage());
64 scanner.setCalculateRepetitions(true);
65 scanner.setPosition(theAdd.getStart());
66
67 for (;;) {
68 if (!scanner.findTagEnclosingCurrentPosition()) {
69 break;
70 }
71
72 String currentTagName = scanner.getCurrentTagName();
73 if (scanner.getCurrentTagRepetition() > 1) {
74 currentTagName = currentTagName + "(" + scanner.getCurrentTagRepetition() + ")";
75 }
76
77 pathElements.add(0, currentTagName);
78 scanner.decrementPositionByOne();
79 }
80
81 ourLog.info("Path elements " + pathElements);
82
83 if (pathElements.size() < 2) {
84 return;
85 }
86
87 boolean inSegment = false;
88 StringBuilder b = new StringBuilder();
89 for (int i = 1; i < pathElements.size(); i++) {
90 String nextElement = pathElements.get(i);
91
92 if (inSegment) {
93
94 int dotIndex = nextElement.indexOf('.');
95 if (dotIndex > 0) {
96 b.append('-');
97 b.append(nextElement.substring(dotIndex + 1));
98 }
99
100 } else {
101
102 b.append('/');
103
104 if (nextElement.matches("^[A-Z][A-Z0-9][A-Z0-9]$")) {
105 inSegment = true;
106 b.append(nextElement);
107 } else {
108 int dotIndex = nextElement.indexOf('.');
109 b.append(nextElement.substring(dotIndex + 1));
110 }
111
112 }
113
114 }
115
116 myHilightedPath = b.toString();
117
118 ourLog.info("Highlited path is now: " + myHilightedPath);
119
120 }
121
122 @Override
123 public String getHighlitedPath() {
124 return myHilightedPath;
125 }
126
127 @Override
128 public void setHighlitedRangeBasedOnSegment(Segment... theSegment) {
129
130 }
131
132 @Override
133 public Range getHighlitedRange() {
134
135 return null;
136 }
137
138 @Override
139 public void setHighlitedField(SegmentAndComponentPath thePath) {
140
141
142 }
143
144 @Override
145 public Hl7V2MessageBase asEncoding(Hl7V2EncodingTypeEnum theEncoding) {
146 switch (theEncoding) {
147 case XML:
148 return this;
149 case ER_7:
150 default:
151 Hl7V2MessageEr7 retVal = new Hl7V2MessageEr7();
152 try {
153 PipeParser pipeParser = new PipeParser();
154 String encode = pipeParser.encode(getParsedMessage());
155 retVal.setSourceMessage(encode);
156 } catch (PropertyVetoException e) {
157 ourLog.error("Failed to create XML message", e);
158 } catch (HL7Exception e) {
159 ourLog.error("Failed to create XML message", e);
160 }
161
162 return retVal;
163 }
164
165 }
166
167 }