KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.util.*;
23
24 import org.openharmonise.him.harmonise.*;
25 import org.openharmonise.vfs.*;
26 import org.openharmonise.vfs.context.*;
27 import org.openharmonise.vfs.status.*;
28
29
30 /**
31  * The editor controller is the external interface to the editor
32  * architecture. It makes the decisions about which {@link org.openharmonise.him.editors.Editor}
33  * is applicable for a virtual file, manages workfing file and monitors them for
34  * changes.
35  *
36  * @author Matthew Large
37  * @version $Revision: 1.2 $
38  *
39  */

40 public class EditorController implements ContextListener {
41
42     /**
43      * Map of full paths to {@link FileDetails} objects.
44      */

45     private HashMap m_VirtualPhysicalMapping = new HashMap();
46
47     /**
48      * Instance of editor controller, follows singleton pattern.
49      */

50     private static EditorController m_instance = null;
51
52     /**
53      *
54      */

55     private EditorController() {
56         super();
57         FileMonitor fm = new FileMonitor(1000 * 5);
58         fm.start();
59         ContextHandler.getInstance().addListener(
60             ContextType.CONTEXT_SHUTDOWN,
61             this);
62     }
63
64     /**
65      * Returns the instance of the editor controller, follows the
66      * singleton pattern.
67      *
68      * @return Instance
69      */

70     public static EditorController getInstance() {
71         if (m_instance == null) {
72             m_instance = new EditorController();
73         }
74         return m_instance;
75     }
76
77     /**
78      * Opens a virtual file for editing.
79      *
80      * @param sPath Full path
81      * @param vfs Virtual file system
82      */

83     public StatusData open(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
84         StatusData status = new VFSStatus();
85         
86         Editor editor = this.getEditor(sPath, vfs);
87         if (editor != null) {
88             PathStatusWrapper pathStatus = editor.open(sPath, vfs);
89             String JavaDoc sRealPath = pathStatus.getPath();
90             status = pathStatus.getStatus();
91             if (sRealPath != null) {
92                 File fFile = new File(sRealPath);
93                 long modified = fFile.lastModified();
94                 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor);
95                 details.setModified(modified);
96                 this.m_VirtualPhysicalMapping.put(sPath, details);
97             }
98         }
99         
100         return status;
101     }
102     
103     /**
104      * Opens a virtual file for previewing.
105      *
106      * @param sPath Full path
107      * @param vfs Virtual file system
108      */

109     public StatusData preview(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
110         StatusData status = new VFSStatus();
111
112         Editor editor = this.getEditor(sPath, vfs);
113         if (editor != null) {
114             PathStatusWrapper pathStatus = editor.preview(sPath, vfs);
115             String JavaDoc sRealPath = pathStatus.getPath();
116             status = pathStatus.getStatus();
117             if (sRealPath != null) {
118                 File fFile = new File(sRealPath);
119             }
120         }
121         
122         return status;
123     }
124     
125     /**
126      * Exports the content of a virtual file.
127      *
128      * @param sPath Full path
129      * @param vfs Virtual file system
130      */

131     public StatusData export(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
132         StatusData status = new VFSStatus();
133         
134         Editor editor = this.getEditor(sPath, vfs);
135         if (editor != null) {
136             status = editor.export(sPath, vfs);
137         }
138         
139         return status;
140     }
141
142     /**
143      * Creates a new virtual file.
144      *
145      * @param sPath Full path
146      * @param vfs Virtual file system
147      * @return true if the resource was actually created
148      */

149     public StatusData createNew(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
150         StatusData status = new VFSStatus();
151         
152         Editor editor = this.getEditor(sPath, vfs);
153         if (editor != null) {
154             PathStatusWrapper pathStatus = editor.createNew(sPath, vfs);
155             String JavaDoc sRealPath = pathStatus.getPath();
156             status = pathStatus.getStatus();
157             if (sRealPath != null) {
158                 File fFile = new File(sRealPath);
159                 long modified = fFile.lastModified();
160                 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor);
161                 details.setModified(modified);
162                 this.m_VirtualPhysicalMapping.put(sPath, details);
163             }
164             if(!editor.hasResourceBeenCreated()) {
165                 status.setStatusLevel(StatusData.LEVEL_ERROR);
166                 System.err.println("Editor says resource NOT created.");
167             } else {
168                 System.err.println("Editor says resource WAS created.");
169             }
170             return status;
171         }
172         status.setStatusLevel(StatusData.LEVEL_ERROR);
173         return status;
174     }
175
176     /**
177      * Creates a new virtual file.
178      *
179      * @param sPath Full path
180      * @param content Content for new virtual file
181      * @param vfs Virtual file system
182      * @return true if the resource was actually created
183      */

184     public StatusData createNew(
185         String JavaDoc sPath,
186         byte[] content,
187         AbstractVirtualFileSystem vfs) {
188         StatusData status = new VFSStatus();
189         
190         Editor editor = this.getEditor(sPath, vfs);
191         if (editor != null) {
192             PathStatusWrapper pathStatus = editor.createNew(sPath, content, vfs);
193             String JavaDoc sRealPath = pathStatus.getPath();
194             status = pathStatus.getStatus();
195             if (sRealPath != null) {
196                 File fFile = new File(sRealPath);
197                 long modified = fFile.lastModified();
198                 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor);
199                 details.setModified(modified);
200                 this.m_VirtualPhysicalMapping.put(sPath, details);
201             }
202             if(!editor.hasResourceBeenCreated()) {
203                 status.setStatusLevel(StatusData.LEVEL_ERROR);
204                 System.err.println("Editor says resource NOT created.");
205             } else {
206                 System.err.println("Editor says resource WAS created.");
207             }
208             return status;
209         }
210         status.setStatusLevel(StatusData.LEVEL_ERROR);
211         return status;
212     }
213
214     /**
215      * Discards any changes that have been made to a virtual file's
216      * content.
217      *
218      * @param sPath Full path
219      * @param vfs Virtual file system
220      */

221     public StatusData discardChanges(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
222         StatusData status = new VFSStatus();
223         
224         Editor editor = this.getEditor(sPath, vfs);
225         if (editor != null) {
226             status = editor.discardChanges(sPath, vfs);
227         }
228         return status;
229     }
230
231     /**
232      * @param fullPath
233      * @param vfs
234      */

235     public StatusData upload(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
236         StatusData status = new VFSStatus();
237         
238         Editor editor = this.getEditor(sPath, vfs);
239         if (editor != null) {
240             PathStatusWrapper pathStatus = editor.upload(sPath, vfs);
241             String JavaDoc sRealPath = pathStatus.getPath();
242             status = pathStatus.getStatus();
243             if (sRealPath != null) {
244                 File fFile = new File(sRealPath);
245                 long modified = fFile.lastModified();
246                 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor);
247                 details.setModified(modified);
248                 this.m_VirtualPhysicalMapping.put(sPath, details);
249             }
250         }
251         return status;
252     }
253
254     /**
255      * Returns an editor appropriate to the given path and virtual file
256      * system.
257      *
258      * @param sPath Full path
259      * @param vfs Virtual file system
260      * @return Editor or null if none found
261      */

262     private Editor getEditor(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
263         Editor editor = null;
264
265         editor = this.getLiveEditor(sPath, vfs);
266         if (editor == null) {
267             editor = this.getNoneLiveEditor(sPath, vfs);
268             if (editor == null) {
269             }
270         }
271
272         return editor;
273     }
274
275     /**
276      * Returns an editor appropriate to the given live path and virtual file
277      * system.
278      *
279      * @param sPath Full path
280      * @param vfs Virtual file system
281      * @return Editor or null if none found
282      */

283     private Editor getLiveEditor(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
284
285         Editor editor = null;
286         if (sPath.startsWith(HarmonisePaths.PATH_DOCUMENTS)) {
287             editor = new XMetaLEditor();
288         } else if (sPath.startsWith(HarmonisePaths.PATH_NEWSLETTER)) {
289             editor = new XMetaLEditor();
290         } else if (sPath.startsWith(HarmonisePaths.PATH_COMPOSITION)) {
291             editor = new CompositionEditor();
292         } else if (sPath.startsWith(HarmonisePaths.PATH_XSLT)) {
293             editor = new XSLTEditor();
294         } else if (sPath.startsWith(HarmonisePaths.PATH_PAGE_DEFINITION)) {
295             editor = new PageDefinitionEditor();
296         } else if (
297             sPath.startsWith(HarmonisePaths.PATH_LINKS)
298                 || sPath.startsWith(HarmonisePaths.PATH_EMAIL)) {
299             editor = new LinkEditor();
300         } else if (sPath.startsWith(HarmonisePaths.PATH_USERS)) {
301             editor = new UserEditor();
302         } else if (sPath.startsWith(HarmonisePaths.PATH_REPORTS_QUERIES)) {
303             editor = new SystemReportEditor();
304         } else if (sPath.startsWith(HarmonisePaths.PATH_REPORTS_OUTPUT)) {
305             editor = new SystemReportOutputEditor();
306         } else if (
307             sPath.startsWith(HarmonisePaths.PATH_WORKFLOW_PROPS)
308             || sPath.startsWith(HarmonisePaths.PATH_WORKFLOW_STAGES)) {
309             editor = new WorkflowEditor();
310         } else if (
311             sPath.startsWith(HarmonisePaths.PATH_METADATA)
312                 || sPath.startsWith(HarmonisePaths.PATH_RBS_PROPS)
313                 || sPath.startsWith(HarmonisePaths.PATH_RBS_VALS)
314                 || sPath.startsWith(HarmonisePaths.PATH_RBS_PROPS)
315                 || sPath.startsWith(HarmonisePaths.PATH_RBS_PROPS)) {
316             editor = new BlankEditor();
317         }
318         
319         return editor;
320     }
321
322     /**
323      * Returns an editor appropriate to the given none live path and virtual file
324      * system.
325      *
326      * @param sPath Full path
327      * @param vfs Virtual file system
328      * @return Editor or null if none found
329      */

330     private Editor getNoneLiveEditor(
331         String JavaDoc sPath,
332         AbstractVirtualFileSystem vfs) {
333         Editor editor = null;
334
335         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
336         if (vfFile != null
337             && (vfFile.getState() == VirtualFile.STATE_PENDING
338                 || vfFile.getState() == VirtualFile.STATE_HISTORICAL)
339             && ((VersionedVirtualFile) vfFile).getLiveVersionPath() != null) {
340             sPath = ((VersionedVirtualFile) vfFile).getLiveVersionPath();
341         } else if (
342             vfFile != null
343                 && (vfFile.getState() == VirtualFile.STATE_PENDING
344                     || vfFile.getState() == VirtualFile.STATE_HISTORICAL)
345                 && ((VersionedVirtualFile) vfFile).getLiveVersionPath() == null) {
346         }
347         editor = this.getLiveEditor(sPath, vfs);
348         if(editor==null) {
349             editor = new FileAssetEditor();
350         }
351         return editor;
352     }
353
354     /**
355      * Checks all current working files for changes to their modification
356      * date. If changes are found the content of the working file is
357      * copied into the virtual file.
358      *
359      */

360     protected void checkFiles() {
361         if (this.m_VirtualPhysicalMapping.size() > 0) {
362             Iterator itor = this.m_VirtualPhysicalMapping.values().iterator();
363             while (itor.hasNext()) {
364                 FileDetails details = (FileDetails) itor.next();
365                 String JavaDoc sRealPath = details.getRealPath();
366                 File fFile = new File(sRealPath);
367                 long modified = fFile.lastModified();
368
369                 if (modified > details.getModified()) {
370                     VirtualFile vfFile =
371                         details.getVFS().getVirtualFile(
372                             details.getVirtualPath()).getResource();
373
374                     if(details.getEditor() instanceof XMetaLEditor) {
375                         Long JavaDoc lFileLength = new Long JavaDoc(fFile.length());
376                         byte[] byteContents = new byte[lFileLength.intValue()];
377                         char[] chars = new char[1];
378
379                         StringBuffer JavaDoc sBuffContent = new StringBuffer JavaDoc();
380
381                         FileInputStream fis = null;
382                         InputStreamReader isr = null;
383                         try {
384                             fis = new FileInputStream(fFile);
385                             isr = new InputStreamReader(fis, "UTF-8");
386                             
387                             char tempChar = ' ';
388                             int nByte = isr.read(chars);
389                             
390                             while(nByte!=-1) {
391                                 sBuffContent.append(chars);
392                                 nByte = isr.read(chars);
393                             }
394
395                             try {
396                                 vfFile.setContent(sBuffContent.toString().trim().getBytes("UTF-8"));
397                             } catch(Exception JavaDoc e) {
398                                 e.printStackTrace();
399                                 vfFile.setContent(sBuffContent.toString().trim().getBytes());
400                             }
401
402                             details.setModified(modified);
403                         } catch (Exception JavaDoc e2) {
404                             e2.printStackTrace(System.out);
405                         } finally {
406                             try {
407                                 isr.close();
408                                 fis.close();
409                             } catch (Exception JavaDoc e3) {
410                                 e3.printStackTrace(System.out);
411                             }
412                         }
413                     } else {
414                         Long JavaDoc lFileLength = new Long JavaDoc(fFile.length());
415                         byte[] byteContents = new byte[lFileLength.intValue()];
416
417                         FileInputStream fis = null;
418                         try {
419                             fis = new FileInputStream(fFile);
420                             fis.read(byteContents);
421                             vfFile.setContent(byteContents);
422
423                             details.setModified(modified);
424                         } catch (Exception JavaDoc e2) {
425                             e2.printStackTrace(System.out);
426                         } finally {
427                             try {
428                                 fis.close();
429                             } catch (Exception JavaDoc e3) {
430                                 e3.printStackTrace(System.out);
431                             }
432                         }
433                     }
434                 }
435             }
436         }
437     }
438     
439     /**
440      * Checks if a file contains validly parsable XML.
441      *
442      * @param file File to check
443      * @return true if the file contains validly parsable XML
444      */

445     private boolean isValidXML(File file) {
446         boolean bValid = true;
447         
448         return bValid;
449     }
450
451     /**
452      * Returns the file details for a specified working file path.
453      *
454      * @param sRealPath Local path
455      * @return File details or null if none found
456      */

457     private FileDetails getDetails(String JavaDoc sRealPath) {
458         FileDetails details = null;
459
460         Iterator itor = this.m_VirtualPhysicalMapping.values().iterator();
461         while (itor.hasNext()) {
462             FileDetails element = (FileDetails) itor.next();
463             if (element.getRealPath().equalsIgnoreCase(sRealPath)) {
464                 details = element;
465             }
466         }
467
468         return details;
469     }
470
471     /**
472      * Wrapper for the details about a virtual file and its' local
473      * working file.
474      *
475      * @author Matthew Large
476      * @version $Revision: 1.2 $
477      *
478      */

479     class FileDetails {
480         
481         /**
482          * Path to local working file.
483          */

484         private String JavaDoc m_sRealPath = null;
485         
486         /**
487          * Full path to virtual file.
488          */

489         private String JavaDoc m_sVirtualPath = null;
490         
491         /**
492          * Virtual file system.
493          */

494         private AbstractVirtualFileSystem m_vfs = null;
495         
496         /**
497          * Last recorded modified date for working file.
498          */

499         private long m_lModified = 0;
500         
501         /**
502          * Editor used for this resoruce.
503          */

504         private Editor m_editor = null;
505
506         /**
507          * Constructs a new file details object.
508          *
509          * @param sRealPath Path to local working file
510          * @param sVirtualPath Full path to virtual file
511          * @param vfs Virtual file system
512          * @param editor Editor used for this resource
513          */

514         public FileDetails(
515             String JavaDoc sRealPath,
516             String JavaDoc sVirtualPath,
517             AbstractVirtualFileSystem vfs,
518             Editor editor) {
519             super();
520             this.m_sRealPath = sRealPath;
521             this.m_sVirtualPath = sVirtualPath;
522             this.m_vfs = vfs;
523             this.m_editor = editor;
524         }
525         
526         /**
527          * Returns the editor used for this resource.
528          *
529          * @return Editor
530          */

531         public Editor getEditor() {
532             return this.m_editor;
533         }
534
535         /**
536          * Returns the path to the local working resource.
537          *
538          * @return Path
539          */

540         public String JavaDoc getRealPath() {
541             return this.m_sRealPath;
542         }
543
544         /**
545          * Returns the full path to the virtual file.
546          *
547          * @return Full path
548          */

549         public String JavaDoc getVirtualPath() {
550             return this.m_sVirtualPath;
551         }
552
553         /**
554          * Returns the virtual file system.
555          *
556          * @return Virtual file system
557          */

558         public AbstractVirtualFileSystem getVFS() {
559             return this.m_vfs;
560         }
561
562         /**
563          * Returns the last recorded modified date for the local
564          * working file.
565          *
566          * @return Modified date
567          */

568         public long getModified() {
569             return this.m_lModified;
570         }
571
572         /**
573          * Sets the modified date for the local working file.
574          *
575          * @param lModified Modified date
576          */

577         public void setModified(long lModified) {
578             this.m_lModified = lModified;
579         }
580     }
581
582     /**
583      * Thread to monitor the working files for changes.
584      *
585      * @author Matthew Large
586      * @version $Revision: 1.2 $
587      *
588      */

589     class FileMonitor extends Thread JavaDoc {
590         
591         /**
592          * Sleep time in milliseconds.
593          */

594         private long sleepTime;
595
596         /**
597          * Constructs a new file monitor thread.
598          *
599          * @param sleepTime Sleep time in milliseconds
600          */

601         FileMonitor(long sleepTime) {
602             this.sleepTime = sleepTime;
603         }
604
605         /* (non-Javadoc)
606          * @see java.lang.Runnable#run()
607          */

608         public void run() {
609             while (true) {
610                 try {
611                     sleep(sleepTime);
612                 } catch (InterruptedException JavaDoc e) {
613                     // ignore it
614
}
615
616                 EditorController.getInstance().checkFiles();
617             }
618         }
619     }
620
621     /* (non-Javadoc)
622      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
623      */

624     public void contextMessage(ContextEvent ce) {
625         if (ce.CONTEXT_TYPE == ContextType.CONTEXT_SHUTDOWN) {
626             this.clearTempDir();
627         }
628     }
629
630     /**
631      * Clears the directory containing the local working files.
632      *
633      */

634     public void clearTempDir() {
635         File fTempDir = new File("C:\\ContentManager\\temp");
636         this.deleteDirContents(fTempDir);
637     }
638
639     /**
640      * Deletes the contents of a local directory.
641      *
642      * @param fDir Directory to have contents deleted
643      */

644     private void deleteDirContents(File fDir) {
645         File[] children = fDir.listFiles();
646         for (int i = 0; i < children.length; i++) {
647             File file = children[i];
648             if (file.isDirectory()) {
649                 this.deleteDirContents(file);
650             }
651             file.delete();
652         }
653     }
654     
655     public boolean isMonitoringVirtualFile(VirtualFile vfFile) {
656         boolean bRetn = false;
657         if(vfFile!=null && this.m_VirtualPhysicalMapping.keySet().contains(vfFile.getFullPath())) {
658             bRetn=true;
659         }
660         return bRetn;
661     }
662     
663     public String JavaDoc getRealPath(VirtualFile vfFile) {
664         String JavaDoc sRealPath = null;
665         
666         FileDetails details = (FileDetails) this.m_VirtualPhysicalMapping.get(vfFile.getFullPath());
667         if(details!=null) {
668             sRealPath = details.getRealPath();
669         }
670         
671         return sRealPath;
672     }
673
674 }
675
Popular Tags