001/* 002 * (C) Copyright 2006-2011 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 * btatar 018 * 019 * $Id: XMLDirectoryWriter.java 30235 2008-02-18 15:35:09Z fguillaume $ 020 */ 021 022package org.nuxeo.ecm.core.io.impl.plugins; 023 024import java.io.File; 025import java.io.FileOutputStream; 026import java.io.IOException; 027import java.util.Map; 028 029import org.dom4j.Document; 030import org.dom4j.io.OutputFormat; 031import org.dom4j.io.XMLWriter; 032import org.nuxeo.ecm.core.api.Blob; 033import org.nuxeo.ecm.core.io.DocumentTranslationMap; 034import org.nuxeo.ecm.core.io.ExportedDocument; 035import org.nuxeo.ecm.core.io.impl.AbstractDocumentWriter; 036 037/** 038 * This class plays a role in the export pipe.It is used to generate xml files that have a nuxeo specific format.Each 039 * file contains information about a document model such as,general information like name, uid or document type, and 040 * information about the schemas that the document includes. 041 * 042 * @author btatar 043 */ 044public class XMLDirectoryWriter extends AbstractDocumentWriter { 045 046 private File destination; 047 048 /** 049 * Allow to skip the blob from export : useful in case of a Nuxeo 2 Nuxeo migration 050 * 051 * @since 7.4 052 */ 053 protected boolean skipBlobs = false; 054 055 public XMLDirectoryWriter(String destinationPath) { 056 this(new File(destinationPath)); 057 } 058 059 public XMLDirectoryWriter(File destination) { 060 this.destination = destination; 061 } 062 063 /** 064 * @since 7.4 065 */ 066 public boolean skipBlobs() { 067 return skipBlobs; 068 } 069 070 /** 071 * @since 7.4 072 */ 073 public void setSkipBlobs(boolean skipBlobs) { 074 this.skipBlobs = skipBlobs; 075 } 076 077 /** 078 * Gives the destination where the XML file will be generated. 079 */ 080 public Object getDestination() { 081 return destination; 082 } 083 084 /** 085 * Sets the destination where the XML file will be generated. 086 */ 087 public void setDestination(File destination) { 088 this.destination = destination; 089 } 090 091 @Override 092 public void close() { 093 destination = null; 094 } 095 096 @Override 097 public DocumentTranslationMap write(ExportedDocument doc) throws IOException { 098 099 File file = new File(getDestination() + File.separator + doc.getPath().toString()); 100 if (!file.mkdirs()) { 101 throw new IOException("Cannot create target directory: " + file.getAbsolutePath()); 102 } 103 OutputFormat format = AbstractDocumentWriter.createPrettyPrint(); 104 XMLWriter writer = null; 105 try { 106 writer = new XMLWriter(new FileOutputStream(file.getAbsolutePath() + File.separator + "document.xml"), 107 format); 108 writer.write(doc.getDocument()); 109 } finally { 110 if (writer != null) { 111 writer.close(); 112 } 113 } 114 115 if (!skipBlobs) { 116 Map<String, Blob> blobs = doc.getBlobs(); 117 for (Map.Entry<String, Blob> entry : blobs.entrySet()) { 118 String blobPath = file.getAbsolutePath() + File.separator + entry.getKey(); 119 entry.getValue().transferTo(new File(blobPath)); 120 } 121 } 122 123 // write external documents 124 for (Map.Entry<String, Document> entry : doc.getDocuments().entrySet()) { 125 writer = null; 126 try { 127 writer = new XMLWriter(new FileOutputStream(file.getAbsolutePath() + File.separator + entry.getKey() 128 + ".xml"), format); 129 writer.write(entry.getValue()); 130 } finally { 131 if (writer != null) { 132 writer.close(); 133 } 134 } 135 } 136 137 return null; 138 } 139 140}