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.remoting.marshaling; 019 020import org.dom4j.Document; 021import org.dom4j.DocumentException; 022import org.dom4j.DocumentFactory; 023import org.dom4j.DocumentHelper; 024import org.dom4j.QName; 025import org.nuxeo.ecm.core.api.DocumentRef; 026import org.nuxeo.ecm.core.api.IdRef; 027import org.nuxeo.ecm.core.api.NuxeoException; 028import org.nuxeo.ecm.core.api.PathRef; 029import org.nuxeo.ecm.platform.publisher.api.PublishedDocument; 030import org.nuxeo.ecm.platform.publisher.remoting.marshaling.basic.BasicPublishedDocument; 031import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.PublishedDocumentMarshaler; 032 033/** 034 * {@link PublishedDocument} marshaler using simple XML representation. 035 * 036 * @author tiry 037 */ 038public class DefaultPublishedDocumentMarshaler extends AbstractDefaultXMLMarshaler implements 039 PublishedDocumentMarshaler { 040 041 protected static QName rootTag = DocumentFactory.getInstance().createQName("publishedDocument", 042 publisherSerializerNSPrefix, publisherSerializerNS); 043 044 protected static QName sourceRefTag = DocumentFactory.getInstance().createQName("sourceRef", 045 publisherSerializerNSPrefix, publisherSerializerNS); 046 047 protected static QName repositoryNameTag = DocumentFactory.getInstance().createQName("repositoryName", 048 publisherSerializerNSPrefix, publisherSerializerNS); 049 050 protected static QName serverNameTag = DocumentFactory.getInstance().createQName("serverName", 051 publisherSerializerNSPrefix, publisherSerializerNS); 052 053 protected static QName versionLabelTag = DocumentFactory.getInstance().createQName("versionLabel", 054 publisherSerializerNSPrefix, publisherSerializerNS); 055 056 protected static QName pathTag = DocumentFactory.getInstance().createQName("path", publisherSerializerNSPrefix, 057 publisherSerializerNS); 058 059 protected static QName parentPathTag = DocumentFactory.getInstance().createQName("parentPath", 060 publisherSerializerNSPrefix, publisherSerializerNS); 061 062 protected static QName isPendingTag = DocumentFactory.getInstance().createQName("isPending", 063 publisherSerializerNSPrefix, publisherSerializerNS); 064 065 public String marshalPublishedDocument(PublishedDocument pubDoc) { 066 if (pubDoc == null) 067 return ""; 068 069 org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootTag); 070 rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS); 071 org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem); 072 073 org.dom4j.Element sourceElem = rootElem.addElement(sourceRefTag); 074 sourceElem.setText(pubDoc.getSourceDocumentRef().toString()); 075 076 org.dom4j.Element repoElem = rootElem.addElement(repositoryNameTag); 077 repoElem.setText(pubDoc.getSourceRepositoryName()); 078 079 org.dom4j.Element srvElem = rootElem.addElement(serverNameTag); 080 srvElem.setText(pubDoc.getSourceServer()); 081 082 org.dom4j.Element versionElem = rootElem.addElement(versionLabelTag); 083 versionElem.setText("" + pubDoc.getSourceVersionLabel()); 084 085 org.dom4j.Element pathElem = rootElem.addElement(pathTag); 086 pathElem.setText("" + pubDoc.getPath()); 087 088 org.dom4j.Element parentPathElem = rootElem.addElement(parentPathTag); 089 parentPathElem.setText("" + pubDoc.getParentPath()); 090 091 org.dom4j.Element isPendingElem = rootElem.addElement(isPendingTag); 092 isPendingElem.setText("" + pubDoc.isPending()); 093 094 String data = rootDoc.asXML(); 095 096 return cleanUpXml(data); 097 098 } 099 100 public PublishedDocument unMarshalPublishedDocument(String data) { 101 102 PublishedDocument pubDoc; 103 try { 104 Document document = DocumentHelper.parseText(data); 105 org.dom4j.Element rootElem = document.getRootElement(); 106 107 String strDocRef = rootElem.element(sourceRefTag).getTextTrim(); 108 DocumentRef docRef; 109 if (strDocRef.startsWith("/")) 110 docRef = new PathRef(strDocRef); 111 else 112 docRef = new IdRef(strDocRef); 113 114 String repo = rootElem.element(repositoryNameTag).getTextTrim(); 115 String server = rootElem.element(serverNameTag).getTextTrim(); 116 String version = rootElem.element(versionLabelTag).getTextTrim(); 117 String path = rootElem.element(pathTag).getTextTrim(); 118 String parentPath = rootElem.element(parentPathTag).getTextTrim(); 119 boolean isPending = Boolean.parseBoolean(rootElem.element(isPendingTag).getTextTrim()); 120 121 pubDoc = new BasicPublishedDocument(docRef, repo, server, version, path, parentPath, isPending); 122 } catch (DocumentException e) { 123 throw new NuxeoException("Unable to unmarshal Published Document", e); 124 } 125 return pubDoc; 126 } 127 128}