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