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 */ 044// XXX AT: is this stil useful? 045public class XMLDirectoryWriter extends AbstractDocumentWriter { 046 047 private File destination; 048 049 public XMLDirectoryWriter(String destinationPath) { 050 this(new File(destinationPath)); 051 } 052 053 public XMLDirectoryWriter(File destination) { 054 this.destination = destination; 055 } 056 057 /** 058 * Gives the destination where the XML file will be generated. 059 */ 060 public Object getDestination() { 061 return destination; 062 } 063 064 /** 065 * Sets the destination where the XML file will be generated. 066 */ 067 public void setDestination(File destination) { 068 this.destination = destination; 069 } 070 071 @Override 072 public void close() { 073 destination = null; 074 } 075 076 @Override 077 public DocumentTranslationMap write(ExportedDocument doc) throws IOException { 078 079 File file = new File(getDestination() + File.separator + doc.getPath().toString()); 080 if (!file.mkdirs()) { 081 throw new IOException("Cannot create target directory: " + file.getAbsolutePath()); 082 } 083 OutputFormat format = AbstractDocumentWriter.createPrettyPrint(); 084 XMLWriter writer = null; 085 try { 086 writer = new XMLWriter(new FileOutputStream(file.getAbsolutePath() + File.separator + "document.xml"), 087 format); 088 writer.write(doc.getDocument()); 089 } finally { 090 if (writer != null) { 091 writer.close(); 092 } 093 } 094 Map<String, Blob> blobs = doc.getBlobs(); 095 for (Map.Entry<String, Blob> entry : blobs.entrySet()) { 096 String blobPath = file.getAbsolutePath() + File.separator + entry.getKey(); 097 entry.getValue().transferTo(new File(blobPath)); 098 } 099 100 // write external documents 101 for (Map.Entry<String, Document> entry : doc.getDocuments().entrySet()) { 102 writer = null; 103 try { 104 writer = new XMLWriter(new FileOutputStream(file.getAbsolutePath() + File.separator + entry.getKey() 105 + ".xml"), format); 106 writer.write(entry.getValue()); 107 } finally { 108 if (writer != null) { 109 writer.close(); 110 } 111 } 112 } 113 114 return null; 115 } 116 117}