2 **************************************************************************
\r
3 * Copyright (C) 2005-2007, International Business Machines Corporation *
\r
4 * and others. All Rights Reserved. *
\r
5 **************************************************************************
\r
9 package com.ibm.icu.dev.demo.charsetdet;
\r
11 import java.awt.event.*;
\r
14 import java.net.URL;
\r
15 import java.nio.ByteBuffer;
\r
16 import java.nio.charset.Charset;
\r
17 import java.security.AccessControlException;
\r
19 import javax.swing.*;
\r
21 import com.ibm.icu.charset.CharsetICU;
\r
22 import com.ibm.icu.dev.demo.impl.DemoApplet;
\r
23 import com.ibm.icu.text.CharsetDetector;
\r
24 import com.ibm.icu.text.CharsetMatch;
\r
27 * This simple application demonstrates how to use the CharsetDetector API. It
\r
28 * opens a file or web page, detects the encoding, and then displays it using that
\r
31 public class DetectingViewer extends JFrame implements ActionListener
\r
37 private static final long serialVersionUID = -2307065724464747775L;
\r
38 private JTextPane text;
\r
39 private JFileChooser fileChooser;
\r
42 * @throws java.awt.HeadlessException
\r
44 public DetectingViewer()
\r
47 DemoApplet.demoFrameOpened();
\r
50 fileChooser = new JFileChooser();
\r
51 } catch (AccessControlException ace) {
\r
52 System.err.println("no file chooser - access control exception. Continuing without file browsing. "+ace.toString());
\r
53 fileChooser = null; //
\r
56 // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
\r
59 setJMenuBar(makeMenus());
\r
60 text = new JTextPane();
\r
61 text.setContentType("text/plain");
\r
63 text.setSize(800, 800);
\r
65 Font font = new Font("Arial Unicode MS", Font.PLAIN, 24);
\r
68 JScrollPane scrollPane = new JScrollPane(text);
\r
70 getContentPane().add(scrollPane);
\r
74 new WindowAdapter() {
\r
75 public void windowClosing(WindowEvent e) {
\r
76 // setVisible(false);
\r
86 public void actionPerformed(ActionEvent event)
\r
88 String cmd = event.getActionCommand();
\r
90 if (cmd.equals("New...")) {
\r
92 } else if (cmd.equals("Open File...")) {
\r
94 } else if (cmd.equals("Open URL...")) {
\r
96 } else if (cmd.equals("Quit")) {
\r
101 public static void main(String[] args)
\r
103 new DetectingViewer();
\r
106 private void errorDialog(String title, String msg)
\r
108 JOptionPane.showMessageDialog(this, msg, title, JOptionPane.ERROR_MESSAGE);
\r
111 private BufferedInputStream openFile(File file)
\r
113 FileInputStream fileStream = null;
\r
116 fileStream = new FileInputStream(file);
\r
117 } catch (Exception e) {
\r
118 errorDialog("Error Opening File", e.getMessage());
\r
122 return new BufferedInputStream(fileStream);
\r
125 // private void openFile(String directory, String filename)
\r
127 // openFile(new File(directory, filename));
\r
131 private BufferedInputStream openURL(String url)
\r
133 InputStream s = null;
\r
136 URL aURL = new URL(url);
\r
137 s = aURL.openStream();
\r
138 } catch (Exception e) {
\r
139 errorDialog("Error Opening URL", e.getMessage());
\r
143 return new BufferedInputStream(s);
\r
146 private String encodingName(CharsetMatch match)
\r
148 return match.getName() + " (" + match.getLanguage() + ")";
\r
151 private void setMatchMenu(CharsetMatch[] matches)
\r
153 JMenu menu = getJMenuBar().getMenu(1);
\r
154 JMenuItem menuItem;
\r
158 for (int i = 0; i < matches.length; i += 1) {
\r
159 CharsetMatch match = matches[i];
\r
161 menuItem = new JMenuItem(encodingName(match) + " " + match.getConfidence());
\r
163 menu.add(menuItem);
\r
167 private byte[] scriptTag = {(byte) 's', (byte) 'c', (byte) 'r', (byte) 'i', (byte) 'p', (byte) 't'};
\r
168 private byte[] styleTag = {(byte) 's', (byte) 't', (byte) 'y', (byte) 'l', (byte) 'e'};
\r
169 private static int BUFFER_SIZE = 100000;
\r
171 private boolean openTag(byte[] buffer, int offset, int length, byte[] tag)
\r
173 int tagLen = tag.length;
\r
174 int bufRem = length - offset;
\r
177 for (b = 0; b < tagLen && b < bufRem; b += 1) {
\r
178 if (buffer[b + offset] != tag[b]) {
\r
183 return b == tagLen;
\r
186 private boolean closedTag(byte[] buffer, int offset, int length, byte[] tag)
\r
188 if (buffer[offset] != (byte) '/') {
\r
192 return openTag(buffer, offset + 1, length, tag);
\r
195 private byte[] filter(InputStream in)
\r
197 byte[] buffer = new byte[BUFFER_SIZE];
\r
198 int bytesRemaining = BUFFER_SIZE;
\r
201 in.mark(BUFFER_SIZE);
\r
204 while (bytesRemaining > 0) {
\r
205 int bytesRead = in.read(buffer, bufLen, bytesRemaining);
\r
207 if (bytesRead <= 0) {
\r
211 bufLen += bytesRead;
\r
212 bytesRemaining -= bytesRead;
\r
214 } catch (Exception e) {
\r
215 // TODO: error handling?
\r
219 boolean inTag = false;
\r
220 boolean skip = false;
\r
223 for (int i = 0; i < bufLen; i += 1) {
\r
224 byte b = buffer[i];
\r
226 if (b == (byte) '<') {
\r
229 if (openTag(buffer, i + 1, bufLen, scriptTag) ||
\r
230 openTag(buffer, i + 1, bufLen, styleTag)) {
\r
232 } else if (closedTag(buffer, i + 1, bufLen, scriptTag) ||
\r
233 closedTag(buffer, i + 1, bufLen, styleTag)) {
\r
236 } else if (b == (byte) '>') {
\r
238 } else if (! (inTag || skip)) {
\r
243 byte[] filtered = new byte[out];
\r
245 System.arraycopy(buffer, 0, filtered, 0, out);
\r
249 private CharsetMatch[] detect(byte[] bytes)
\r
251 CharsetDetector det = new CharsetDetector();
\r
253 det.setText(bytes);
\r
255 return det.detectAll();
\r
258 private CharsetMatch[] detect(BufferedInputStream inputStream)
\r
260 CharsetDetector det = new CharsetDetector();
\r
263 det.setText(inputStream);
\r
265 return det.detectAll();
\r
266 } catch (Exception e) {
\r
267 // TODO: error message?
\r
272 private void show(InputStream inputStream, CharsetMatch[] matches, String title)
\r
274 InputStreamReader isr;
\r
275 char[] buffer = new char[1024];
\r
278 if (matches == null || matches.length == 0) {
\r
279 errorDialog("Match Error", "No matches!");
\r
284 StringBuffer sb = new StringBuffer();
\r
285 String encoding = matches[0].getName();
\r
287 inputStream.reset();
\r
289 if (encoding.startsWith("UTF-32")) {
\r
290 byte[] bytes = new byte[1024];
\r
293 Charset utf32 = CharsetICU.forNameICU(encoding);
\r
295 while ((bytesRead = inputStream.read(bytes, offset, 1024)) >= 0) {
\r
296 offset = bytesRead % 4;
\r
297 chBytes = bytesRead - offset;
\r
299 sb.append(utf32.decode(ByteBuffer.wrap(bytes)).toString());
\r
302 for (int i = 0; i < offset; i += 1) {
\r
303 bytes[i] = bytes[chBytes + i];
\r
308 isr = new InputStreamReader(inputStream, encoding);
\r
310 while ((bytesRead = isr.read(buffer, 0, 1024)) >= 0) {
\r
311 sb.append(buffer, 0, bytesRead);
\r
317 this.setTitle(title + " - " + encodingName(matches[0]));
\r
319 setMatchMenu(matches);
\r
320 text.setText(sb.toString());
\r
321 } catch (IOException e) {
\r
322 errorDialog("IO Error", e.getMessage());
\r
323 } catch (Exception e) {
\r
324 errorDialog("Internal Error", e.getMessage());
\r
328 private void doNew()
\r
330 // open a new window...
\r
333 private void doOpenFile()
\r
335 int retVal = fileChooser.showOpenDialog(this);
\r
337 if (retVal == JFileChooser.APPROVE_OPTION) {
\r
338 File file = fileChooser.getSelectedFile();
\r
339 BufferedInputStream inputStream = openFile(file);
\r
341 if (inputStream != null) {
\r
342 CharsetMatch[] matches = detect(inputStream);
\r
344 show(inputStream, matches, file.getName());
\r
349 private void doOpenURL()
\r
351 String url = (String) JOptionPane.showInputDialog(this, "URL to open:", "Open URL", JOptionPane.PLAIN_MESSAGE,
\r
354 if (url != null && url.length() > 0) {
\r
355 BufferedInputStream inputStream = openURL(url);
\r
357 if (inputStream != null) {
\r
358 byte[] filtered = filter(inputStream);
\r
359 CharsetMatch[] matches = detect(filtered);
\r
361 show(inputStream, matches, url);
\r
366 private void doQuit()
\r
368 DemoApplet.demoFrameClosed();
\r
369 this.setVisible(false);
\r
373 private JMenuBar makeMenus()
\r
375 JMenu menu = new JMenu("File");
\r
378 mi = new JMenuItem("Open File...");
\r
379 mi.setAccelerator((KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)));
\r
380 mi.addActionListener(this);
\r
382 if(fileChooser == null) {
\r
383 mi.setEnabled(false); // no file chooser.
\r
386 mi = new JMenuItem("Open URL...");
\r
387 mi.setAccelerator((KeyStroke.getKeyStroke(KeyEvent.VK_U, ActionEvent.CTRL_MASK)));
\r
388 mi.addActionListener(this);
\r
391 mi = new JMenuItem("Quit");
\r
392 mi.setAccelerator((KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK)));
\r
393 mi.addActionListener(this);
\r
396 JMenuBar mbar = new JMenuBar();
\r
399 menu = new JMenu("Detected Encodings");
\r