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 045 @Override 046 public void service(ServiceManager sManager) throws ServiceException 047 { 048 super.service(sManager); 049 _domSerializer = (DOMSerializer) sManager.lookup(DOMSerializer.ROLE); 050 } 051 052 @Override 053 public void generate() throws IOException, SAXException, ProcessingException 054 { 055 @SuppressWarnings("unchecked") 056 Map<String, Object> context = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 057 058 Document document = (Document) context.get("document"); 059 Document relations = (Document) context.get("relations"); 060 061 ZipFile zipFile = (ZipFile) context.get("zipFile"); 062 063 contentHandler.startDocument(); 064 contentHandler.startElement("", "document", "document", new AttributesImpl()); 065 _domSerializer.serialize(document, new IgnoreRootHandler(contentHandler), lexicalHandler); 066 _domSerializer.serialize(relations, new IgnoreRootHandler(contentHandler), lexicalHandler); 067 068 ZipArchiveEntry zipEntry = zipFile.getEntry("word/styles.xml"); 069 070 SAXParser saxParser = null; 071 try 072 { 073 saxParser = (SAXParser) manager.lookup(SAXParser.ROLE); 074 try (InputStream is = zipFile.getInputStream(zipEntry)) 075 { 076 saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler)); 077 } 078 079 zipEntry = zipFile.getEntry("word/numbering.xml"); 080 if (zipEntry != null) 081 { 082 try (InputStream is = zipFile.getInputStream(zipEntry)) 083 { 084 saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler)); 085 } 086 } 087 } 088 catch (ServiceException e) 089 { 090 throw new SAXException("Unable to get a SAX parser.", e); 091 } 092 finally 093 { 094 manager.release(saxParser); 095 } 096 097 contentHandler.endElement("", "document", "document"); 098 contentHandler.endDocument(); 099 } 100}