001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $Id: FormatDefUtil.java 6617 2007-04-11 01:04:44Z zjin $
023: *
024: */
025: package com.bostechcorp.cbesb.common.mdl.util;
026:
027: import java.io.File;
028: import java.io.IOException;
029:
030: import com.bostechcorp.cbesb.common.mdl.DefaultDocLocationResolver;
031: import com.bostechcorp.cbesb.common.mdl.IDocLocationResolver;
032: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
033: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
034: import com.bostechcorp.cbesb.common.mdl.MDLDocumentFactory;
035: import com.bostechcorp.cbesb.common.mdl.hl7.Hl7DocLocationResolver;
036: import com.bostechcorp.cbesb.common.mdl.x12.X12DocLocationResolver;
037:
038: public class FormatDefUtil {
039:
040: public static IMDLDocument getMdlDocFromFormatSpec(String format,
041: String formatSpec) {
042: IDocLocationResolver docLocResolver;
043: if (format.equals("x12")) {
044: docLocResolver = new X12DocLocationResolver();
045: } else if (format.equals("hl7")) {
046: docLocResolver = new Hl7DocLocationResolver();
047: } else {
048: docLocResolver = new DefaultDocLocationResolver();
049: }
050:
051: MDLDocumentFactory mdlFactory = new MDLDocumentFactory(
052: docLocResolver);
053:
054: return mdlFactory.getMDLDocumentFromFormatSpec(formatSpec);
055: }
056:
057: public static IMDLDocument getMdlDocFromPath(String path) {
058: return getMdlDocFromPath(path, false);
059: }
060:
061: public static IMDLDocument getMdlDocFromPath(String path,
062: boolean useDefaultMdlResolver) {
063: File absPath = new File(path);
064: //Figure out the details of the format from the path
065: String project = null;
066: String format = null;
067: String version = null;
068: String variant = null;
069: int srcIndex = -1;
070: try {
071: String pathStr = absPath.getCanonicalPath();
072:
073: /**
074: * If the File.separator="\". it can't be used as a parameter for split method,
075: * because it can't be as regular expression.
076: */
077: String tempStr = pathStr.replace(File.separator, ";");
078:
079: String[] pathArr = tempStr.split(";");
080: for (int i = 0; i < pathArr.length; i++) {
081: if (pathArr[i].equals("src")) {
082: srcIndex = i;
083: break;
084: }
085: }
086:
087: if (srcIndex > 1) {
088: project = pathArr[srcIndex - 1];
089: }
090: if (pathArr.length > srcIndex + 2) {
091: format = pathArr[srcIndex + 2];
092: if (format.equals("x12") || format.equals("hl7")) {
093: if (pathArr.length > srcIndex + 4) {
094: version = pathArr[srcIndex + 3];
095: variant = pathArr[srcIndex + 4];
096: }
097: } else {
098: format = "mdl";
099: }
100: }
101:
102: } catch (IOException e) {
103: //Shouldn't happen
104: }
105:
106: // This is a special case which used by the migration project. It always falls back to use tge deafult resolver
107: // even if the x12 or hl7 is in the path.
108: if (useDefaultMdlResolver)
109: format = "mdl";
110:
111: IDocLocationResolver docLocResolver;
112: if (format.equals("x12")) {
113: docLocResolver = new X12DocLocationResolver();
114: ((X12DocLocationResolver) docLocResolver)
115: .setProject(project);
116: ((X12DocLocationResolver) docLocResolver)
117: .setVersion(version);
118: ((X12DocLocationResolver) docLocResolver)
119: .setVariant(variant);
120: } else if (format.equals("hl7")) {
121: docLocResolver = new Hl7DocLocationResolver();
122: ((Hl7DocLocationResolver) docLocResolver)
123: .setProject(project);
124: ((Hl7DocLocationResolver) docLocResolver)
125: .setVersion(version);
126: ((Hl7DocLocationResolver) docLocResolver)
127: .setVariant(variant);
128: } else {
129: docLocResolver = new DefaultDocLocationResolver();
130: }
131:
132: MDLDocumentFactory mdlFactory = new MDLDocumentFactory(
133: docLocResolver);
134:
135: return mdlFactory.getMDLDocument(absPath.getAbsolutePath(),
136: null);
137: }
138:
139: public static IMessageDefinition getMdlMessageDefFromFormatSpec(
140: String format, String formatSpec, String rootName) {
141: IMDLDocument mdlDoc = getMdlDocFromFormatSpec(format,
142: formatSpec);
143: return mdlDoc.getMessageDefinition(mdlDoc.getTargetNamespace(),
144: rootName);
145: }
146:
147: public static IMessageDefinition getMdlMessageDefFromPath(
148: String path, String rootName) {
149: IMDLDocument mdlDoc = getMdlDocFromPath(path);
150: return mdlDoc.getMessageDefinition(mdlDoc.getTargetNamespace(),
151: rootName);
152: }
153:
154: }
|