001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 * $Id: XMLDocumentTreeWriter.java 29029 2008-01-14 18:38:14Z ldoguin $ 013 */ 014 015package org.nuxeo.ecm.core.io.impl.plugins; 016 017import java.io.File; 018import java.io.IOException; 019import java.io.OutputStream; 020import java.io.UnsupportedEncodingException; 021import java.util.Collection; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.dom4j.io.OutputFormat; 026import org.dom4j.io.XMLWriter; 027import org.nuxeo.ecm.core.api.DocumentLocation; 028import org.nuxeo.ecm.core.api.DocumentRef; 029import org.nuxeo.ecm.core.io.DocumentTranslationMap; 030import org.nuxeo.ecm.core.io.ExportedDocument; 031import org.nuxeo.ecm.core.io.impl.AbstractDocumentWriter; 032import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl; 033 034public class XMLDocumentTreeWriter extends XMLDocumentWriter { 035 036 private static final Log log = LogFactory.getLog(XMLDocumentTreeWriter.class); 037 038 private XMLWriter writer; 039 040 public XMLDocumentTreeWriter(File file) throws IOException { 041 super(file); 042 } 043 044 public XMLDocumentTreeWriter(OutputStream out) { 045 super(out); 046 } 047 048 protected XMLWriter initWriter() { 049 if (writer == null) { 050 try { 051 OutputFormat format = AbstractDocumentWriter.createCompactFormat(); 052 format.setSuppressDeclaration(true); 053 writer = new XMLWriter(out, format); 054 } catch (UnsupportedEncodingException e) { 055 // XXX 056 } 057 } 058 059 return writer; 060 } 061 062 @Override 063 public DocumentTranslationMap write(ExportedDocument doc) throws IOException { 064 065 initWriter(); 066 writer.write(doc.getDocument()); 067 // out.write(doc.getDocument().asXML().getBytes()); 068 069 // keep location unchanged 070 DocumentLocation oldLoc = doc.getSourceLocation(); 071 String oldServerName = oldLoc.getServerName(); 072 DocumentRef oldDocRef = oldLoc.getDocRef(); 073 DocumentTranslationMap map = new DocumentTranslationMapImpl(oldServerName, oldServerName); 074 map.put(oldDocRef, oldDocRef); 075 return map; 076 } 077 078 @Override 079 public DocumentTranslationMap write(ExportedDocument[] docs) throws IOException { 080 initWriter(); 081 out.write("<documents>".getBytes()); 082 out.flush(); 083 DocumentTranslationMap map = super.write(docs); 084 writer.flush(); 085 out.write("</documents>".getBytes()); 086 return map; 087 } 088 089 @Override 090 public DocumentTranslationMap write(Collection<ExportedDocument> docs) throws IOException { 091 initWriter(); 092 out.write("<documents>".getBytes()); 093 out.flush(); 094 DocumentTranslationMap map = super.write(docs); 095 writer.flush(); 096 out.write("</documents>".getBytes()); 097 return map; 098 } 099 100 @Override 101 public void close() { 102 if (writer != null) { 103 try { 104 writer.close(); 105 } catch (IOException e) { 106 // XXX 107 } 108 } 109 if (out != null) { 110 try { 111 out.close(); 112 } catch (IOException e) { 113 log.error(e); 114 } 115 } 116 } 117 118}