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 PreviewAdapterManager getPreviewManager() { 054 return Framework.getService(PreviewAdapterManager.class); 055 } 056 057 @Override 058 public void setAdaptedDocument(DocumentModel doc) { 059 adaptedDoc = doc; 060 } 061 062 @Override 063 public String getFilePreviewURL() { 064 return PreviewHelper.getPreviewURL(adaptedDoc); 065 } 066 067 @Override 068 public String getFilePreviewURL(String xpath) { 069 return PreviewHelper.getPreviewURL(adaptedDoc, xpath); 070 } 071 072 protected String updateTitleInHtml(String htmlContent) { 073 Matcher m = TITLE_PATTERN.matcher(htmlContent); 074 // if (m.matches()) 075 // return m.replaceFirst("<title>" + getPreviewTitle() + "</title>"); 076 if (m.find()) { 077 String found = m.group(); 078 htmlContent = htmlContent.replaceFirst(found, "<title>" + getPreviewTitle() + "</title>"); 079 } 080 081 return htmlContent; 082 } 083 084 protected void updateTitleInHtml(File file) throws IOException { 085 String htmlContent = FileUtils.readFileToString(file, UTF_8); 086 htmlContent = updateTitleInHtml(htmlContent); 087 FileUtils.writeStringToFile(file, htmlContent, UTF_8); 088 } 089 090 protected String getPreviewTitle() { 091 StringBuilder sb = new StringBuilder(); 092 093 sb.append(adaptedDoc.getTitle()); 094 sb.append(" "); 095 String vl = adaptedDoc.getVersionLabel(); 096 if (vl != null) { 097 sb.append(vl); 098 } 099 sb.append(" (preview)"); 100 101 return sb.toString(); 102 } 103 104 @Override 105 public List<Blob> getFilePreviewBlobs() throws PreviewException { 106 return getFilePreviewBlobs(false); 107 } 108 109 @Override 110 public List<Blob> getFilePreviewBlobs(String xpath) throws PreviewException { 111 return getFilePreviewBlobs(xpath, false); 112 } 113 114 @Override 115 public List<Blob> getFilePreviewBlobs(boolean postProcess) throws PreviewException { 116 List<Blob> blobs = getPreviewBlobs(); 117 if (postProcess) { 118 blobs = postProcessBlobs(blobs); 119 } 120 return blobs; 121 } 122 123 protected abstract List<Blob> getPreviewBlobs() throws PreviewException; 124 125 @Override 126 public List<Blob> getFilePreviewBlobs(String xpath, boolean postProcess) throws PreviewException { 127 List<Blob> blobs = getPreviewBlobs(xpath); 128 if (postProcess) { 129 blobs = postProcessBlobs(blobs); 130 } 131 return blobs; 132 } 133 134 protected abstract List<Blob> getPreviewBlobs(String xpath) throws PreviewException; 135 136 protected List<Blob> postProcessBlobs(List<Blob> blobs) throws PreviewException { 137 List<Blob> processedBlobs = new ArrayList<>(); 138 for (Blob blob : blobs) { 139 for (BlobPostProcessor processor : getPreviewManager().getBlobPostProcessors()) { 140 blob = processor.process(blob); 141 } 142 processedBlobs.add(blob); 143 } 144 return processedBlobs; 145 } 146 147}