001/*
002 *  Copyright 2014 Anyware Services
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 */
016package org.ametys.plugins.flipbook;
017
018import java.awt.image.BufferedImage;
019import java.io.File;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025
026import javax.imageio.ImageIO;
027
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.ProcessingException;
031import org.apache.cocoon.environment.ObjectModelHelper;
032import org.apache.cocoon.environment.Request;
033import org.apache.cocoon.generation.ServiceableGenerator;
034import org.apache.cocoon.xml.AttributesImpl;
035import org.apache.cocoon.xml.XMLUtils;
036import org.apache.commons.io.FileUtils;
037import org.apache.commons.lang.StringUtils;
038import org.xml.sax.SAXException;
039
040import org.ametys.core.util.FilenameUtils;
041import org.ametys.core.util.URIUtils;
042import org.ametys.plugins.repository.AmetysObjectResolver;
043
044/**
045 * Generates information on the generated images.
046 */
047public class ImagesGenerator extends ServiceableGenerator
048{
049    /** Request attribute from path to images' directory */
050    public static final String IMAGES_DIRECTORY_PATH_REQUEST_ATTR = "images-base-directory";
051    
052    /** The ametys object resolver. */
053    protected AmetysObjectResolver _resolver;
054    
055    @Override
056    public void service(ServiceManager serviceManager) throws ServiceException
057    {
058        super.service(serviceManager);
059        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
060    }
061    
062    @Override
063    public void generate() throws IOException, SAXException, ProcessingException
064    {
065        Request request = ObjectModelHelper.getRequest(objectModel);
066        String basePath = (String) request.getAttribute(IMAGES_DIRECTORY_PATH_REQUEST_ATTR);
067        
068        // Search the document file.
069        File documentFolder = new File(basePath, "/document");
070        Iterator<File> documentFiles = FileUtils.listFiles(documentFolder, new String[] {"document"}, false).iterator();
071        File documentFile = documentFiles.hasNext() ? documentFiles.next() : null;
072        
073        // List the image files.
074        File imageFolder = new File(basePath + "/pages");
075        
076        List<File> imageFiles = new ArrayList<>(FileUtils.listFiles(imageFolder, new String[] {"png"}, false));
077        Collections.sort(imageFiles);
078        
079        // Compute the width and height of the images.
080        int width = -1;
081        int height = -1;
082        if (!imageFiles.isEmpty())
083        {
084            File file = imageFiles.get(0);
085            BufferedImage image = ImageIO.read(file);
086            width = image.getWidth();
087            height = image.getHeight();
088        }
089        
090        contentHandler.startDocument();
091        
092        String relPath = URIUtils.encodePath(StringUtils.substringAfter(basePath, "flipbook"));
093        AttributesImpl attrs = new AttributesImpl();
094        attrs.addCDATAAttribute("basePath", relPath);
095        attrs.addCDATAAttribute("pagePath", relPath + "/pages");
096        attrs.addCDATAAttribute("width", Integer.toString(width));
097        attrs.addCDATAAttribute("height", Integer.toString(height));
098        if (documentFile != null)
099        {
100            attrs.addCDATAAttribute("documentPath", relPath + "/document");
101            attrs.addCDATAAttribute("documentName", documentFile.getName());
102            attrs.addCDATAAttribute("documentSize", Long.toString(documentFile.length()));
103        }
104        
105        attrs.addCDATAAttribute("nb-page", String.valueOf(imageFiles.size()));
106        XMLUtils.startElement(contentHandler, "images", attrs);
107        for (File file : imageFiles)
108        {
109            AttributesImpl pageAttrs = new AttributesImpl();
110            pageAttrs.addCDATAAttribute("fileName", file.getName());
111            
112            XMLUtils.createElement(contentHandler, "image", pageAttrs);
113        }
114        
115        File previewFile = new File(imageFolder, "preview.jpg");
116        if (previewFile.exists())
117        {
118            BufferedImage bi = ImageIO.read(previewFile);
119            
120            AttributesImpl previewAttrs = new AttributesImpl();
121            previewAttrs.addCDATAAttribute("name", previewFile.getName());
122            previewAttrs.addCDATAAttribute("width", String.valueOf(bi.getWidth()));
123            
124            int rows = (int) Math.ceil(1 + imageFiles.size() / 2);
125            previewAttrs.addCDATAAttribute("height", String.valueOf(bi.getHeight() / rows));
126            XMLUtils.createElement(contentHandler, "preview", previewAttrs);
127        }
128        
129        XMLUtils.endElement(contentHandler, "images");
130        
131        contentHandler.endDocument();
132    }
133}