View Javadoc

1   package org.apache.commons.digester3.examples.xmlrules.addressbook;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */ 
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /**
26   * See Main.java.
27   */
28  public class Person
29  {
30  
31      private int id;
32  
33      private String category;
34  
35      private String name;
36  
37      private HashMap<String, String> emails = new HashMap<String, String>();
38  
39      private List<Address> addresses = new ArrayList<Address>();
40  
41      /**
42       * A unique id for this person. Note that the Digester automatically converts the id to an integer.
43       */
44      public void setId( int id )
45      {
46          this.id = id;
47      }
48  
49      public void setCategory( String category )
50      {
51          this.category = category;
52      }
53  
54      public void setName( String name )
55      {
56          this.name = name;
57      }
58  
59      /** we assume only one email of each type... */
60      public void addEmail( String type, String address )
61      {
62          emails.put( type, address );
63      }
64  
65      public void addAddress( Address addr )
66      {
67          addresses.add( addr );
68      }
69  
70      public void print()
71      {
72          System.out.println( "Person #" + id );
73          System.out.println( "  category=" + category );
74          System.out.println( "  name=" + name );
75  
76          for ( Iterator<String> i = emails.keySet().iterator(); i.hasNext(); )
77          {
78              String type = i.next();
79              String address = emails.get( type );
80  
81              System.out.println( "  email (type " + type + ") : " + address );
82          }
83  
84          for ( Iterator<Address> i = addresses.iterator(); i.hasNext(); )
85          {
86              Address addr = i.next();
87              addr.print( System.out, 2 );
88          }
89      }
90  
91  }