KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > DocumentTypeSpecificStyler


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.frontend;
17
18 import org.apache.cocoon.xml.SaxBuffer;
19 import org.apache.cocoon.components.flow.util.PipelineUtil;
20 import org.apache.cocoon.components.LifecycleHelper;
21 import org.apache.avalon.framework.context.Context;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.outerj.daisy.repository.Repository;
24 import org.outerj.daisy.repository.VariantKey;
25 import org.outerj.daisy.util.Constants;
26
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * This class is concerned with applying document type specific styling (by using
33  * Cocoon pipelines) to the documents stored in a {@link PreparedDocuments} object.
34  */

35 public class DocumentTypeSpecificStyler {
36     private final String JavaDoc publishType;
37     private final String JavaDoc basePath;
38     private final String JavaDoc daisyCocoonPath;
39     private final StylesheetProvider stylesheetProvider;
40     private final PageContext pageContext;
41     private final Repository repository;
42     private final Context context;
43     private final ServiceManager serviceManager;
44
45     /**
46      *
47      * @param publishType 'html' or 'xslfo' or anything for which a pipeline is defined
48      * @param basePath the base path for addressing documents (should end on a slash).
49      * This is simply passed as a parameter to the stylesheets, who can use it if they need.
50      * @param stylesheetProvider
51      */

52     public DocumentTypeSpecificStyler(String JavaDoc publishType, String JavaDoc basePath, String JavaDoc daisyCocoonPath,
53             StylesheetProvider stylesheetProvider, PageContext pageContext,
54             Repository repository, Context context, ServiceManager serviceManager) {
55         this.publishType = publishType;
56         this.basePath = basePath;
57         this.daisyCocoonPath = daisyCocoonPath;
58         this.stylesheetProvider = stylesheetProvider;
59         this.repository = repository;
60         this.context = context;
61         this.serviceManager = serviceManager;
62         this.pageContext = pageContext;
63     }
64
65     public static interface StylesheetProvider {
66         String JavaDoc getStylesheet(String JavaDoc documentTypeName) throws Exception JavaDoc;
67         String JavaDoc getDefaultStylesheet() throws Exception JavaDoc;
68         String JavaDoc getStylesheetByHint(String JavaDoc styleHint) throws Exception JavaDoc;
69     }
70
71     public void transformPreparedDocuments(PreparedDocuments preparedDocuments) throws Exception JavaDoc {
72         int[] ids = preparedDocuments.getPreparedDocumentIds();
73         for (int i = 0; i < ids.length; i++) {
74             PreparedDocuments.PreparedDocument preparedDocument = preparedDocuments.getPreparedDocument(ids[i]);
75             SaxBuffer buffer = preparedDocument.getSaxBuffer();
76
77             Object JavaDoc styleInfo = searchStyleInfo(buffer);
78             String JavaDoc stylesheet;
79             if (styleInfo != null) {
80                 if (styleInfo instanceof Long JavaDoc) {
81                     long documentTypeId = ((Long JavaDoc)styleInfo).longValue();
82                     String JavaDoc documentTypeName = repository.getRepositorySchema().getDocumentTypeById(documentTypeId, false).getName();
83                     stylesheet = stylesheetProvider.getStylesheet(documentTypeName);
84                 } else {
85                     stylesheet = stylesheetProvider.getStylesheetByHint((String JavaDoc)styleInfo);
86                 }
87             } else {
88                 stylesheet = stylesheetProvider.getDefaultStylesheet();
89             }
90
91             Map JavaDoc viewData = new HashMap JavaDoc();
92             viewData.put("stylesheet", stylesheet);
93             viewData.put("pageContext", pageContext);
94             viewData.put("repository", repository);
95             viewData.put("document", buffer);
96             viewData.put("documentBasePath", basePath);
97             viewData.put("isIncluded", String.valueOf(ids[i] != 1)); // root document has ID 1 in prep-docs
98
VariantKey documentKey = preparedDocument.getDocumentKey();
99             viewData.put("documentKey", documentKey);
100             viewData.put("documentBranch", repository.getVariantManager().getBranch(documentKey.getBranchId(), false).getName());
101             viewData.put("documentLanguage", repository.getVariantManager().getLanguage(documentKey.getLanguageId(), false).getName());
102
103             SaxBuffer resultBuffer = executePipeline(daisyCocoonPath + "/" + publishType + "-StyleDocumentPipe", viewData);
104             preparedDocuments.putPreparedDocument(preparedDocument.getId(), preparedDocument.getDocumentKey(), resultBuffer);
105         }
106     }
107
108     /**
109      * Searches the supplied buffer and returns:
110      * <ul>
111      * <li>a String containing the value of the styleHint attribute on the p:publisherResponse element, if found
112      * <li>otherwise a Long containing the typeId attribute of the first d:document element encountered
113      * <li>otherwise null
114      * </ul>
115      *
116      * @param buffer containing the XML of the publisher request for the document.
117      */

118     public static Object JavaDoc searchStyleInfo(SaxBuffer buffer) {
119         List JavaDoc bits = buffer.getBits();
120         boolean publisherResponseElementFound = false;
121         for (int i = 0; i < bits.size(); i++) {
122             Object JavaDoc bit = bits.get(i);
123             if (bit instanceof SaxBuffer.StartElement) {
124                 SaxBuffer.StartElement startElement = (SaxBuffer.StartElement)bit;
125                 if (!publisherResponseElementFound
126                         && startElement.namespaceURI.equals("http://outerx.org/daisy/1.0#publisher")
127                         && startElement.localName.equals("publisherResponse")) {
128                     String JavaDoc styleHint = startElement.attrs.getValue("styleHint");
129                     if (styleHint != null && styleHint.length() != 0)
130                         return styleHint;
131                     publisherResponseElementFound = true;
132                 } else if (startElement.namespaceURI.equals(Constants.DAISY_NAMESPACE) && startElement.localName.equals("document")) {
133                     return new Long JavaDoc(Long.parseLong(startElement.attrs.getValue("typeId")));
134                 }
135             }
136         }
137         return null;
138     }
139
140     /**
141      * Executes a Cocoon pipeline and store the result in a SaxBuffer instance.
142      */

143     private SaxBuffer executePipeline(String JavaDoc pipe, Map JavaDoc viewData) throws Exception JavaDoc {
144         PipelineUtil pipelineUtil = new PipelineUtil();
145         try {
146             LifecycleHelper.setupComponent(pipelineUtil, null, context, serviceManager, null, false);
147
148             SaxBuffer buffer = new SaxBuffer();
149             pipelineUtil.processToSAX(pipe, viewData, buffer);
150
151             return buffer;
152         } finally {
153             LifecycleHelper.dispose(pipelineUtil);
154         }
155     }
156 }
157
Popular Tags