001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.mmtk.utility.statistics;
014
015import org.mmtk.utility.Log;
016import org.vmmagic.pragma.Uninterruptible;
017import org.vmmagic.unboxed.Word;
018
019/**
020 * Utility class for writing statistics out in XML format.
021 */
022@Uninterruptible
023public class Xml {
024  /**
025   * Mark the start of XML output
026   */
027  public static void begin() {
028    Log.writeln("<xml-begin/> <!-- Everything until xml-end is now valid xml -->");
029  }
030
031  /**
032   * Mark the end of XML output
033   */
034  public static void end() {
035    Log.writeln("<xml-end/> <!-- Non-xml data follows ... -->");
036  }
037
038  /**
039   * Closes the innermost XML tag and pops it from the stack.
040   *
041   * @param name the tag's name
042   */
043  public static void closeTag(String name) {
044    Log.write("</"); Log.write(name); Log.writeln(">");
045  }
046
047  /**
048   * Open an XML tag.
049   *
050   * @param name Tag name
051   * @param endTag Should the tag be closed, or left open for
052   *               adding additional attributes
053   */
054  static void openTag(String name, boolean endTag) {
055    openMinorTag(name);
056    if (endTag)
057      closeTag(false);
058  }
059
060  /**
061   * Open a simple XML entity.
062   *
063   * @param name Name of the entity
064   */
065  static void openTag(String name) {
066    openTag(name,true);
067  }
068
069  /**
070   * Output a "stat" entity, with a given name, <code>double</code>value and optionally, units.
071   *
072   * @param name Name of the entity
073   * @param value The value of the entity
074   * @param units The units, or null for no units.
075   */
076  public static void singleValue(String name, double value, String units) {
077    openMinorTag("stat");
078    attribute("name",name);
079    attribute("value",value);
080    if (units != null) attribute("units",units);
081    closeMinorTag();
082  }
083
084  /**
085   * Convenience version of singleValue where units are not specified.
086   *
087   * @param name Name of the entity
088   * @param value The value of the entity
089   */
090  public static void singleValue(String name, double value) {
091    singleValue(name,value,null);
092  }
093
094  /**
095   * Output a "config" entity, with a given name and <code>boolean</code>value.
096   *
097   * @param name Name of the entity
098   * @param value The value of the entity
099   */
100  public static void configItem(String name, boolean value) {
101    openMinorTag("conf");
102    attribute("name",name);
103    attribute("value",value);
104    closeMinorTag();
105  }
106
107  /**
108   * Output a "config" entity, with a given name and <code>String</code>value.
109   *
110   * @param name Name of the entity
111   * @param value The value of the entity
112   */
113  public static void configItem(String name, String value) {
114    openMinorTag("conf");
115    attribute("name",name);
116    attribute("value",value);
117    closeMinorTag();
118  }
119
120  /**
121   * Output a "stat" entity, with a given name, <code>long</code> value and
122   * optionally, units.
123   *
124   * @param name Name of the entity
125   * @param value The value of the entity
126   * @param units The units, or null for no units.
127   */
128  public static void singleValue(String name, long value, String units) {
129    openMinorTag("stat");
130    attribute("name",name);
131    attribute("value",value);
132    if (units != null) attribute("units",units);
133    closeMinorTag();
134  }
135
136  /**
137   * Convenience version of singleValue where units are not specified.
138   *
139   * @param name Name of the entity
140   * @param value The value of the entity
141   */
142  public static void singleValue(String name, long value) {
143    singleValue(name,value,null);
144  }
145
146  /**
147   * Add a word-valued attribute to an open XML tag.
148   *
149   * @param name Name of the entity
150   * @param value The value of the entity
151   */
152  public static void attribute(String name, Word value) {
153    openAttribute(name); Log.write(value); closeAttribute();
154  }
155
156  /**
157   * Add a byte[]-valued attribute to an open XML tag.
158   *
159   * @param name Name of the entity
160   * @param value The value of the entity
161   */
162  public static void attribute(String name, byte[] value) {
163    openAttribute(name); Log.write(value); closeAttribute();
164  }
165
166  /**
167   * Add a String-valued attribute to an open XML tag.
168   *
169   * @param name Name of the entity
170   * @param value The value of the entity
171   */
172  public static void attribute(String name, String value) {
173    openAttribute(name); Log.write(value); closeAttribute();
174  }
175
176  /**
177   * Add a boolean-valued attribute to an open XML tag.
178   *
179   * @param name Name of the entity
180   * @param value The value of the entity
181   */
182  public static void attribute(String name, boolean value) {
183    openAttribute(name); Log.write(value); closeAttribute();
184  }
185
186  /**
187   * Add a double-valued attribute to an open XML tag.
188   *
189   * @param name Name of the entity
190   * @param value The value of the entity
191   */
192  public static void attribute(String name, double value) {
193    openAttribute(name); Log.write(value); closeAttribute();
194  }
195
196  /**
197   * Add a long-valued attribute to an open XML tag.
198   *
199   * @param name Name of the entity
200   * @param value The value of the entity
201   */
202  public static void attribute(String name, long value) {
203    openAttribute(name); Log.write(value); closeAttribute();
204  }
205
206  /**
207   * Add an int-valued attribute to an open XML tag.
208   *
209   * @param name Name of the entity
210   * @param value The value of the entity
211   */
212  public static void attribute(String name, int value) {
213    openAttribute(name); Log.write(value); closeAttribute();
214  }
215
216  /**
217   * Close an attribute (actually a simple close-quote)
218   */
219  public static void closeAttribute() {
220    Log.write("\"");
221  }
222
223  /**
224   * Open an attribute (write "{name}=\")
225   *
226   * @param name Name of the entity
227   */
228  public static void openAttribute(String name) {
229    Log.write(" "); Log.write(name); Log.write("=\"");
230  }
231
232  /**
233   * Start a tag
234   */
235  public static void startTag() {
236    Log.write("<");
237  }
238
239  /**
240   * End a tag, optionally closing it (if it is a simple entity)
241   *
242   * @param close If true, close the tag with "/&gt;" rather than "&gt;"
243   */
244  public static void closeTag(boolean close) {
245    closeTag(close,true);
246  }
247
248  /**
249   * End a tag, optionally closing it (if it is a simple entity),
250   * and optionally printing end-of-line
251   *
252   * @param close If true, close the tag with "/&gt;" rather than "&gt;"
253   * @param endLine If true end the current line.
254   */
255  public static void closeTag(boolean close, boolean endLine) {
256    if (close) Log.write("/");
257    Log.write(">");
258    if (endLine) Log.writeln();
259  }
260
261  /**
262   * Close a tag with a "/&gt;"
263   */
264  public static void closeMinorTag() {
265    closeTag(true,true);
266  }
267
268  /**
269   * Open a tag without pushing it on the tag stack - must end this
270   * with a call to closeMinorTag()
271   *
272   * @param name Name of the entity
273   */
274  public static void openMinorTag(String name) {
275    Log.write("<"); Log.write(name);
276  }
277
278  /**
279   * Open an XML comment
280   */
281  public static void openComment() {
282    Log.write("<!-- ");
283  }
284
285  /**
286   * Close an XML comment
287   */
288  public static void closeComment() {
289    Log.write(" -->");
290  }
291
292  /**
293   * Add a comment, bracketing it with open- and close-comment tags.
294   *
295   * @param comment The comment.
296   */
297  public static void comment(String comment) {
298    openComment();
299    Log.write(comment);
300    closeComment();
301    Log.writeln();
302  }
303
304}