Autopsy  4.10.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HtmlPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.contentviewers;
20 
21 import org.jsoup.Jsoup;
22 import org.jsoup.nodes.Document;
23 import org.openide.util.NbBundle.Messages;
24 
28 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
29 final class HtmlPanel extends javax.swing.JPanel {
30 
31  private static final long serialVersionUID = 1L;
32 
33  private String htmlText;
34 
38  HtmlPanel() {
39  initComponents();
40 
41  Utilities.configureTextPaneAsHtml(htmlbodyTextPane);
42  }
43 
49  void setHtmlText(String htmlText) {
50  this.htmlText = htmlText;
51  refresh();
52  }
53 
57  void reset() {
58  htmlbodyTextPane.setText("");
59  showImagesToggleButton.setEnabled(false);
60  }
61 
69  private String wrapInHtmlBody(String htmlText) {
70  return "<html><body>" + htmlText + "</body></html>";
71  }
72 
80  private String cleanseHTML(String htmlInString) {
81 
82  Document doc = Jsoup.parse(htmlInString);
83 
84  // Update all 'img' tags.
85  doc.select("img[src]").forEach(img -> img.attr("src", ""));
86 
87  return doc.html();
88  }
89 
93  @Messages({
94  "HtmlPanel_showImagesToggleButton_show=Show Images",
95  "HtmlPanel_showImagesToggleButton_hide=Hide Images",
96  "Html_text_display_error=The HTML text cannot be displayed, it may not be correctly formed HTML.",
97  })
98  private void refresh() {
99  if (false == htmlText.isEmpty()) {
100  try {
101  if (showImagesToggleButton.isSelected()) {
102  showImagesToggleButton.setText(Bundle.HtmlPanel_showImagesToggleButton_hide());
103  this.htmlbodyTextPane.setText(wrapInHtmlBody(htmlText));
104  } else {
105  showImagesToggleButton.setText(Bundle.HtmlPanel_showImagesToggleButton_show());
106  this.htmlbodyTextPane.setText(wrapInHtmlBody(cleanseHTML(htmlText)));
107  }
108  showImagesToggleButton.setEnabled(true);
109  htmlbodyTextPane.setCaretPosition(0);
110  } catch(Exception ex) {
111  this.htmlbodyTextPane.setText(wrapInHtmlBody(Bundle.Html_text_display_error()));
112  }
113  }
114  }
115 
121  @SuppressWarnings("unchecked")
122  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
123  private void initComponents() {
124 
125  htmlScrollPane = new javax.swing.JScrollPane();
126  htmlbodyTextPane = new javax.swing.JTextPane();
127  showImagesToggleButton = new javax.swing.JToggleButton();
128 
129  htmlScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
130 
131  htmlbodyTextPane.setEditable(false);
132  htmlScrollPane.setViewportView(htmlbodyTextPane);
133 
134  org.openide.awt.Mnemonics.setLocalizedText(showImagesToggleButton, org.openide.util.NbBundle.getMessage(HtmlPanel.class, "HtmlPanel.showImagesToggleButton.text")); // NOI18N
135  showImagesToggleButton.addActionListener(new java.awt.event.ActionListener() {
136  public void actionPerformed(java.awt.event.ActionEvent evt) {
137  showImagesToggleButtonActionPerformed(evt);
138  }
139  });
140 
141  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
142  this.setLayout(layout);
143  layout.setHorizontalGroup(
144  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
145  .addComponent(htmlScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
146  .addGroup(layout.createSequentialGroup()
147  .addComponent(showImagesToggleButton)
148  .addGap(0, 0, Short.MAX_VALUE))
149  );
150  layout.setVerticalGroup(
151  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
152  .addGroup(layout.createSequentialGroup()
153  .addComponent(showImagesToggleButton)
154  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
155  .addComponent(htmlScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
156  );
157  }// </editor-fold>//GEN-END:initComponents
158 
159  private void showImagesToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showImagesToggleButtonActionPerformed
160  refresh();
161  }//GEN-LAST:event_showImagesToggleButtonActionPerformed
162 
163 
164  // Variables declaration - do not modify//GEN-BEGIN:variables
165  private javax.swing.JScrollPane htmlScrollPane;
166  private javax.swing.JTextPane htmlbodyTextPane;
167  private javax.swing.JToggleButton showImagesToggleButton;
168  // End of variables declaration//GEN-END:variables
169 }

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.