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.wiki;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.wiki.interfaces.LibresourceWikiService;
038:
039: import org.libresource.xml.ImportExportLogger;
040: import org.libresource.xml.LibresourceImportHandler;
041:
042: import org.xml.sax.Attributes;
043:
044: import java.net.URI;
045:
046: import java.text.SimpleDateFormat;
047:
048: import java.util.Date;
049: import java.util.TreeSet;
050:
051: public class PageImportHandler extends LibresourceImportHandler {
052: private URI node;
053: private String name;
054: private String lastEditor;
055: private Date lastEdited;
056: private String content;
057: private boolean versionable;
058:
059: // history
060: private TreeSet history;
061: private HistoryEntry entry;
062: private int entryId;
063: private String entryUser;
064: private Date entryDate;
065: private String entryComment;
066: private String entryContent;
067: private boolean enteredInDefinition = false;
068: private StringBuffer tmpContent;
069: private LibresourceWikiService libresourceWikiService;
070:
071: public void handleBeginElement(String name, Attributes attributes,
072: ImportExportLogger logger) throws Exception {
073: if (name.equals("wiki:page")) {
074: enteredInDefinition = true;
075: lastEditor = attributes.getValue("lastEditor");
076: lastEdited = Libresource.parseDate(attributes
077: .getValue("lastEdited"));
078: }
079:
080: if (name.equals("page:name")) {
081: tmpContent = new StringBuffer();
082: }
083:
084: if (name.equals("page:content")) {
085: tmpContent = new StringBuffer();
086: }
087:
088: if (name.equals("page:versionable")) {
089: tmpContent = new StringBuffer();
090: }
091:
092: if (name.equals("page:history")) {
093: history = new TreeSet(new EntryComparator());
094: }
095:
096: if (name.equals("history:entry")) {
097: entry = null;
098: entryId = new Integer(attributes.getValue("id")).intValue();
099: entryUser = attributes.getValue("user");
100: entryDate = Libresource.parseDate(attributes
101: .getValue("date"));
102: }
103:
104: if (name.equals("entry:comment")) {
105: tmpContent = new StringBuffer();
106: }
107:
108: if (name.equals("entry:content")) {
109: tmpContent = new StringBuffer();
110: }
111: }
112:
113: public boolean handleEndElement(String name,
114: ImportExportLogger logger) throws Exception {
115: if (name.equals("wiki:page")) {
116: enteredInDefinition = false;
117: libresourceWikiService.createPage(node, this .name, false);
118: libresourceWikiService.editPageContent(node, this .content,
119: lastEditor, lastEdited);
120:
121: // import history
122: if (this .versionable) {
123: libresourceWikiService.editPage(node, this .name,
124: this .versionable);
125: libresourceWikiService.importPageHistory(node, history);
126: }
127:
128: return true;
129: }
130:
131: if (name.equals("page:name")) {
132: this .name = tmpContent.toString().trim();
133:
134: return false;
135: }
136:
137: if (name.equals("page:content")) {
138: this .content = tmpContent.toString().trim();
139:
140: return false;
141: }
142:
143: if (name.equals("page:versionable")) {
144: this .versionable = new Boolean(tmpContent.toString().trim())
145: .booleanValue();
146:
147: return false;
148: }
149:
150: if (name.equals("page:history")) {
151: return false;
152: }
153:
154: if (name.equals("history:entry")) {
155: entry = new HistoryEntry(this .entryId, this .entryDate,
156: this .entryContent, this .entryUser,
157: this .entryComment);
158: history.add(entry);
159:
160: return false;
161: }
162:
163: if (name.equals("entry:comment")) {
164: this .entryComment = tmpContent.toString().trim();
165:
166: return false;
167: }
168:
169: if (name.equals("entry:content")) {
170: this .entryContent = tmpContent.toString().trim();
171:
172: return false;
173: }
174:
175: return false;
176: }
177:
178: public void handleContent(String content, ImportExportLogger logger)
179: throws Exception {
180: if (tmpContent != null) {
181: tmpContent.append(content);
182: }
183: }
184:
185: public void init(URI node, ImportExportLogger logger)
186: throws Exception {
187: this .node = node;
188: name = null;
189: content = null;
190: versionable = false;
191: history = null;
192: entry = null;
193: entryUser = null;
194: entryDate = null;
195: entryComment = null;
196: entryContent = null;
197: enteredInDefinition = false;
198: tmpContent = null;
199: libresourceWikiService = (LibresourceWikiService) Libresource
200: .getService(WikiConstants.SERVICE);
201: }
202: }
|