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

Quick Search    Search Deep

Source code: com/memoire/xml/XmlReader.java


1   /**
2    * @modification 2001-10-25
3    * @statut       unstable
4    * @file         XmlReader.java
5    * @version      0.04
6    * @author       Guillaume Desnoix
7    * @email        guillaume@desnoix.com
8    * @license      GNU General Public License 2 (GPL2)
9    * @copyright    1998-2001 Guillaume Desnoix
10   */
11  
12  package com.memoire.xml;
13  import  com.memoire.xml.*;
14  
15  import java.util.*;
16  
17  public class XmlReader
18         implements XmlListener
19  {
20    private static Hashtable ORDER;
21  
22    static {
23      ORDER=new Hashtable();
24  
25      Integer I90=new Integer(90);
26      Integer I80=new Integer(80);
27      Integer I70=new Integer(70);
28      Integer I60=new Integer(60);
29      Integer I55=new Integer(55);
30      Integer I50=new Integer(50);
31      Integer I45=new Integer(45);
32      Integer I42=new Integer(42);
33      Integer I40=new Integer(40);
34      Integer I30=new Integer(30);
35      Integer I20=new Integer(20);
36      Integer I10=new Integer(10);
37  
38      ORDER.put("HTML",I90);
39  
40      ORDER.put("HEAD",I80);
41      ORDER.put("BODY",I80);
42  
43      ORDER.put("APPLET",I70);
44  
45      ORDER.put("DIV",I60);
46  
47      ORDER.put("TABLE",I55);
48  
49      ORDER.put("TR",I50);
50  
51      ORDER.put("TD",I45);
52  
53      ORDER.put("P",I42);
54  
55      ORDER.put("UL",I40);
56      ORDER.put("OL",I40);
57  
58      ORDER.put("LI",I20);
59  
60      ORDER.put("A",I10);
61      ORDER.put("IMG",I10);
62      ORDER.put("B",I10);
63      ORDER.put("I",I10);
64    }
65  
66    private int       mode_;
67  
68    private XmlNode   current_;
69    private Stack     nodes_     =new Stack();
70    private Hashtable attributes_=new Hashtable(11);
71  
72    private String origin_;
73    private int    lineno_;
74    private int    charno_;
75  
76    public XmlReader()
77    {
78      mode_=XmlParser.XML;
79    }
80    
81    public XmlReader(int _mode)
82    {
83      mode_=_mode;
84    }
85    
86    public void location(String _origin, int _lineno, int _charno)
87    {
88      origin_=_origin;
89      lineno_=_lineno;
90      charno_=_charno;
91    }
92  
93    public void startElement(String _tag)
94    {
95      //System.err.println("START "+_tag);
96  
97      XmlNode n=new XmlNode();
98      n.tag_=_tag;
99      n.attributes_=attributes_;
100     attributes_=new Hashtable(11);
101 
102     if(current_!=null)
103     {
104       if(  (mode_==XmlParser.HTML)
105    &&current_.tag_.equals(_tag)&&!_tag.equals("FONT"))
106   missingTag(_tag);
107     }
108 
109     if(current_!=null) current_.children_.addElement(n);
110     current_=n;
111     nodes_.push(n);
112   }
113 
114   private void missingTag(String _tag)
115   {
116     if(  !_tag.equals("P")
117        &&!_tag.equals("LI")
118        &&!_tag.equals("OPTION")
119        &&!_tag.equals("PARAM")
120        &&!_tag.equals("A"))
121       System.err.println("MISSING END: "+_tag);
122 
123     endElement(_tag);
124   }
125 
126   public void endElement(String _tag)
127   {
128     if(nodes_.size()==0)
129     {
130       System.err.println("END NEEDED "+_tag);
131       return;
132     }
133 
134     XmlNode n=(XmlNode)nodes_.peek();
135 
136     if(_tag.equals(n.tag_))
137     {
138       nodes_.pop();
139     }
140     else
141     {
142       if(mode_==XmlParser.HTML)
143       {
144   boolean done=false;
145 
146   while(!done)
147   {
148     /*
149       if(  n.tag_.equals("P")
150       ||n.tag_.equals("LI")
151       ||n.tag_.equals("OPTION")
152       ||n.tag_.equals("PARAM")
153       ||_tag.equals("FONT"))
154     */
155     Integer ni=(Integer)ORDER.get(n.tag_);
156     Integer ti=(Integer)ORDER.get(_tag);
157     if((ni!=null)&&(ti!=null)&&(ni.intValue()<ti.intValue()))
158     {
159       current_=n;
160       nodes_.pop();
161       //System.err.println("=== "+_tag+" (remove "+n.tag_+")");
162 
163       if(nodes_.size()>0)
164       {
165         n=(XmlNode)nodes_.peek();
166         done=_tag.equals(n.tag_);
167       }
168       else done=true;
169     }
170     else
171     {
172       //System.err.println("=== "+_tag+" (keep "+n.tag_+")");
173       done=true;
174     }
175   }
176       }
177       else
178       {
179   error("block: "+n.tag_+" /"+_tag);
180   //dump();
181       }
182     }
183 
184     if(nodes_.size()>0) current_=(XmlNode)nodes_.peek();
185     //if(current_!=null) System.err.println("+++ current="+current_.tag_);
186   }
187 
188   private void dump()
189   {
190     String t="";
191     while(!nodes_.isEmpty())
192     {
193       XmlNode n=(XmlNode)nodes_.pop();
194       System.err.println(t+n.tag_); t+="  ";
195     }
196     System.exit(-3);
197   }
198 
199   public void attribute(String _name,String _value)
200   {
201     attributes_.put(_name,_value);
202   }
203 
204   public void text(String _data)
205   {
206     current_.children_.addElement(_data);
207   }
208 
209   public void error(String _message)
210   {
211     System.err.println("ERROR "+_message+" ["+lineno_+":"+charno_+"]");
212     //System.exit(1);
213   }
214 
215   public XmlNode getTree()
216   {
217     return current_;
218   }
219 
220   public static void main(String[] _args)
221   {
222     for(int i=0;i<_args.length;i++)
223     {
224       try
225       {
226         String f=_args[i];
227   int    m=XmlParser.XML;
228   if(f.endsWith(".html")||f.endsWith(".htm")) m=XmlParser.HTML;
229   if(f.endsWith(".txt"))                      m=XmlParser.TXT;
230   System.err.println("FILE: "+f+" MODE: "+m);
231   XmlParser p=new XmlParser(f,m);
232   XmlReader r=new XmlReader(m);
233   p.setXmlListener(r);
234   p.parse();
235   System.out.println(r.getTree());
236       }
237       catch(Exception ex)
238       {
239   ex.printStackTrace();
240   //System.err.println(ex);
241       }
242     }
243   }
244 }