001/*
002 *  Copyright 2011 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.webcontentio.docx;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
028import org.apache.commons.compress.archivers.zip.ZipFile;
029import org.apache.excalibur.xml.dom.DOMSerializer;
030import org.apache.excalibur.xml.sax.SAXParser;
031import org.w3c.dom.Document;
032import org.xml.sax.InputSource;
033import org.xml.sax.SAXException;
034import org.xml.sax.helpers.AttributesImpl;
035
036import org.ametys.core.util.IgnoreRootHandler;
037
038/**
039 * Aggregate all relevants XML parts of a .docx file.
040 */
041public class DocxGenerator extends ServiceableGenerator
042{
043    private DOMSerializer _domSerializer;
044    private SAXParser _saxParser;
045    
046    @Override
047    public void service(ServiceManager sManager) throws ServiceException
048    {
049        super.service(sManager);
050        _domSerializer = (DOMSerializer) sManager.lookup(DOMSerializer.ROLE);
051        _saxParser = (SAXParser) sManager.lookup(SAXParser.ROLE);
052    }
053    
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        @SuppressWarnings("unchecked")
058        Map<String, Object> context = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
059        
060        Document document = (Document) context.get("document");
061        Document relations = (Document) context.get("relations");
062        
063        ZipFile zipFile = (ZipFile) context.get("zipFile");
064
065        contentHandler.startDocument();
066        contentHandler.startElement("", "document", "document", new AttributesImpl());
067        _domSerializer.serialize(document, new IgnoreRootHandler(contentHandler), lexicalHandler);
068        _domSerializer.serialize(relations, new IgnoreRootHandler(contentHandler), lexicalHandler);
069        
070        ZipArchiveEntry zipEntry = zipFile.getEntry("word/styles.xml");
071        
072        try (InputStream is = zipFile.getInputStream(zipEntry))
073        {
074            _saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler));
075        }
076        
077        zipEntry = zipFile.getEntry("word/numbering.xml");
078        if (zipEntry != null)
079        {
080            try (InputStream is = zipFile.getInputStream(zipEntry))
081            {
082                _saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler));
083            }
084        }
085        
086        contentHandler.endElement("", "document", "document");
087        contentHandler.endDocument();
088    }
089}