001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/archive/tags/sakai_2-4-1/import-parsers/common-cartridge/src/java/org/sakaiproject/importer/impl/CommonCartridgeFileParser.java $
003: * $Id: CommonCartridgeFileParser.java 17726 2006-11-01 15:39:28Z lance@indiana.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.importer.impl;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.io.FileOutputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.TreeSet;
037: import java.util.zip.ZipEntry;
038: import java.util.zip.ZipInputStream;
039:
040: import javax.xml.parsers.DocumentBuilder;
041: import javax.xml.parsers.DocumentBuilderFactory;
042: import javax.xml.parsers.ParserConfigurationException;
043:
044: import org.sakaiproject.archive.api.ImportMetadata;
045: import org.sakaiproject.importer.api.ImportDataSource;
046: import org.sakaiproject.importer.api.ImportFileParser;
047: import org.sakaiproject.importer.impl.importables.FileResource;
048: import org.sakaiproject.importer.impl.importables.Folder;
049: import org.sakaiproject.importer.impl.importables.WebLink;
050: import org.sakaiproject.importer.impl.translators.CCAssessmentTranslator;
051: import org.sakaiproject.importer.impl.translators.CCWebContentTranslator;
052: import org.sakaiproject.importer.impl.translators.CCWebLinkTranslator;
053: import org.w3c.dom.Document;
054: import org.w3c.dom.Node;
055: import org.xml.sax.SAXException;
056:
057: public class CommonCartridgeFileParser extends IMSFileParser {
058: private static final String CC_SCHEMA_NAME = "IMS Common Cartridge";
059:
060: public CommonCartridgeFileParser() {
061: // add resource translators here
062: addResourceTranslator(new CCAssessmentTranslator());
063: addResourceTranslator(new CCWebLinkTranslator());
064: // addResourceTranslator(new CCWebContentTranslator());
065: resourceHelper = new CCResourceHelper();
066: itemHelper = new CCItemHelper();
067: fileHelper = new CCFileHelper();
068: manifestHelper = new CCManifestHelper();
069: }
070:
071: public boolean isValidArchive(byte[] fileData) {
072: if (super .isValidArchive(fileData)) {
073: Document manifest = extractFileAsDOM("/imsmanifest.xml",
074: fileData);
075: return CC_SCHEMA_NAME.equals(XPathHelper.getNodeValue(
076: "/manifest/metadata/schema", manifest));
077: } else
078: return false;
079: }
080:
081: protected Collection getCategoriesFromArchive(String pathToData) {
082: Collection categories = new ArrayList();
083: ImportMetadata im;
084: // if(XPathHelper.getNodeValue("//resource[@type='webcontent']", this.archiveManifest) != null) {
085: // im = new BasicImportMetadata();
086: // im.setId("webcontent");
087: // im.setLegacyTool("Web Content");
088: // im.setMandatory(false);
089: // im.setFileName(".xml");
090: // im.setSakaiServiceName("ContentHostingService");
091: // im.setSakaiTool("Resources");
092: // categories.add(im);
093: // }
094: // if(XPathHelper.getNodeValue("//resource[@type='imsqti_xmlv1p2/imscc_xmlv1p0/assessment']", this.archiveManifest) != null) {
095: // im = new BasicImportMetadata();
096: // im.setId("assessments");
097: // im.setLegacyTool("Assessments");
098: // im.setMandatory(false);
099: // im.setFileName(".xml");
100: // im.setSakaiServiceName("Samigo");
101: // im.setSakaiTool("Tests and Quizzes");
102: // categories.add(im);
103: // }
104: Node topLevelItem;
105: List topLevelItems = manifestHelper
106: .getTopLevelItemNodes(this .archiveManifest);
107: for (Iterator i = topLevelItems.iterator(); i.hasNext();) {
108: topLevelItem = (Node) i.next();
109: im = new BasicImportMetadata();
110: im.setId(itemHelper.getId(topLevelItem));
111: im.setLegacyTool(itemHelper.getTitle(topLevelItem));
112: im.setMandatory(false);
113: im.setFileName(".xml");
114: im.setSakaiServiceName("ContentHostingService");
115: im.setSakaiTool("Resources");
116: categories.add(im);
117: }
118: return categories;
119: }
120:
121: protected boolean isCompoundDocument(Node node,
122: Document resourceDescriptor) {
123: // TODO Auto-generated method stub
124: return false;
125: }
126:
127: protected class CCResourceHelper extends ResourceHelper {
128: public String getTitle(Node resourceNode) {
129: Document descriptor = getDescriptor(resourceNode);
130: return XPathHelper.getNodeValue("/CONTENT/TITLE",
131: descriptor);
132: }
133:
134: public String getType(Node resourceNode) {
135: return XPathHelper.getNodeValue("./@type", resourceNode);
136: }
137:
138: public String getId(Node resourceNode) {
139: return XPathHelper.getNodeValue("./@identifier",
140: resourceNode);
141: }
142:
143: public Document getDescriptor(Node resourceNode) {
144: String descriptorFilename = XPathHelper.getNodeValue(
145: "./file/@href", resourceNode);
146: DocumentBuilder docBuilder;
147: try {
148: docBuilder = DocumentBuilderFactory.newInstance()
149: .newDocumentBuilder();
150: InputStream fis = new FileInputStream(pathToData + "/"
151: + descriptorFilename);
152: return (Document) docBuilder.parse(fis);
153: } catch (Exception e) {
154: return null;
155: }
156: }
157:
158: public String getDescription(Node resourceNode) {
159: Document descriptor = resourceHelper
160: .getDescriptor(resourceNode);
161: String desc = XPathHelper
162: .getNodeValue("//TEXT", descriptor);
163: return desc;
164: }
165:
166: public boolean isFolder(Document resourceDescriptor) {
167: return "true".equals(XPathHelper.getNodeValue(
168: "/CONTENT/FLAGS/ISFOLDER/@value",
169: resourceDescriptor));
170: }
171: }
172:
173: protected class CCItemHelper extends ItemHelper {
174:
175: public String getId(Node itemNode) {
176: return XPathHelper.getNodeValue("./@identifier", itemNode);
177: }
178:
179: public String getTitle(Node itemNode) {
180: return XPathHelper.getNodeValue("./title", itemNode);
181: }
182:
183: public String getDescription(Node itemNode) {
184: String resourceId = XPathHelper.getNodeValue(
185: "./@identifierref", itemNode);
186: Node resourceNode = manifestHelper.getResourceForId(
187: resourceId, archiveManifest);
188: return resourceHelper.getDescription(resourceNode);
189: }
190:
191: }
192:
193: protected class CCManifestHelper extends ManifestHelper {
194:
195: public List getItemNodes(Document manifest) {
196: return XPathHelper.selectNodes("//item", manifest);
197: }
198:
199: public Node getResourceForId(String resourceId,
200: Document manifest) {
201: return XPathHelper.selectNode("//resource[@identifier='"
202: + resourceId + "']", archiveManifest);
203: }
204:
205: public List getResourceNodes(Document manifest) {
206: return XPathHelper.selectNodes("//resource", manifest);
207: }
208:
209: public List getTopLevelItemNodes(Document manifest) {
210: List items = XPathHelper.selectNodes("//organization/item",
211: manifest);
212: if (items != null && items.size() > 1)
213: return items;
214:
215: items = XPathHelper.selectNodes("//organization/item/item",
216: manifest);
217: if (items != null && items.size() > 1)
218: return items;
219:
220: return XPathHelper.selectNodes(
221: "//organization/item/item/item", manifest);
222: }
223:
224: }
225:
226: protected class CCFileHelper extends FileHelper {
227:
228: public String getFilePathForNode(Node node, String basePath) {
229: return XPathHelper.getNodeValue("./@href", node);
230: }
231: }
232:
233: }
|