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.DocumentLocation; 026import org.nuxeo.ecm.core.api.DocumentRef; 027import org.nuxeo.ecm.core.api.IdRef; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.api.PathRef; 030import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl; 031import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.DocumentLocationMarshaler; 032 033/** 034 * {@link DocumentLocation} marshaler using simple XML representation. 035 * 036 * @author tiry 037 */ 038public class DefaultDocumentLocationMarshaler extends AbstractDefaultXMLMarshaler implements DocumentLocationMarshaler { 039 040 protected String sourceServer; 041 042 protected static QName rootTag = DocumentFactory.getInstance().createQName("documentLocation", 043 publisherSerializerNSPrefix, publisherSerializerNS); 044 045 public String marshalDocumentLocation(DocumentLocation docLoc) { 046 org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootTag); 047 rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS); 048 org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem); 049 050 rootElem.addAttribute("repository", docLoc.getServerName()); 051 rootElem.addAttribute("ref", docLoc.getDocRef().toString()); 052 053 if (sourceServer != null) { 054 rootElem.addAttribute("originalServer", sourceServer); 055 } 056 057 String data = rootDoc.asXML(); 058 059 return cleanUpXml(data); 060 } 061 062 public DocumentLocation unMarshalDocumentLocation(String data) { 063 064 DocumentLocation docLoc; 065 try { 066 Document document = DocumentHelper.parseText(data); 067 org.dom4j.Element rootElem = document.getRootElement(); 068 069 String repoName = rootElem.attribute("repository").getValue(); 070 String refStr = rootElem.attribute("ref").getValue(); 071 DocumentRef ref = null; 072 if (refStr.startsWith("/")) { 073 ref = new PathRef(refStr); 074 } else { 075 ref = new IdRef(refStr); 076 } 077 078 if (rootElem.attributeValue("originalServer") != null) { 079 docLoc = new ExtendedDocumentLocation(rootElem.attributeValue("originalServer"), repoName, ref); 080 } else { 081 docLoc = new DocumentLocationImpl(repoName, ref); 082 } 083 084 } catch (DocumentException e) { 085 throw new NuxeoException("Unable to unmarshal Publication Node", e); 086 } 087 return docLoc; 088 } 089 090 public void setOriginatingServer(String serverName) { 091 this.sourceServer = serverName; 092 } 093 094}