-
Notifications
You must be signed in to change notification settings - Fork 0
/
JremoveBOM.java
242 lines (211 loc) · 7.9 KB
/
JremoveBOM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
public class JremoveBOM extends JFrame implements ActionListener {
JMenuBar mb;
JMenu mFile;
JMenu jmnuHelp;
JMenuItem mOpen;
JMenuItem mExit;
JMenuItem mnuiHelpTopics;
JMenuItem mnuiAbout;
JFileChooser fc;
JScrollPane scrollPaneEditor;
BasicUtils basicUtils;
MediaTracker tracker;
String stringDocsDir;
String fullStatString;
private JEditorPane editorpane;
private URL helpURL;
public JremoveBOM(String title, String[] bomFiles) {
super(title);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println(e.toString());
}
basicUtils = new BasicUtils();
fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
stringDocsDir = basicUtils.getWorkingDir() + "/docs/";
fullStatString = "JremoveBOM loaded with args[]: " + Arrays.toString(bomFiles) + "<br>";
this.setEnabled(true);
tracker = new MediaTracker(this);
Image iconHelpImg = Toolkit.getDefaultToolkit().getImage(basicUtils.getUfile("docs/JremoveBOM.gif"));
tracker.addImage(iconHelpImg, 1);
try {
tracker.waitForAll();
} catch (Exception interruptedexception) {
System.out.println(interruptedexception.toString());
}
setIconImage(iconHelpImg);
mb = new JMenuBar();
// mb.setBorderPainted(true);
mFile = new JMenu("File");
mFile.getPopupMenu().setLightWeightPopupEnabled(false);
mOpen = new JMenuItem("Open");
mExit = new JMenuItem("Exit");
mFile.add(mOpen);
mFile.addSeparator();
mFile.add(mExit);
jmnuHelp = new JMenu("Help");
jmnuHelp.getPopupMenu().setLightWeightPopupEnabled(false);
jmnuHelp.setForeground(new Color(0, 0, 255)); //XXX windows lnf?
mnuiAbout = new JMenuItem("About");
mnuiHelpTopics = new JMenuItem("Help", new ImageIcon(basicUtils.getUfile("cbox/images/iconHelp.gif")));
jmnuHelp.add(mnuiHelpTopics);
jmnuHelp.addSeparator();
jmnuHelp.add(mnuiAbout);
mb.add(mFile);
mb.add(jmnuHelp);
mOpen.addActionListener(this);
mnuiHelpTopics.addActionListener(this);
mnuiAbout.addActionListener(this);
mExit.addActionListener(this);
editorpane = new JEditorPane();
scrollPaneEditor = new JScrollPane(editorpane);
editorpane.setEditable(false);
HTMLEditorKit kit = (HTMLEditorKit) editorpane.getEditorKitForContentType("text/html");
editorpane.setEditorKit(kit);
//anonymous inner listener
editorpane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent ev) {
try {
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("URL getSourceElement: " + ev.getSourceElement());
System.out.println("URL: getDescription" + ev.getDescription());
doHEventUrlAction(ev.getDescription());
}
} catch (Exception ex) {
//put message in window
ex.printStackTrace();
}
}
});
JPanel panelHWmain = new JPanel(new BorderLayout());
panelHWmain.add("Center", scrollPaneEditor);
getContentPane().add(panelHWmain, BorderLayout.CENTER);
addButtons();
// no need for close listener just dispose
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// dynamically set location
calculateLocation();
setVisible(true);
requestFocus();
try {
for (String bomFile : bomFiles) {
editorpane.setText(getTemplate(bomFile));
}
} catch (Exception ex) {
editorpane.setText(ex.toString());
}
setJMenuBar(mb);
}
public static void main(String[] args) {
try {
new JremoveBOM("JremoveBOM", args);
} catch (Exception e) {
String[] gg = new String[2];
gg[0] = "error";
gg[1] = e.toString();
gg[2] = Arrays.toString(args);
new JremoveBOM("JremoveBOM", gg);
}
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
try {
if (obj == mOpen) {
int returnVal = fc.showOpenDialog(JremoveBOM.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File[] file = fc.getSelectedFiles();
for (File aFile : file) {
editorpane.setText(getTemplate(aFile.toString()));
}
} else {
// canceled
}
}
if (obj == mnuiHelpTopics) {
new HelpWindow("JremoveBOM Help", "index_body.html");
}
if (obj == mnuiAbout) {
new AboutDialog(this, "JremoveBOM");
}
if (obj == mExit) {
System.exit(0);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void addButtons() {
JButton btncontents = new JButton("Contents");
btncontents.addActionListener(this);
JButton btnclose = new JButton("Close");
btnclose.addActionListener(this);
JPanel panebuttons = new JPanel();
getContentPane().add(panebuttons, BorderLayout.SOUTH);
}
/**
* locate in middle of screen
*/
private void calculateLocation() {
Dimension screendim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(730, 300);
int locationx = (screendim.width - (screendim.width - 100)) / 2;
int locationy = (screendim.height - (screendim.height - 100)) / 2;
setLocation(locationx, locationy);
}
public String getTemplate(String s) {
String cleanTplateString;
try {
File file = new File(s);
FileInputStream fis = new FileInputStream(file);
long size = file.length();
byte[] b = new byte[(int) size];
int bytesRead = fis.read(b, 0, (int) size);
if (bytesRead != size) {
throw new IOException("cannot read file");
} else {
fis.close();
}
int b0 = b[0] & 0xff;
int b1 = b[1] & 0xff;
int b2 = b[2] & 0xff;
if (b0 == 0xef && b1 == 0xbb && b2 == 0xbf) {
System.out.println("Hint: the file starts with a UTF-8 BOM.");
cleanTplateString = new String(b, 3, b.length - 3, "UTF8");
fullStatString += "<b>BOM found in</b>: " + s + "<br>";
basicUtils.saveTextString(cleanTplateString, s);
} else {
fullStatString += "no BOM found in: " + s + "<br>";
}
} catch (Exception ex) {
fullStatString += ex.toString();
}
return fullStatString;
}
public void doHEventUrlAction(String urlString) {
if (urlString.startsWith("http")) {
try {
BrowserControl.displayURL(urlString);
} catch (Exception ex) {
editorpane.setText(ex.toString());
}
} else {
editorpane.setText(getTemplate(basicUtils.getUfile("docs/" + urlString)));
editorpane.repaint();
editorpane.setCaretPosition(0);
}
}
}