001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/qti/XMLController.java $
003: * $Id: XMLController.java 15083 2006-09-20 20:03:55Z lydial@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.ui.bean.qti;
021:
022: import java.io.InputStream;
023: import java.io.Serializable;
024: import java.util.StringTokenizer; //import javax.faces.context.FacesContext;
025:
026: import org.w3c.dom.Document;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.sakaiproject.tool.assessment.qti.constants.QTIVersion; //import org.sakaiproject.tool.assessment.qti.helper.AuthoringHelper;
032: import org.sakaiproject.tool.assessment.qti.helper.AuthoringXml;
033: import org.sakaiproject.tool.assessment.services.qti.QTIService;
034: import org.sakaiproject.tool.assessment.qti.util.XmlUtil;
035:
036: /**
037: * <p>Bean for QTI XML or XML fragments and descriptive information. </p>
038: * <p>Used to maintain information or to dump XML to client.</p>
039: * <p>Copyright: Copyright (c) 2004 Sakai</p>
040: * @author Ed Smiley esmiley@stanford.edu
041: * @version $Id: XMLController.java 15083 2006-09-20 20:03:55Z lydial@stanford.edu $
042: */
043:
044: public class XMLController implements Serializable {
045: /**
046: *
047: */
048: private static final long serialVersionUID = 7064783681056628447L;
049:
050: private static Log log = LogFactory.getLog(XMLController.class);
051:
052: private static final String XML_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
053: + "\n";
054:
055: private XMLDisplay xmlBean;
056: private String documentType;
057: private String id;
058: private int qtiVersion;
059:
060: public XMLController() {
061: qtiVersion = QTIVersion.VERSION_1_2; //default
062: }
063:
064: public XMLDisplay getXmlBean() {
065: return xmlBean;
066: }
067:
068: public void setXmlBean(XMLDisplay xmlBean) {
069: this .xmlBean = xmlBean;
070: }
071:
072: /**
073: * sets needed info in xml display bean when id set to assessment id
074: * @return
075: */
076: public String displayAssessmentXml() {
077: log.debug("XMLController debug getQtiVersion(): "
078: + this .getQtiVersion());
079: documentType = AuthoringXml.ASSESSMENT;
080: return display();
081: }
082:
083: public String displaySectionXmlTemplate() {
084: documentType = AuthoringXml.SECTION;
085: return display();
086: }
087:
088: public String displayItemXml() {
089: documentType = AuthoringXml.ITEM_MCSC; // this is just a default, we will override
090: item();
091: return "xmlDisplay";
092: }
093:
094: public String displayItemBankXml() {
095: this .itemBank();
096: return "xmlDisplay";
097: }
098:
099: public String display() {
100: AuthoringXml ax = getAuthoringXml();
101:
102: try {
103: if (ax.isAssessment(documentType)) {
104: assessment();
105: } else if (ax.isSection(documentType)) {
106: section();
107: } else if (ax.isItem(documentType)) {
108: log.debug("item type detected");
109: item();
110: } else if (ax.isSurveyFragment(documentType)) {
111: frag();
112: }
113: } catch (Exception ex) {
114: setUpXmlNoDecl("<error-report>" + "\n" + ex.toString()
115: + "\n" + "</error-report>" + "\n");
116: xmlBean.setDescription(ex.toString());
117: xmlBean.setName("error");
118: xmlBean.setId("");
119: ex.printStackTrace();
120: }
121:
122: return "xmlDisplay";
123:
124: }
125:
126: public String getDocumentType() {
127: return documentType;
128: }
129:
130: public void setDocumentType(String documentType) {
131: this .documentType = documentType;
132: }
133:
134: public String getId() {
135: return id;
136: }
137:
138: public void setId(String id) {
139: this .id = id;
140: }
141:
142: private void assessment() {
143: xmlBean.setId(id);
144: xmlBean.setName(documentType);
145:
146: if (id != null && id.length() > 0) // return populated xml from template
147: {
148: QTIService qtiService = new QTIService();
149: log.debug("XMLController.assessment() assessment "
150: + "qtiService.getExportedAssessment(id=" + id
151: + ", qtiVersion=" + qtiVersion + ")");
152: Document doc = qtiService.getExportedAssessment(id,
153: qtiVersion);
154: xmlBean
155: .setDescription("Exported QTI XML Copyright: Copyright (c) 2005 Sakai");
156: xmlBean.setName("assessment " + id);
157: setUpXmlNoDecl(XmlUtil.getDOMString(doc));
158: } else // return xml template, for testing
159: {
160: xmlBean.setDescription("assessment template");
161: //AuthoringHelper authHelper = new AuthoringHelper(qtiVersion);
162: AuthoringXml ax = getAuthoringXml();
163:
164: String xml = ax.getTemplateAsString(ax
165: .getTemplateInputStream(AuthoringXml.ASSESSMENT));
166: setUpXmlNoDecl(xml);
167: }
168: }
169:
170: /**
171: * A utility method to set xml string in xml bean.
172: * @param xml the XML string
173: */
174: private void setUpXmlNoDecl(String xml) {
175: setUpXml(xml, false);
176: }
177:
178: /**
179: * Logic for set xml string in xml bean with or without xml declaration.
180: * @param xml the XML string
181: * @param includeXmlDecl include "<?xml version... ? true or false?
182: */
183: private void setUpXml(String xml, boolean includeXmlDecl) {
184: boolean hasXmlDecl = false;
185: String startDecl = "<?xml version";
186: String endDecl = "?>";
187: int endDeclLength = endDecl.length();
188:
189: if (xml.startsWith(startDecl)) {
190: hasXmlDecl = true;
191: }
192:
193: // if we want a decl add it if it doesn't have one
194: // if we don't want decl remove it if it is there
195: if (includeXmlDecl) {
196: if (!hasXmlDecl) {
197: xml = XML_DECL + xml;
198: }
199: } else {
200: if (hasXmlDecl) {
201: int declEndIndex = xml.indexOf(endDecl);
202: xml = xml.substring(declEndIndex + endDeclLength);
203: }
204: }
205: xmlBean.setXml(xml);
206: }
207:
208: /**
209: * @todo add code to populate from SectionHelper
210: */
211: private void section() {
212: xmlBean.setId(id);
213: AuthoringXml ax = getAuthoringXml();
214: if (id != null && id.length() > 0) {
215: xmlBean.setDescription("section fragment id=" + id);
216: xmlBean.setName(documentType); // get from document later
217: InputStream is = ax
218: .getTemplateInputStream(AuthoringXml.SECTION);
219: setUpXmlNoDecl(ax.getTemplateAsString(is));
220: } else {
221: xmlBean.setDescription("section template");
222: xmlBean.setName(documentType); // get from document later
223: InputStream is = ax
224: .getTemplateInputStream(AuthoringXml.SECTION);
225: setUpXmlNoDecl(ax.getTemplateAsString(is));
226: }
227: }
228:
229: /**
230: * read in XML from item or item template
231: */
232: private void item() {
233: xmlBean.setId(id);
234: if (id != null && id.length() > 0) {
235: QTIService qtiService = new QTIService();
236: Document doc = qtiService.getExportedItem(id, qtiVersion);
237: xmlBean
238: .setDescription("Exported QTI XML Copyright: Copyright (c) 2005 Sakai");
239: xmlBean.setName("item " + id); // get from document later
240: setUpXmlNoDecl(XmlUtil.getDOMString(doc));
241: } else // for testing
242: {
243: //AuthoringHelper ah = new AuthoringHelper(qtiVersion);
244: AuthoringXml ax = getAuthoringXml();
245: xmlBean.setDescription("item template");
246: xmlBean.setName(documentType); // get from document later
247: InputStream is = ax.getTemplateInputStream(documentType);
248: setUpXmlNoDecl(ax.getTemplateAsString(is));
249: }
250: }
251:
252: /**
253: * read in XML from item list (comma separated id string)
254: */
255: private void itemBank() {
256: xmlBean.setId(id); // this will be an item list
257: if (id != null && id.length() > 0) {
258: QTIService qtiService = new QTIService();
259: StringTokenizer st = new StringTokenizer(id, ",");
260: int tokens = st.countTokens();
261: String[] ids = new String[tokens];
262: for (int i = 0; st.hasMoreTokens(); i++) {
263: ids[i] = st.nextToken();
264: }
265: Document doc = qtiService.getExportedItemBank(ids,
266: qtiVersion);
267: xmlBean
268: .setDescription("Exported QTI XML Copyright: Copyright (c) 2005 Sakai");
269: xmlBean.setName("object bank for items " + id); // get from document later
270: setUpXmlNoDecl(XmlUtil.getDOMString(doc));
271: } else {
272: log.debug("object bank empty");
273: }
274: }
275:
276: private void frag() {
277: xmlBean.setDescription("survey item fragment template");
278: xmlBean.setName(documentType); // get from document later
279: InputStream is = getAuthoringXml().getTemplateInputStream(
280: documentType);
281: setUpXmlNoDecl(getAuthoringXml().getTemplateAsString(is));
282: }
283:
284: // /**
285: // * derived property, uses String value of qtiVersion
286: // * @return String value of qtiVersion
287: // */
288: // public String getVersion()
289: // {
290: // return "" + getQtiVersion();
291: // }
292:
293: // /**
294: // * derived property, uses String value of qtiVersion
295: // * if invalid will not set it
296: // * @param version String value of qtiVersion
297: // */
298: // public void setVersion(String version)
299: // {
300: //
301: // try {
302: // int v = Integer.parseInt(version);
303: // setQtiVersion(v);
304: // }
305: // catch (NumberFormatException ex) {
306: // // leave value alone
307: // }
308: // }
309:
310: /**
311: * Always returns a valid QTI version.
312: * @return
313: */
314: public int getQtiVersion() {
315: if (!QTIVersion.isValid(qtiVersion)) {
316: qtiVersion = QTIVersion.VERSION_1_2; // default
317: }
318: log.debug("xml controller getQtiVersion()=" + qtiVersion);
319: return qtiVersion;
320: }
321:
322: /**
323: * Only accepts valid QTI version.
324: * @param qtiVersion
325: */
326: public void setQtiVersion(int qtiVersion) {
327: if (!QTIVersion.isValid(qtiVersion)) {
328: throw new IllegalArgumentException("NOT Legal Qti Version.");
329: }
330:
331: this .qtiVersion = qtiVersion;
332: log.debug("xml controller setQtiVersion()=" + qtiVersion);
333: }
334:
335: public AuthoringXml getAuthoringXml() {
336: return new AuthoringXml(getQtiVersion());
337: }
338:
339: }
|