1    package com.instantbank.collections.util;
2    
3    
4    /**
5     * A Class class.
6     * <P>
7     *
8     * @author Guillermo Posse
9     */
10   public class XMLCleaner extends Object {
11     private LexicalAnalyzer analyzer;
12     private int currentToken;
13   
14   
15     /**
16      * Constructor
17      *
18      * @param xml Description of the Parameter
19      */
20     public XMLCleaner(String xml) {
21       analyzer = new LexicalAnalyzer(xml);
22     }
23   
24   
25     private void advance() {
26       currentToken = analyzer.getToken();
27     }
28   
29   
30     private void assertCheck(int token) throws Exception {
31       advance();
32       check(token);
33     }
34   
35   
36     private void assert_name(String value) throws Exception {
37       advance();
38       check(analyzer.TNAME);
39       if(!checkValue(value)) {
40         throw new Exception("Expecting " + value);
41       }
42     }
43   
44   
45     private void check(int token) throws Exception {
46       if(currentToken != token) {
47         throw new Exception("Expecting " + analyzer.getTokenString(token));
48       }
49     }
50   
51   
52     private String checkEncoding() throws Exception {
53       String text = "";
54   
55       if(currentToken == analyzer.TNAME && checkValue("ENCODING")) {
56         assertCheck(analyzer.TEQUAL);
57         assertCheck(analyzer.TVALUE);
58         text += "encoding=\"" + analyzer.getValue() + "\" ";
59         advance();
60       }
61       return text;
62     }
63   
64   
65     private String checkHeader() throws Exception {
66       String text;
67   
68       check(analyzer.TSTARTHEADER);
69       assert_name("xml");
70       assert_name("version");
71       assertCheck(analyzer.TEQUAL);
72       assertCheck(analyzer.TVALUE);
73       text = "<? xml version='" + analyzer.getValue() + "' ";
74       advance();
75       text += checkEncoding();
76       text += checkStandalone();
77       check(analyzer.TQUESTION);
78       assertCheck(analyzer.TGREATERTHAN);
79       text += "?>";
80       advance();
81       return text;
82     }
83   
84   
85     private String checkStandalone() throws Exception {
86       String text = "";
87   
88       if(currentToken == analyzer.TNAME && checkValue("STANDALONE")) {
89         assertCheck(analyzer.TEQUAL);
90         assertCheck(analyzer.TVALUE);
91         text += "standalone=\"" + analyzer.getValue() + "\" ";
92         advance();
93       }
94       return text;
95     }
96   
97   
98     private String checkTag() throws Exception {
99       String name;
100      String text;
101  
102      check(analyzer.TLESSTHAN);
103      assertCheck(analyzer.TNAME);
104      name = analyzer.getValue();
105      text = "<" + name;
106      advance();
107      while(currentToken == analyzer.TNAME) {
108        text += " " + analyzer.getValue();
109        assertCheck(analyzer.TEQUAL);
110        assertCheck(analyzer.TVALUE);
111        text += "=\"" + analyzer.getValue() + "\"";
112        advance();
113      }
114      if(currentToken == analyzer.TSLASH) {
115        assertCheck(analyzer.TGREATERTHAN);
116        text += "/>";
117        return text;
118      }
119      check(analyzer.TGREATERTHAN);
120      text += ">";
121      text += parseElement();
122      check(analyzer.TENDTAG);
123      assertCheck(analyzer.TNAME);
124      if(!name.equals(analyzer.getValue())) {
125        throw new Exception("Tag " + name + " not closed");
126      }
127      assertCheck(analyzer.TGREATERTHAN);
128      text += "</" + name + ">";
129      return text;
130    }
131  
132  
133    private String checkText() throws Exception {
134      check(analyzer.TTEXT);
135      return analyzer.getValue();
136    }
137  
138  
139    private boolean checkValue(String value) {
140      return analyzer.getValue().toUpperCase().equals(value.toUpperCase());
141    }
142  
143  
144    public String cleaned() throws Exception {
145      String result = "";
146      String text;
147  
148      advance();
149      if(currentToken == analyzer.TSTARTHEADER) {
150        text = checkHeader();
151      }
152      result = checkTag();
153      return result;
154    }
155  
156  
157    private String parseElement() throws Exception {
158      String text = "";
159  
160      while(true) {
161        currentToken = analyzer.getTokenElement();
162        if(currentToken == analyzer.TENDTAG) {
163          break;
164        }
165        if(currentToken == analyzer.TLESSTHAN) {
166          text += checkTag();
167        }
168        else {
169          text += StringFormat.toSafeJavaString(checkText());
170        }
171      }
172      return text;
173    }
174  }
175  
176