KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > util > StateIdParser


1 package org.sapia.soto.state.util;
2
3
4 /**
5  * @author Yanick Duchesne
6  *
7  * <dl>
8  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
9  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
10  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
11  * </dl>
12  */

13 public class StateIdParser {
14   private static final char SEP = '/';
15   private static final char DOT = '.';
16
17   public static Created parseStateFrom(String JavaDoc pathInfo, String JavaDoc extension) {
18     if (pathInfo.length() == 0) {
19       return null;
20     }
21
22     if (pathInfo.charAt(0) == SEP) {
23       if (pathInfo.length() == 1) {
24         return null;
25       }
26
27       pathInfo = pathInfo.substring(1);
28
29       if (pathInfo.length() == 0) {
30         return null;
31       }
32     }
33
34     int i;
35     String JavaDoc path = null;
36
37     if ((i = pathInfo.lastIndexOf(SEP)) > 0) {
38       path = pathInfo.substring(0, i);
39       pathInfo = pathInfo.substring(i + 1);
40     }
41
42     if (extension != null) {
43       i = pathInfo.lastIndexOf(DOT);
44
45       if ((i > 0) && extension.equals(pathInfo.substring(i + 1))) {
46         pathInfo = pathInfo.substring(0, i);
47       }
48     }
49
50     i = pathInfo.lastIndexOf(DOT);
51
52     if (i > 0) {
53       String JavaDoc module = pathInfo.substring(0, i);
54
55       if (path != null) {
56         pathInfo = new StringBuffer JavaDoc(path).append(SEP)
57                                          .append(pathInfo.substring(i + 1))
58                                          .toString();
59       } else {
60         pathInfo = pathInfo.substring(i + 1);
61       }
62
63       return new Created(pathInfo, module);
64     } else {
65       if (path != null) {
66         pathInfo = new StringBuffer JavaDoc(path).append(SEP)
67                                          .append(pathInfo.substring(i + 1))
68                                          .toString();
69       } else {
70         pathInfo = pathInfo.substring(i + 1);
71       }
72
73       return new Created(pathInfo, null);
74     }
75   }
76
77   public static class Created {
78     public final String JavaDoc state;
79     public final String JavaDoc module;
80
81     Created(String JavaDoc state, String JavaDoc module) {
82       this.state = state;
83       this.module = module;
84     }
85   }
86 }
87
Popular Tags