<commit_before>/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2006, 2007, 2008, 2009 The Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.importer.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.sakaiproject.archive.api.ImportMetadata;
import org.sakaiproject.importer.api.ImportDataSource;
import org.sakaiproject.importer.api.ImportFileParser;
import org.sakaiproject.importer.api.Importable;
import org.sakaiproject.importer.impl.importables.FileResource;
import org.sakaiproject.importer.impl.importables.Folder;
import org.sakaiproject.importer.impl.importables.WebLink;
import org.sakaiproject.importer.impl.translators.CCAssessmentTranslator;
import org.sakaiproject.importer.impl.translators.CCDiscussionTopicTranslator;
import org.sakaiproject.importer.impl.translators.CCLearningApplicationResourceTranslator;
import org.sakaiproject.importer.impl.translators.CCWebContentTranslator;
import org.sakaiproject.importer.impl.translators.CCWebLinkTranslator;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class CommonCartridgeFileParser extends IMSFileParser {
	private static final String CC_SCHEMA_NAME = "IMS Common Cartridge";
	
	public CommonCartridgeFileParser() {
		// add resource translators here
		addResourceTranslator(new CCAssessmentTranslator());
		addResourceTranslator(new CCWebLinkTranslator());
		addResourceTranslator(new CCWebContentTranslator());
		addResourceTranslator(new CCDiscussionTopicTranslator());
		addResourceTranslator(new CCLearningApplicationResourceTranslator());
		resourceHelper = new CCResourceHelper();
		itemHelper = new CCItemHelper();
		fileHelper = new CCFileHelper();
		manifestHelper = new CCManifestHelper();
	}

	public boolean isValidArchive(byte[] fileData) {
		if (super.isValidArchive(fileData)) {
			Document manifest = extractFileAsDOM("/imsmanifest.xml", fileData);
			return CC_SCHEMA_NAME.equals(XPathHelper.getNodeValue("/manifest/metadata/schema", manifest));
		} else return false;
	}
	
	public ImportFileParser newParser() {
		return new CommonCartridgeFileParser();
	}

	protected Collection getCategoriesFromArchive(String pathToData) {
		Collection categories = new ArrayList();
		ImportMetadata im;
//		if(XPathHelper.getNodeValue("//resource[@type='webcontent']", this.archiveManifest) != null) {
//			im = new BasicImportMetadata();
//			im.setId("webcontent");
//			im.setLegacyTool("Web Content");
//			im.setMandatory(false);
//			im.setFileName(".xml");
//			im.setSakaiServiceName("ContentHostingService");
//			im.setSakaiTool("Resources");
//			categories.add(im);
//		}<commit_msg>SAK-18634 Regression: IMS-CC Packages can no longer be imported (patch from <NAME>)<commit_after>/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2006, 2007, 2008, 2009 The Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.importer.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.sakaiproject.archive.api.ImportMetadata;
import org.sakaiproject.importer.api.ImportFileParser;
import org.sakaiproject.importer.api.Importable;
import org.sakaiproject.importer.impl.importables.Folder;
import org.sakaiproject.importer.impl.translators.CCAssessmentTranslator;
import org.sakaiproject.importer.impl.translators.CCDiscussionTopicTranslator;
import org.sakaiproject.importer.impl.translators.CCLearningApplicationResourceTranslator;
import org.sakaiproject.importer.impl.translators.CCWebContentTranslator;
import org.sakaiproject.importer.impl.translators.CCWebLinkTranslator;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class CommonCartridgeFileParser extends IMSFileParser {
	private static final String CC_NAMESPACE_URI = "http://www.imsglobal.org/xsd/imscc/imscp_v1p1";
	
	public CommonCartridgeFileParser() {
		// add resource translators here
		addResourceTranslator(new CCAssessmentTranslator());
		addResourceTranslator(new CCWebLinkTranslator());
		addResourceTranslator(new CCWebContentTranslator());
		addResourceTranslator(new CCDiscussionTopicTranslator());
		addResourceTranslator(new CCLearningApplicationResourceTranslator());
		resourceHelper = new CCResourceHelper();
		itemHelper = new CCItemHelper();
		fileHelper = new CCFileHelper();
		manifestHelper = new CCManifestHelper();
	}

	public boolean isValidArchive(byte[] fileData) {
		if (super.isValidArchive(fileData)) {
			Document manifest = extractFileAsDOM("/imsmanifest.xml", fileData);
			return (enclosingDocumentContainsNamespaceDeclaration(manifest, CC_NAMESPACE_URI));
		} else return false;
	}
	
	private boolean enclosingDocumentContainsNamespaceDeclaration(Node node, String nameSpaceURI) {
		return node.isDefaultNamespace(CC_NAMESPACE_URI);
	}

	public ImportFileParser newParser() {
		return new CommonCartridgeFileParser();
	}

	protected Collection getCategoriesFromArchive(String pathToData) {
		Collection categories = new ArrayList();
		ImportMetadata im;
//		if(XPathHelper.getNodeValue("//resource[@type='webcontent']", this.archiveManifest) != null) {
//			im = new BasicImportMetadata();
//			im.setId("webcontent");
//			im.setLegacyTool("Web Content");
//			im.setMandatory(false);
//			im.setFileName(".xml");
//			im.setSakaiServiceName("ContentHostingService");
//			im.setSakaiTool("Resources");
//			categories.add(im);
//		}
{'fraction_non_alphanumeric': 0.15861104044772323, 'fraction_numerical': 0.006486899007886034, 'mean_word_length': 6.109403254972875, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822202', 'n_tokens_mistral': 2377, 'n_tokens_neox': 2206, 'n_words': 493}