WoordXMLParser.java

00001 /*
00002  * Created on 24-mei-2005
00003  * WoordXMLParser
00004  * 
00005  * Een XMLParser die Eindwoordensets inleest
00006  */
00007 package basis;
00008 
00009 import java.io.File;
00010 import java.util.ArrayList;
00011 import java.util.Iterator;
00012 
00013 import org.xml.sax.Attributes;
00014 import org.xml.sax.SAXException;
00015 
00019 public class WoordXMLParser extends XMLParser 
00020 {
00021         private ArrayList woorden;
00022         private ArrayList categorieen;
00023         private boolean woordGevonden = false;
00024         private String categorie;
00025         
00026         private Woord woord;
00027         
00028         public WoordXMLParser()
00029         {
00030                 super(); // initaliseer de basis XML parser
00031                 woorden = new ArrayList();
00032                 categorieen = new ArrayList();
00033         }
00034 
00035         public boolean laadBestand(String bestandsNaam)
00036         {
00037                 boolean gelezen = false;
00038                 
00039                 try
00040                 {
00041                         // Parse the input
00042                         saxParser = factory.newSAXParser();
00043                         saxParser.parse( new File(bestandsNaam), handler);
00044                         gelezen = true;
00045                 }
00046                 catch (Throwable t)
00047                 {
00048                         t.printStackTrace();
00049                         gelezen = false;
00050                 }
00051                 
00052                 return gelezen;
00053         }       
00054         
00055         public void leesWoordenSet(String bestandsNaam)
00056         {
00057                 laadBestand(bestandsNaam);              
00058         }
00059         
00060         public void characters(char buf[], int offset, int len) throws SAXException
00061         {
00062         String s = new String(buf, offset, len);
00063         
00064         if (woordGevonden)
00065         {
00066                 woord = new Woord(s, categorie);
00067                 woorden.add(woord);
00068         }
00069         }       
00070         
00071         public void startElement(String nameSpaceURI, String lName, String qName, Attributes attrs) throws SAXException
00072         {
00073              String eName = lName; // element name
00074                 if ("".equals(eName))
00075                     eName = qName; // namespaceAware = false
00076                 
00077                 if (eName.equals("woordenset"))
00078                 {
00079                     if (attrs != null) 
00080                     {
00081                         for (int i = 0; i < attrs.getLength(); i++) 
00082                         {
00083                             String aName = attrs.getLocalName(i); // Attr name
00084 
00085                             if ("".equals(aName))
00086                                 aName = attrs.getQName(i);
00087                             
00088                             if (aName.equals("categorie"))
00089                             {
00090                                 categorie = attrs.getValue(i);
00091                                 categorieen.add(categorie);                             
00092                             }
00093                         }
00094                     }
00095                 }
00096                 
00097                 if (eName.equals("woord"))
00098                 {
00099                         woordGevonden = true;
00100                 }
00101               
00102         }
00103         
00104         public void endElement(String namespaceURI, String lName, String qName) throws SAXException
00105         {       
00106              String eName = qName; // element name
00107                 if (eName.equals("woord"))
00108                 {
00109                         woordGevonden = false;
00110                         woord = null;   
00111                 }
00112                         
00113 
00114         }       
00115         
00116         public boolean laadDirectory(String dir)
00117         {
00118                 File directory = new File(dir); 
00119                 String bestanden[] = directory.list();
00120                 
00121                 for (int i = 0; i < bestanden.length; i++)
00122                 {
00123                         File bestand = new File(bestanden[i]);
00124                         
00125                         if (!bestand.isDirectory() && bestand.getName().contains("woord") && bestand.getName().endsWith(".xml"))
00126                         {
00127                                 leesWoordenSet(dir + bestand.getName());
00128                         }
00129                 }
00130                 
00131                 return true;
00132         }
00133         
00134         public ArrayList getWoorden()
00135         {
00136                 return woorden;         
00137         }               
00138         
00139         public String getWoord(String categorie)
00140         {
00141                 String woordString = new String();
00142                 
00143                 Iterator woordit = woorden.iterator();
00144                 
00145                 while (woordit.hasNext())
00146                 {
00147                         Woord woord = (Woord) woordit.next();
00148                         if (woord.getCategorie() == categorie)
00149                         {
00150                                 woordString = woord.getWoord();
00151                                 break;
00152                         }
00153                 }
00154                 
00155                 return woordString;
00156                 
00157         }
00158         
00159         public ArrayList getCategorieen()
00160         {
00161                 return categorieen;
00162         }
00163 }

Generated on Mon Jul 18 21:59:05 2005 for Twee Voor Twaalf by  doxygen 1.4.3