19 package org.sleuthkit.autopsy.contentviewers;
21 import java.awt.Component;
22 import java.awt.Cursor;
23 import java.io.UnsupportedEncodingException;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.logging.Level;
27 import org.apache.tika.parser.txt.CharsetDetector;
28 import org.apache.tika.parser.txt.CharsetMatch;
29 import org.openide.util.NbBundle;
30 import org.openide.windows.WindowManager;
38 @SuppressWarnings(
"PMD.SingularField")
39 final class HtmlViewer extends javax.swing.JPanel implements FileTypeViewer {
41 private static final long serialVersionUID = 1L;
42 private static final Logger logger = Logger.getLogger(HtmlViewer.class.getName());
43 private static final String[] SUPPORTED_MIMETYPES =
new String[]{
45 "application/xhtml+xml"
65 "HtmlViewer_file_error=This file is missing or unreadable.",
66 "HtmlViewer_encoding_error=This file has unsupported encoding"})
67 private String getHtmlText(AbstractFile abstractFile) {
69 int fileSize = (int) abstractFile.getSize();
70 byte[] buffer =
new byte[fileSize];
71 abstractFile.read(buffer, 0, fileSize);
72 CharsetMatch match =
new CharsetDetector().setText(buffer).detect();
74 return new String(buffer, match.getName());
76 return new String(buffer);
78 }
catch (TskCoreException ex) {
79 logger.log(Level.SEVERE, String.format(
"Unable to read from file '%s' (id=%d).",
80 abstractFile.getName(), abstractFile.getId()), ex);
81 return String.format(
"<p>%s</p>", Bundle.HtmlViewer_file_error());
82 }
catch (UnsupportedEncodingException ex) {
83 logger.log(Level.SEVERE, String.format(
"Unsupported encoding for file '%s' (id=%d).",
84 abstractFile.getName(), abstractFile.getId()), ex);
85 return String.format(
"<p>%s</p>", Bundle.HtmlViewer_encoding_error());
94 @SuppressWarnings(
"unchecked")
96 private
void initComponents() {
98 setLayout(
new java.awt.BorderLayout());
105 public List<String> getSupportedMIMETypes() {
106 return Arrays.asList(SUPPORTED_MIMETYPES);
110 public void setFile(AbstractFile file) {
111 WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
112 htmlPanel.setHtmlText(getHtmlText(file));
113 WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
117 public Component getComponent() {
122 public void resetComponent() {
127 public boolean isSupported(AbstractFile file) {