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