001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 */ 017package org.nuxeo.ecm.platform.preview.adapter.base; 018 019import java.io.File; 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.List; 023import java.util.regex.Matcher; 024import java.util.regex.Pattern; 025 026import org.apache.commons.io.FileUtils; 027import org.nuxeo.ecm.core.api.Blob; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.platform.preview.adapter.BlobPostProcessor; 030import org.nuxeo.ecm.platform.preview.adapter.PreviewAdapterManager; 031import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter; 032import org.nuxeo.ecm.platform.preview.api.PreviewException; 033import org.nuxeo.ecm.platform.preview.helper.PreviewHelper; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Abstract base class for PreviewAdapters 038 * 039 * @author tiry 040 */ 041public abstract class AbstractHtmlPreviewAdapter implements HtmlPreviewAdapter { 042 043 // private static final String TITLE_REGEXP = "<title\b[^>]*>(.*?)</title>"; 044 private static final String TITLE_REGEXP = "<title>(.*?)</title>"; 045 046 // private static final String TITLE_REGEXP = "<title[^>]*>[^<]*</title>"; 047 private static final Pattern TITLE_PATTERN = Pattern.compile(TITLE_REGEXP, Pattern.CASE_INSENSITIVE); 048 049 protected DocumentModel adaptedDoc; 050 051 protected static PreviewAdapterManager previewManager; 052 053 protected PreviewAdapterManager getPreviewManager() { 054 if (previewManager == null) { 055 previewManager = Framework.getService(PreviewAdapterManager.class); 056 } 057 return previewManager; 058 } 059 060 @Override 061 public void setAdaptedDocument(DocumentModel doc) { 062 adaptedDoc = doc; 063 } 064 065 @Override 066 public String getFilePreviewURL() { 067 return PreviewHelper.getPreviewURL(adaptedDoc); 068 } 069 070 @Override 071 public String getFilePreviewURL(String xpath) { 072 return PreviewHelper.getPreviewURL(adaptedDoc, xpath); 073 } 074 075 protected String updateTitleInHtml(String htmlContent) { 076 Matcher m = TITLE_PATTERN.matcher(htmlContent); 077 // if (m.matches()) 078 // return m.replaceFirst("<title>" + getPreviewTitle() + "</title>"); 079 if (m.find()) { 080 String found = m.group(); 081 htmlContent = htmlContent.replaceFirst(found, "<title>" + getPreviewTitle() + "</title>"); 082 } 083 084 return htmlContent; 085 } 086 087 protected void updateTitleInHtml(File file) throws IOException { 088 String htmlContent = FileUtils.readFileToString(file); 089 htmlContent = updateTitleInHtml(htmlContent); 090 FileUtils.writeStringToFile(file, htmlContent); 091 } 092 093 protected String getPreviewTitle() { 094 StringBuilder sb = new StringBuilder(); 095 096 sb.append(adaptedDoc.getTitle()); 097 sb.append(" "); 098 String vl = adaptedDoc.getVersionLabel(); 099 if (vl != null) { 100 sb.append(vl); 101 } 102 sb.append(" (preview)"); 103 104 return sb.toString(); 105 } 106 107 @Override 108 public List<Blob> getFilePreviewBlobs() throws PreviewException { 109 return getFilePreviewBlobs(false); 110 } 111 112 @Override 113 public List<Blob> getFilePreviewBlobs(String xpath) throws PreviewException { 114 return getFilePreviewBlobs(xpath, false); 115 } 116 117 @Override 118 public List<Blob> getFilePreviewBlobs(boolean postProcess) throws PreviewException { 119 List<Blob> blobs = getPreviewBlobs(); 120 if (postProcess) { 121 blobs = postProcessBlobs(blobs); 122 } 123 return blobs; 124 } 125 126 protected abstract List<Blob> getPreviewBlobs() throws PreviewException; 127 128 @Override 129 public List<Blob> getFilePreviewBlobs(String xpath, boolean postProcess) throws PreviewException { 130 List<Blob> blobs = getPreviewBlobs(xpath); 131 if (postProcess) { 132 blobs = postProcessBlobs(blobs); 133 } 134 return blobs; 135 } 136 137 protected abstract List<Blob> getPreviewBlobs(String xpath) throws PreviewException; 138 139 protected List<Blob> postProcessBlobs(List<Blob> blobs) throws PreviewException { 140 List<Blob> processedBlobs = new ArrayList<Blob>(); 141 for (Blob blob : blobs) { 142 for (BlobPostProcessor processor : getPreviewManager().getBlobPostProcessors()) { 143 blob = processor.process(blob); 144 } 145 processedBlobs.add(blob); 146 } 147 return processedBlobs; 148 } 149 150}