KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > impl > DefaultNameParser


1 package org.sapia.archie.impl;
2
3 import org.sapia.archie.Name;
4 import org.sapia.archie.NameParser;
5 import org.sapia.archie.NamePart;
6 import org.sapia.archie.ProcessingException;
7
8
9 /**
10  * Implements the <code>NameParser</code> interface. Parses names whose parts are
11  * delimited by '/', such as in: some/object/name, where "some/object/name" is
12  * a <code>Name</code>, and "some", "object" and "name" are <code>NamePart</code>s.
13  *
14  * @see org.sapia.archie.impl.DefaultNamePart
15  *
16  * @author Yanick Duchesne
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class DefaultNameParser implements NameParser {
24   public static final char SEPARATOR = '/';
25
26
27   /**
28    * @see NameParser#parse(String)
29    */

30   public Name parse(String JavaDoc name) throws ProcessingException {
31     Name n = new Name();
32     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
33
34     for (int i = 0; i < name.length(); i++) {
35       if (name.charAt(i) == SEPARATOR) {
36         n.add(new DefaultNamePart(buf.toString()));
37         buf.delete(0, buf.length());
38       } else {
39         buf.append(name.charAt(i));
40       }
41     }
42
43     if (buf.length() > 0) {
44       n.add(new DefaultNamePart(buf.toString()));
45     }
46
47     return n;
48   }
49   
50   /**
51    * @see org.sapia.archie.NameParser#parseNamePart(java.lang.String)
52    */

53   public NamePart parseNamePart(String JavaDoc namePart) throws ProcessingException {
54     return new DefaultNamePart(namePart);
55   }
56   
57   /**
58    * @see org.sapia.archie.NameParser#asString(org.sapia.archie.Name)
59    */

60   public String JavaDoc asString(Name name) {
61     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
62     for(int i = 0; i < name.count(); i++){
63       buff.append(name.get(i).asString());
64       if(i < name.count() - 1){
65         buff.append(SEPARATOR);
66       }
67     }
68     return buff.toString();
69   }
70 }
71
Popular Tags