Return-Path: Received: from fort-point-station.mit.edu by po10.mit.edu (8.9.2/4.7) id OAA19934; Tue, 17 Dec 2002 14:07:12 -0500 (EST) Received: from hermes.sun.com (hermes.sun.com [64.124.140.169]) by fort-point-station.mit.edu (8.9.2/8.9.2) with SMTP id OAA22146 for ; Tue, 17 Dec 2002 14:07:12 -0500 (EST) Date: 17 Dec 2002 09:28:32 -0800 From: "JDC Tech Tips" To: alexp@mit.edu Message-Id: <27354915-1543012476@hermes.sun.com> Subject: Core Java Technologies Tech Tips , Dec. 17, 2002 (Programmer Challenge) Mime-Version: 1.0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Mailer: SunMail 1.0 Core Java Technologies Technical Tips
image
image
Core Java Technologies Technical Tips
image
   View this issue as simple text December 17, 2002    

In this Issue

Welcome to the Core JavaTM Technologies Tech Tips, December 17, 2002. Here you'll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SETM). This issue of the Tech Tips is the last of the year. To close the year out, this issue presents a programmer challenge. The challenge tests your ability to use some features covered in past Tech Tips.

These tips were developed using Java 2 SDK, Standard Edition, v 1.4.

This issue of the Core Java Technologies Tech Tips is written by John Zukowski, president of JZ Ventures, Inc.

Pixel

PROGRAMMER CHALLENGE

The programming challenge is as follows: create a "Find File" program that searches for files in a user-specified directory, and whose content fits a user-specified search pattern. The program should display the matching set of files for each search query. The user should be able to select a file in the list of files that satisfy the search criteria. If a file is selected, the program should display its contents, and highlight areas of the content that match the search pattern. In addition, the program should log all search queries.

Here's a more detailed set of instructions:

  1. Create the primary user interface for the application. The interface should have an input field for the directory and an input field for the search pattern (that is, a regular expression). In addition:

    - Offer a Browse button to assist in directory selection by using a JFileChooser. Refer to the June 15, 1999 Tech Tip titled "File Choosers". You'll need to set the file selection mode to DIRECTORIES_ONLY to limit selection to directory names.

    - Offer the option of searching subdirectories.

    - Place a JTabbedPane in the middle of the user interface to display the search results.

    - Offer a Submit button that triggers the searching of files in the directory that have content matching the regular expression.

    If you coded this step correctly, your program should display something like this



  2. When the user presses the Submit button (or presses the Enter key), the program should:

    - Add a tab to the JTabbedPane. The tab label should be the search expression. Refer to the July 12, 2001 Tech Tip titled "JTabbed Pane". Consider setting the tab layout policy to SCROLL_TAB_LAYOUT, so all the tabs appear on one row.

    - Add every file in the path (and subdirectories if appropriate) to a JList within that tab.

    - Offer a Remove button that closes a tab if a user is done with the search results.

    If you coded this step correctly, your program should display something like this after the user presses the Submit button or presses Enter.



  3. Add code that checks to see if any file contains the regular expression entered into the Expression field. Refer to the October 8, 2002 Tech Tip titled "Using Regular Expression Groups" describes how to use regular expression groups. You'll need to change the examples in the tip to look in a file. After the pattern is found anywhere in the file, the program can stop reading the rest of the file.

    Consider using the NIO buffers and channels to read from the file. This is explored in the June 4, 2002 Tech Tip titled "Programming With Buffers".

    If you coded this step correctly, your program should display something like this:



  4. If a user selects a particular file, display the file in a JTextPane. Highlight the area(s) in the file that match the regular expression. Refer to the August 21, 2002 Tech Tip titled "Displaying Text in Multiple Styles". You'll need to use the start and end indices available from the Matcher to find the area to highlight. Remember to highlight each area matched, not just the first. Also, instead of inserting a string, use the setCharacterAttributes method of DefaultStyledDocument to change the attributes of the matched area.

    There is one extra thing you'll need to add to your program that hasn't been covered in previous Tech Tips. Your program will need to handle differences in the line separator characters across platforms. On disk, the line separator characters are whatever is in the file. Typically, \n for Unix (Solaris/Linux), \r\n for Windows, and \r for the Mac. The regular expression library deals with these accordingly. The problem occurs when you try to map file positions to the position of the text in the JTextPane. In memory, no matter what platform, the end-of-line indicator is always \n. That means that file positions won't map to in-memory positions, without a little bit of extra work. That little bit of extra work is defined in javax.swing.text.DefaultEditorKit, in the setting for EndOfLineStringProperty. By setting this to always be \n, the two positions will match accordingly. For more information about this, see the javadoc for DefaultEditorKit. In the meantime, set the property to \n when it is two characters for this to work:
              document.putProperty(
                 DefaultEditorKit.EndOfLineStringProperty, "\n");
       
    This means that when you getText from the component, it will have only a \n, not the platform-specific line. You can find positions there, and use those same positions when you set the character attributes for the component.

    If you coded this step correctly, your program should display something like this:



  5. As a final task, use the Logging API to log each search performed. Refer to the October 22, 2002 Tech Tip titled "Filtering Logged Messages". You won't need to filter any messages, but the framework for logging can be found in that tip.

    Depending on the information you log, you should see text that looks something like this:
           Dec 17, 2002 1:31:59 PM 
               project.Search submitButton_actionPerformed
           INFO: Pattern: 
               John\sZukowski / Directory: /home/eo86671/tech_tips
      
    You can find one possible solution to the challenge at: http://java.sun.com/jdc/JDCTechTips/2002/tts1217.txt.

    If you're looking for more of a challenge, here are a few more things you can try:

  6. Provide support for case-insensitive search.

  7. Allow the user to search by date, such as files newer than five days old, or files created between 1 May 2002 and 4 May 2002.

  8. Allow the user to search by size, that is, either a maximum or minimum size.

  9. Allow the user to search only through files that match a certain naming pattern, such as "*.java" files.

  10. Consider changing the program to search a web site. Have the user enter a base URL and "spider" the site (that is, follow the links) until the program finds the desired text pattern.

Solutions for these added steps are left as an exercise to the reader. Search through the tips to find even more features that you can add to the program.

Pixel
Pixel

Reader Feedback

  Very worth reading    Worth reading    Not worth reading 

If you have other comments or ideas for future technical tips, please type them here:

 

Have a question about JavaTM programming? Use Java Online Support.

Pixel
Pixel
Pixel

IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developer.java.sun.com/berkeley_license.html


Comments? Send your feedback on the Core JavaTM Technologies Tech Tips to: jdc-webmaster@sun.com

Subscribe to other Java developer Tech Tips:

- Enterprise Java Technologies Tech Tips. Get tips on using enterprise Java technologies and APIs, such as those in the Java 2 Platform, Enterprise Edition (J2EETM).
- Wireless Developer Tech Tips. Get tips on using wireless Java technologies and APIs, such as those in the Java 2 Platform, Micro Edition (J2METM).

To subscribe to these and other JDC publications:
- Go to the JDC Newsletters and Publications page, choose the newsletters you want to subscribe to and click "Update".
- To unsubscribe, go to the subscriptions page, uncheck the appropriate checkbox, and click "Update".


ARCHIVES: You'll find the Core Java Technologies Tech Tips archives at:
http://java.sun.com/jdc/TechTips/index.html


Copyright 2002 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.


This document is protected by copyright. For more information, see:
http://java.sun.com/jdc/copyright.html


Sun, Sun Microsystems, Java, Java Developer Connection, J2SE, J2EE, and J2ME are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

Sun Microsystems, Inc.
Please send me newsletters in text.
Please unsubscribe me from this newsletter.