001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo 016 */ 017 018package org.nuxeo.ecm.platform.publisher.impl.localfs; 019 020import java.io.File; 021import java.io.IOException; 022import java.io.StringReader; 023 024import org.dom4j.Document; 025import org.dom4j.DocumentException; 026import org.dom4j.DocumentFactory; 027import org.dom4j.Element; 028import org.dom4j.Namespace; 029import org.dom4j.QName; 030import org.dom4j.io.SAXReader; 031import org.nuxeo.common.utils.FileUtils; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentRef; 034import org.nuxeo.ecm.core.api.IdRef; 035import org.nuxeo.ecm.platform.publisher.api.PublishedDocument; 036import org.nuxeo.ecm.platform.publisher.helper.VersioningHelper; 037import org.nuxeo.ecm.platform.publisher.remoting.marshaling.CoreIODocumentModelMarshaler; 038 039public class FSPublishedDocument implements PublishedDocument { 040 041 private static final long serialVersionUID = 1L; 042 043 public static final Namespace nxfspub = new Namespace("nxfspub", "http://www.nuxeo.org/publisher/filesystem"); 044 045 public static final QName pubInfoQN = DocumentFactory.getInstance().createQName("publicationInfo", nxfspub); 046 047 public static final QName sourceDocRefQN = DocumentFactory.getInstance().createQName("sourceDocumentRef", nxfspub); 048 049 public static final QName sourceRepositoryNameQN = DocumentFactory.getInstance().createQName( 050 "sourceRepositoryName", nxfspub); 051 052 public static final QName sourceServerQN = DocumentFactory.getInstance().createQName("sourceServer", nxfspub); 053 054 public static final QName sourceVersionQN = DocumentFactory.getInstance().createQName("sourceVersion", nxfspub); 055 056 public static final QName isPendingQN = DocumentFactory.getInstance().createQName("isPending", nxfspub); 057 058 protected DocumentRef sourceDocumentRef; 059 060 protected String sourceRepositoryName; 061 062 protected String sourceServer; 063 064 protected String sourceVersion; 065 066 protected String persistPath; 067 068 protected String parentPath; 069 070 protected boolean isPending; 071 072 protected String xmlRepresentation; 073 074 public FSPublishedDocument(File file) throws NotFSPublishedDocumentException { 075 parseXML(file); 076 persistPath = file.getAbsolutePath(); 077 parentPath = file.getParent(); 078 } 079 080 protected void parseXML(File file) throws NotFSPublishedDocumentException { 081 SAXReader xmlReader = new SAXReader(); 082 try { 083 Document doc = xmlReader.read(file); 084 Element info = doc.getRootElement().element(pubInfoQN); 085 if (info == null) { 086 // valid xml file, but not a published document 087 throw new NotFSPublishedDocumentException(); 088 } 089 sourceDocumentRef = new IdRef(info.element(sourceDocRefQN).getTextTrim()); 090 sourceRepositoryName = info.element(sourceRepositoryNameQN).getTextTrim(); 091 sourceServer = info.element(sourceServerQN).getTextTrim(); 092 sourceVersion = info.element(sourceVersionQN).getTextTrim(); 093 isPending = Boolean.parseBoolean(info.element(isPendingQN).getTextTrim()); 094 } catch (DocumentException e) { 095 throw new NotFSPublishedDocumentException(e); 096 } 097 } 098 099 public void persist(String containerPath) throws IOException { 100 File output = new File(containerPath, sourceDocumentRef.toString()); 101 FileUtils.writeFile(output, xmlRepresentation); 102 persistPath = output.getAbsolutePath(); 103 } 104 105 public FSPublishedDocument(String server, DocumentModel doc) throws DocumentException { 106 this(server, doc, false); 107 } 108 109 public FSPublishedDocument(String server, DocumentModel doc, boolean isPending) throws DocumentException { 110 111 sourceRepositoryName = doc.getRepositoryName(); 112 sourceDocumentRef = doc.getRef(); 113 sourceVersion = VersioningHelper.getVersionLabelFor(doc); 114 sourceServer = server; 115 this.isPending = isPending; 116 117 CoreIODocumentModelMarshaler marshaler = new CoreIODocumentModelMarshaler(); 118 String xmlDoc = marshaler.marshalDocument(doc); 119 120 SAXReader xmlReader = new SAXReader(); 121 Document xml = xmlReader.read(new StringReader(xmlDoc)); 122 123 xml.getRootElement().add(nxfspub); 124 Element info = xml.getRootElement().addElement(pubInfoQN); 125 126 info.addElement(sourceDocRefQN).setText(sourceDocumentRef.toString()); 127 info.addElement(sourceRepositoryNameQN).setText(sourceRepositoryName); 128 if (sourceServer != null) { 129 info.addElement(sourceServerQN).setText(sourceServer); 130 } else { 131 info.addElement(sourceServerQN); 132 } 133 if (sourceVersion != null) { 134 info.addElement(sourceVersionQN).setText(sourceVersion); 135 } else { 136 info.addElement(sourceVersionQN); 137 } 138 info.addElement(isPendingQN).setText(String.valueOf(isPending)); 139 xmlRepresentation = xml.asXML(); 140 } 141 142 @Override 143 public DocumentRef getSourceDocumentRef() { 144 return sourceDocumentRef; 145 } 146 147 @Override 148 public String getSourceRepositoryName() { 149 return sourceRepositoryName; 150 } 151 152 @Override 153 public String getSourceServer() { 154 return sourceServer; 155 } 156 157 @Override 158 public String getSourceVersionLabel() { 159 return sourceVersion; 160 } 161 162 public String getPersistPath() { 163 return persistPath; 164 } 165 166 @Override 167 public String getPath() { 168 return getPersistPath(); 169 } 170 171 @Override 172 public String getParentPath() { 173 return parentPath; 174 } 175 176 @Override 177 public boolean isPending() { 178 return isPending; 179 } 180 181 @Override 182 public Type getType() { 183 return Type.FILE_SYSTEM; 184 } 185 186}