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