001: /**
002: *
003: */package com.bostechcorp.cbesb.common.mdl.hl7;
004:
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileNotFoundException;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.zip.ZipEntry;
011: import java.util.zip.ZipFile;
012:
013: import org.xml.sax.InputSource;
014:
015: import com.bostechcorp.cbesb.common.mdl.IDocLocationResolver;
016: import com.bostechcorp.cbesb.common.mdl.DocLocationResolverBase;
017: import com.bostechcorp.cbesb.common.mdl.MDLException;
018: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
019:
020: public class Hl7DocLocationResolver extends DocLocationResolverBase
021: implements IDocLocationResolver {
022:
023: private String project;
024: private String sharedProject;
025: private String version;
026: private String variant;
027:
028: /**
029: * @return the projectName
030: */
031: public String getProject() {
032: return project;
033: }
034:
035: /**
036: * @param projectName the projectName to set
037: */
038: public void setProject(String project) {
039: this .project = project;
040: }
041:
042: /**
043: * @return the sharedProject
044: */
045: public String getSharedProject() {
046: return sharedProject;
047: }
048:
049: /**
050: * @param sharedProject the sharedProject to set
051: */
052: public void setSharedProject(String sharedProject) {
053: this .sharedProject = sharedProject;
054: }
055:
056: /**
057: * @return the variant
058: */
059: public String getVariant() {
060: return variant;
061: }
062:
063: /**
064: * @param variant the variant to set
065: */
066: public void setVariant(String variant) {
067: this .variant = variant;
068: }
069:
070: /**
071: * @return the version
072: */
073: public String getVersion() {
074: return version;
075: }
076:
077: /**
078: * @param version the version to set
079: */
080: public void setVersion(String version) {
081: this .version = version;
082: }
083:
084: /**
085: * This method relies on the following properties to already
086: * be set when called:
087: * project
088: * version
089: * variant
090: */
091: public String getCacheIDFromDocLocation(String docLocation,
092: String currentWorkingDirectory) throws MDLException {
093: String docFilePath;
094: File docLocFile = new File(docLocation);
095: // Analyze the value of docLocation
096: String filename = docLocFile.getName();
097: if (filename.equals("datatypes.mdl")
098: || filename.equals("delimiters.mdl")) {
099: docFilePath = filename;
100: } else {
101: String parentFolder = docLocFile.getParentFile().getName();
102: //This will be either "messages" or "segments"
103: docFilePath = parentFolder + "/" + filename;
104: }
105:
106: return project + "/src/formats/hl7/" + version + "/" + variant
107: + "/" + docFilePath;
108: }
109:
110: /* (non-Javadoc)
111: * @see com.bostechcorp.cbesb.common.mdl.IDocLocationResolver#getCacheIDFromFormatSpec(java.lang.String)
112: */
113: public String getCacheIDFromFormatSpec(String formatSpec)
114: throws MDLException {
115: // the Format Spec is in the format of "projName::v004010/variant/m270"
116: //or "projName::v004010/variant/m270/message" or "projName::v004010/variant/A3/segment
117: // or "projName::ESBproj::/v004010/variant/m270"
118: //we should identify it is a message or segment when used in x12 editor
119:
120: String[] arr = splitProjectsFromFormatSpec(formatSpec);
121: project = arr[0];
122: sharedProject = arr[1];
123: String path = arr[2];
124: arr = path.split("/");
125: if (arr.length < 3) {
126: throw new MDLException("Invalid Format Specification: "
127: + formatSpec);
128: }
129: version = arr[0];
130: variant = arr[1];
131: String message = arr[2];
132: String parentFolder = "messages";
133: if (arr.length > 3) {
134: if (!arr[3].equalsIgnoreCase("message")) {
135: parentFolder = "segments";
136: }
137: }
138:
139: // Form cacheID like a path in this format:
140: // project/src/formats/x12/version/variant/messages/message.mdl
141: // This path can uniquely identify a message definition, but is
142: // not really used to load the actual file.
143: String cacheId = project + "/src/formats/hl7/" + version + "/"
144: + variant + "/" + parentFolder + "/" + message + ".mdl";
145:
146: return cacheId;
147: }
148:
149: /* (non-Javadoc)
150: * @see com.bostechcorp.cbesb.common.mdl.IDocLocationResolver#resolveDocLocation(java.lang.String, java.lang.String)
151: */
152: public InputSource resolveDocLocation(String docLocation,
153: String currentWorkingDir) throws MDLException {
154: String docFilePath;
155: File docLocFile = new File(docLocation);
156: // Analyze the value of docLocation
157: String filename = docLocFile.getName();
158: if (filename.equals("datatypes.mdl")
159: || filename.equals("delimiters.mdl")) {
160: docFilePath = filename;
161: } else {
162: String parentFolder = docLocFile.getParentFile().getName();
163: //This will be either "messages" or "segments"
164: docFilePath = parentFolder + "/" + filename;
165: }
166:
167: //If a variant is defined, look there first for a definition
168: if (variant != null && !variant.equals("")) {
169: String uiWorkspace = EsbPathHelper.getCbesbUiWorkSpace();
170: //File defPath = new File(uiWorkspace, project);
171: String defPath = uiWorkspace + File.separator + project
172: + File.separator;
173: if (sharedProject != null && !sharedProject.equals("")) {
174: defPath = defPath + sharedProject + File.separator;
175: }
176: defPath = defPath + "src" + File.separator + "formats"
177: + File.separator + "hl7" + File.separator + version
178: + File.separator + variant + File.separator
179: + docFilePath;
180:
181: File defFile = new File(defPath);
182: if (defFile.exists()) {
183: try {
184: //Found a definition in the variant
185: InputSource inputSrc = null;
186: FileInputStream fis = new FileInputStream(defFile);
187: inputSrc = new InputSource(fis);
188: return inputSrc;
189: } catch (FileNotFoundException e) {
190: //Shouldn't happen since we already checked
191: //that the file exists.
192: }
193: }
194: }
195:
196: //Either no variant is being used, or the definition doesn't
197: //exist in the variant, fall back on the base definition
198: return getInputSourceFromZipFile(docFilePath);
199:
200: }
201:
202: /* (non-Javadoc)
203: * @see com.bostechcorp.cbesb.common.mdl.IDocLocationResolver#resolveFormatSpec(java.lang.String)
204: */
205: public InputSource resolveFormatSpec(String formatSpec)
206: throws MDLException {
207:
208: // the Format Spec is in the format of "projName::v004010/variant/m270"
209: //or "projName::v004010/variant/m270/message" or "projName::v004010/variant/A3/segment
210: // or "projName::ESBproj::/v004010/variant/m270"
211: //we should identify it is a message or segment when used in x12 editor
212: String[] arr = splitProjectsFromFormatSpec(formatSpec);
213: project = arr[0];
214: sharedProject = arr[1];
215: String path = arr[2];
216: arr = path.split("/");
217: if (arr.length < 3) {
218: throw new MDLException("Invalid Format Specification: "
219: + formatSpec);
220: }
221: version = arr[0];
222: variant = arr[1];
223: String message = arr[2];
224:
225: String parentFolder = "messages";
226: if (arr.length > 3) {
227: if (arr[3].equalsIgnoreCase("message"))
228: parentFolder = "messages";
229: else
230: parentFolder = "segments";
231: }
232: //If a variant is defined, look there first for a definition
233: if (variant != null && !variant.equals("")) {
234: String uiWorkspace = EsbPathHelper.getCbesbUiWorkSpace();
235: //File defPath = new File(uiWorkspace, project);
236: String defPath = uiWorkspace + File.separator + project
237: + File.separator;
238: if (sharedProject != null && !sharedProject.equals("")) {
239: defPath = defPath + sharedProject + File.separator;
240: }
241: defPath = defPath + "src" + File.separator + "formats"
242: + File.separator + "hl7" + File.separator + version
243: + File.separator + variant + File.separator
244: + parentFolder + File.separator + message + ".mdl";
245:
246: File defFile = new File(defPath);
247: if (defFile.exists()) {
248: try {
249: //Found a definition in the variant
250: InputSource inputSrc = null;
251: FileInputStream fis = new FileInputStream(defFile);
252: inputSrc = new InputSource(fis);
253: return inputSrc;
254: } catch (FileNotFoundException e) {
255: //Shouldn't happen since we already checked
256: //that the file exists.
257: }
258: }
259: }
260:
261: //Either no variant is being used, or the definition doesn't
262: //exist in the variant, fall back on the base definition
263: return getInputSourceFromZipFile(parentFolder + "/" + message
264: + ".mdl");
265: }
266:
267: protected InputSource getInputSourceFromZipFile(
268: String definitionPath) throws MDLException {
269: String esbHome = EsbPathHelper.getCbesbHomeDir();
270: String hl7ZipFullPath = esbHome + File.separator + "formats"
271: + File.separator + "hl7" + File.separator + version
272: + ".zip";
273:
274: File defPathFile = new File(definitionPath);
275: String zipEntryName;
276: if (defPathFile.getName().equals("datatypes.mdl")) {
277: zipEntryName = version + "/datatypes.mdl";
278: } else if (defPathFile.getName().equals("delimiters.mdl")) {
279: zipEntryName = version + "/delimiters.mdl";
280: } else {
281: //Parent folder should be either "messages" or "segments"
282: String parentFolder = defPathFile.getParentFile().getName();
283: zipEntryName = version + "/" + parentFolder + "/"
284: + defPathFile.getName();
285: }
286:
287: try {
288: ZipFile zip = new ZipFile(hl7ZipFullPath);
289: ZipEntry entry = zip.getEntry(zipEntryName);
290: InputStream is = zip.getInputStream(entry);
291: return new InputSource(is);
292: } catch (IOException e) {
293: throw new MDLException(e);
294: }
295: }
296:
297: /* (non-Javadoc)
298: * @see com.bostechcorp.cbesb.common.mdl.IDocLocationResolver#getAbsolutePathFromDocLocation(java.lang.String, java.lang.String)
299: */
300: public String getAbsolutePathFromDocLocation(String docLocation,
301: String currentWorkingDirectory) throws MDLException {
302: String docFilePath;
303: File docLocFile = new File(docLocation);
304: // Analyze the value of docLocation
305: String filename = docLocFile.getName();
306: if (filename.equals("datatypes.mdl")
307: || filename.equals("delimiters.mdl")) {
308: docFilePath = filename;
309: } else {
310: String parentFolder = docLocFile.getParentFile().getName();
311: //This will be either "messages" or "segments"
312: docFilePath = parentFolder + "/" + filename;
313: }
314:
315: String uiWorkspace = EsbPathHelper.getCbesbUiWorkSpace();
316: //File defPath = new File(uiWorkspace, project);
317: String defPath = uiWorkspace + File.separator + project
318: + File.separator;
319: if (sharedProject != null && !sharedProject.equals("")) {
320: defPath = defPath + sharedProject + File.separator;
321: }
322: defPath = defPath + "src" + File.separator + "formats"
323: + File.separator + "hl7" + File.separator + version
324: + File.separator;
325: if (variant != null && !variant.equals("")) {
326: defPath = defPath + variant + File.separator;
327: }
328: defPath = defPath + docFilePath;
329:
330: return defPath;
331: }
332:
333: /* (non-Javadoc)
334: * @see com.bostechcorp.cbesb.common.mdl.IDocLocationResolver#getAbsolutePathFromFormatSpec(java.lang.String)
335: */
336: public String getAbsolutePathFromFormatSpec(String formatSpec)
337: throws MDLException {
338: String[] arr = splitProjectsFromFormatSpec(formatSpec);
339: project = arr[0];
340: sharedProject = arr[1];
341: String path = arr[2];
342: arr = path.split("/");
343: if (arr.length < 3) {
344: throw new MDLException("Invalid Format Specification: "
345: + formatSpec);
346: }
347: version = arr[0];
348: variant = arr[1];
349: String message = arr[2];
350: String parentFolder = "messages";
351: if (arr.length > 3) {
352: if (arr[3].equalsIgnoreCase("message"))
353: parentFolder = "messages";
354: else
355: parentFolder = "segments";
356: }
357:
358: String uiWorkspace = EsbPathHelper.getCbesbUiWorkSpace();
359: //File defPath = new File(uiWorkspace, project);
360: String defPath = uiWorkspace + File.separator + project
361: + File.separator;
362: if (sharedProject != null && !sharedProject.equals("")) {
363: defPath = defPath + sharedProject + File.separator;
364: }
365: defPath = defPath + "src" + File.separator + "formats"
366: + File.separator + "hl7" + File.separator + version
367: + File.separator;
368: if (variant != null && !variant.equals("")) {
369: defPath = defPath + variant + File.separator;
370: }
371: defPath = defPath + parentFolder + File.separator + message
372: + ".mdl";
373:
374: return defPath;
375: }
376:
377: }
|