001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/archive/tags/sakai_2-4-1/import-parsers/sakai-archive/src/java/org/sakaiproject/importer/impl/SakaiArchiveFileParser.java $
003: * $Id: SakaiArchiveFileParser.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.FileInputStream;
023: import java.io.InputStream;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.List;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030:
031: import org.sakaiproject.archive.api.ImportMetadata;
032: import org.sakaiproject.archive.cover.ImportMetadataService;
033: import org.sakaiproject.importer.api.ImportDataSource;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: public class SakaiArchiveFileParser extends ZipFileParser {
040:
041: private static final String LEGACY_TOOL = "legacyTool";
042: private static final String SAKAI_TOOL = "sakaiTool";
043: private static final String ROOT = "importConfiguration";
044: private static final String MAPPINGS = "mappings";
045: private static final String MAP = "map";
046: private static final String SERVICE_NAME = "serviceName";
047: private static final String FILE_NAME = "filename";
048: private static final String MANDATORY = "mandatory";
049: private static final String ID = "id";
050:
051: protected Document importMappings;
052:
053: public boolean isValidArchive(byte[] fileData) {
054: if (super .isValidArchive(fileData)) {
055: if (!fileExistsInArchive("/import_mappings.xml", fileData))
056: return false;
057: return true;
058: } else
059: return false;
060: }
061:
062: protected void awakeFromUnzip(String unArchiveLocation) {
063: this .pathToData = unArchiveLocation;
064: String absolutepathToManifest = pathToData + "/"
065: + "import_mappings.xml";
066: absolutepathToManifest = absolutepathToManifest.replace('\\',
067: '/');
068: DocumentBuilder docBuilder;
069: try {
070: docBuilder = DocumentBuilderFactory.newInstance()
071: .newDocumentBuilder();
072: InputStream fis = new FileInputStream(
073: absolutepathToManifest);
074: this .importMappings = (Document) docBuilder.parse(fis);
075: } catch (Exception e) {
076: e.printStackTrace();
077: }
078:
079: }
080:
081: public ImportDataSource parse(byte[] fileData,
082: String unArchiveLocation) {
083: this .localArchiveLocation = unzipArchive(fileData,
084: unArchiveLocation);
085: this .pathToData = unArchiveLocation + "/"
086: + localArchiveLocation;
087: awakeFromUnzip(pathToData);
088: List categories = new ArrayList();
089: Collection items = new ArrayList();
090: categories.addAll(getCategoriesFromArchive(pathToData));
091: items.addAll(getImportableItemsFromArchive(pathToData));
092:
093: SakaiArchiveDataSource dataSource = new SakaiArchiveDataSource(
094: fileData, localArchiveLocation, pathToData);
095: dataSource.setItemCategories(categories);
096: dataSource.setItems(items);
097: return dataSource;
098: }
099:
100: protected Collection getCategoriesFromArchive(String pathToData) {
101: return ImportMetadataService
102: .getImportMetadataElements(importMappings);
103: }
104:
105: protected Collection getImportableItemsFromArchive(String pathToData) {
106: return new ArrayList();
107: }
108:
109: // public List getImportMetadataElements(Document doc)
110: // {
111: // if (doc == null)
112: // {
113: // throw new IllegalArgumentException("Illegal document argument!");
114: // }
115: // else
116: // {
117: // //TODO: Validate the Doc against DTD
118: // Element root = doc.getDocumentElement();
119: // if (root.getTagName().equals(ROOT))
120: // {
121: // NodeList rootNodeList = root.getChildNodes();
122: // final int length = rootNodeList.getLength();
123: // for (int i = 0; i < length; i++)
124: // {
125: // Node mapping = rootNodeList.item(i);
126: // if (mapping.getNodeType() != Node.ELEMENT_NODE)
127: // {
128: // continue;
129: // }
130: // Element mappingElement = (Element) mapping;
131: // if (mappingElement.getTagName().equals(MAPPINGS))
132: // {
133: // List maps = new ArrayList();
134: // NodeList mapNode = mappingElement.getChildNodes();
135: // final int mapLength = mapNode.getLength();
136: // for (int j = 0; j < mapLength; j++)
137: // {
138: // Node mapNodes = mapNode.item(j);
139: // if (mapNodes.getNodeType() != Node.ELEMENT_NODE)
140: // {
141: // continue;
142: // }
143: // Element mapElement = (Element) mapNodes;
144: // if (mapElement.getTagName().equals(MAP))
145: // {
146: // ImportMetadataImpl importMetadataMap = new ImportMetadataImpl();
147: // importMetadataMap.setId(mapElement.getAttribute(ID));
148: // importMetadataMap.setFileName(mapElement
149: // .getAttribute(FILE_NAME));
150: // importMetadataMap.setLegacyTool(mapElement
151: // .getAttribute(LEGACY_TOOL));
152: //
153: // importMetadataMap.setSakaiTool(mapElement
154: // .getAttribute(SAKAI_TOOL));
155: // importMetadataMap.setSakaiServiceName(mapElement
156: // .getAttribute(SERVICE_NAME));
157: // if (mapElement.getAttribute(MANDATORY) != null
158: // && mapElement.getAttribute(MANDATORY).length() > 0
159: // && mapElement.getAttribute(MANDATORY).endsWith("true"))
160: // {
161: // importMetadataMap.setMandatory(true);
162: // }
163: // maps.add(importMetadataMap);
164: // }
165: // }
166: // // import_mapping shall contain only one mapping element, after the
167: // // first one is done return
168: // return maps;
169: // }
170: // }
171: // }
172: // }
173: //
174: // return null;
175: // }
176:
177: // public class ImportMetadataImpl implements ImportMetadata
178: // {
179: //
180: // private String id;
181: // private String legacyTool;
182: // private String sakaiTool;
183: // private String sakaiServiceName;
184: // private String fileName;
185: // private boolean mandatory = false;
186: //
187: // /**
188: // * Should only be constructed by ImportMetadataService.
189: // */
190: // ImportMetadataImpl()
191: // {
192: // }
193: //
194: // /**
195: // * @return Returns the id.
196: // */
197: // public String getId()
198: // {
199: // return id;
200: // }
201: //
202: // /**
203: // * @param id
204: // * The id to set.
205: // */
206: // public void setId(String id)
207: // {
208: // this.id = id;
209: // }
210: //
211: // /**
212: // * @return Returns the fileName.
213: // */
214: // public String getFileName()
215: // {
216: // return fileName;
217: // }
218: //
219: // /**
220: // * @param fileName
221: // * The fileName to set.
222: // */
223: // public void setFileName(String fileName)
224: // {
225: // this.fileName = fileName;
226: // }
227: //
228: // /**
229: // * @return Returns the legacyTool.
230: // */
231: // public String getLegacyTool()
232: // {
233: // return legacyTool;
234: // }
235: //
236: // /**
237: // * @param legacyTool
238: // * The legacyTool to set.
239: // */
240: // public void setLegacyTool(String legacyTool)
241: // {
242: // this.legacyTool = legacyTool;
243: // }
244: //
245: // /**
246: // * @return Returns the mandatory.
247: // */
248: // public boolean isMandatory()
249: // {
250: // return mandatory;
251: // }
252: //
253: // /**
254: // * @param mandatory
255: // * The mandatory to set.
256: // */
257: // public void setMandatory(boolean mandatory)
258: // {
259: // this.mandatory = mandatory;
260: // }
261: //
262: // /**
263: // * @return Returns the sakaiServiceName.
264: // */
265: // public String getSakaiServiceName()
266: // {
267: // return sakaiServiceName;
268: // }
269: //
270: // /**
271: // * @param sakaiServiceName
272: // * The sakaiServiceName to set.
273: // */
274: // public void setSakaiServiceName(String sakaiServiceName)
275: // {
276: // this.sakaiServiceName = sakaiServiceName;
277: // }
278: //
279: // /**
280: // * @return Returns the sakaiTool.
281: // */
282: // public String getSakaiTool()
283: // {
284: // return sakaiTool;
285: // }
286: //
287: // /**
288: // * @param sakaiTool
289: // * The sakaiTool to set.
290: // */
291: // public void setSakaiTool(String sakaiTool)
292: // {
293: // this.sakaiTool = sakaiTool;
294: // }
295: //
296: // }
297: }
|