You are previewing Jakarta Commons Cookbook.
O'Reilly logo
Jakarta Commons Cookbook

Book Description

As a Java developer, you're always looking for tools to help in your web and application development. Maybe you've heard of the Jakarta Commons open source Java tools. The Jakarta Commons is an open source Jakarta subproject where developers create and maintain a collection of twenty-plus independent Java components and utilities that serve useful purposes. Jakarta Commons packages include utilities for web, XML, networking, building and testing applications, and some that help other packages work better together. Jakarta Commons packages are designed to be reusable. Each one is a time saver by itself, and when used in combination the results can be very powerful. But in searching for information on the Jakarta Commons tools, you find it hard to locate documentation relevant to your needs. You don't have lots of time to spend searching for information on a specific Jakarta Commons tool to determine what it does and how to incorporate it in your code. If you are looking for a single source for clear information and samples on how to use the Jakarta Commons tools, then the Jakarta Commons Cookbook is for you. The Jakarta Commons Cookbook summarizes each of the available Jakarta Commons packages and contains practical and efficient recipes for making the most out of the Jakarta Commons open source Java tools. You don't have to be an expert, since the book explains how and why to use a utility, pitfalls to avoid, and where to look for additional information on Jakarta Commons utilities. It introduces design possibilities and explores combining Jakarta Commons utilities in novel assemblies to create complex applications. The book offers detailed code samples and insider tips--making it a valuable resource whether you are an expert Java developer or a novice. If you want to quickly learn how to use Jakarta Commons timing-saving utilities or have an invaluable resource for Jakarta Commons questions and techniques, then the Jakarta Commons Cookbook is for you.

Table of Contents

  1. Jakarta Commons Cookbook
    1. Preface
      1. What’s Inside
      2. Conventions Used in This Book
      3. What You’ll Need
      4. The Jakarta Commons Community
      5. The Apache Software License
      6. Using Code Examples
      7. We’d Like to Hear from You
      8. Acknowledgments
    2. 1. Supplements to the Java 2 Platform
      1. Introduction
      2. 1.1. Obtaining Commons Lang
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 1.2. Joining the Commons-User Mailing List
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 1.3. Getting the Commons Lang Source Code
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 1.4. Automating the Generation of toString( ) Content
        1. Problem
        2. Solution
        3. Discussion
      6. 1.5. Customizing Generated toString( ) Content
        1. Problem
        2. Solution
        3. Discussion
      7. 1.6. Automating hashCode( ) and equals( )
        1. Problem
        2. Solution
        3. Discussion
      8. 1.7. Automating compareTo( )
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 1.8. Printing an Array
        1. Problem
        2. Solution
        3. Discussion
      10. 1.9. Cloning and Reversing Arrays
        1. Problem
        2. Solution
        3. Discussion
      11. 1.10. Transforming Between Object Arraysand Primitive Arrays
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 1.11. Finding Items in an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 1.12. Creating a Map from a Multidimensional Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 1.13. Formatting Dates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 1.14. Rounding Date Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 1.15. Truncating Date Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 1.16. Creating an Enum
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 1.17. Generating Unique Numeric Identifiers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 1.18. Validation of Method Parameters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 1.19. Measuring Time
        1. Problem
        2. Solution
        3. Discussion
    3. 2. Manipulating Text
      1. Introduction
      2. 2.1. Setting Up StringUtils and WordUtils
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 2.2. Checking for an Empty String
        1. Problem
        2. Solution
        3. Discussion
      4. 2.3. Abbreviating Strings
        1. Problem
        2. Solution
        3. Discussion
      5. 2.4. Splitting a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 2.5. Finding Nested Strings
        1. Problem
        2. Solution
        3. Discussion
      7. 2.6. Stripping and Trimming a String
        1. Problem
        2. Solution
        3. Discussion
          1. Stripping a string
          2. Trimming to null
      8. 2.7. Chomping a String
        1. Problem
        2. Solution
      9. 2.8. Creating an Emphasized Header
        1. Problem
        2. Solution
        3. Discussion
      10. 2.9. Reversing a String
        1. Problem
        2. Solution
        3. Discussion
      11. 2.10. Wrapping Words
        1. Problem
        2. Solution
        3. Discussion
      12. 2.11. Testing the Contents of a String
        1. Problem
        2. Solution
        3. Discussion
      13. 2.12. Measuring the Frequency of a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 2.13. Parsing Formatted Strings
        1. Problem
        2. Solution
        3. Discussion
      15. 2.14. Calculating String Difference
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 2.15. Using Commons Codec
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 2.16. Getting the Commons Codec Source Code
        1. Problem
        2. Solution
        3. See Also
      18. 2.17. Calculating Soundex
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    4. 3. JavaBeans
      1. Introduction
      2. 3.1. Representing Beans Graphically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 3.2. Obtaining Commons BeanUtils
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 3.3. Getting the Commons BeanUtils Source Code
        1. Problem
        2. Solution
        3. See Also
      5. 3.4. Accessing Simple Bean Properties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 3.5. Accessing Nested Bean Properties
        1. Problem
        2. Solution
        3. Discussion
      7. 3.6. Accessing Indexed Bean Properties
        1. Problem
        2. Solution
        3. Discussion
      8. 3.7. Accessing Mapped Bean Properties
        1. Problem
        2. Solution
        3. Discussion
      9. 3.8. Accessing a Simple, Nested, Indexed, and Mapped Bean Property
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 3.9. Determining the Type of a Bean Property
        1. Problem
        2. Solution
        3. Discussion
      11. 3.10. Comparing Beans
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 3.11. Copying Bean Properties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 3.12. Cloning a Bean
        1. Problem
        2. Solution
        3. Discussion
      14. 3.13. Setting a Bean Property
        1. Problem
        2. Solution
        3. Discussion
      15. 3.14. Testing Property Access
        1. Problem
        2. Solution
        3. Discussion
      16. 3.15. Validating Beans with Predicates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 3.16. Creating a Map of Bean Properties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 3.17. Wrapping a Bean with a Map
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 3.18. Creating a Dynamic Bean
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 3.19. Getting and Setting Properties as Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    5. 4. Functors
      1. Introduction
      2. 4.1. Obtaining Commons Collections
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 4.2. Getting the Commons Collections Source Code
        1. Problem
        2. Solution
        3. See Also
      4. 4.3. Reversing a Comparator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 4.4. Chaining Comparators
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 4.5. Comparing Nulls
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 4.6. Fixed-Order Comparison
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 4.7. Using Simple Predicates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 4.8. Writing a Custom Predicate
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 4.9. Creating Composite Predicates
        1. Problem
        2. Solution
        3. Discussion
      11. 4.10. Transforming Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 4.11. Creating a Chain of Transformations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 4.12. Applying Conditional Transformations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 4.13. Writing a Closure
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 4.14. Chaining Closures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 4.15. Modeling Conditional Statements with Closures
        1. Problem
        2. Solution
        3. Discussion
      17. 4.16. Modeling Loops with Closures
        1. Problem
        2. Solution
        3. Discussion
    6. 5. Collections
      1. Introduction
      2. 5.1. Obtaining Commons Collections
        1. Problem
        2. Solution
        3. See Also
      3. 5.2. Using a Looping Iterator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 5.3. Iterating Over an ArrayList
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 5.4. Filtering a Collection with a Predicate
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 5.5. Iterating Through Distinct Elements
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 5.6. Using a Bag
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 5.7. Using a Buffer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 5.8. Creating a Priority Queue
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 5.9. Using a Blocking Buffer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 5.10. Storing Multiple Values in a Map
        1. Problem
        2. Solution
        3. Discussion
      12. 5.11. Retrieving a Key by a Value
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 5.12. Using a Case-Insensitive Map
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 5.13. Creating Typed Collections and Maps
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 5.14. Constraining Map Values
        1. Problem
        2. Solution
        3. Discussion
      16. 5.15. Constraining List Contents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 5.16. Transforming Collections
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 5.17. Creating a Least Recently Used Cache
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 5.18. Using a Lazy Map
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 5.19. Counting Objects in a Collection
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. 5.20. Performing Set Operations
        1. Problem
        2. Solution
        3. Discussion
      22. 5.21. Retrieving Map Values Without Casting
        1. Problem
        2. Solution
        3. Discussion
    7. 6. XML
      1. Introduction
      2. 6.1. Obtaining Jakarta Commons Digester
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 6.2. Turning XML Documents into Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 6.3. Namespace-Aware Parsing
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 6.4. Creating a Simple XML Command Language
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 6.5. Variable Substitution and XML Parsing
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 6.6. Obtaining Jakarta Commons Betwixt
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 6.7. Turning Beans into XML Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 6.8. Customizing XML Generated from an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 6.9. Turning XML Documents into Beans
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    8. 7. Application Infrastructure
      1. Introduction
      2. 7.1. Obtaining Commons CLI
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 7.2. Parsing a Simple Command Line
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 7.3. Parsing a Complex Command Line
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 7.4. Printing Usage Information
        1. Problem
        2. Solution
        3. Discussion
      6. 7.5. Obtaining Commons Configuration
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 7.6. Configuring Applications with Properties Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 7.7. Configuring Applications with XML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 7.8. Using Composite Configuration
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 7.9. Obtaining Commons Logging
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 7.10. Using an Abstract Logging Interface
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 7.11. Specifying a Logging Implementation
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 7.12. Obtaining Apache Log4J
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 7.13. Configuring Log4J with a Properties File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 7.14. Configuring Log4J with XML
        1. Problem
        2. Solution
        3. See Also
    9. 8. Math
      1. Introduction
      2. 8.1. Using Fractions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 8.2. Finding the Maximum and Minimum in an Array
        1. Problem
        2. Solution
        3. Discussion
      4. 8.3. Using Number Ranges
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 8.4. Generating Random Variables
        1. Problem
        2. Solution
        3. Discussion
      6. 8.5. Obtaining Commons Math
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 8.6. Calculating Simple Univariate Statistics
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 8.7. Solving a System of Linear Equations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 8.8. Arithmetic with Complex Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See also
      10. 8.9. Establishing Relationships Between Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 8.10. Estimating the Amount of Time Left in a Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    10. 9. Templating
      1. Introduction
        1. Common Templating Problems in Applications
        2. Templating in Web Applications
      2. 9.1. Obtaining Commons JEXL
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 9.2. Using an Expression Language
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 9.3. Invoking Methods in an Expression
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 9.4. Externalizing Logic with an Expression Language
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 9.5. Obtaining Jakarta Velocity
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 9.6. Using a Simple Templating Language
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 9.7. Writing Templates with Conditionals and Loops
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 9.8. Using Macros in a Templating Engine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 9.9. Invoking Methods in a Template
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 9.10. Obtaining FreeMarker
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 9.11. Using a Complex Scripting Engine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 9.12. Accessing XML Documents from a Templating Engine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 9.13. Using Velocity in a Web Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 9.14. Using FreeMarker in a Web Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 9.15. Writing Templates in Eclipse
        1. Problem
        2. Solution
        3. See Also
    11. 10. I/O and Networking
      1. Introduction
      2. 10.1. Obtaining Commons IO
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 10.2. Copying Streams, byte[ ], Readers, and Writers
        1. Problem
        2. Solution
        3. Discussion
      4. 10.3. Closing Streams, Readers, and Writers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 10.4. Printing a Human-Readable File Size
        1. Problem
        2. Solution
        3. Discussion
      6. 10.5. Copying Files, Strings, and URLs
        1. Problem
        2. Solution
        3. Discussion
      7. 10.6. Deleting Directories Recursively
        1. Problem
        2. Solution
        3. Discussion
      8. 10.7. Obtaining the Size of a Directory
        1. Problem
        2. Solution
      9. 10.8. Touching a File
        1. Problem
        2. Solution
      10. 10.9. Filtering Files
        1. Problem
        2. Solution
        3. Discussion
      11. 10.10. Measuring Stream Traffic
        1. Problem
        2. Solution
        3. Discussion
      12. 10.11. Splitting an OutputStream
        1. Problem
        2. Solution
      13. 10.12. Obtaining Jakarta ORO
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 10.13. Using Globs and Perl5 Regular Expressions to List Files
        1. Problem
        2. Solution
      15. 10.14. Obtaining Commons Net
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 10.15. Writing an FTP Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 10.16. Sending Mail with SMTP
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 10.17. Checking a POP3 Mailbox
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    12. 11. HTTP and WebDAV
      1. Introduction
      2. 11.1. Obtaining Jakarta HttpClient
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 11.2. Getting Jakarta HttpClient Source Code
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 11.3. Performing an HTTP GET
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 11.4. Sending Parameters in a Query String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 11.5. Retrieving Content with a Conditional GET
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 11.6. Debugging HTTP Communications
        1. Problem
        2. Solution
        3. Discussion
      8. 11.7. Making an HTTP POST Request
        1. Problem
        2. Discussion
        3. Discussion
        4. See Also
      9. 11.8. Sending POST Data from a File
        1. Problem
        2. Solution
        3. Discussion
      10. 11.9. Uploading Files with a Multipart POST
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 11.10. Basic Authentication
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 11.11. NTLM Authentication
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 11.12. Working with Cookies
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 11.13. Handling Redirects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 11.14. SSL
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 11.15. Accepting a Self-Signed Certificate
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 11.16. Obtaining Jakarta Slide
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 11.17. Connecting to WebDAV Resources
        1. Problem
        2. Solution
        3. Discussion
      19. 11.18. Modifying a WebDAV Resource
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    13. 12. Searching and Filtering
      1. Introduction
      2. 12.1. Obtaining Commons JXPath
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 12.2. Querying an Object Graph with XPath
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 12.3. Search a Collection of Simple Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 12.4. Applying XPath Queries to Complex Object Graphs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 12.5. Obtaining Jakarta Lucene
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 12.6. Creating an Index of XML Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 12.7. Searching for a Specific Term in a Document Index
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 12.8. Finding the Frequency of Terms in an Index
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    14. Index
    15. Colophon