00001
00002
00003
00004
00005
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 TeamXMLWriter extends XMLWriter
00026 {
00027 private int bewerkRonde;
00028 private String bewerkTeam;
00029
00030 private boolean bewerken = false;
00031 private boolean teamGevonden = false;
00032 private boolean rondeGevonden = false;
00033 private boolean tijdGevonden = false;
00034 private boolean scoreGevonden = false;
00035
00036 private int oudeTijd;
00037 private int nieuweTijd;
00038
00039 public TeamXMLWriter(String bestandsNaam)
00040 {
00041 super(bestandsNaam);
00042 }
00043
00044 public boolean laadBestand(String bestandsNaam)
00045 {
00046 boolean gelezen = false;
00047
00048 try
00049 {
00050
00051 saxParser = factory.newSAXParser();
00052 file = new File(bestandsNaam);
00053 if (file.length() == 0)
00054 {
00055 tekstTag = "";
00056 eindTag = "</config>";
00057
00058 startDocument();
00059 }
00060 else
00061 {
00062 saxParser.parse(file, handler);
00063 }
00064
00065
00066 gelezen = true;
00067 }
00068 catch (Throwable t)
00069 {
00070 t.printStackTrace();
00071 gelezen = false;
00072 }
00073
00074 return gelezen;
00075 }
00076
00077 public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException
00078 {
00079 String eName = sName;
00080 if ("".equals(eName)) eName = qName;
00081
00082 if (eName.equals("ronde"))
00083 rondeGevonden = true;
00084
00085 if (eName.equals("tijd"))
00086 tijdGevonden = true;
00087
00088 if (eName.equals("score"))
00089 scoreGevonden = true;
00090
00091 emit("<"+eName);
00092 if (attrs != null) {
00093 for (int i = 0; i < attrs.getLength(); i++) {
00094 String aName = attrs.getLocalName(i);
00095 if ("".equals(aName)) aName = attrs.getQName(i);
00096 emit(" ");
00097
00098 if (attrs.getValue(i).equals(bewerkTeam) && bewerken == true)
00099 {
00100 teamGevonden = true;
00101 }
00102
00103 emit(aName+"=\""+attrs.getValue(i)+"\"");
00104 }
00105 }
00106 emit(">");
00107 }
00108
00109 public void endElement(String namespaceURI, String sName, String qName) throws SAXException
00110 {
00111 if (qName.equals("team"))
00112 teamGevonden = false;
00113
00114 if (qName.equals("ronde"))
00115 rondeGevonden = false;
00116
00117 if (qName.equals("tijd"))
00118 tijdGevonden = false;
00119
00120 if (qName.equals("score"))
00121 scoreGevonden = false;
00122
00123 if (qName.equals("config"))
00124 eindTag = "</config>";
00125 else
00126 emit("</"+qName+">");
00127 }
00128
00129 public void characters(char buf[], int offset, int len) throws SAXException
00130 {
00131
00132
00133
00134
00135
00136
00137
00138 String s = new String(buf, offset, len);
00139
00140 int oudeScore = bewerkRonde - 1;
00141
00142 String nScore = "" + bewerkRonde;
00143 String oScore = "" + oudeScore;
00144
00145 String nieuweS;
00146
00147 char ns = nScore.charAt(0);
00148 char os = oScore.charAt(0);
00149
00150 nieuweS = s;
00151
00152 if (teamGevonden && rondeGevonden)
00153 {
00154 nieuweS = s.replace(os, ns);
00155 }
00156
00157 if (teamGevonden && tijdGevonden)
00158 {
00159 String oTijd, nTijd;
00160 char oT, nT;
00161
00162 oTijd = ""+oudeTijd;
00163 nTijd = ""+nieuweTijd;
00164
00165 oT = oTijd.charAt(0);
00166 nT = nTijd.charAt(0);
00167
00168 nieuweS = s.replaceAll(oTijd, nTijd);
00169 }
00170
00171 emit(nieuweS);
00172 }
00173
00174 public void startDocument()
00175 throws SAXException
00176 {
00177 emit("<?xml version='1.0' encoding='UTF-8'?>");
00178 nl();
00179 }
00180
00181 public void endDocument()
00182 throws SAXException
00183 {
00184
00185 nl();
00186
00187
00188
00189
00190 }
00191
00192 public void emit(String s)
00193 throws SAXException
00194 {
00195
00196 tekstTag += s;
00197
00198
00199
00200
00201
00202 }
00203
00204
00205 public void nl()
00206 throws SAXException
00207 {
00208 String lineEnd = System.getProperty("line.separator");
00209 tekstTag += lineEnd;
00210 }
00211
00212 public void nieuweConfig(String naam) throws SAXException
00213 {
00214 tekstTag = "";
00215 eindTag = "</config>";
00216
00217 startDocument();
00218 emit("<config>");
00219 nl();
00220 }
00221
00222 public void voegNieuwTeamToe(String naam, String speler1, String speler2) throws SAXException
00223 {
00224
00225 char quote = (char) 34;
00226
00227 emit("<team naam="+quote+naam+quote+">");
00228 nl();
00229 emit("<score>650</score>");
00230 nl();
00231 emit("<tijd>15</tijd>");
00232 nl();
00233 emit("<ronde>0</ronde>");
00234 nl();
00235 emit("<speler naam="+quote+speler1+quote+"></speler>");
00236 nl();
00237 emit("<speler naam="+quote+speler2+quote+"></speler>");
00238 nl();
00239 emit("</team>");
00240 nl();
00241 }
00242
00243 public void schrijfTeam()
00244 {
00245 try
00246 {
00247 out = new FileOutputStream(bestandsNaam);
00248 }
00249 catch(FileNotFoundException e)
00250 {
00251 System.out.println("Foutje bij het openen van de FileOutputStream");
00252 }
00253 p = new PrintStream(out);
00254
00255
00256 try
00257 {
00258 wOut = new OutputStreamWriter(out, "UTF8");
00259 }
00260 catch(Throwable t)
00261 {
00262 t.printStackTrace();
00263 }
00264
00265 try
00266 {
00267 wOut.write(tekstTag);
00268 wOut.write(eindTag);
00269 wOut.flush();
00270 }
00271 catch(IOException e)
00272 {
00273 e.printStackTrace();
00274 }
00275
00276 }
00277
00278 public void bewerkTeamRonde(String teamNaam, int ronde, int oudeTijd, int nieuweTijd)
00279 {
00280 bewerken = true;
00281 bewerkRonde = ronde;
00282 bewerkTeam = teamNaam;
00283 this.oudeTijd = oudeTijd;
00284 this.nieuweTijd = nieuweTijd;
00285
00286 tekstTag = "";
00287 laadBestand("XML/config.xml");
00288 schrijfTeam();
00289 }
00290
00291 public void setBestandsNaam(String bestandsNaam)
00292 {
00293 this.bestandsNaam = bestandsNaam;
00294 }
00295
00296 }