001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/qti/asi/Assessment.java $
003: * $Id: Assessment.java 9274 2006-05-10 22:50:48Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 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.contentpackaging;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.tool.assessment.qti.asi.ASIBaseClass;
035: import org.sakaiproject.tool.assessment.qti.constants.QTIConstantStrings;
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Element;
038:
039: /**
040: * <p>Copyright: Copyright (c) 2003-5</p>
041: * <p>Organization: Sakai Project</p>
042: * @author casong
043: * @author Ed Smiley esmiley@stanford.edu
044: * @version $Id: Assessment.java 9274 2006-05-10 22:50:48Z daisyf@stanford.edu $
045: */
046: public class Manifest extends ASIBaseClass {
047: private static Log log = LogFactory.getLog(Manifest.class);
048: private String basePath;
049: private Map sections;
050: private Map items;
051: public static final String MANIFEST_TAG = "manifest";
052: public static final String RESOURCE_PATH = "manifest/resources/resource";
053: public static final String FILE_PATH = "manifest/resources/resource/file";
054:
055: /**
056: * Creates a new Assessment object.
057: */
058: protected Manifest() {
059: super ();
060: this .sections = new HashMap();
061: this .items = new HashMap();
062: this .basePath = QTIConstantStrings.QUESTESTINTEROP + "/"
063: + QTIConstantStrings.ASSESSMENT;
064: }
065:
066: /**
067: * Creates a new Assessment object.
068: *
069: * @param document the Document containing the assessment.
070: */
071: public Manifest(Document document) {
072: super (document);
073: this .sections = new HashMap();
074: this .items = new HashMap();
075: this .basePath = QTIConstantStrings.QUESTESTINTEROP + "/"
076: + QTIConstantStrings.ASSESSMENT;
077: }
078:
079: /**
080: * @param fieldlabel
081: * @param setValue
082: */
083: public void setFieldentry(String prefixPath, String fieldlabel,
084: String setValue) {
085: //String xpath = "questestinterop/assessment/qtimetadata/qtimetadatafield/fieldlabel[text()='" + fieldlabel + "']/following-sibling::fieldentry";
086: StringBuffer xpath = new StringBuffer(prefixPath);
087: xpath.append("/fieldlabel[text()='");
088: xpath.append(fieldlabel);
089: xpath.append("']/following-sibling::fieldentry");
090:
091: super .setFieldentry(xpath.toString(), setValue);
092: }
093:
094: /**
095: * Add a section ref with section Id sectionId.
096: *
097: * @param sectionId
098: */
099: public void addSectionRef(String sectionId) {
100: if (log.isDebugEnabled()) {
101: log.debug("addSection(String " + sectionId + ")");
102: }
103:
104: try {
105: String xpath = basePath;
106: DocumentBuilderFactory dbf = DocumentBuilderFactory
107: .newInstance();
108: DocumentBuilder db = dbf.newDocumentBuilder();
109: Document document = db.newDocument();
110: Element element = document
111: .createElement(QTIConstantStrings.SECTIONREF);
112: element.setAttribute(QTIConstantStrings.LINKREFID,
113: sectionId);
114: this .addElement(xpath, element);
115: } catch (ParserConfigurationException pce) {
116: log.error("Exception thrown from addSectionRef() : "
117: + pce.getMessage());
118: pce.printStackTrace();
119: }
120: }
121:
122: /**
123: * Remove a section ref with section Id sectionId.
124: *
125: * @param sectionId
126: */
127: public void removeSectionRef(String sectionId) {
128: if (log.isDebugEnabled()) {
129: log.debug("removeSectionRef(String " + sectionId + ")");
130: }
131:
132: String xpath = basePath + "/" + QTIConstantStrings.SECTIONREF
133: + "[@" + QTIConstantStrings.LINKREFID + "='"
134: + sectionId + "']";
135: this .removeElement(xpath);
136: }
137:
138: /**
139: * Remove all section refs.
140: */
141: public void removeSectionRefs() {
142: log.debug("removeSectionRefs()");
143: String xpath = basePath + "/" + QTIConstantStrings.SECTIONREF;
144: this .removeElement(xpath);
145: }
146:
147: /**
148: * Get a collection of section refs.
149: *
150: * @return
151: */
152: public List getSectionRefs() {
153: log.debug("getSectionRefs()");
154: String xpath = basePath + "/" + QTIConstantStrings.SECTIONREF;
155:
156: return this .selectNodes(xpath);
157: }
158:
159: /**
160: * Get a collection of sections.
161: * @return the sections
162: */
163: public Collection getSections() {
164: return this .sections.values();
165: }
166:
167: /**
168: * Get a collection of items.
169: * @return the items
170: */
171: public Collection getItems() {
172: return this .items.values();
173: }
174:
175: /**
176: *
177: *
178: * @return
179: */
180: public List getSectionRefIds() {
181: log.debug("getSectionRefIds()");
182: List refs = this .getSectionRefs();
183: List ids = new ArrayList();
184: int size = refs.size();
185: for (int i = 0; i < size; i++) {
186: Element ref = (Element) refs.get(0);
187: String idString = ref
188: .getAttribute(QTIConstantStrings.LINKREFID);
189: ids.add(idString);
190: }
191:
192: return ids;
193: }
194:
195: /**
196: * Assessment title.
197: * @return title
198: */
199: public String getTitle() {
200: String title = "";
201: String xpath = basePath;
202: List list = this .selectNodes(xpath);
203: if (list.size() > 0) {
204: Element element = (Element) list.get(0);
205: title = element.getAttribute("title");
206: }
207: return title;
208: }
209:
210: /**
211: * Assessment id (ident attribute)
212: * @return ident
213: */
214: public String getIdent() {
215: String ident = "";
216: String xpath = basePath;
217: List list = this .selectNodes(xpath);
218: if (list.size() > 0) {
219: Element element = (Element) list.get(0);
220: ident = element.getAttribute("ident");
221: }
222: return ident;
223: }
224:
225: /**
226: * Assessment id (ident attribute)
227: * @param ident the ident
228: */
229: public void setIdent(String ident) {
230: String xpath = basePath;
231: List list = this .selectNodes(xpath);
232: if (list.size() > 0) {
233: Element element = (Element) list.get(0);
234: element.setAttribute("ident", ident);
235: }
236: }
237:
238: /**
239: * Assessment title.
240: * @param title
241: */
242: public void setTitle(String title) {
243: String xpath = basePath;
244: List list = this .selectNodes(xpath);
245: if (list.size() > 0) {
246: Element element = (Element) list.get(0);
247: element.setAttribute("title", escapeXml(title));
248: }
249: }
250:
251: /**
252: * Base XPath for the assessment.
253: * @return
254: */
255: public String getBasePath() {
256: return basePath;
257: }
258:
259: /**
260: * Base XPath for the assessment.
261: * @param basePath
262: */
263: public void setBasePath(String basePath) {
264: this.basePath = basePath;
265: }
266: }
|