KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > AbstractEditor


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors;
20
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.openharmonise.commons.net.*;
28 import org.openharmonise.vfs.*;
29 import org.openharmonise.vfs.status.*;
30
31
32 /**
33  * Base class for assisting in the creation of {@link org.openharmonise.him.editors.Editor}
34  * implementations.
35  *
36  * @author Matthew Large
37  * @version $Revision: 1.1 $
38  *
39  */

40 public abstract class AbstractEditor {
41     
42     // TODO remove the hard codings!
43
protected final String JavaDoc m_sEditFilePath = "C:\\ContentManager\\temp\\edit\\";
44     protected final String JavaDoc m_sPreviewFilePath = "C:\\ContentManager\\temp\\preview\\";
45     
46     /**
47      * Creates a local working file for the content of the specified
48      * virtual file.
49      *
50      * @param vfFile Virtual file
51      * @return Path to local working file
52      */

53     protected String JavaDoc createWorkingFile(VirtualFile vfFile) {
54         return this.createWorkingFile(vfFile, null);
55     }
56
57     /**
58      * Creates a local working file for the content of the specified
59      * virtual file.
60      *
61      * @param vfFile Virtual file
62      * @param fExportFile Local working file to use
63      * @return Path to local working file
64      */

65     protected String JavaDoc createWorkingFile(VirtualFile vfFile, File JavaDoc fExportFile) {
66         
67         String JavaDoc sFilename = this.getFileName(vfFile);
68         
69         sFilename = sFilename.replaceAll(" ", "");
70         sFilename = sFilename.toLowerCase();
71         
72         File JavaDoc fFile = null;
73         if(fExportFile!=null) {
74             fFile=fExportFile;
75         } else {
76             if(EditorController.getInstance().isMonitoringVirtualFile(vfFile)) {
77                 fFile = new File JavaDoc( EditorController.getInstance().getRealPath(vfFile) );
78             } else {
79                 fFile = new File JavaDoc(m_sEditFilePath + sFilename);
80                 int nCount = 1;
81                 while(fFile.exists()) {
82                     String JavaDoc sTempName = this.appendToFilename(sFilename, nCount);
83                     fFile = new File JavaDoc(m_sEditFilePath + sTempName);
84                     nCount++;
85                 }
86             }
87         }
88
89         FileOutputStream JavaDoc fos = null;
90         
91         try {
92             byte[] byteContent = vfFile.getContent();
93             fos = new FileOutputStream JavaDoc(fFile);
94             fos.write(byteContent);
95         } catch (FileNotFoundException JavaDoc e) {
96             e.printStackTrace();
97         } catch (IOException JavaDoc e) {
98             e.printStackTrace();
99         } finally {
100             try {
101                 fos.close();
102             } catch (IOException JavaDoc e1) {
103                 e1.printStackTrace();
104             }
105         }
106         
107         return fFile.getAbsolutePath();
108     }
109     protected String JavaDoc createPreviewFile(String JavaDoc sFilename) {
110         
111         sFilename = sFilename.replaceAll(" ", "");
112         sFilename = sFilename.toLowerCase();
113         
114         File JavaDoc fFile = new File JavaDoc(m_sPreviewFilePath + sFilename);
115         
116         return fFile.getAbsolutePath();
117     }
118     protected File JavaDoc createPreviewFile(String JavaDoc sFilename, byte[] content) {
119         
120         sFilename = sFilename.replaceAll(" ", "");
121         sFilename = sFilename.toLowerCase();
122         
123         File JavaDoc fFile = new File JavaDoc(m_sPreviewFilePath + sFilename);
124
125         FileOutputStream JavaDoc fos = null;
126         
127         try {
128             fos = new FileOutputStream JavaDoc(fFile);
129             fos.write(content);
130         } catch (FileNotFoundException JavaDoc e) {
131             e.printStackTrace();
132         } catch (IOException JavaDoc e) {
133             e.printStackTrace();
134         } finally {
135             try {
136                 fos.close();
137             } catch (IOException JavaDoc e1) {
138                 e1.printStackTrace();
139             }
140         }
141         
142         return fFile;
143     }
144     
145     private String JavaDoc appendToFilename(String JavaDoc sFilename, int nAppend) {
146         return this.appendToFilename(sFilename, "_" + nAppend);
147     }
148     
149     private String JavaDoc appendToFilename(String JavaDoc sFilename, String JavaDoc sAppend) {
150         String JavaDoc sReturn = null;
151         
152         if(sFilename.indexOf(".")>1) {
153             String JavaDoc sStart = sFilename.substring(0, sFilename.indexOf("."));
154             String JavaDoc 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     /**
165      * Returns a safe, human readable filename for the specified
166      * virtual file.
167      *
168      * @param vfFile Virtual file
169      * @return Filename
170      */

171     protected String JavaDoc getFileName(VirtualFile vfFile) {
172         String JavaDoc sContentType = vfFile.getVFS().getVirtualFileSystemView().getContentType(vfFile);
173         String JavaDoc 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 JavaDoc aExts = MimeTypeMapping.getExtensions(sContentType);
182             String JavaDoc sExt = (String JavaDoc) aExts.get(0);
183         
184             sFilename = vfFileForName.getFileName() + "." + sExt;
185         } else {
186             sFilename = vfFileForName.getFileName();
187             
188             if(sFilename.indexOf(".")>-1) {
189                 String JavaDoc sExt = sFilename.substring(sFilename.indexOf(".")+1);
190                 List JavaDoc types = MimeTypeMapping.getMimeTypes(sExt);
191                 if(types!=null && types.size()>0) {
192                     vfFile.getVFS().getVirtualFileSystemView().setContentType(vfFile, (String JavaDoc)types.get(0));
193                 }
194             }
195         }
196         return sFilename;
197     }
198
199     /* (non-Javadoc)
200      * @see com.simulacramedia.contentmanager.editors.Editor#discardChanges(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
201      */

202     public StatusData discardChanges(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
203         
204         this.createWorkingFile(vfs.getVirtualFile(sPath).getResource());
205         
206         return new VFSStatus();
207     }
208 }
Popular Tags