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.NuxeoException;
026import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
027import org.nuxeo.ecm.platform.publisher.remoting.marshaling.basic.BasicPublicationNode;
028import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.PublicationNodeMarshaler;
029
030/**
031 * {@link PublicationNode} marshaler using simple XML representation.
032 *
033 * @author tiry
034 */
035public class DefaultPublicationNodeMarshaler extends AbstractDefaultXMLMarshaler implements PublicationNodeMarshaler {
036
037    protected static QName rootTag = DocumentFactory.getInstance().createQName("publicationNode",
038            publisherSerializerNSPrefix, publisherSerializerNS);
039
040    protected static QName nodePathTag = DocumentFactory.getInstance().createQName("nodePath",
041            publisherSerializerNSPrefix, publisherSerializerNS);
042
043    protected static QName nodeTitleTag = DocumentFactory.getInstance().createQName("nodeTile",
044            publisherSerializerNSPrefix, publisherSerializerNS);
045
046    protected static QName nodeTypeTag = DocumentFactory.getInstance().createQName("nodeType",
047            publisherSerializerNSPrefix, publisherSerializerNS);
048
049    protected static QName treeNameTag = DocumentFactory.getInstance().createQName("treeName",
050            publisherSerializerNSPrefix, publisherSerializerNS);
051
052    protected static QName sidTag = DocumentFactory.getInstance().createQName("sid", publisherSerializerNSPrefix,
053            publisherSerializerNS);
054
055    public String marshalPublicationNode(PublicationNode node) {
056
057        org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootTag);
058        rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS);
059        org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);
060
061        org.dom4j.Element pathElem = rootElem.addElement(nodePathTag);
062        pathElem.setText(node.getPath());
063
064        org.dom4j.Element titleElem = rootElem.addElement(nodeTitleTag);
065        titleElem.setText(node.getTitle());
066
067        org.dom4j.Element typeElem = rootElem.addElement(nodeTypeTag);
068        typeElem.setText(node.getNodeType());
069
070        org.dom4j.Element treeElem = rootElem.addElement(treeNameTag);
071        treeElem.setText(node.getTreeConfigName());
072
073        org.dom4j.Element sidElem = rootElem.addElement(sidTag);
074        if (node.getSessionId() != null) {
075            sidElem.setText(node.getSessionId());
076        } else {
077            sidElem.setText("");
078        }
079
080        String data = rootDoc.asXML();
081
082        return cleanUpXml(data);
083    }
084
085    public PublicationNode unMarshalPublicationNode(String data) {
086        PublicationNode node = null;
087
088        try {
089            Document document = DocumentHelper.parseText(data);
090            org.dom4j.Element rootElem = document.getRootElement();
091
092            String nodePath = rootElem.element(nodePathTag).getTextTrim();
093            String nodeTitle = rootElem.element(nodeTitleTag).getTextTrim();
094            String nodeType = rootElem.element(nodeTypeTag).getTextTrim();
095            String treeName = rootElem.element(treeNameTag).getTextTrim();
096            String sid = rootElem.element(sidTag).getTextTrim();
097            node = new BasicPublicationNode(nodeType, nodePath, nodeTitle, treeName, sid);
098
099        } catch (DocumentException e) {
100            throw new NuxeoException("Unable to unmarshal Piublication Node", e);
101        }
102        return node;
103    }
104
105}