WoordenXMLWriter.java

00001 /*
00002  * Created on 17-jun-2005
00003  *
00004  * TODO To change the template for this generated file go to
00005  * Window - Preferences - Java - Code Style - Code Templates
00006  */
00007 package basis;
00008 
00009 import java.io.File;
00010 import java.io.FileNotFoundException;
00011 import java.io.FileOutputStream;
00012 import java.io.IOException;
00013 import java.io.OutputStreamWriter;
00014 import java.io.PrintStream;
00015 
00016 import org.xml.sax.Attributes;
00017 import org.xml.sax.SAXException;
00018 
00025 public class WoordenXMLWriter extends XMLWriter {
00026         
00027         public WoordenXMLWriter(String bestandsNaam)
00028         {
00029                 super(bestandsNaam);
00030         }
00031         
00032         public boolean laadBestand(String bestandsNaam)
00033         {
00034                 boolean gelezen = false;
00035                 
00036                 try
00037                 {
00038                         // Parse the input
00039                         saxParser = factory.newSAXParser();
00040                         file = new File(bestandsNaam);
00041                         if (file.length() == 0)
00042                         {               
00043                                 tekstTag = "";
00044                                 eindTag  = "</woordenset>";
00045                                 
00046                                 startDocument();
00047                         }
00048                         else
00049                         {
00050                                 saxParser.parse(file, handler);
00051                         }
00052                         
00053                         
00054                         gelezen = true;
00055                 }
00056                 catch (Throwable t)
00057                 {
00058                         t.printStackTrace();
00059                         gelezen = false;
00060                 }
00061                 
00062                 return gelezen;                 
00063         }
00064         
00065         public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException
00066         {
00067                 String eName = sName; // element name
00068                 if ("".equals(eName)) eName = qName; // namespaceAware = false
00069                                 
00070                 emit("<"+eName);
00071                 if (attrs != null) {
00072                         for (int i = 0; i < attrs.getLength(); i++) {
00073                                 String aName = attrs.getLocalName(i); // Attr name
00074                                 if ("".equals(aName)) aName = attrs.getQName(i);
00075                                 emit(" ");
00076                                 emit(aName+"=\""+attrs.getValue(i)+"\"");
00077                         }
00078                 }
00079                 emit(">");
00080         }
00081         
00082         public void endElement(String namespaceURI, String sName, String qName) throws SAXException
00083         {
00084                 if (qName.equals("woordenset"))
00085                         eindTag = "</woordenset>";
00086                 else
00087                         emit("</"+qName+">");
00088         }
00089         
00090         public void characters(char buf[], int offset, int len) throws SAXException
00091         {
00092                 String s = new String(buf, offset, len);
00093                 emit(s);
00094         }
00095         
00096         public void startDocument()
00097     throws SAXException
00098     {
00099         emit("<?xml version='1.0' encoding='UTF-8'?>");
00100         nl();
00101     }
00102 
00103     public void endDocument()
00104     throws SAXException
00105     {
00106 //        try {
00107             nl();
00108 //            wOut.flush();
00109 //        } catch (IOException e) {
00110 //            throw new SAXException("I/O error", e);
00111 //        }
00112     }
00113         
00114     public void emit(String s)
00115         throws SAXException
00116         {
00117 //          try {
00118                 tekstTag += s;
00119 //              wOut.write(s);
00120 //              wOut.flush();
00121 //          } catch (IOException e) {
00122 //              throw new SAXException("I/O error", e);
00123 //          }
00124         }
00125 
00126         
00127         public void nl()
00128     throws SAXException
00129     {
00130         String lineEnd =  System.getProperty("line.separator");
00131         tekstTag += lineEnd;
00132     }   
00133 
00134         public void nieuweWoordenSet(String categorie) throws SAXException
00135         {
00136                 char quote = (char) 34; // quote character, je kan namelijk geen " echoen in java :S
00137                 tekstTag = "";
00138                 eindTag  = "</woordenset>";
00139                 
00140                 startDocument();
00141                 emit("<woordenset categorie="+quote+categorie+quote+">"); 
00142                 nl();           
00143         }
00144         
00145         public void voegWoordToe(String woord) throws SAXException
00146         {
00147                 emit("<woord>"+woord+"</woord>");
00148                 nl();
00149         }       
00150         
00151         public void schrijfWoordenSet()
00152         {
00153                 try
00154                 {
00155                         out = new FileOutputStream(bestandsNaam);                       
00156                 }
00157                 catch(FileNotFoundException e)
00158                 {
00159                         System.out.println("Foutje bij het openen van de FileOutputStream");
00160                 }
00161                 p = new PrintStream(out);
00162 
00163         
00164         try 
00165                 {
00166                 wOut = new OutputStreamWriter(out, "UTF8");
00167                 }
00168         catch(Throwable t)
00169                 {
00170                  t.printStackTrace();
00171                 }
00172                 
00173                 try
00174                 {
00175                         wOut.write(tekstTag);
00176                         wOut.write(eindTag);
00177                         wOut.flush();
00178                 }
00179                 catch(IOException e)
00180                 {
00181                         e.printStackTrace();
00182                 }
00183         }
00184 }

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