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 it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * 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 MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id:$
023: */
024: package com.bostechcorp.cbesb.ui.util;
025:
026: import java.io.File;
027: import java.util.List;
028:
029: import org.eclipse.core.resources.ResourcesPlugin;
030: import org.eclipse.swt.widgets.Text;
031: import org.eclipse.ui.PlatformUI;
032: import org.eclipse.ui.part.FileEditorInput;
033:
034: import com.bostechcorp.cbesb.common.util.xfm.XmlFormatModelFactory;
035: import com.bostechcorp.cbesb.common.util.xfm.XmlFormatModelNode;
036:
037: //import com.bostechcorp.cbesb.ui.ide.util.XMLValidation;
038:
039: public class AddRootUtil {
040: private static AddRootUtil instance = null;
041:
042: private File file;
043:
044: private String sourcefilePath = "";
045:
046: private String sourceType = "";
047:
048: private String currentProject = "";
049: private String projectName = "";
050:
051: private AddRootUtil() {
052: }
053:
054: public static AddRootUtil getInstance() {
055: if (instance == null)
056: instance = new AddRootUtil();
057: FileEditorInput temp = (FileEditorInput) PlatformUI
058: .getWorkbench().getActiveWorkbenchWindow()
059: .getActivePage().getActiveEditor().getEditorInput();
060: instance.currentProject = temp.getFile().getProject()
061: .getLocation().toString();
062: instance.projectName = temp.getFile().getProject().getName();
063:
064: return instance;
065:
066: }
067:
068: public static AddRootUtil getInstance(String projectLocation) {
069: if (instance == null)
070: instance = new AddRootUtil();
071: if (projectLocation.indexOf("\\") != -1) {
072: projectLocation = projectLocation.replace("\\", "/");
073: }
074: instance.currentProject = projectLocation;
075: String[] arr = instance.currentProject.split("/");
076: instance.projectName = arr[arr.length - 1];
077:
078: return instance;
079:
080: }
081:
082: public XmlFormatModelNode getXmlFormatModelNode(Object[] result)
083: throws Exception {
084: XmlFormatModelNode tarFormatModelNode = null;
085: if (result[0] instanceof File) {
086: file = (File) result[0];
087: String name = handleThePath(file.getAbsolutePath());
088: if (name.endsWith(".mdl")) {
089: if (name.indexOf("/x12/") != -1) {
090: sourceType = "x12";
091: String[] arr = name.split("/messages/");
092: name = arr[0].substring(0, arr[0].indexOf("src"))
093: + arr[0].split("/x12/")[1] + "/" + arr[1];
094: if (name.toLowerCase().endsWith(".mdl")) {
095: name = name.substring(0, name.length() - 4);
096: }
097:
098: } else if (name.indexOf("/hl7/") != -1) {
099: sourceType = "hl7";
100: String[] arr = name.split("/messages/");
101:
102: name = arr[0].substring(0, arr[0].indexOf("src"))
103: + arr[0].split("/hl7/")[1] + "/" + arr[1];
104: if (name.toLowerCase().endsWith(".mdl")) {
105: name = name.substring(0, name.length() - 4);
106: }
107: }
108:
109: else {
110: sourceType = "mdl";
111:
112: }
113: } else if (name.endsWith(".xsd")) {
114: sourceType = "xsd";
115:
116: }
117:
118: if (file.getAbsolutePath().replace("\\", "/").toString()
119: .contains(currentProject)) {
120:
121: sourcefilePath = name.replaceFirst("//", "::");
122: } else {
123: sourcefilePath = projectName + "::"
124: + name.replaceFirst("//", "::");
125:
126: }
127:
128: tarFormatModelNode = XmlFormatModelFactory.newInstance(
129: sourcefilePath, sourceType).getTopLevelNode();
130:
131: //XMLValidation.check(file.getAbsolutePath());
132:
133: } else if (result[0] instanceof List) {
134:
135: List list = (List) result[0];
136:
137: String name = getZipPath((String) list.get(0));
138: sourcefilePath = projectName
139: + "::"
140: + name.substring(0, name.lastIndexOf("."))
141: .replaceFirst("//", "::");
142: String zipFormat = (String) list.get(2);
143: if (zipFormat.contains("/x12/")
144: || zipFormat.contains("\\x12\\")) {
145: sourceType = "x12";
146: } else {
147: sourceType = "hl7";
148: }
149: tarFormatModelNode = XmlFormatModelFactory.newInstance(
150: sourcefilePath, sourceType).getTopLevelNode();
151:
152: }
153:
154: return tarFormatModelNode;
155: }
156:
157: public String getZipPath(String name) {
158: int index = ResourcesPlugin.getWorkspace().getRoot()
159: .getLocation().toString().length() + 1;
160: String[] path = name.replace("\\", "/").substring(index).split(
161: ".zip/");
162: String projectName = path[0].substring(0,
163: path[0].indexOf("/") + 1);// ESB/
164: String subPath = path[1].replaceAll("/messages/", "//");// v1111111//xxx.mdl
165: String realPath = projectName.concat("/" + subPath);
166: return realPath;
167: }
168:
169: public static String handleThePath(String path) {
170: String temp = "";
171: temp = ResourcesPlugin.getWorkspace().getRoot().getLocation()
172: .toString();
173: int index = temp.length() + 1;
174: temp = path.trim().substring(index).replace("\\", "/")
175: .replaceFirst("/", "//");
176: return temp;
177: }
178:
179: public String getSourcefilePath() {
180: return sourcefilePath;
181: }
182:
183: public String getSourceType() {
184: return sourceType;
185: }
186:
187: }
|