1 16 package org.outerj.daisy.books.publisher.impl.publicationprocess; 17 18 import org.outerj.daisy.books.publisher.impl.bookmodel.SectionContainer; 19 import org.outerj.daisy.books.publisher.impl.bookmodel.Section; 20 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 21 import org.outerj.daisy.books.publisher.impl.util.XMLPropertiesHelper; 22 import org.outerj.daisy.frontend.PreparedDocuments; 23 import org.outerj.daisy.frontend.DocumentTypeSpecificStyler; 24 import org.outerj.daisy.xmlutil.XmlSerializer; 25 import org.apache.cocoon.xml.SaxBuffer; 26 import org.apache.cocoon.components.flow.util.PipelineUtil; 27 import org.apache.cocoon.components.LifecycleHelper; 28 import org.apache.excalibur.source.Source; 29 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 35 public class ApplyDocumentTypeStylingTask implements PublicationProcessTask { 36 37 public void run(PublicationContext context) throws Exception { 38 context.getPublicationLog().info("Running apply document type styling task."); 39 processSections(context.getBook(), context); 40 } 41 42 private void processSections(SectionContainer sectionContainer, PublicationContext context) throws Exception { 43 Section[] sections = sectionContainer.getSections(); 44 for (int i = 0; i < sections.length; i++) { 45 if (sections[i].getBookStorePath() != null) { 46 String bookStorePath = sections[i].getBookStorePath(); 47 48 PreparedDocuments preparedDocuments; 49 InputStream is = context.getBookInstance().getResource(bookStorePath); 50 try { 51 preparedDocuments = PreparedDocumentsBuilder.build(is); 52 } finally { 53 is.close(); 54 } 55 56 applyDocumentTypeStyling(preparedDocuments, context); 57 58 String path = BookInstanceLayout.getDocumentInPublicationStorePath(bookStorePath, context.getPublicationOutputName()); 59 OutputStream os = context.getBookInstance().getResourceOutputStream(path); 60 try { 61 storePreparedDocuments(os, preparedDocuments); 62 } finally { 63 os.close(); 64 } 65 } 66 processSections(sections[i], context); 67 } 68 } 69 70 private void applyDocumentTypeStyling(PreparedDocuments preparedDocuments, PublicationContext context) throws Exception { 71 SaxBuffer publicationPropertiesBuffer = new SaxBuffer(); 72 XMLPropertiesHelper.generateSaxFragment(context.getProperties(), publicationPropertiesBuffer); 73 SaxBuffer bookMetadataBuffer = new SaxBuffer(); 74 XMLPropertiesHelper.generateSaxFragment(context.getBookMetadata(), "metadata", bookMetadataBuffer); 75 76 int[] ids = preparedDocuments.getPreparedDocumentIds(); 77 for (int i = 0; i < ids.length; i++) { 78 PreparedDocuments.PreparedDocument preparedDocument = preparedDocuments.getPreparedDocument(ids[i]); 79 SaxBuffer buffer = preparedDocument.getSaxBuffer(); 80 81 String stylesheet; 82 Object styleInfo = DocumentTypeSpecificStyler.searchStyleInfo(buffer); 83 if (styleInfo != null && styleInfo instanceof Long ) { 84 long documentTypeId = ((Long )styleInfo).longValue(); 85 String documentTypeName = context.getRepository().getRepositorySchema().getDocumentTypeById(documentTypeId, false).getName(); 86 stylesheet = determineStylesheet(documentTypeName, context); 87 } else { 88 throw new Exception ("Did not receive a document type ID from searchStyleInfo, instead got: " + styleInfo); 90 } 91 92 Map viewData = new HashMap (); 93 viewData.put("stylesheet", stylesheet); 94 viewData.put("isIncluded", String.valueOf(ids[i] != 1)); 95 viewData.put("repository", context.getRepository()); 96 viewData.put("document", buffer); 97 viewData.put("bookMetadataBuffer", bookMetadataBuffer); 98 viewData.put("publicationTypeName", context.getPublicationTypeName()); 99 viewData.put("publicationPropertiesBuffer", publicationPropertiesBuffer); 100 101 SaxBuffer resultBuffer = executePipeline(context.getDaisyCocoonPath() + "/books/StyleDocumentPipe", viewData, context); 102 preparedDocuments.putPreparedDocument(preparedDocument.getId(), preparedDocument.getDocumentKey(), resultBuffer); 103 } 104 } 105 106 private String determineStylesheet(String documentTypeName, PublicationContext context) throws Exception { 107 org.apache.excalibur.source.SourceResolver sourceResolver = (org.apache.excalibur.source.SourceResolver)context.getServiceManager().lookup(org.apache.excalibur.source.SourceResolver.ROLE); 108 Source source = null; 109 try { 110 source = sourceResolver.resolveURI("wikidata:/books/publicationtypes/" + context.getPublicationTypeName() + "/document-styling/" + documentTypeName + ".xsl"); 113 if (source.exists()) { 114 return source.getURI(); 115 } 116 sourceResolver.release(source); 117 source = null; 118 119 source = sourceResolver.resolveURI("wikidata:/books/publicationtypes/document-styling/" + documentTypeName + ".xsl"); 121 if (source.exists()) { 122 return source.getURI(); 123 } 124 125 return "wikidata:/books/publicationtypes/common/book-document-to-html.xsl"; 127 } finally { 128 if (source != null) 129 sourceResolver.release(source); 130 context.getServiceManager().release(sourceResolver); 131 } 132 } 133 134 137 private SaxBuffer executePipeline(String pipe, Map viewData, PublicationContext context) throws Exception { 138 PipelineUtil pipelineUtil = new PipelineUtil(); 139 try { 140 LifecycleHelper.setupComponent(pipelineUtil, null, context.getAvalonContext(), context.getServiceManager(), null, false); 141 142 SaxBuffer buffer = new SaxBuffer(); 143 pipelineUtil.processToSAX(pipe, viewData, buffer); 144 145 return buffer; 146 } finally { 147 LifecycleHelper.dispose(pipelineUtil); 148 } 149 } 150 151 private void storePreparedDocuments(OutputStream os, PreparedDocuments preparedDocuments) throws Exception { 152 XmlSerializer serializer = new XmlSerializer(os); 153 serializer.startDocument(); 154 preparedDocuments.generateSax(serializer); 155 serializer.endDocument(); 156 } 157 158 } 159 | Popular Tags |