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.files;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: import javax.xml.parsers.*;
025:
026: import org.openharmonise.commons.net.*;
027: import org.openharmonise.him.window.messages.*;
028: import org.openharmonise.vfs.*;
029: import org.openharmonise.vfs.status.*;
030: import org.w3c.dom.*;
031: import org.xml.sax.*;
032:
033: /**
034: * Delegate class for synchronising Virtual Files, so that we can interpose
035: * validation code on the content.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class FilesSynchroniser {
042:
043: public FilesSynchroniser() {
044: super ();
045: }
046:
047: /**
048: * Synchronises a Virtual File, capturing error or success messages
049: * and outputting them to the console. Also validates the content before
050: * attempting a synchronisation.
051: *
052: * @param vfFile Virtual File to synchronise
053: * @return true if the method executed without error
054: */
055: public boolean syncFile(VirtualFile vfFile) {
056: boolean bOK = true;
057:
058: String sFileName = null;
059: sFileName = vfFile.getVFS().getVirtualFileSystemView()
060: .getDisplayName(vfFile);
061: if (this .validateContent(vfFile, sFileName)) {
062: StatusData status = vfFile.sync();
063: if (!status.isOK()) {
064: bOK = false;
065: MessageHandler
066: .getInstance()
067: .fireMessageEvent(
068: "Resource "
069: + sFileName
070: + " failed to be submitted, due to an error.",
071: MessageHandler.TYPE_ERROR);
072: } else {
073: MessageHandler.getInstance().fireMessageEvent(
074: "You have successfully submitted the resource \""
075: + sFileName + "\".",
076: MessageHandler.TYPE_CONFIRM);
077: }
078: } else {
079: bOK = false;
080: }
081:
082: return bOK;
083: }
084:
085: /**
086: * Synchronises a list of Virtual Files, capturing error or success messages
087: * and outputting them to the console. Also validates the content before
088: * attempting a synchronisation.
089: *
090: * @param aVirtualFiles List of {@link VirtualFile} objects
091: * @return true if the method executed without error
092: */
093: public boolean syncFiles(List aVirtualFiles) {
094: boolean bOK = true;
095: Iterator itor = aVirtualFiles.iterator();
096: while (itor.hasNext()) {
097: VirtualFile vfFile = (VirtualFile) itor.next();
098: if (!this .syncFile(vfFile)) {
099: bOK = false;
100: } else {
101: }
102: }
103:
104: return bOK;
105: }
106:
107: /**
108: * Validates the metadata of a Virtual File.
109: *
110: */
111: private void validateMetadata() {
112:
113: }
114:
115: /**
116: * Validates the content of a Virtual File.
117: *
118: * @param vfFile Virtual file whos contents is to be checked
119: * @param sFileName Display name of the file
120: * @return true if the content validated ok
121: */
122: private boolean validateContent(VirtualFile vfFile, String sFileName) {
123: boolean bOK = true;
124:
125: AbstractVirtualFileSystem vfs = vfFile.getVFS();
126: String sContentType = vfs.getVirtualFileSystemView()
127: .getContentType(vfFile);
128: if ((sContentType.equalsIgnoreCase(MimeTypeMapping.XML
129: .getMimeType()) || sContentType
130: .equalsIgnoreCase(MimeTypeMapping.XSLT.getMimeType()))
131: && vfFile.getContent() != null) {
132:
133: Document xml = null;
134: try {
135: ByteArrayInputStream bais = new ByteArrayInputStream(
136: vfFile.getContent());
137: InputStreamReader isr = new InputStreamReader(bais,
138: "UTF-8");
139:
140: xml = DocumentBuilderFactory.newInstance()
141: .newDocumentBuilder().parse(
142: new org.xml.sax.InputSource(isr));
143: } catch (SAXException e1) {
144: bOK = false;
145: MessageHandler.getInstance().fireMessageEvent(
146: "There was a problem submitting the resource "
147: + sFileName + ", due to an error: "
148: + e1.getMessage(),
149: MessageHandler.TYPE_ERROR);
150: e1.printStackTrace();
151: } catch (ParserConfigurationException e1) {
152: bOK = false;
153: MessageHandler.getInstance().fireMessageEvent(
154: "There was a problem submitting the resource "
155: + sFileName + ", due to an error: "
156: + e1.getMessage(),
157: MessageHandler.TYPE_ERROR);
158: e1.printStackTrace();
159: } catch (FactoryConfigurationError e1) {
160: bOK = false;
161: MessageHandler.getInstance().fireMessageEvent(
162: "There was a problem submitting the resource "
163: + sFileName + ", due to an error: "
164: + e1.getMessage(),
165: MessageHandler.TYPE_ERROR);
166: e1.printStackTrace();
167: } catch (IOException e) {
168: bOK = false;
169: MessageHandler.getInstance().fireMessageEvent(
170: "There was a problem submitting the resource "
171: + sFileName + ", due to an error: "
172: + e.getMessage(),
173: MessageHandler.TYPE_ERROR);
174: e.printStackTrace();
175: }
176:
177: }
178:
179: return bOK;
180: }
181:
182: }
|