Source code: org/alicebot/server/core/util/XMLWriter.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.server.core.util;
6
7 import java.io.*;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 import org.alicebot.server.core.logging.Log;
11
12 // Referenced classes of package org.alicebot.server.core.util:
13 // XMLResourceSpec, Toolkit, Trace
14
15 public class XMLWriter
16 {
17
18 public XMLWriter()
19 {
20 }
21
22 public static void write(String s, XMLResourceSpec xmlresourcespec)
23 {
24 RandomAccessFile randomaccessfile = null;
25 Toolkit.checkOrCreate(xmlresourcespec.path, "xml file");
26 try
27 {
28 randomaccessfile = new RandomAccessFile(xmlresourcespec.path, "rw");
29 }
30 catch(FileNotFoundException filenotfoundexception)
31 {
32 Log.userinfo("Can't write to file \"" + xmlresourcespec.path + "\".", Log.ERROR);
33 return;
34 }
35 long l = 0L;
36 try
37 {
38 l = randomaccessfile.length();
39 }
40 catch(IOException ioexception)
41 {
42 Log.userinfo("Error reading file \"" + xmlresourcespec.path + "\".", Log.ERROR);
43 }
44 String s1 = "</" + xmlresourcespec.root + ">" + LINE_SEPARATOR;
45 if(l == 0L)
46 {
47 try
48 {
49 randomaccessfile.writeBytes("<?xml version=\"1.0\" encoding=\"");
50 if(xmlresourcespec.encoding != null)
51 randomaccessfile.writeBytes(xmlresourcespec.encoding);
52 else
53 randomaccessfile.writeBytes("UTF-8");
54 randomaccessfile.writeBytes("\"?>" + LINE_SEPARATOR);
55 if(xmlresourcespec.stylesheet != null)
56 randomaccessfile.writeBytes("<?xml-stylesheet type=\"text/xsl\" href=\"" + xmlresourcespec.stylesheet + "\"?>" + LINE_SEPARATOR);
57 if(xmlresourcespec.dtd != null)
58 randomaccessfile.writeBytes(xmlresourcespec.dtd + LINE_SEPARATOR);
59 randomaccessfile.writeBytes("<" + xmlresourcespec.root);
60 if(xmlresourcespec.backlink != null)
61 randomaccessfile.writeBytes(" backlink=\"" + xmlresourcespec.backlink + "\"");
62 if(xmlresourcespec.starttime != null)
63 randomaccessfile.writeBytes(" starttime=\"" + xmlresourcespec.starttime + "\"");
64 randomaccessfile.writeBytes(">" + LINE_SEPARATOR);
65 }
66 catch(IOException ioexception1)
67 {
68 Log.userinfo("Error writing to \"" + xmlresourcespec.path + "\".", Log.ERROR);
69 return;
70 }
71 } else
72 {
73 long l1 = l - (long)s1.length();
74 try
75 {
76 randomaccessfile.seek(l1);
77 }
78 catch(IOException ioexception3)
79 {
80 Log.userinfo("Error reading \"" + xmlresourcespec.path + "\".", Log.ERROR);
81 return;
82 }
83 }
84 try
85 {
86 if(xmlresourcespec.encoding == null)
87 randomaccessfile.write(s.getBytes("UTF-8"));
88 else
89 randomaccessfile.write(s.getBytes(xmlresourcespec.encoding));
90 randomaccessfile.writeBytes(s1);
91 randomaccessfile.close();
92 }
93 catch(IOException ioexception2)
94 {
95 Log.userinfo("Error writing to \"" + xmlresourcespec.encoding + "\".", Log.ERROR);
96 return;
97 }
98 }
99
100 public static void rollover(XMLResourceSpec xmlresourcespec)
101 {
102 if(xmlresourcespec == null)
103 {
104 Trace.userinfo("No resource found to roll over.");
105 return;
106 }
107 if(xmlresourcespec.description != null)
108 Trace.devinfo("Rolling over " + xmlresourcespec.description + ".");
109 Date date = new Date();
110 File file = new File(xmlresourcespec.path);
111 String s = file.getParent();
112 String s1 = file.getName();
113 int i = s1.lastIndexOf('.');
114 String s2 = (new SimpleDateFormat("yyyy-MM-dd-HHmmss")).format(date);
115 if(i > 0)
116 xmlresourcespec.backlink = s1.substring(0, i) + "-end-" + s2 + s1.substring(i);
117 else
118 xmlresourcespec.backlink = s1 + "-end-" + s2;
119 xmlresourcespec.starttime = (new SimpleDateFormat("yyyy-MM-dd H:mm:ss")).format(date);
120 File file1 = new File(s + File.separator + xmlresourcespec.backlink);
121 (new File(xmlresourcespec.path)).renameTo(file1);
122 }
123
124 private static final String ENC_UTF8 = "UTF-8";
125 private static final String XML_PI_START = "<?xml version=\"1.0\" encoding=\"";
126 private static final String STYLESHEET_PI_START = "<?xml-stylesheet type=\"text/xsl\" href=\"";
127 private static final String PI_END = "\"?>";
128 private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
129 private static final String OPEN_MARKER_START = "<";
130 private static final String CLOSE_MARKER_START = "</";
131 private static final String NONATOMIC_MARKER_END = ">";
132 private static final String RW = "rw";
133 private static final String XMLFILE = "xml file";
134 private static final String BACKLINK_EQUALS = " backlink=\"";
135 private static final String STARTTIME_EQUALS = " starttime=\"";
136 private static final String QUOTE_MARK = "\"";
137 private static final String LINK_DATE_FORMAT = "yyyy-MM-dd-HHmmss";
138 private static final String STARTTIME_DATE_FORMAT = "yyyy-MM-dd H:mm:ss";
139
140 }