Skip to content

Commit 56eb4b8

Browse files
committed
#79 Allow FindAll Dialog to search and display nonbinary files.
1 parent 7e6be07 commit 56eb4b8

File tree

2 files changed

+106
-28
lines changed

2 files changed

+106
-28
lines changed

src/us/deathmarine/luyten/FindAllBox.java

+90-15
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
import java.awt.event.MouseEvent;
2020
import java.awt.event.WindowAdapter;
2121
import java.awt.event.WindowEvent;
22+
import java.io.BufferedReader;
2223
import java.io.File;
24+
import java.io.IOException;
25+
import java.io.InputStreamReader;
2326
import java.io.StringWriter;
2427
import java.util.Collections;
2528
import java.util.Enumeration;
2629
import java.util.jar.JarEntry;
2730
import java.util.jar.JarFile;
31+
import java.util.regex.Pattern;
2832

2933
import javax.swing.*;
3034
import javax.swing.GroupLayout.Alignment;
@@ -36,7 +40,12 @@ public class FindAllBox extends JDialog {
3640

3741
private JButton findButton;
3842
private JTextField textField;
43+
private JCheckBox mcase;
44+
private JCheckBox regex;
45+
private JCheckBox wholew;
46+
private JList<String> list;
3947
private JProgressBar progressBar;
48+
4049
private JLabel statusLabel = new JLabel("");
4150

4251
private DefaultListModel<String> classesList = new DefaultListModel<String>();
@@ -54,9 +63,13 @@ public FindAllBox(final MainWindow mainWindow) {
5463
findButton = new JButton("Find");
5564
findButton.addActionListener(new FindButton());
5665

66+
mcase = new JCheckBox("Match Case");
67+
regex = new JCheckBox("Regex");
68+
wholew = new JCheckBox("Whole Words");
69+
5770
this.getRootPane().setDefaultButton(findButton);
5871

59-
JList<String> list = new JList<String>(classesList);
72+
list = new JList<String>(classesList);
6073
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
6174
list.setLayoutOrientation(JList.VERTICAL_WRAP);
6275
list.setVisibleRowCount(-1);
@@ -68,18 +81,36 @@ public void mouseClicked(MouseEvent evt) {
6881
int index = list.locationToIndex(evt.getPoint());
6982
String entryName = (String) list.getModel().getElementAt(index);
7083
String[] array = entryName.split("/");
71-
String internalName = StringUtilities.removeRight(entryName, ".class");
72-
TypeReference type = Model.metadataSystem.lookupType(internalName);
73-
try {
74-
mainWindow.getModel().extractClassToTextPane(type, array[array.length - 1], entryName, null);
75-
} catch (Exception e) {
76-
Luyten.showExceptionDialog("Exception!", e);
84+
if (entryName.toLowerCase().endsWith(".class")) {
85+
String internalName = StringUtilities.removeRight(entryName, ".class");
86+
TypeReference type = Model.metadataSystem.lookupType(internalName);
87+
try {
88+
mainWindow.getModel().extractClassToTextPane(type, array[array.length - 1], entryName,
89+
null);
90+
} catch (Exception e) {
91+
Luyten.showExceptionDialog("Exception!", e);
92+
}
93+
94+
} else {
95+
try {
96+
JarFile jfile = new JarFile(MainWindow.model.getOpenedFile());
97+
mainWindow.getModel().extractSimpleFileEntryToTextPane(
98+
jfile.getInputStream(jfile.getEntry(entryName)), array[array.length - 1],
99+
entryName);
100+
jfile.close();
101+
} catch (IOException e) {
102+
e.printStackTrace();
103+
} catch (Exception e) {
104+
e.printStackTrace();
105+
}
77106
}
78107

79108
}
80109
}
81110
});
82-
JScrollPane listScroller = new JScrollPane(list);
111+
list.setLayoutOrientation(JList.VERTICAL);
112+
JScrollPane listScroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
113+
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
83114

84115
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
85116
final Dimension center = new Dimension((int) (screenSize.width * 0.35), 500);
@@ -95,11 +126,17 @@ public void mouseClicked(MouseEvent evt) {
95126

96127
layout.setHorizontalGroup(
97128
layout.createSequentialGroup().addComponent(label)
98-
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(statusLabel)
99-
.addComponent(textField)
100-
.addGroup(layout.createSequentialGroup()
101-
.addGroup(layout.createParallelGroup(Alignment.LEADING)
102-
.addComponent(listScroller).addComponent(progressBar))))
129+
.addGroup(
130+
layout.createParallelGroup(Alignment.LEADING).addComponent(statusLabel)
131+
.addComponent(textField)
132+
.addGroup(layout.createSequentialGroup()
133+
.addGroup(layout.createParallelGroup(Alignment.LEADING)
134+
.addComponent(mcase))
135+
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(wholew))
136+
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(regex)))
137+
.addGroup(layout.createSequentialGroup()
138+
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(listScroller)
139+
.addComponent(progressBar))))
103140
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(findButton))
104141

105142
);
@@ -108,6 +145,8 @@ public void mouseClicked(MouseEvent evt) {
108145
layout.setVerticalGroup(layout.createSequentialGroup()
109146
.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(textField)
110147
.addComponent(findButton))
148+
.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(mcase).addComponent(wholew)
149+
.addComponent(regex))
111150
.addGroup(layout.createParallelGroup(Alignment.LEADING)
112151
.addGroup(layout.createSequentialGroup()
113152
.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(listScroller))))
@@ -149,6 +188,7 @@ public void run() {
149188
JarEntry entry = ent.nextElement();
150189
String name = entry.getName();
151190
setStatus(name);
191+
System.out.println(entry.getName());
152192
if (filter && name.contains("$"))
153193
continue;
154194
if (entry.getName().endsWith(".class")) {
@@ -169,11 +209,30 @@ public void run() {
169209
decompilationOptions.getSettings().isUnicodeOutputEnabled());
170210
settings.getLanguage().decompileType(resolvedType, plainTextOutput,
171211
decompilationOptions);
172-
String decompiledSource = stringwriter.toString().toLowerCase();
173-
if (decompiledSource.contains(textField.getText().toLowerCase())) {
212+
if (search(stringwriter.toString()))
174213
addClassName(entry.getName());
214+
}
215+
} else {
216+
217+
StringBuilder sb = new StringBuilder();
218+
long nonprintableCharactersCount = 0;
219+
try (InputStreamReader inputStreamReader = new InputStreamReader(
220+
jfile.getInputStream(entry));
221+
BufferedReader reader = new BufferedReader(inputStreamReader);) {
222+
String line;
223+
while ((line = reader.readLine()) != null) {
224+
sb.append(line).append("\n");
225+
226+
for (byte nextByte : line.getBytes()) {
227+
if (nextByte <= 0) {
228+
nonprintableCharactersCount++;
229+
}
230+
}
231+
175232
}
176233
}
234+
if (nonprintableCharactersCount < 5 && search(sb.toString()))
235+
addClassName(entry.getName());
177236
}
178237
}
179238
setSearching(false);
@@ -195,6 +254,22 @@ public void run() {
195254

196255
}
197256

257+
private boolean search(String bulk) {
258+
String a = textField.getText();
259+
String b = bulk;
260+
if (regex.isSelected())
261+
return Pattern.matches(a, b);
262+
if (wholew.isSelected())
263+
a = " " + a + " ";
264+
if (!mcase.isSelected()) {
265+
a = a.toLowerCase();
266+
b = b.toLowerCase();
267+
}
268+
if (b.contains(a))
269+
return true;
270+
return false;
271+
}
272+
198273
private void setHideOnEscapeButton() {
199274
Action escapeAction = new AbstractAction() {
200275
private static final long serialVersionUID = 6846566740472934801L;

src/us/deathmarine/luyten/FindBox.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public FindBox(final MainWindow mainWindow) {
6363
wholew = new JCheckBox("Whole Words");
6464
reverse = new JCheckBox("Search Backwards");
6565
wrap = new JCheckBox("Wrap");
66-
66+
6767
findButton = new JButton("Find");
6868
findButton.addActionListener(new FindButton());
6969
this.getRootPane().setDefaultButton(findButton);
70-
70+
7171
KeyStroke funcF3 = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0, false);
7272
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(funcF3, "FindNext");
7373
this.getRootPane().getActionMap().put("FindNext", new FindExploreAction(true));
74-
74+
7575
KeyStroke sfuncF3 = KeyStroke.getKeyStroke(KeyEvent.VK_F3, InputEvent.SHIFT_DOWN_MASK, false);
7676
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sfuncF3, "FindPrevious");
7777
this.getRootPane().getActionMap().put("FindPrevious", new FindExploreAction(false));
@@ -144,10 +144,10 @@ public void actionPerformed(ActionEvent event) {
144144
context.setWholeWord(wholew.isSelected());
145145

146146
if (!SearchEngine.find(pane, context).wasFound()) {
147-
if(wrap.isSelected()){
147+
if (wrap.isSelected()) {
148148
pane.setSelectionStart(0);
149149
pane.setSelectionEnd(0);
150-
}else{
150+
} else {
151151
mainWindow.getLabel().setText("Search Complete");
152152
}
153153
}
@@ -187,19 +187,22 @@ public void windowDeactivated(WindowEvent e) {
187187
}
188188
});
189189
}
190-
191-
public void fireExploreAction(boolean direction){
190+
191+
public void fireExploreAction(boolean direction) {
192192
new FindExploreAction(direction).actionPerformed(null);
193193
}
194-
class FindExploreAction extends AbstractAction{
194+
195+
class FindExploreAction extends AbstractAction {
195196
/**
196197
*
197198
*/
198199
private static final long serialVersionUID = -4391670062679240573L;
199200
boolean direction;
200-
public FindExploreAction(boolean forward){
201+
202+
public FindExploreAction(boolean forward) {
201203
direction = forward;
202204
}
205+
203206
@Override
204207
public void actionPerformed(ActionEvent e) {
205208
if (textField.getText().length() == 0)
@@ -215,15 +218,15 @@ public void actionPerformed(ActionEvent e) {
215218
context.setWholeWord(wholew.isSelected());
216219

217220
if (!SearchEngine.find(pane, context).wasFound()) {
218-
if(wrap.isSelected()){
221+
if (wrap.isSelected()) {
219222
pane.setSelectionStart(0);
220223
pane.setSelectionEnd(0);
221-
}else{
224+
} else {
222225
mainWindow.getLabel().setText("Search Complete");
223226
}
224227
}
225-
228+
226229
}
227-
230+
228231
}
229232
}

0 commit comments

Comments
 (0)