001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (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 - initial API and implementation
016 *
017 * $Id$
018 *
019 */
020package org.nuxeo.ecm.platform.pictures.tiles.serializer;
021
022import org.dom4j.DocumentFactory;
023import org.dom4j.QName;
024import org.nuxeo.ecm.platform.picture.api.ImageInfo;
025import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles;
026
027/**
028 * XML serializer for PictureTiles structure
029 *
030 * @author tiry
031 */
032public class XMLPictureTilesSerializer implements PictureTilesSerializer {
033
034    private static final String picturetilesNS = "http://www.nuxeo.org/picturetiles";
035
036    private static final String picturetilesNSPrefix = "nxpt";
037
038    private static QName rootTag = DocumentFactory.getInstance().createQName("pictureTiles", picturetilesNSPrefix,
039            picturetilesNS);
040
041    protected void dumpImageInfo(ImageInfo info, org.dom4j.Element root) {
042        root.addElement("format").setText(info.getFormat());
043        root.addElement("width").setText(info.getWidth() + "");
044        root.addElement("height").setText(info.getHeight() + "");
045    }
046
047    public String serialize(PictureTiles tiles) {
048        org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootTag);
049        rootElem.addNamespace(picturetilesNSPrefix, picturetilesNS);
050        org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);
051
052        // tile info
053        org.dom4j.Element tileInfo = rootElem.addElement("tileInfo");
054        tileInfo.addElement("zoom").setText(tiles.getZoomfactor() + "");
055        tileInfo.addElement("maxTiles").setText(tiles.getMaxTiles() + "");
056        tileInfo.addElement("tileWidth").setText(tiles.getTilesWidth() + "");
057        tileInfo.addElement("tileHeight").setText(tiles.getTilesHeight() + "");
058        tileInfo.addElement("xTiles").setText(tiles.getXTiles() + "");
059        tileInfo.addElement("yTiles").setText(tiles.getYTiles() + "");
060
061        // original img info
062        org.dom4j.Element originalInfo = rootElem.addElement("originalImage");
063        ImageInfo oInfo = tiles.getOriginalImageInfo();
064        dumpImageInfo(oInfo, originalInfo);
065
066        // source img info
067        org.dom4j.Element srcInfo = rootElem.addElement("srcImage");
068        ImageInfo sInfo = tiles.getSourceImageInfo();
069        dumpImageInfo(sInfo, srcInfo);
070
071        // dump misc info returned by the tiler
072        org.dom4j.Element addInfo = rootElem.addElement("additionalInfo");
073        for (String k : tiles.getInfo().keySet()) {
074            org.dom4j.Element propElem = addInfo.addElement(k);
075            propElem.setText(tiles.getInfo().get(k));
076        }
077
078        // debug info
079        org.dom4j.Element debugInfo = rootElem.addElement("debug");
080        debugInfo.addElement("cacheKey").setText(tiles.getCacheKey());
081        debugInfo.addElement("formatKey").setText(tiles.getTileFormatCacheKey());
082        debugInfo.addElement("tilePath").setText(tiles.getTilesPath());
083
084        return rootDoc.asXML();
085    }
086
087}