1 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 ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 31 35 public class DocumentTypeSpecificStyler { 36 private final String publishType; 37 private final String basePath; 38 private final String 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 52 public DocumentTypeSpecificStyler(String publishType, String basePath, String 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 getStylesheet(String documentTypeName) throws Exception ; 67 String getDefaultStylesheet() throws Exception ; 68 String getStylesheetByHint(String styleHint) throws Exception ; 69 } 70 71 public void transformPreparedDocuments(PreparedDocuments preparedDocuments) throws Exception { 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 styleInfo = searchStyleInfo(buffer); 78 String stylesheet; 79 if (styleInfo != null) { 80 if (styleInfo instanceof Long ) { 81 long documentTypeId = ((Long )styleInfo).longValue(); 82 String documentTypeName = repository.getRepositorySchema().getDocumentTypeById(documentTypeId, false).getName(); 83 stylesheet = stylesheetProvider.getStylesheet(documentTypeName); 84 } else { 85 stylesheet = stylesheetProvider.getStylesheetByHint((String )styleInfo); 86 } 87 } else { 88 stylesheet = stylesheetProvider.getDefaultStylesheet(); 89 } 90 91 Map viewData = new HashMap (); 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)); 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 118 public static Object searchStyleInfo(SaxBuffer buffer) { 119 List bits = buffer.getBits(); 120 boolean publisherResponseElementFound = false; 121 for (int i = 0; i < bits.size(); i++) { 122 Object 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 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 (Long.parseLong(startElement.attrs.getValue("typeId"))); 134 } 135 } 136 } 137 return null; 138 } 139 140 143 private SaxBuffer executePipeline(String pipe, Map viewData) throws Exception { 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 |