Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: html/Attributes.java


1   package html;
2   
3   import java.util.*;
4   
5   public class Attributes extends LinkedList {
6   
7       public Attributes() {
8     super();
9       }
10  
11      public Attributes(String attr) throws IllegalArgumentException {
12    super();
13    setAttributes(attr);
14      }
15  
16      public Attributes(Attribute[] attrs) {
17    super();
18    for (int i = 0; i < attrs.length; i++) {
19        add(attrs[i]);
20    }
21      }
22  
23      public Attributes(Attribute attr) {
24    super();
25    add(attr);
26      }
27  
28      /**
29         * Set the value of attributes.
30         * @param str  String with HTML attributes like:
31         * border="0" cellpadding="0" cellspacing="0"
32         */
33  
34      public void setAttributes(String str) throws IllegalArgumentException {
35    Attribute attribute = new Attribute();  
36    final int START = 0;
37    // final int EOL = 1;
38    final int ERR = -1;
39    final int Attr = 2;
40    final int Val0 = 3;
41    final int Val1 = 4;
42    final int Value = 5;
43    final int QVal = 6;
44    // finite state machine for parsing
45    int state = START;
46    int prev = -1; // debug
47    int i = 0;
48    StringBuffer token = new StringBuffer();
49  
50    while ((i <= str.length()) && (state != ERR)) {
51        char chr;
52        if (i < str.length())
53      chr = str.charAt(i);
54        else
55      chr = ' ';
56        i++;
57        prev = state;
58        switch(state) {
59        case START:
60      switch(chr) {
61      case ' ': break;
62      case '"': case '=': 
63          state = ERR; break;
64      default: token.append(chr); state = Attr;
65      }
66      break;
67        case Attr:
68      switch(chr) {
69      case ' ': 
70          attribute = new Attribute(token.toString());
71          state = Val0;
72          break;
73      case '=':         
74        attribute = new Attribute(token.toString());
75        state = Val1; 
76        break;
77      case '"':
78          state = ERR; break;
79      default: token.append(chr);
80      }
81      break;
82        case Val0:
83            switch(chr) {
84                case '=': state = Val1; break;
85                case ' ': break;
86                default: 
87            this.add(attribute);
88            token = new StringBuffer();
89            token.append(chr);
90            state = Attr;
91            }
92      break;
93        case Val1:
94            token = new StringBuffer();
95            switch(chr) {
96            case '"': state = QVal; break;
97            case '=': state = ERR; break;
98            case ' ': break;
99            default: 
100       token.append(chr);
101       state = Value;
102       break;
103     }
104     break;
105       case Value:
106     switch(chr) {
107     case ' ':
108         attribute.setValue(token.toString());
109         this.add(attribute);
110         token = new StringBuffer();
111         state = START;
112         break;
113     case '=': state = ERR; break;
114     default: token.append(chr);
115     }
116     break;
117       case QVal:
118     switch(chr) {
119     case '"':
120         attribute.setValue(token.toString());
121         this.add(attribute);
122         token = new StringBuffer();
123         state = START;
124         break;
125     default: token.append(chr);
126     }
127     break;
128       }
129   }
130   if ((i <= str.length()) || (state == ERR)) {
131       System.err.println("Parse error:");
132       System.err.println(str);
133       for (int j = 0; j < i; j++)
134     System.err.print(' ');
135       System.err.println('^');
136       throw new IllegalArgumentException("Parse error in state " + 
137                  Integer.toString(prev));
138   }
139     }
140   
141     public String toString() {
142   StringBuffer out = new StringBuffer(" ");
143   ListIterator iterator = this.listIterator();
144   while (iterator.hasNext()) {
145       out.append(iterator.next().toString());
146       out.append(' ');
147   }
148   out.deleteCharAt(out.length() - 1);
149   return out.toString();
150     }
151 }