1 19 package org.openharmonise.him.editors; 20 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.util.List ; 26 27 import org.openharmonise.commons.net.*; 28 import org.openharmonise.vfs.*; 29 import org.openharmonise.vfs.status.*; 30 31 32 40 public abstract class AbstractEditor { 41 42 protected final String m_sEditFilePath = "C:\\ContentManager\\temp\\edit\\"; 44 protected final String m_sPreviewFilePath = "C:\\ContentManager\\temp\\preview\\"; 45 46 53 protected String createWorkingFile(VirtualFile vfFile) { 54 return this.createWorkingFile(vfFile, null); 55 } 56 57 65 protected String createWorkingFile(VirtualFile vfFile, File fExportFile) { 66 67 String sFilename = this.getFileName(vfFile); 68 69 sFilename = sFilename.replaceAll(" ", ""); 70 sFilename = sFilename.toLowerCase(); 71 72 File fFile = null; 73 if(fExportFile!=null) { 74 fFile=fExportFile; 75 } else { 76 if(EditorController.getInstance().isMonitoringVirtualFile(vfFile)) { 77 fFile = new File ( EditorController.getInstance().getRealPath(vfFile) ); 78 } else { 79 fFile = new File (m_sEditFilePath + sFilename); 80 int nCount = 1; 81 while(fFile.exists()) { 82 String sTempName = this.appendToFilename(sFilename, nCount); 83 fFile = new File (m_sEditFilePath + sTempName); 84 nCount++; 85 } 86 } 87 } 88 89 FileOutputStream fos = null; 90 91 try { 92 byte[] byteContent = vfFile.getContent(); 93 fos = new FileOutputStream (fFile); 94 fos.write(byteContent); 95 } catch (FileNotFoundException e) { 96 e.printStackTrace(); 97 } catch (IOException e) { 98 e.printStackTrace(); 99 } finally { 100 try { 101 fos.close(); 102 } catch (IOException e1) { 103 e1.printStackTrace(); 104 } 105 } 106 107 return fFile.getAbsolutePath(); 108 } 109 protected String createPreviewFile(String sFilename) { 110 111 sFilename = sFilename.replaceAll(" ", ""); 112 sFilename = sFilename.toLowerCase(); 113 114 File fFile = new File (m_sPreviewFilePath + sFilename); 115 116 return fFile.getAbsolutePath(); 117 } 118 protected File createPreviewFile(String sFilename, byte[] content) { 119 120 sFilename = sFilename.replaceAll(" ", ""); 121 sFilename = sFilename.toLowerCase(); 122 123 File fFile = new File (m_sPreviewFilePath + sFilename); 124 125 FileOutputStream fos = null; 126 127 try { 128 fos = new FileOutputStream (fFile); 129 fos.write(content); 130 } catch (FileNotFoundException e) { 131 e.printStackTrace(); 132 } catch (IOException e) { 133 e.printStackTrace(); 134 } finally { 135 try { 136 fos.close(); 137 } catch (IOException e1) { 138 e1.printStackTrace(); 139 } 140 } 141 142 return fFile; 143 } 144 145 private String appendToFilename(String sFilename, int nAppend) { 146 return this.appendToFilename(sFilename, "_" + nAppend); 147 } 148 149 private String appendToFilename(String sFilename, String sAppend) { 150 String sReturn = null; 151 152 if(sFilename.indexOf(".")>1) { 153 String sStart = sFilename.substring(0, sFilename.indexOf(".")); 154 String sExt = sFilename.substring(sFilename.indexOf(".")+1); 155 156 sReturn = sStart + sAppend + "." + sExt; 157 } else { 158 sReturn = sFilename + sAppend; 159 } 160 161 return sReturn; 162 } 163 164 171 protected String getFileName(VirtualFile vfFile) { 172 String sContentType = vfFile.getVFS().getVirtualFileSystemView().getContentType(vfFile); 173 String sFilename = null; 174 175 VirtualFile vfFileForName = vfFile; 176 if(vfFile.isVersionable() && ((VersionedVirtualFile)vfFile).getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)vfFile).getLiveVersionPath()!=null ) { 177 vfFileForName = vfFile.getVFS().getVirtualFile( ((VersionedVirtualFile)vfFile).getLiveVersionPath() ).getResource(); 178 } 179 180 if(sContentType!=null && !sContentType.equals("")) { 181 List aExts = MimeTypeMapping.getExtensions(sContentType); 182 String sExt = (String ) aExts.get(0); 183 184 sFilename = vfFileForName.getFileName() + "." + sExt; 185 } else { 186 sFilename = vfFileForName.getFileName(); 187 188 if(sFilename.indexOf(".")>-1) { 189 String sExt = sFilename.substring(sFilename.indexOf(".")+1); 190 List types = MimeTypeMapping.getMimeTypes(sExt); 191 if(types!=null && types.size()>0) { 192 vfFile.getVFS().getVirtualFileSystemView().setContentType(vfFile, (String )types.get(0)); 193 } 194 } 195 } 196 return sFilename; 197 } 198 199 202 public StatusData discardChanges(String sPath, AbstractVirtualFileSystem vfs) { 203 204 this.createWorkingFile(vfs.getVirtualFile(sPath).getResource()); 205 206 return new VFSStatus(); 207 } 208 } | Popular Tags |