mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-19 11:07:43 +00:00
Address some issues in PListViewer detected by Codacy/IDE
This commit is contained in:
parent
6ef7317c8a
commit
88edf39ca5
@ -16,7 +16,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.sleuthkit.autopsy.contentviewers;
|
||||
|
||||
import java.awt.Component;
|
||||
@ -56,13 +55,13 @@ import org.sleuthkit.autopsy.coreutils.Logger;
|
||||
import org.sleuthkit.datamodel.TskCoreException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
||||
/**
|
||||
* PListViewer - a file viewer for binary plist files.
|
||||
*
|
||||
*/
|
||||
public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, ExplorerManager.Provider {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final String[] MIMETYPES = new String[]{"application/x-bplist"};
|
||||
private static final Logger LOGGER = Logger.getLogger(PListViewer.class.getName());
|
||||
|
||||
@ -77,7 +76,6 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
*/
|
||||
public PListViewer() {
|
||||
|
||||
|
||||
// Create an Outlineview and add to the panel
|
||||
outlineView = new org.openide.explorer.view.OutlineView();
|
||||
|
||||
@ -95,8 +93,8 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
}
|
||||
|
||||
@NbBundle.Messages({"PListNode.KeyCol=Key",
|
||||
"PListNode.TypeCol=Type",
|
||||
"PListNode.ValueCol=Value" })
|
||||
"PListNode.TypeCol=Type",
|
||||
"PListNode.ValueCol=Value"})
|
||||
|
||||
private void customize() {
|
||||
|
||||
@ -104,11 +102,10 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
|
||||
outline.setRootVisible(false);
|
||||
if (null == explorerManager) {
|
||||
explorerManager = new ExplorerManager();
|
||||
explorerManager = new ExplorerManager();
|
||||
}
|
||||
|
||||
//outline.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
||||
|
||||
plistTableScrollPane.setViewportView(outlineView);
|
||||
|
||||
outline.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
|
||||
@ -116,6 +113,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
this.setVisible(true);
|
||||
outline.setRowSelectionAllowed(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called from within the constructor to initialize the form.
|
||||
* WARNING: Do NOT modify this code. The content of this method is always
|
||||
@ -180,8 +178,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
}// </editor-fold>//GEN-END:initComponents
|
||||
|
||||
@NbBundle.Messages({"PListViewer.ExportSuccess.message=Plist file exported successfully",
|
||||
"PListViewer.ExportFailed.message=Plist file export failed.",
|
||||
})
|
||||
"PListViewer.ExportFailed.message=Plist file export failed.",})
|
||||
|
||||
/**
|
||||
* Handles the Export button pressed action
|
||||
@ -204,14 +201,14 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
//Save the propery list as XML
|
||||
PropertyListParser.saveAsXML(this.rootDict, selectedFile);
|
||||
JOptionPane.showMessageDialog(this,
|
||||
String.format("Plist file exported successfully to %s ", selectedFile.getName() ),
|
||||
Bundle.PListViewer_ExportSuccess_message(),
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
String.format("Plist file exported successfully to %s ", selectedFile.getName()),
|
||||
Bundle.PListViewer_ExportSuccess_message(),
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
} catch (IOException ex) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
String.format("Failed to export plist file to %s ", selectedFile.getName() ),
|
||||
Bundle.PListViewer_ExportFailed_message(),
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
String.format("Failed to export plist file to %s ", selectedFile.getName()),
|
||||
Bundle.PListViewer_ExportFailed_message(),
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
|
||||
LOGGER.log(Level.SEVERE, "Error exporting plist to XML file " + selectedFile.getName(), ex);
|
||||
}
|
||||
@ -225,7 +222,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
*/
|
||||
@Override
|
||||
public List<String> getSupportedMIMETypes() {
|
||||
return Arrays.asList(MIMETYPES);
|
||||
return Arrays.asList(MIMETYPES);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -245,7 +242,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
*/
|
||||
@Override
|
||||
public Component getComponent() {
|
||||
return this;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -254,10 +251,9 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
*/
|
||||
@Override
|
||||
public void resetComponent() {
|
||||
rootDict = null;
|
||||
rootDict = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process the given Plist file
|
||||
*
|
||||
@ -274,25 +270,28 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
LOGGER.log(Level.SEVERE, "Error reading bytes of plist file.", ex);
|
||||
}
|
||||
|
||||
final List<PropKeyValue> plist = parsePList(plistFileBuf);
|
||||
final List<PropKeyValue> plist;
|
||||
try {
|
||||
plist = parsePList(plistFileBuf);
|
||||
new SwingWorker<Void, Void>() {
|
||||
@Override
|
||||
protected Void doInBackground() {
|
||||
setupTable(plist);
|
||||
return null;
|
||||
}
|
||||
|
||||
new SwingWorker<Void, Void>() {
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception {
|
||||
setupTable(plist);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
super.done();
|
||||
setColumnWidths();
|
||||
}
|
||||
}.execute();
|
||||
@Override
|
||||
protected void done() {
|
||||
super.done();
|
||||
setColumnWidths();
|
||||
}
|
||||
}.execute();
|
||||
|
||||
} catch (IOException | PropertyListFormatException | ParseException | ParserConfigurationException | SAXException ex) {
|
||||
LOGGER.log(Level.SEVERE, String.format("Error parsing plist for file (obj_id = %d)", plistFile.getId()), ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets up the columns in the display table
|
||||
*
|
||||
@ -310,7 +309,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
final int margin = 4;
|
||||
final int padding = 8;
|
||||
|
||||
// find the maximum width needed to fit the values for the first N rows, at most
|
||||
// find the maximum width needed to fit the values for the first N rows, at most
|
||||
final int rows = Math.min(20, outline.getRowCount());
|
||||
for (int col = 0; col < outline.getColumnCount(); col++) {
|
||||
final int columnWidthLimit = 2000;
|
||||
@ -329,7 +328,6 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses the given plist key/value
|
||||
*/
|
||||
@ -342,17 +340,16 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
} else if (value instanceof NSNumber) {
|
||||
final NSNumber number = (NSNumber) value;
|
||||
if (number.isInteger()) {
|
||||
return new PropKeyValue(key, PropertyType.NUMBER, Long.valueOf(number.longValue()) );
|
||||
return new PropKeyValue(key, PropertyType.NUMBER, number.longValue());
|
||||
} else if (number.isBoolean()) {
|
||||
return new PropKeyValue(key, PropertyType.BOOLEAN, Boolean.valueOf(number.boolValue()) );
|
||||
return new PropKeyValue(key, PropertyType.BOOLEAN, number.boolValue());
|
||||
} else {
|
||||
return new PropKeyValue(key, PropertyType.NUMBER, Float.valueOf(number.floatValue())) ;
|
||||
return new PropKeyValue(key, PropertyType.NUMBER, number.floatValue());
|
||||
}
|
||||
} else if (value instanceof NSDate) {
|
||||
final NSDate date = (NSDate) value;
|
||||
return new PropKeyValue(key, PropertyType.DATE, date.toString());
|
||||
}
|
||||
else if (value instanceof NSData ) {
|
||||
return new PropKeyValue(key, PropertyType.DATE, date.toString());
|
||||
} else if (value instanceof NSData) {
|
||||
return new PropKeyValue(key, PropertyType.DATA, Bundle.PListViewer_DataType_message());
|
||||
} else if (value instanceof NSArray) {
|
||||
final List<PropKeyValue> children = new ArrayList<>();
|
||||
@ -363,7 +360,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
children.add(parseProperty("", array.objectAtIndex(i)));
|
||||
}
|
||||
|
||||
pkv.setChildren(children.toArray(new PropKeyValue[children.size()] ));
|
||||
pkv.setChildren(children.toArray(new PropKeyValue[children.size()]));
|
||||
return pkv;
|
||||
} else if (value instanceof NSDictionary) {
|
||||
final List<PropKeyValue> children = new ArrayList<>();
|
||||
@ -375,10 +372,10 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
children.add(parseProperty(key2, obj));
|
||||
}
|
||||
|
||||
pkv.setChildren(children.toArray(new PropKeyValue[children.size()] ));
|
||||
pkv.setChildren(children.toArray(new PropKeyValue[children.size()]));
|
||||
return pkv;
|
||||
} else {
|
||||
LOGGER.severe("Can't parse Plist for key = " + key + " value of type " + value.getClass());
|
||||
LOGGER.log(Level.SEVERE, "Can''t parse Plist for key = {0} value of type {1}", new Object[]{key, value.getClass()});
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -389,24 +386,19 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
*
|
||||
* @param plistbytes
|
||||
*
|
||||
* @return list of PropKeyValue
|
||||
* @return list of PropKeyValue
|
||||
*/
|
||||
private List<PropKeyValue> parsePList(final byte[] plistbytes) {
|
||||
private List<PropKeyValue> parsePList(final byte[] plistbytes) throws IOException, PropertyListFormatException, ParseException, ParserConfigurationException, SAXException {
|
||||
|
||||
final List<PropKeyValue> plist = new ArrayList<>();
|
||||
try {
|
||||
rootDict = (NSDictionary) PropertyListParser.parse(plistbytes);
|
||||
rootDict = (NSDictionary) PropertyListParser.parse(plistbytes);
|
||||
|
||||
final String[] keys = rootDict.allKeys();
|
||||
for (final String key : keys) {
|
||||
final PropKeyValue pkv = parseProperty(key, rootDict.objectForKey(key));
|
||||
if (null != pkv) {
|
||||
plist.add(pkv);
|
||||
}
|
||||
final String[] keys = rootDict.allKeys();
|
||||
for (final String key : keys) {
|
||||
final PropKeyValue pkv = parseProperty(key, rootDict.objectForKey(key));
|
||||
if (null != pkv) {
|
||||
plist.add(pkv);
|
||||
}
|
||||
} catch (PropertyListFormatException | IOException | ParseException | ParserConfigurationException | SAXException ex) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to parse PList.", ex);
|
||||
return null;
|
||||
}
|
||||
|
||||
return plist;
|
||||
@ -421,20 +413,20 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
* Plist property type
|
||||
*/
|
||||
enum PropertyType {
|
||||
STRING,
|
||||
NUMBER,
|
||||
BOOLEAN,
|
||||
DATE,
|
||||
DATA,
|
||||
ARRAY,
|
||||
DICTIONARY
|
||||
STRING,
|
||||
NUMBER,
|
||||
BOOLEAN,
|
||||
DATE,
|
||||
DATA,
|
||||
ARRAY,
|
||||
DICTIONARY
|
||||
};
|
||||
|
||||
/**
|
||||
* Encapsulates a Plist property
|
||||
*
|
||||
*/
|
||||
class PropKeyValue {
|
||||
final static class PropKeyValue {
|
||||
|
||||
private final String key;
|
||||
private final PropertyType type;
|
||||
@ -454,13 +446,12 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
* Copy constructor
|
||||
*/
|
||||
PropKeyValue(PropKeyValue other) {
|
||||
this.key = other.key;
|
||||
this.type = other.type;
|
||||
this.value = other.value;
|
||||
this.key = other.getKey();
|
||||
this.type = other.getType();
|
||||
this.value = other.getValue();
|
||||
|
||||
this.setChildren(other.getChildren());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
String getKey() {
|
||||
return this.key;
|
||||
@ -476,6 +467,7 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
|
||||
/**
|
||||
* Returns an array of children, if any.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
PropKeyValue[] getChildren() {
|
||||
@ -485,15 +477,16 @@ public class PListViewer extends javax.swing.JPanel implements FileTypeViewer, E
|
||||
|
||||
// return a copy
|
||||
return Arrays.stream(children)
|
||||
.map(child -> new PropKeyValue(child) )
|
||||
.map(child -> new PropKeyValue(child))
|
||||
.toArray(PropKeyValue[]::new);
|
||||
}
|
||||
|
||||
void setChildren(final PropKeyValue...children) {
|
||||
this.children = children;
|
||||
void setChildren(final PropKeyValue... children) {
|
||||
this.children = Arrays.stream(children)
|
||||
.map(child -> new PropKeyValue(child))
|
||||
.toArray(PropKeyValue[]::new);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
|
Loading…
x
Reference in New Issue
Block a user