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  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  		// nothing
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 		// not implemented
130 	}
131 
132 	@Override
133 	public Range getHighlitedRange() {
134 		// not implemented
135 		return null;
136 	}
137 
138 	@Override
139 	public void setHighlitedField(SegmentAndComponentPath thePath) {
140 		// TODO Auto-generated method stub
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 }