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