KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationprocess > ApplyDocumentTypeStylingTask


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 public class ApplyDocumentTypeStylingTask implements PublicationProcessTask {
36
37     public void run(PublicationContext context) throws Exception JavaDoc {
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 JavaDoc {
43         Section[] sections = sectionContainer.getSections();
44         for (int i = 0; i < sections.length; i++) {
45             if (sections[i].getBookStorePath() != null) {
46                 String JavaDoc bookStorePath = sections[i].getBookStorePath();
47
48                 PreparedDocuments preparedDocuments;
49                 InputStream JavaDoc 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 JavaDoc path = BookInstanceLayout.getDocumentInPublicationStorePath(bookStorePath, context.getPublicationOutputName());
59                 OutputStream JavaDoc 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 JavaDoc {
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 JavaDoc stylesheet;
82             Object JavaDoc styleInfo = DocumentTypeSpecificStyler.searchStyleInfo(buffer);
83             if (styleInfo != null && styleInfo instanceof Long JavaDoc) {
84                 long documentTypeId = ((Long JavaDoc)styleInfo).longValue();
85                 String JavaDoc documentTypeName = context.getRepository().getRepositorySchema().getDocumentTypeById(documentTypeId, false).getName();
86                 stylesheet = determineStylesheet(documentTypeName, context);
87             } else {
88                 // Support for stylehints and possible missing d:document might be implemented later
89
throw new Exception JavaDoc("Did not receive a document type ID from searchStyleInfo, instead got: " + styleInfo);
90             }
91
92             Map JavaDoc viewData = new HashMap JavaDoc();
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 JavaDoc determineStylesheet(String JavaDoc documentTypeName, PublicationContext context) throws Exception JavaDoc {
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             // search for a stylesheet for this document
111
// first try a publication-type-specific, document-type-specific XSLT
112
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             // then try a document-type-specific XSLT shared by all publication types
120
source = sourceResolver.resolveURI("wikidata:/books/publicationtypes/document-styling/" + documentTypeName + ".xsl");
121             if (source.exists()) {
122                 return source.getURI();
123             }
124
125             // finally use the default XSL
126
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     /**
135      * Executes a Cocoon pipeline and store the result in a SaxBuffer instance.
136      */

137     private SaxBuffer executePipeline(String JavaDoc pipe, Map JavaDoc viewData, PublicationContext context) throws Exception JavaDoc {
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 JavaDoc os, PreparedDocuments preparedDocuments) throws Exception JavaDoc {
152         XmlSerializer serializer = new XmlSerializer(os);
153         serializer.startDocument();
154         preparedDocuments.generateSax(serializer);
155         serializer.endDocument();
156     }
157
158 }
159
Popular Tags