001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors;
020:
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.util.List;
026:
027: import org.openharmonise.commons.net.*;
028: import org.openharmonise.vfs.*;
029: import org.openharmonise.vfs.status.*;
030:
031: /**
032: * Base class for assisting in the creation of {@link org.openharmonise.him.editors.Editor}
033: * implementations.
034: *
035: * @author Matthew Large
036: * @version $Revision: 1.1 $
037: *
038: */
039: public abstract class AbstractEditor {
040:
041: // TODO remove the hard codings!
042: protected final String m_sEditFilePath = "C:\\ContentManager\\temp\\edit\\";
043: protected final String m_sPreviewFilePath = "C:\\ContentManager\\temp\\preview\\";
044:
045: /**
046: * Creates a local working file for the content of the specified
047: * virtual file.
048: *
049: * @param vfFile Virtual file
050: * @return Path to local working file
051: */
052: protected String createWorkingFile(VirtualFile vfFile) {
053: return this .createWorkingFile(vfFile, null);
054: }
055:
056: /**
057: * Creates a local working file for the content of the specified
058: * virtual file.
059: *
060: * @param vfFile Virtual file
061: * @param fExportFile Local working file to use
062: * @return Path to local working file
063: */
064: protected String createWorkingFile(VirtualFile vfFile,
065: File fExportFile) {
066:
067: String sFilename = this .getFileName(vfFile);
068:
069: sFilename = sFilename.replaceAll(" ", "");
070: sFilename = sFilename.toLowerCase();
071:
072: File fFile = null;
073: if (fExportFile != null) {
074: fFile = fExportFile;
075: } else {
076: if (EditorController.getInstance().isMonitoringVirtualFile(
077: vfFile)) {
078: fFile = new File(EditorController.getInstance()
079: .getRealPath(vfFile));
080: } else {
081: fFile = new File(m_sEditFilePath + sFilename);
082: int nCount = 1;
083: while (fFile.exists()) {
084: String sTempName = this .appendToFilename(sFilename,
085: nCount);
086: fFile = new File(m_sEditFilePath + sTempName);
087: nCount++;
088: }
089: }
090: }
091:
092: FileOutputStream fos = null;
093:
094: try {
095: byte[] byteContent = vfFile.getContent();
096: fos = new FileOutputStream(fFile);
097: fos.write(byteContent);
098: } catch (FileNotFoundException e) {
099: e.printStackTrace();
100: } catch (IOException e) {
101: e.printStackTrace();
102: } finally {
103: try {
104: fos.close();
105: } catch (IOException e1) {
106: e1.printStackTrace();
107: }
108: }
109:
110: return fFile.getAbsolutePath();
111: }
112:
113: protected String createPreviewFile(String sFilename) {
114:
115: sFilename = sFilename.replaceAll(" ", "");
116: sFilename = sFilename.toLowerCase();
117:
118: File fFile = new File(m_sPreviewFilePath + sFilename);
119:
120: return fFile.getAbsolutePath();
121: }
122:
123: protected File createPreviewFile(String sFilename, byte[] content) {
124:
125: sFilename = sFilename.replaceAll(" ", "");
126: sFilename = sFilename.toLowerCase();
127:
128: File fFile = new File(m_sPreviewFilePath + sFilename);
129:
130: FileOutputStream fos = null;
131:
132: try {
133: fos = new FileOutputStream(fFile);
134: fos.write(content);
135: } catch (FileNotFoundException e) {
136: e.printStackTrace();
137: } catch (IOException e) {
138: e.printStackTrace();
139: } finally {
140: try {
141: fos.close();
142: } catch (IOException e1) {
143: e1.printStackTrace();
144: }
145: }
146:
147: return fFile;
148: }
149:
150: private String appendToFilename(String sFilename, int nAppend) {
151: return this .appendToFilename(sFilename, "_" + nAppend);
152: }
153:
154: private String appendToFilename(String sFilename, String sAppend) {
155: String sReturn = null;
156:
157: if (sFilename.indexOf(".") > 1) {
158: String sStart = sFilename.substring(0, sFilename
159: .indexOf("."));
160: String sExt = sFilename
161: .substring(sFilename.indexOf(".") + 1);
162:
163: sReturn = sStart + sAppend + "." + sExt;
164: } else {
165: sReturn = sFilename + sAppend;
166: }
167:
168: return sReturn;
169: }
170:
171: /**
172: * Returns a safe, human readable filename for the specified
173: * virtual file.
174: *
175: * @param vfFile Virtual file
176: * @return Filename
177: */
178: protected String getFileName(VirtualFile vfFile) {
179: String sContentType = vfFile.getVFS()
180: .getVirtualFileSystemView().getContentType(vfFile);
181: String sFilename = null;
182:
183: VirtualFile vfFileForName = vfFile;
184: if (vfFile.isVersionable()
185: && ((VersionedVirtualFile) vfFile).getState().equals(
186: VirtualFile.STATE_PENDING)
187: && ((VersionedVirtualFile) vfFile).getLiveVersionPath() != null) {
188: vfFileForName = vfFile.getVFS().getVirtualFile(
189: ((VersionedVirtualFile) vfFile)
190: .getLiveVersionPath()).getResource();
191: }
192:
193: if (sContentType != null && !sContentType.equals("")) {
194: List aExts = MimeTypeMapping.getExtensions(sContentType);
195: String sExt = (String) aExts.get(0);
196:
197: sFilename = vfFileForName.getFileName() + "." + sExt;
198: } else {
199: sFilename = vfFileForName.getFileName();
200:
201: if (sFilename.indexOf(".") > -1) {
202: String sExt = sFilename.substring(sFilename
203: .indexOf(".") + 1);
204: List types = MimeTypeMapping.getMimeTypes(sExt);
205: if (types != null && types.size() > 0) {
206: vfFile.getVFS().getVirtualFileSystemView()
207: .setContentType(vfFile,
208: (String) types.get(0));
209: }
210: }
211: }
212: return sFilename;
213: }
214:
215: /* (non-Javadoc)
216: * @see com.simulacramedia.contentmanager.editors.Editor#discardChanges(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
217: */
218: public StatusData discardChanges(String sPath,
219: AbstractVirtualFileSystem vfs) {
220:
221: this .createWorkingFile(vfs.getVirtualFile(sPath).getResource());
222:
223: return new VFSStatus();
224: }
225: }
|