KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > files > FilesSynchroniser


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.files;
20
21 import java.io.*;
22 import java.util.*;
23
24 import javax.xml.parsers.*;
25
26 import org.openharmonise.commons.net.*;
27 import org.openharmonise.him.window.messages.*;
28 import org.openharmonise.vfs.*;
29 import org.openharmonise.vfs.status.*;
30 import org.w3c.dom.*;
31 import org.xml.sax.*;
32
33
34 /**
35  * Delegate class for synchronising Virtual Files, so that we can interpose
36  * validation code on the content.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class FilesSynchroniser {
43
44
45     public FilesSynchroniser() {
46         super();
47     }
48     
49     /**
50      * Synchronises a Virtual File, capturing error or success messages
51      * and outputting them to the console. Also validates the content before
52      * attempting a synchronisation.
53      *
54      * @param vfFile Virtual File to synchronise
55      * @return true if the method executed without error
56      */

57     public boolean syncFile(VirtualFile vfFile) {
58         boolean bOK = true;
59         
60         String JavaDoc sFileName = null;
61         sFileName = vfFile.getVFS().getVirtualFileSystemView().getDisplayName(vfFile);
62         if(this.validateContent(vfFile, sFileName)) {
63             StatusData status = vfFile.sync();
64             if(!status.isOK()) {
65                 bOK=false;
66                 MessageHandler.getInstance().fireMessageEvent("Resource " + sFileName + " failed to be submitted, due to an error.", MessageHandler.TYPE_ERROR);
67             } else {
68                 MessageHandler.getInstance().fireMessageEvent("You have successfully submitted the resource \"" + sFileName + "\".", MessageHandler.TYPE_CONFIRM);
69             }
70         } else {
71             bOK=false;
72         }
73         
74         return bOK;
75     }
76     
77     /**
78      * Synchronises a list of Virtual Files, capturing error or success messages
79      * and outputting them to the console. Also validates the content before
80      * attempting a synchronisation.
81      *
82      * @param aVirtualFiles List of {@link VirtualFile} objects
83      * @return true if the method executed without error
84      */

85     public boolean syncFiles(List aVirtualFiles) {
86         boolean bOK = true;
87         Iterator itor = aVirtualFiles.iterator();
88         while (itor.hasNext()) {
89             VirtualFile vfFile = (VirtualFile) itor.next();
90             if(!this.syncFile(vfFile)) {
91                 bOK = false;
92             } else {
93             }
94         }
95         
96         return bOK;
97     }
98     
99     /**
100      * Validates the metadata of a Virtual File.
101      *
102      */

103     private void validateMetadata() {
104         
105     }
106     
107     /**
108      * Validates the content of a Virtual File.
109      *
110      * @param vfFile Virtual file whos contents is to be checked
111      * @param sFileName Display name of the file
112      * @return true if the content validated ok
113      */

114     private boolean validateContent(VirtualFile vfFile, String JavaDoc sFileName) {
115         boolean bOK = true;
116         
117         AbstractVirtualFileSystem vfs = vfFile.getVFS();
118         String JavaDoc sContentType = vfs.getVirtualFileSystemView().getContentType(vfFile);
119         if( (sContentType.equalsIgnoreCase(MimeTypeMapping.XML.getMimeType()) || sContentType.equalsIgnoreCase(MimeTypeMapping.XSLT.getMimeType()))
120             && vfFile.getContent()!=null) {
121             
122             Document xml = null;
123             try {
124                 ByteArrayInputStream bais = new ByteArrayInputStream(vfFile.getContent());
125                 InputStreamReader isr = new InputStreamReader(bais, "UTF-8");
126             
127                 xml =
128                     DocumentBuilderFactory
129                         .newInstance()
130                         .newDocumentBuilder()
131                         .parse(
132                         new org.xml.sax.InputSource JavaDoc(isr));
133             } catch (SAXException e1) {
134                 bOK = false;
135                 MessageHandler.getInstance().fireMessageEvent("There was a problem submitting the resource " + sFileName + ", due to an error: " + e1.getMessage(), MessageHandler.TYPE_ERROR);
136                 e1.printStackTrace();
137             } catch (ParserConfigurationException e1) {
138                 bOK = false;
139                 MessageHandler.getInstance().fireMessageEvent("There was a problem submitting the resource " + sFileName + ", due to an error: " + e1.getMessage(), MessageHandler.TYPE_ERROR);
140                 e1.printStackTrace();
141             } catch (FactoryConfigurationError e1) {
142                 bOK = false;
143                 MessageHandler.getInstance().fireMessageEvent("There was a problem submitting the resource " + sFileName + ", due to an error: " + e1.getMessage(), MessageHandler.TYPE_ERROR);
144                 e1.printStackTrace();
145             } catch (IOException e) {
146                 bOK = false;
147                 MessageHandler.getInstance().fireMessageEvent("There was a problem submitting the resource " + sFileName + ", due to an error: " + e.getMessage(), MessageHandler.TYPE_ERROR);
148                 e.printStackTrace();
149             }
150             
151         }
152         
153         return bOK;
154     }
155
156 }
157
Popular Tags