KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CodeGen > ID


1 /* $Id: ID.java,v 1.1.1.1 2003/02/11 16:19:40 bures Exp $ */
2 package SOFA.SOFAnode.Made.CodeGen;
3  
4 import java.util.ArrayList JavaDoc;
5
6 /** Representing ID of architecture given on command line.
7   *
8   * @author Petr Hnetynka
9   */

10 public class ID {
11   ArrayList JavaDoc name;
12   String JavaDoc ver;
13
14   /** Parse textual representation of id.
15     *
16     * @param nm textual representation of id in format <tt>absolute_name?version</tt> or <tt>absolute_name</tt>
17     * @exception BadIDException if <tt>nm</tt> is in bad format
18     */

19   public ID(String JavaDoc nm) throws BadIDException {
20     name = new ArrayList JavaDoc();
21     int iOfQ = nm.indexOf('?');
22     if (iOfQ == -1) { // absolute name without version
23
parseAN(nm, name);
24     } else { // absolute name with version
25
String JavaDoc absnm = nm.substring(0, iOfQ);
26       ver = nm.substring(iOfQ+1);
27       if (ver.length()==0 || ver.indexOf('?')!=-1)
28         throw new BadIDException();
29       parseAN(absnm, name);
30     }
31   }
32
33   /** Parse absolute name in nm.
34     *
35     * @exception BadIDException if <tt>nm</tt> is in bad format
36     * @param nm string with absolute name
37     * @param name array for name elements (must be allocated)
38     */

39   private void parseAN(String JavaDoc nm, ArrayList JavaDoc name) throws BadIDException {
40     StringBuffer JavaDoc sn = new StringBuffer JavaDoc();
41     boolean dv = false;
42     for (int i=0;i<nm.length();i++) {
43       if (dv && nm.charAt(i)!=':')
44         throw new BadIDException();
45       if (dv) { // nm.charAt(i) == ':'
46
if (name.size()!=0 || sn.length()!=0) { // it's not double colon in the begining of name
47
if (sn.length()==0) // more than two colons
48
throw new BadIDException();
49           name.add(sn.toString());
50           sn.delete(0, sn.length());
51         }
52         dv = false;
53         continue;
54       }
55       if (nm.charAt(i)==':') {
56         dv = true;
57         continue;
58       }
59       sn.append(nm.charAt(i));
60     }
61     if (sn.length() != 0) // append last item
62
name.add(sn.toString());
63     else
64       throw new BadIDException();
65   }
66
67   /** Returns textual representation of id.
68     */

69   public String JavaDoc toString() {
70     StringBuffer JavaDoc ret = new StringBuffer JavaDoc("::");
71     for (int i=0; i<name.size();i++) {
72       ret.append((String JavaDoc) name.get(i));
73       if (i+1 != name.size())
74         ret.append("::");
75     }
76     if (ver != null && ver.length() != 0) {
77       ret.append("?");
78       ret.append(ver);
79     }
80     return ret.toString();
81   }
82
83   /** Returns absolute name (without version)
84     */

85   public String JavaDoc absoluteName() {
86     StringBuffer JavaDoc ret = new StringBuffer JavaDoc("::");
87     for (int i=0; i<name.size();i++) {
88       ret.append((String JavaDoc) name.get(i));
89       if (i+1 != name.size())
90         ret.append("::");
91     }
92     return ret.toString();
93   }
94
95   /** Returns number of elements in the absolute name of id.
96     */

97   public int size() {
98     return name.size();
99   }
100
101   /** Returns version of id.
102     */

103   public String JavaDoc version() {
104     return ver;
105   }
106
107   /** Returns i-th element of absolute name
108     *
109     * @param index index of element to return
110     * @exception IndexOutOfBoundsException if index is out of range
111     */

112   public String JavaDoc get(int index) {
113     return (String JavaDoc) name.get(index);
114   }
115 }
116
Popular Tags