View Javadoc

1   
2   
3   package org.textensor.xml;
4   
5   public class XMLToken {
6   
7       final static int NONE = 0;
8       final static int OPEN = 1;
9       final static int CLOSE = 2;
10  
11      final static int NUMBER = 3;
12      final static int STRING = 4;
13  
14      final static int OPENCLOSE = 5;
15  
16      final static int INTRO = 6;
17      final static int COMMENT = 7;
18  
19  
20      String[] types = {"NONE", "OPEN", "CLOSE", "NUMBER", "STRING",
21                        "OPENCLOSE", "INTRO", "COMMENT"
22                       };
23  
24  
25      int type;
26  
27      String svalue;
28      double dvalue;
29  
30      int natt;
31      String[] attNV;
32  
33  
34  
35      public XMLToken() {
36          type = NONE;
37      }
38  
39      /*
40      public String toString() {
41         String sr = ("XMLToken type=" + type + " sv=" + svalue + " dv=" + dvalue +
42      	   " natt=" + natt);
43         for (int i = 0; i < natt; i++) {
44       sr += "   att[" + i + "]:" + attNV[i] + "\n";
45         }
46         return sr;
47      }
48      */
49  
50  
51      public boolean isOpen() {
52          return (type == OPEN || type == OPENCLOSE);
53      }
54  
55      public boolean isClose() {
56          return (type == OPENCLOSE || type == CLOSE);
57      }
58  
59      public boolean isNumber() {
60          return (type == NUMBER);
61      }
62  
63      public boolean isString() {
64          return (type == STRING);
65      }
66  
67      public boolean isNone() {
68          return (type == NONE);
69      }
70  
71      public boolean isIntro() {
72          return (type == INTRO);
73      }
74  
75      public boolean isComment() {
76          return (type == COMMENT);
77      }
78  
79  
80  
81      public String toString() {
82          String s = types[type] + " " ;
83          if (type == OPEN ||
84                  type == STRING ||
85                  type == INTRO ||
86                  type == COMMENT ||
87                  type == CLOSE ||
88                  type == OPENCLOSE) {
89              s += svalue;
90  
91              if (type == OPEN || type==OPENCLOSE) {
92                  if (natt > 0) {
93                      for (int i = 0; i < natt; i++) {
94                          s += "\n    " + attNV[2*i] + "=" + attNV[2*i+1];
95                      }
96                  }
97              }
98          } else if (type == NUMBER) {
99              s += " " + dvalue;
100         }
101         return s;
102     }
103 
104     public void setType(int itype) {
105         type = itype;
106     }
107 
108 
109     public void setStringValue(String s) {
110         svalue = s;
111     }
112 
113 
114     public void setDValue(double d) {
115         dvalue = d;
116     }
117 
118 
119     public void setAttributes(String[] sa) {
120         attNV = sa;
121         natt = sa.length / 2;
122     }
123 
124 
125 
126     public boolean hasAttribute(String sat) {
127         boolean bret = false;
128         for (int i = 0; i < natt; i++) {
129             if (attNV[2*i].equals(sat)) bret = true;
130         }
131         return bret;
132     }
133 
134     public Attribute[] getAttributes() {
135         Attribute[] nvpa = new Attribute[natt];
136         for (int i = 0; i < natt; i++) {
137             nvpa[i] = new Attribute(attNV[2*i], attNV[2*i+1]);
138         }
139         return nvpa;
140     }
141 
142 
143 
144     public String getAttribute(String sat) {
145         String sret = null;
146         for (int i = 0; i < natt; i++) {
147             if (attNV[2*i].equals(sat)) sret = attNV[2*i+1];
148         }
149         return sret;
150     }
151 
152 
153     public String getName() {
154         return svalue;
155     }
156 
157     public String getOpenTagString() {
158         return ("<" + svalue + ">");
159     }
160 
161     public String getCloseTagString() {
162         return ("</" + svalue + ">");
163     }
164 
165 
166 
167     public boolean closes(XMLToken start) {
168         return (svalue.equals(start.getName()) && isClose());
169     }
170 
171 
172     public int getNumAttributes() {
173         return natt;
174     }
175 
176 
177     public String getAttributeName(int i) {
178         return attNV[2*i];
179     }
180 
181     public String getAttributeValue(int i) {
182         return attNV[2*i+1];
183     }
184 
185 }