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.core;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.interfaces.LibresourceCoreService;
038:
039: import org.libresource.xml.ImportExportLogger;
040: import org.libresource.xml.LibresourceImportHandler;
041:
042: import org.xml.sax.Attributes;
043:
044: import sun.misc.BASE64Decoder;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.File;
048: import java.io.FileOutputStream;
049: import java.io.OutputStream;
050:
051: import java.net.URI;
052:
053: public class TemplateImportHandler extends LibresourceImportHandler {
054: private URI node;
055: private String name;
056: private String description;
057: private String mimeType;
058: private boolean enteredInDefinition = false;
059: private StringBuffer tmpContent;
060: private LibresourceCoreService coreService;
061: private boolean contentMode;
062: private File tmpContentFile;
063: private OutputStream tmpContentOS;
064:
065: public void handleBeginElement(String name, Attributes attributes,
066: ImportExportLogger logger) throws Exception {
067: if (name.equals("libresource:template")) {
068: enteredInDefinition = true;
069: }
070:
071: if (name.equals("template:name")) {
072: tmpContent = new StringBuffer();
073: }
074:
075: if (name.equals("template:description")) {
076: tmpContent = new StringBuffer();
077: }
078:
079: if (name.equals("template:content")) {
080: tmpContent = new StringBuffer();
081: tmpContentFile = File.createTempFile("import", "template");
082: tmpContentOS = new FileOutputStream(tmpContentFile);
083: contentMode = true;
084: }
085: }
086:
087: public boolean handleEndElement(String name,
088: ImportExportLogger logger) throws Exception {
089: if (name.equals("libresource:template")) {
090: enteredInDefinition = false;
091: coreService.createTemplate(node, this .name,
092: this .description);
093: coreService.setTemplateContent(node, new FileDataImpl(
094: tmpContentFile));
095:
096: return true;
097: }
098:
099: if (name.equals("template:name")) {
100: this .name = tmpContent.toString().trim();
101:
102: return false;
103: }
104:
105: if (name.equals("template:description")) {
106: this .description = tmpContent.toString().trim();
107:
108: return false;
109: }
110:
111: if (name.equals("template:content")) {
112: contentMode = false;
113: new BASE64Decoder().decodeBuffer(new ByteArrayInputStream(
114: tmpContent.toString().trim().getBytes()),
115: tmpContentOS);
116: tmpContentOS.close();
117:
118: return false;
119: }
120:
121: return false;
122: }
123:
124: public void handleContent(String content, ImportExportLogger logger)
125: throws Exception {
126: if (tmpContent != null) {
127: if (contentMode) {
128: tmpContent.append(content.trim());
129: } else {
130: tmpContent.append(content);
131: }
132: }
133:
134: if (contentMode) {
135: if (tmpContent.length() >= 4194304) {
136: char[] buf = new char[tmpContent.length()];
137: tmpContent.getChars(0, tmpContent.length(), buf, 0);
138: new BASE64Decoder().decodeBuffer(
139: new ByteArrayInputStream(new String(buf)
140: .getBytes()), tmpContentOS);
141: tmpContent.delete(0, 4194304);
142: }
143: }
144: }
145:
146: public void init(URI node, ImportExportLogger logger)
147: throws Exception {
148: this .node = node;
149: name = null;
150: description = null;
151: enteredInDefinition = false;
152: tmpContent = null;
153: coreService = (LibresourceCoreService) Libresource
154: .getService("LibresourceCore");
155: }
156: }
|