001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.files;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.FileDataImpl;
038: import org.libresource.core.interfaces.LibresourceCoreService;
039:
040: import org.libresource.files.interfaces.LibresourceFilesService;
041:
042: import org.libresource.xml.ImportExportLogger;
043: import org.libresource.xml.LibresourceImportHandler;
044:
045: import org.xml.sax.Attributes;
046:
047: import sun.misc.BASE64Decoder;
048:
049: import java.io.ByteArrayInputStream;
050: import java.io.File;
051: import java.io.FileOutputStream;
052: import java.io.OutputStream;
053:
054: import java.net.URI;
055:
056: import java.text.SimpleDateFormat;
057:
058: import java.util.Date;
059:
060: public class FileImportHandler extends LibresourceImportHandler {
061: private URI node;
062: private String name;
063: private String description;
064: private String author;
065: private String mimeType;
066: private String throwEvent;
067: private Date date;
068: private boolean enteredInDefinition = false;
069: private StringBuffer tmpContent;
070: private LibresourceFilesService libresourceFilesService;
071: private LibresourceCoreService libresourceCoreService;
072: private boolean contentMode;
073: private File tmpContentFile;
074: private OutputStream tmpContentOS;
075: private String tmpDecodeBuffer;
076: private BASE64Decoder decoder;
077:
078: public void handleBeginElement(String name, Attributes attributes,
079: ImportExportLogger logger) throws Exception {
080: if (name.equals("files:file")) {
081: enteredInDefinition = true;
082: }
083:
084: if (name.equals("file:name")) {
085: tmpContent = new StringBuffer();
086: tmpDecodeBuffer = "";
087: }
088:
089: if (name.equals("file:description")) {
090: tmpContent = new StringBuffer();
091: }
092:
093: if (name.equals("file:author")) {
094: tmpContent = new StringBuffer();
095: }
096:
097: if (name.equals("file:type")) {
098: tmpContent = new StringBuffer();
099: }
100:
101: if (name.equals("file:throwEvent")) {
102: tmpContent = new StringBuffer();
103: }
104:
105: if (name.equals("file:date")) {
106: tmpContent = new StringBuffer();
107: }
108:
109: if (name.equals("file:content")) {
110: tmpContent = new StringBuffer();
111: File.createTempFile("fileImport", ".bin", new File(
112: libresourceCoreService.getDistributedFileSystem()));
113: tmpContentFile = File.createTempFile("import", "file");
114: tmpContentOS = new FileOutputStream(tmpContentFile);
115: contentMode = true;
116: }
117: }
118:
119: public boolean handleEndElement(String name,
120: ImportExportLogger logger) throws Exception {
121: if (name.equals("files:file")) {
122: enteredInDefinition = false;
123: libresourceFilesService.createFile(node, this .description,
124: this .author, new FileDataImpl(tmpContentFile,
125: this .name), new Boolean(this .throwEvent)
126: .booleanValue(), this .date);
127:
128: return true;
129: }
130:
131: if (name.equals("file:name")) {
132: this .name = tmpContent.toString().trim();
133:
134: return false;
135: }
136:
137: if (name.equals("file:description")) {
138: this .description = tmpContent.toString().trim();
139:
140: return false;
141: }
142:
143: if (name.equals("file:author")) {
144: this .author = tmpContent.toString().trim();
145:
146: return false;
147: }
148:
149: if (name.equals("file:type")) {
150: this .mimeType = tmpContent.toString().trim();
151:
152: return false;
153: }
154:
155: if (name.equals("file:throwEvent")) {
156: this .throwEvent = tmpContent.toString().trim();
157:
158: return false;
159: }
160:
161: if (name.equals("file:date")) {
162: this .date = Libresource.parseDate(tmpContent.toString()
163: .trim());
164:
165: return false;
166: }
167:
168: if (name.equals("file:content")) {
169: contentMode = false;
170: new BASE64Decoder().decodeBuffer(new ByteArrayInputStream(
171: tmpContent.toString().trim().getBytes()),
172: tmpContentOS);
173: tmpContentOS.close();
174:
175: return false;
176: }
177:
178: return false;
179: }
180:
181: public void handleContent(String content, ImportExportLogger logger)
182: throws Exception {
183: // Seb
184: if (tmpContent != null) {
185: if (contentMode) {
186: tmpDecodeBuffer = tmpDecodeBuffer + content.trim();
187:
188: int decodable = (tmpDecodeBuffer.length() / 4) * 4;
189: decoder.decodeBuffer(new ByteArrayInputStream(
190: tmpDecodeBuffer.substring(0, decodable)
191: .getBytes()), tmpContentOS);
192: tmpDecodeBuffer = tmpDecodeBuffer.substring(decodable);
193: } else {
194: tmpContent.append(content);
195: }
196: }
197:
198: // Bort
199:
200: /*
201: if (tmpContent != null) {
202: if (contentMode) {
203: tmpContent.append(content.trim());
204: } else {
205: tmpContent.append(content);
206: }
207: }
208:
209: if (contentMode) {
210: if (tmpContent.length() >= 4194304) {
211: char[] buf = new char[tmpContent.length()];
212: tmpContent.getChars(0, tmpContent.length(), buf, 0);
213: new BASE64Decoder().decodeBuffer(new ByteArrayInputStream(new String(buf).getBytes()), tmpContentOS);
214: tmpContent.delete(0, 4194304);
215: }
216: }
217: */
218: }
219:
220: public void init(URI node, ImportExportLogger logger)
221: throws Exception {
222: this .node = node;
223: name = null;
224: description = null;
225: author = null;
226: mimeType = null;
227: throwEvent = null;
228: date = null;
229: enteredInDefinition = false;
230: tmpContent = null;
231: decoder = new BASE64Decoder();
232: libresourceFilesService = (LibresourceFilesService) Libresource
233: .getService("LibresourceFiles");
234: libresourceCoreService = (LibresourceCoreService) Libresource
235: .getService("LibresourceCore");
236: }
237: }
|