00001
00067 package com.arcle.rmt.j2se.bridge;
00068
00069 import java.io.File;
00070 import java.io.IOException;
00071 import java.io.PrintWriter;
00072 import java.util.Iterator;
00073
00074 import com.arcle.rmt.rqml.RQMLFirstClass;
00075 import com.arcle.rmt.j2se.model.RQMLDocument;
00076 import com.arcle.rmt.j2se.model.RQMLDocumentFacade;
00077 import com.arcle.rmt.superwaba.model.Document;
00078 import com.arcle.rmt.superwaba.model.DocumentManager;
00079 import com.arcle.rmt.j2se.bridge.swexport.SwExportFacade;
00080 import com.arcle.rmt.j2se.bridge.swimport.SwImportFacade;
00081
00091 public final class BridgeFacade {
00097 private BridgeFacade() {
00098
00099
00100 waba.applet.JavaBridge.showMsgs = false;
00101 waba.applet.JavaBridge.setNonGUIApp();
00102 }
00103
00104
00112 public RQMLDocument importSuperwabaDocument(File pdbFile, PrintWriter log)
00113 throws BridgeFacadeException {
00114 Document swDoc = null;
00115 RQMLDocument desktopDoc = null;
00116 try {
00117 swDoc = openSuperwabaDocument(pdbFile);
00118 desktopDoc = RQMLDocumentFacade.getInstance().createRQMLDocument();
00119 pumpElements(swDoc, desktopDoc, log);
00120 } catch(Exception e) {
00121 rethrowException(e);
00122 }
00123 return desktopDoc;
00124 }
00125
00133 public void exportSuperwabaDocument(RQMLDocument desktopDoc, File pdbFile,
00134 PrintWriter log) throws BridgeFacadeException {
00135 Document swDoc = null;
00136 try {
00137 swDoc = DocumentManager.getInstance().createDocument();
00138
00139 swDoc.setTitle(stripPDBSuffix(pdbFile.getName()));
00140 pumpElements(desktopDoc, swDoc, log);
00141 DocumentManager.getInstance().saveDocument(
00142 makeCatalogFileName(pdbFile), swDoc);
00143 } catch(Exception e) {
00144 rethrowException(e);
00145 }
00146 }
00147
00148
00149
00150
00156 protected Document openSuperwabaDocument(File pdbFile) throws IOException {
00157 DocumentManager dm = DocumentManager.getInstance();
00158 Document doc = dm.loadDocument(makeCatalogFileName(pdbFile));
00159 if (doc == null) {
00160 throw new IOException("Cannot open Catalog: " + pdbFile);
00161 }
00162 return doc;
00163 }
00164
00173 protected void pumpElements(Document source, RQMLDocument destination, PrintWriter log) {
00174 SwImportFacade.getInstance().importFirstClass(source, destination, log);
00175 }
00176
00185 protected int pumpElements(RQMLDocument source, Document destination,
00186 PrintWriter log) {
00187 int exportedElements = 0;
00188 SwExportFacade facade = SwExportFacade.getInstance();
00189 Iterator elements = source.getElements();
00190
00191 while (elements.hasNext()) {
00192 RQMLFirstClass elem = (RQMLFirstClass) elements.next();
00193 if (facade.export(elem, destination)) {
00194 exportedElements++;
00195 } else {
00196 log.println("Export failed for element: " + elem);
00197 }
00198 }
00199 return exportedElements;
00200 }
00201
00202
00207 protected String makeCatalogFileName(File file) throws IOException {
00208 return stripPDBSuffix(file.getCanonicalPath());
00209 }
00210
00211 protected String stripPDBSuffix(String str) {
00212 final String EXTENSION = "pdb";
00213 int extIndex = str.lastIndexOf('.');
00214 if (extIndex >= 0) {
00215
00216 if (EXTENSION.equalsIgnoreCase(str.substring(extIndex+1))) {
00217 str = str.substring(0, extIndex);
00218 }
00219 }
00220 return str;
00221 }
00222
00223
00224
00229 protected void rethrowException(Exception real)
00230 throws BridgeFacadeException {
00231 throw new BridgeFacadeException(real);
00232 }
00233
00237 public synchronized static BridgeFacade getInstance() {
00238 if (_instance == null) {
00239 _instance = new BridgeFacade();
00240 }
00241 return _instance;
00242 }
00243
00247 private static BridgeFacade _instance = null;
00248 }