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.survey;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.survey.interfaces.LibresourceSurveyService;
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.Iterator;
050: import java.util.Vector;
051:
052: public class SurveyImportHandler extends LibresourceImportHandler {
053: private URI node;
054: private String question;
055: private Date creationDate;
056: private Date lastVoteDate;
057: private String opening;
058: private String displayMode;
059: private String votersList;
060: private String choice;
061: private String nbVotes;
062: private Vector options = null;
063: private boolean enteredInDefinition = false;
064: private StringBuffer tmpContent;
065: private LibresourceSurveyService libresourceSurveyService;
066:
067: public void handleBeginElement(String name, Attributes attributes,
068: ImportExportLogger logger) throws Exception {
069: if (name.equals("survey:survey")) {
070: enteredInDefinition = true;
071: creationDate = Libresource.parseDate(attributes
072: .getValue("creation"));
073: lastVoteDate = Libresource.parseDate(attributes
074: .getValue("lastVote"));
075: }
076:
077: if (name.equals("survey:question")) {
078: tmpContent = new StringBuffer();
079: }
080:
081: if (name.equals("survey:opening")) {
082: tmpContent = new StringBuffer();
083: }
084:
085: if (name.equals("survey:votersList")) {
086: tmpContent = new StringBuffer();
087: }
088:
089: if (name.equals("survey:displayMode")) {
090: tmpContent = new StringBuffer();
091: }
092:
093: if (name.equals("survey:option")) {
094: ;
095: }
096:
097: if (name.equals("option:choice")) {
098: tmpContent = new StringBuffer();
099: }
100:
101: if (name.equals("option:nbVotes")) {
102: tmpContent = new StringBuffer();
103: }
104: }
105:
106: public boolean handleEndElement(String name,
107: ImportExportLogger logger) throws Exception {
108: if (name.equals("survey:survey")) {
109: enteredInDefinition = false;
110: libresourceSurveyService.createSurvey(node, this .question,
111: this .votersList, this .opening, this .displayMode,
112: this .creationDate, this .lastVoteDate);
113:
114: for (Iterator i = options.iterator(); i.hasNext();) {
115: libresourceSurveyService.addOptionInSurvey(node, i
116: .next().toString(), Integer.parseInt(i.next()
117: .toString()));
118: }
119:
120: options.clear();
121:
122: return true;
123: }
124:
125: if (name.equals("survey:question")) {
126: this .question = tmpContent.toString().trim();
127:
128: return false;
129: }
130:
131: if (name.equals("survey:votersList")) {
132: this .votersList = tmpContent.toString().trim();
133:
134: return false;
135: }
136:
137: if (name.equals("survey:opening")) {
138: this .opening = tmpContent.toString().trim();
139:
140: return false;
141: }
142:
143: if (name.equals("survey:displayMode")) {
144: this .displayMode = tmpContent.toString().trim();
145:
146: return false;
147: }
148:
149: if (name.equals("survey:option")) {
150: options.add(this .choice);
151: options.add(this .nbVotes);
152: this .choice = null;
153: this .nbVotes = null;
154:
155: return false;
156: }
157:
158: if (name.equals("option:choice")) {
159: this .choice = tmpContent.toString().trim();
160:
161: return false;
162: }
163:
164: if (name.equals("option:nbVotes")) {
165: this .nbVotes = tmpContent.toString().trim();
166:
167: return false;
168: }
169:
170: return false;
171: }
172:
173: public void handleContent(String content, ImportExportLogger logger)
174: throws Exception {
175: if (tmpContent != null) {
176: tmpContent.append(content);
177: }
178: }
179:
180: public void init(URI node, ImportExportLogger logger)
181: throws Exception {
182: this .node = node;
183: question = null;
184: creationDate = null;
185: lastVoteDate = null;
186: votersList = null;
187: opening = null;
188: choice = null;
189: nbVotes = null;
190: displayMode = null;
191: options = new Vector();
192: enteredInDefinition = false;
193: tmpContent = null;
194: libresourceSurveyService = (LibresourceSurveyService) Libresource
195: .getService("LibresourceSurvey");
196: }
197: }
|