Return-Path: <env_27354915-1543012476@hermes.sun.com>
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 <alexp@mit.edu>; Tue, 17 Dec 2002 14:07:12 -0500 (EST)
Date: 17 Dec 2002 09:28:32 -0800
From: "JDC Tech Tips" <sunmail@hermes.sun.com>
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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Core Java Technologies Technical Tips
</title>

<style type="text/css">

code {color: #333333; font-family: Verdana, Courier, Monospace; font-size: 10pt}
pre {color: #333333; font-family: Verdana, Courier, Monospace; font-size: 10pt}
body, div, span {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
td, th {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
tr {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
table {font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
p {color: #333333; font-family: arial,helvetica,sans-serif; font-size: 10pt}
li {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
br {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
div {color: #666699; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
sup {font-family: Arial, Helvetica, sans-serif; font-size: 5pt}
h3 {color: #666699; font-family: Arial, Helvetica, sans-serif; font-size: 11pt}
h4, b {color: #666699; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
blockquote, input, select {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
ul, ol {color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
a:link {font-size: 10pt; font-family: Arial, Helvetica, sans-serif; color:#666699 }
span.purple {font-weight: bold; color: #666699; font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
span.small {font-size: 8pt; font-family: Arial, Helvetica, sans-serif; color:#333333 }
span.link {font-size: 8pt; font-family: Arial, Helvetica, sans-serif; color:#666699 }
</style>

</head>

<body bgcolor="#ffffff">
<a name="top"></a>
<table border="0" cellpadding="0" cellspacing="0" width="611">
<tr>
<td colspan="3" bgcolor="#cccccc" width="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" border="0" alt="image"></td>
</tr>

<tr>
<td bgcolor="#cccccc" width="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" border="0" alt="image"></td>

<td>
<table border="0" cellpadding="0" cellspacing="0" width="611">
<tr>
<td colspan="2"><a href="http://developer.java.sun.com" target="_blank"><img src="http://developer.java.sun.com/images/headers/core_ttips_hdr.jpg" width="611" height="160" alt="Core Java Technologies Technical Tips" border="0"></a></td>
</tr>

<tr>
<td colspan="2" bgcolor="#cccccc" width="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" border="0" alt="image"></td>
</tr>

<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->

<tr>
<td height="20">&nbsp;&nbsp;&nbsp;<a href="http://developer.java.sun.com/developer/TechTips/txtarchive/2002/Dec02_JohnZ.txt?s=06&w=51" target="_blank" style="text-decoration:none;" target="_blank">View this issue as simple text</a></td>
<td align="right" height="20"><span class="purple">December 17, 2002</span>&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>

<tr><td colspan="2">

<FORM METHOD="GET" ACTION="http://search.java.sun.com/search/java/" target="_blank">
<table border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<td>
        
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tr> 
<td align="left" VALIGN="middle" width="20%"><h3>In this Issue</h3></td>
              
<td valign="middle" align="right" width="70%">
<font size="2">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></td>
            
<td VALIGN="middle" WIDTH="55">
<INPUT TYPE="image" SRC="http://developer.java.sun.com/images/v4a_search.gif" ALT="Search" value="search" BORDER="0" WIDTH="55"></td>
</tr>
</table>

</FORM>

<p>
Welcome to the Core Java<sup>TM</sup> 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 (J2SE<sup>TM</sup>). 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.
</p>

<p>
These tips were developed using Java 2 SDK, Standard Edition, v 1.4. 
</p>

<p>
This issue of the Core Java Technologies Tech Tips is written by John Zukowski, president of <a href="http://www.jzventures.com" target="_blank">JZ Ventures, Inc.</a> 
</p>

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">

<!-- Grey Horizontal Line Begins Here -->

<tr>
<td bgcolor="#cccccc" height="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" ALT="Pixel" border="0"></td>
</tr>
</table> 

<!-- Grey Horizontal Line ends Here -->

<h3>PROGRAMMER CHALLENGE</h3>

<p>
The programming challenge is as follows: create a &quot;Find File&quot; 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.
</p>

<p>
Here's a more detailed set of instructions:
</p>

<ol>
<li>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:
   <br><br>
   - Offer a Browse button to assist in directory selection by 
     using a <code>JFileChooser</code>. Refer to the June 15, 1999 Tech 
     Tip titled "<a href="http://java.sun.com/jdc/TechTips/1999/tt0615.html#tip1?s=06&w=51" target="_blank">File Choosers</a>".
     You'll need to set the file selection mode to 
     <code>DIRECTORIES_ONLY</code> to limit selection to directory names.
   <br><br>
   - Offer the option of searching subdirectories.
   <br><br>
   - Place a <code>JTabbedPane</code> in the middle of the user interface to 
     display the search results.
   <br><br>
   - Offer a Submit button that triggers the searching of files 
     in the directory that have content matching the regular 
     expression.
   <br><br>
   If you coded this step correctly, your program should display 
   something like this
   <br><br>
   <img src="http://developer.java.sun.com/developer/JDCTechTips/images/tt121702_screen1.gif" alt="" border="0">
<br><br>
</li>

<li>When the user presses the Submit button (or presses the Enter
   key), the program should:
   <br><br>
   - Add a tab to the <code>JTabbedPane</code>. The tab label should be the 
    search expression. Refer to the July 12, 2001 Tech Tip titled
    "<a href="http://java.sun.com/jdc/JDCTechTips/2001/tt0712.html#tip1?s=06&w=51" target="_blank">JTabbed Pane</a>".
    Consider setting the tab layout policy to <code>SCROLL_TAB_LAYOUT</code>, 
    so all the tabs appear on one row.
	<br><br>
	- Add every file in the path (and subdirectories if appropriate) 
    to a <code>JList</code> within that tab.
	<br><br>
	- Offer a Remove button that closes a tab if a user is done with 
    the search results.
	<br><br>
	If you coded this step correctly, your program should display 
   something like this after the user presses the Submit button
   or presses Enter.
   <br><br>
	<img src="http://developer.java.sun.com/developer/JDCTechTips/images/tt121702_screen2.gif" alt="" border="0">
<br><br>
</li>

<li>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 "<a href="http://java.sun.com/jdc/JDCTechTips/2002/tt1008.html?s=06&w=51" target="_blank">Using Regular Expression 
   Groups</a>"
   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.
   <br><br>
   Consider using the NIO buffers and channels to read from the 
   file. This is explored in the June 4, 2002 Tech Tip titled
   "<a href="http://java.sun.com/jdc/JDCTechTips/2002/tt0604.html#tip2?s=06&w=51" target="_blank">Programming With Buffers</a>".
   <br><br>
   If you coded this step correctly, your program should display 
   something like this:
   <br><br>
   <img src="http://developer.java.sun.com/developer/JDCTechTips/images/tt121702_screen3.gif" alt="" border="0">
<br><br>
</li>

<li>If a user selects a particular file, display the file in a 
   <code>JTextPane</code>. Highlight the area(s) in the file that match the 
   regular expression. Refer to the August 21, 2002 Tech Tip 
   titled "<a href="http://java.sun.com/jdc/JDCTechTips/2002/tt0821.html#tip2?s=06&w=51" target="_blank">Displaying Text in Multiple Styles"</a>.
   You'll need to use the start and end indices available from 
   the <code>Matcher</code> to find the area to highlight. Remember to 
   highlight each area matched, not just the first. Also, instead 
   of inserting a string, use the <code>setCharacterAttributes</code> method 
   of <code>DefaultStyledDocument</code> to change the attributes of the 
   matched area.  
   <br><br>
   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 <code>JTextPane</code>. 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 <code>javax.swing.text.DefaultEditorKit</code>, in the 
   setting for <code>EndOfLineStringProperty</code>. By setting this to 
   always be \n, the two positions will match accordingly. For
   more information about this, see the javadoc for 
   <code>DefaultEditorKit</code>. In the meantime, set the property to \n when 
   it is two characters for this to work:
   <br>
   <pre>
          document.putProperty(
             DefaultEditorKit.EndOfLineStringProperty, "\n");
   </pre>
   This means that when you <code>getText</code> 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.
   <br><br>
   If you coded this step correctly, your program should display 
   something like this:
   <br><br>
   <img src="http://developer.java.sun.com/developer/JDCTechTips/images/tt121702_screen4.gif" alt="" border="0">
<br><br>
</li>

<li>As a final task, use the Logging API to log each search 
   performed. Refer to the October 22, 2002 Tech Tip titled
   "<a href="http://java.sun.com/jdc/JDCTechTips/2002/tt1022.html#1?s=06&w=51" target="_blank">Filtering Logged Messages</a>". You 
   won't need to filter any messages, but the framework for 
   logging can be found in that tip.
   <br><br>
   Depending on the information you log, you should see text 
   that looks something like this:
   <br>
   <pre>
       Dec 17, 2002 1:31:59 PM 
           project.Search submitButton_actionPerformed
       INFO: Pattern: 
           John\sZukowski / Directory: /home/eo86671/tech_tips
  </pre>
You can find one possible solution to the challenge at:
<a href="http://java.sun.com/jdc/JDCTechTips/2002/tts1217.txt?s=06&w=51" target="_blank">http://java.sun.com/jdc/JDCTechTips/2002/tts1217.txt</a>.
<br><br>
If you're looking for more of a challenge, here are a few more 
things you can try:<br><br>
</li>

<li>Provide support for case-insensitive search.<br><br></li>

<li>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.<br><br></li>

<li>Allow the user to search by size, that is, either a maximum or 
   minimum size.<br><br></li>

<li>Allow the user to search only through files that match a 
   certain naming pattern, such as &quot;<code>*.java</code>&quot; files.<br><br></li>

<li>Consider changing the program to search a web site. Have the 
   user enter a base URL and &quot;spider&quot; the site (that is, follow 
   the links) until the program finds the desired text pattern.<br><br></li>
</ol>

<p>
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.
</p>

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<div align="right">
<a href="#top">back to top<img src="http://developer.java.sun.com/images/back_to_top.gif" border="0" alt="image"></a>
</div>
</td></tr>

<tr><td bgcolor="#ffffff" height="2"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" ALT="Pixel" border="0"></td></tr>

<!-- Grey Horizontal Line Begins Here -->

<tr>
<td bgcolor="#cccccc" height="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" ALT="Pixel" border="0"></td>
</tr>
</table> 

<!-- Grey Horizontal Line ends Here -->

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td bgcolor="#ffffff" height="2"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" ALT="Pixel" border="0"></td></tr>

<h3>Reader Feedback</h3> 
<FORM METHOD="POST" ACTION="http://developer.java.sun.com/servlet/jdc.survey.TabulationServlet">
<INPUT TYPE="hidden" NAME="survey_id" VALUE="2600">
<INPUT TYPE="hidden" NAME="anonymous" VALUE="True">
<INPUT TYPE="hidden" NAME="answer_count" VALUE="3">
<INPUT TYPE="hidden" NAME="answer1" VALUE="DecCore_tt1217"> 
<INPUT NAME="answer2" TYPE="RADIO" VALUE="2">&nbsp; Very worth reading&nbsp; 
<INPUT NAME="answer2" TYPE="RADIO" VALUE="1">&nbsp; Worth reading&nbsp; 
<INPUT NAME="answer2" TYPE="RADIO" VALUE="0">&nbsp; Not worth reading&nbsp; 
 
<p>
If you have other comments or ideas for future technical tips, please type them  here: 
</p>
 
<p>
<TEXTAREA NAME="answer3" ROWS="6" COLS="50"></TEXTAREA> 
</p>
 
<p>
<INPUT TYPE="SUBMIT" VALUE="Submit"> &nbsp; <INPUT TYPE="RESET"> 
</p>

<p> 
Have a question about Java<font size="-2"><sup>TM</sup></font> programming? Use  
<a href="http://developer.java.sun.com/developer/support/?s=06&w=51" target="_blank">Java Online Support.</a> 
</p>

</td></tr></table>
</FORM> 

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<div align="right">
<a href="#top">back to top<img src="http://developer.java.sun.com/images/back_to_top.gif" border="0" alt="image"></a>
</div>
</td></tr>

<tr><td bgcolor="#ffffff" height="2"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" ALT="Pixel" border="0"></td></tr>

<!-- Grey Horizontal Line Begins Here -->

<tr>
<td bgcolor="#cccccc" height="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" ALT="Pixel" border="0"></td>
</tr>
</table> 

<!-- Grey Horizontal Line ends Here -->

<br>

<span class="small">IMPORTANT: Please read our Terms of Use, Privacy, and Licensing 
policies:<br>
<a href="http://www.sun.com/share/text/termsofuse.html?s=06&w=51" target="_blank"><span class="link">http://www.sun.com/share/text/termsofuse.html</span></a><br>
<a href="http://www.sun.com/privacy/?s=06&w=51" target="_blank"><span class="link">http://www.sun.com/privacy/</span></a><br> 
<a href="http://developer.java.sun.com/berkeley_license.html?s=06&w=51" target="_blank"><span class="link">http://developer.java.sun.com/berkeley_license.html</span></a>
</span><br><br>


<span class="small">
Comments? Send your feedback on the Core Java<sup>TM</sup> Technologies Tech Tips to: <a href="mailto:jdc-webmaster@sun.com"><span class="link">jdc-webmaster@sun.com</span></a>
</span><br><br>

<span class="small">
Subscribe to other Java developer Tech Tips:
</span><br><br>

<span class="small">
- Enterprise Java Technologies Tech Tips. Get tips on using enterprise Java technologies and APIs, such as those in the Java 2 Platform, Enterprise Edition (J2EE<sup>TM</sup>).<br>
- Wireless Developer Tech Tips. Get tips on using wireless Java technologies and APIs, such as those in the Java 2 Platform, Micro Edition (J2ME<sup>TM</sup>).<br><br>
</span>
   
<span class="small">
To subscribe to these and other JDC publications:<br>
- Go to the JDC Newsletters and Publications page, choose the newsletters you want to <a href="http://developer.java.sun.com/subscription/?s=06&w=51" target="_blank"><span class="link">subscribe</span></a> to and click &quot;Update&quot;.<br>
  - To unsubscribe, go to the <a href="http://developer.java.sun.com/subscription/?s=06&w=51" target="_blank"><span class="link">subscriptions page</span></a>, uncheck the appropriate checkbox, and click &quot;Update&quot;.
</span><br><br>

<span class="small">ARCHIVES: You'll find the Core Java Technologies Tech Tips archives at:<br>
<a href="http://java.sun.com/jdc/TechTips/?s=06&w=51" target="_blank"><span class="link">http://java.sun.com/jdc/TechTips/index.html</span></a>
</span><br><br>

<span class="small">Copyright 2002 <a href="http://www.sun.com?s=06&w=51" target="_blank"><span class="link">Sun Microsystems, Inc.</span></a> All rights reserved.
<br>901 San Antonio Road, Palo Alto, California 94303 USA.
</span><br><br>

<span class="small">This document is protected by copyright. For more information, see:
<br><a href="http://java.sun.com/jdc/copyright.html?s=06&w=51" target="_blank"><span class="link">http://java.sun.com/jdc/copyright.html</span></a>
</span><br><br>

<span class="small">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.</span><br><br>


<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

<center>
<a href="http://www.sun.com" target="_blank"><img src="http://developer.java.sun.com/images/lgsun.gif" border="0" alt="Sun Microsystems, Inc."></a>
</center>

</td>
</tr>
</table>

</td></tr>
</table>
</td>

<td bgcolor="#cccccc" width="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" border="0" alt=""></td>
</tr>
<tr>
<td colspan="3" bgcolor="#cccccc" width="1"><img src="http://developer.java.sun.com/images/pixel.gif" width="1" height="1" border="0" alt=""></td>
</tr>
</table>

<table bgcolor = "#efefef"><tr><td><a href="http://bulkmail.sun.com/servlet/PreferenceServlet?action=change&pref_name=content-type&pref_value=text&id=27354915-1543012476">Please send me newsletters in text.</a><br><a href="http://bulkmail.sun.com/unsubscribe?27354915-1543012476">Please unsubscribe me from this newsletter.</a><img src="http://bulkmail2.sun.com/OTServlet?id=27354915-1543012476" width=1 height=1></td></tr></table></body>
</html>

