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