KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > util > ServerObjectIDConverter


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.util;
10
11 import java.util.ArrayList JavaDoc;
12
13 import org.jboss.portal.common.value.Converter;
14 import org.jboss.portal.common.value.FormatConversionException;
15 import org.jboss.portal.common.value.NullConversionException;
16 import org.jboss.portal.server.ServerObjectID;
17
18 /**
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.2 $
21  */

22 public class ServerObjectIDConverter implements Converter
23 {
24
25    public static final ServerObjectIDConverter instance = new ServerObjectIDConverter();
26
27    public static ServerObjectID decode(String JavaDoc value) throws NullConversionException, FormatConversionException
28    {
29       if (value == null)
30       {
31          throw new NullConversionException();
32       }
33       if (value.length() == 0)
34       {
35          throw new FormatConversionException();
36       }
37       int index = value.indexOf('.');
38       ArrayList JavaDoc list = new ArrayList JavaDoc();
39       int previous = 0;
40       while (index != -1)
41       {
42          String JavaDoc name = value.substring(previous, index);
43          list.add(name);
44          previous = index + 1;
45          index = value.indexOf('.', previous);
46       }
47       list.add(value.substring(previous));
48       if (list.size() == 0)
49       {
50          throw new NullConversionException();
51       }
52       String JavaDoc[] names = (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
53       return new ServerObjectID(names);
54    }
55
56    public boolean accept(Class JavaDoc clazz)
57    {
58       return ServerObjectID.class.equals(clazz);
59    }
60
61    public Object JavaDoc toObject(String JavaDoc value) throws NullConversionException, FormatConversionException
62    {
63       return decode(value);
64    }
65
66    public String JavaDoc toString(Object JavaDoc value) throws NullConversionException, FormatConversionException
67    {
68       if (value == null)
69       {
70          throw new NullConversionException();
71       }
72       if (value instanceof ServerObjectID)
73       {
74          return value.toString();
75       }
76       throw new FormatConversionException("Unrecognized object " + value.getClass().getName());
77    }
78 }
79
Popular Tags