001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.template.samples.importer; 022 023import java.io.BufferedInputStream; 024import java.io.File; 025import java.io.FileInputStream; 026import java.io.IOException; 027 028import org.dom4j.Document; 029import org.dom4j.DocumentException; 030import org.dom4j.io.SAXReader; 031import org.nuxeo.common.utils.Path; 032import org.nuxeo.ecm.core.api.Blobs; 033import org.nuxeo.ecm.core.io.ExportConstants; 034import org.nuxeo.ecm.core.io.ExportedDocument; 035import org.nuxeo.ecm.core.io.impl.AbstractDocumentReader; 036import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl; 037 038/** 039 * CoreIO reader used to read a exploded XML archive. 040 * <p> 041 * This format is used here to make changes in the models easier 042 * 043 * @author <a href="mailto:[email protected]">Tiry</a> 044 */ 045public class XMLModelReader extends AbstractDocumentReader { 046 047 protected File source; 048 049 protected boolean done; 050 051 protected String modelName; 052 053 public XMLModelReader(File source, String modelName) { 054 this.source = source; 055 this.done = false; 056 this.modelName = modelName; 057 } 058 059 @Override 060 public void close() { 061 source = null; 062 } 063 064 @Override 065 public ExportedDocument read() throws IOException { 066 if (done) { 067 return null; 068 } 069 ExportedDocument xdoc = new ExportedDocumentImpl(); 070 for (File file : source.listFiles()) { 071 if (file.isFile()) { 072 String name = file.getName(); 073 if (ExportConstants.DOCUMENT_FILE.equals(name)) { 074 Document doc = loadXML(file); 075 xdoc.setDocument(doc); 076 xdoc.setPath(new Path(modelName)); 077 } else { // presume a blob 078 xdoc.putBlob(file.getName(), Blobs.createBlob(file)); 079 } 080 } 081 } 082 done = true; 083 return xdoc; 084 } 085 086 private static Document loadXML(File file) throws IOException { 087 BufferedInputStream in = null; 088 try { 089 in = new BufferedInputStream(new FileInputStream(file)); 090 return new SAXReader().read(in); 091 } catch (DocumentException e) { 092 IOException ioe = new IOException("Failed to read file document " + file + ": " + e.getMessage()); 093 ioe.setStackTrace(e.getStackTrace()); 094 throw ioe; 095 } finally { 096 if (in != null) { 097 in.close(); 098 } 099 } 100 } 101 102}