cds.xml
Class XmlGen

java.lang.Object
  extended bycds.xml.XmlGen
Direct Known Subclasses:
VOTable

public class XmlGen
extends java.lang.Object

XmlGen : XML generation class This class has basic methods to generate XML formatted data. The class builds the XML stream in a buffer, and outputs it to an OutputStream object. It is possible to flush() the buffer out, allowing to deal with huge data sets. Flushing is independant of having closed the tags. The class keeps track of the opened tags, and is able to close them automatically, depending on the action performed. Most of the methods returns the XmlGen object itself, allowing for 'concatenating' calls : xmlgen.openTag("VOTABLE").addAttr("name","hd 1"); You can choose to indent or not the output. Indentation increases the size of the output, but makes it more readable.

Version:
1.0 Jan 2002
Author:
Marc Wenger/CDS

Field Summary
private static boolean indentlines
           
private static int level
           
private static java.lang.String mainTag
           
private static char[] spaces
           
private static ArrayListStack tagdefstack
           
private static java.io.BufferedWriter wrt
           
private static java.lang.StringBuffer xmldata
           
 
Constructor Summary
XmlGen(java.io.OutputStream os)
          Constructor.
 
Method Summary
 XmlGen addAttr(Attrib[] attlist)
          Adds a list of attributes to the previously defined tag.
 XmlGen addAttr(java.lang.String name, java.lang.String value)
          Adds an attribute to the previously defined tag.
 XmlGen addID(java.lang.String id)
          Adds an ID as attribute to the previously defined tag.
 XmlGen addText(java.lang.String t)
          Adds text to the XML file This will add the closing character ('>') if a tag was stillopened to receive Attributes.
 XmlGen addText(java.lang.String t, boolean conv)
          Adds text to the XML file
 void close()
          close : definitely closes the XML file after having closed all the tags and flushed the buffer.
 XmlGen closeAllTags()
          Closes all remaining opened tags As an XML file must be embedded in one main tag, and as this method closes ALL tags, including the main one, this method can be called only once during an XML file creation, and at its end !
 XmlGen closeTag()
          Closes the current tag , without having to name it
 XmlGen closeTag(java.lang.String n)
          Closes the opened tags with their endtag mark () until finding a tag with the same name as the one specified If several tags with the name name are opened, the process will stop after having encountered the first one.
 XmlGen doctype(java.lang.String maintag, java.lang.String dtd)
          Generates the DOCTYPE tag.
 XmlGen flush()
          Empties the current XML buffer.
 java.lang.String getCurrentTagName()
          Gets the current tag name
 void indent(boolean indent)
          True or false if you wish or not to indent lines indentation is kept to 1 space / level
private  int indentVal(int level)
           
 XmlGen openTag(java.lang.String name)
          Opens a new tag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

indentlines

private static boolean indentlines

mainTag

private static java.lang.String mainTag

tagdefstack

private static ArrayListStack tagdefstack

xmldata

private static java.lang.StringBuffer xmldata

spaces

private static char[] spaces

level

private static int level

wrt

private static java.io.BufferedWriter wrt
Constructor Detail

XmlGen

public XmlGen(java.io.OutputStream os)
Constructor. Opens the main tag after initialisation of standard XML stuff

Parameters:
os - OutputStream object
Method Detail

indentVal

private final int indentVal(int level)

doctype

public XmlGen doctype(java.lang.String maintag,
                      java.lang.String dtd)
Generates the DOCTYPE tag. With the maintag and DTD link

Parameters:
maintag - Main tag for the XML file
dtd - URL of the corresponding DTD
Returns:
the XmlGen object itself

indent

public void indent(boolean indent)
True or false if you wish or not to indent lines indentation is kept to 1 space / level

Parameters:
indent - true or false

openTag

public XmlGen openTag(java.lang.String name)
Opens a new tag. After this call, the tag itself remains open. Another call for adding text or another tag will automatically close the tag with a '>' character or a '/>' string. This allows attributes to be added after an openTag() call, as long as nothing else is added.

Parameters:
name - Name of the tag
Returns:
the XmlGen object itself

addAttr

public XmlGen addAttr(java.lang.String name,
                      java.lang.String value)
Adds an attribute to the previously defined tag. This method can only be called after an openTag() and other addAttr() and AddID() methods.

Parameters:
name - name of the attribute
value - value of the attribute
Returns:
the XmlGen object itself

addID

public XmlGen addID(java.lang.String id)
Adds an ID as attribute to the previously defined tag. This method can only be called as long as a tag is opened.

Parameters:
id - ID value
Returns:
the XmlGen object itself

addAttr

public XmlGen addAttr(Attrib[] attlist)
Adds a list of attributes to the previously defined tag. This method can only be called as long as a tag is opened.

Parameters:
attlist - array of Attrib objects
Returns:
the XmlGen object itself
See Also:
Attrib

addText

public XmlGen addText(java.lang.String t)
Adds text to the XML file This will add the closing character ('>') if a tag was stillopened to receive Attributes. After this call, no more attributes can be added. Conversion of special characters (&, <, >) into their XML counterparts (&, <, >) will occur

Parameters:
t - Text do add. Conversions ('&' into & will occur)
Returns:
the XmlGen object itself

addText

public XmlGen addText(java.lang.String t,
                      boolean conv)
Adds text to the XML file

Parameters:
t - Text do add
conv - true if conversion of special character should be done
Returns:
the XmlGen object itself

getCurrentTagName

public java.lang.String getCurrentTagName()
Gets the current tag name

Returns:
the tag name or null if none exists (this wouldmean that the XML file has been completed, and that the main tag is closed.

closeTag

public XmlGen closeTag(java.lang.String n)
Closes the opened tags with their endtag mark () until finding a tag with the same name as the one specified If several tags with the name name are opened, the process will stop after having encountered the first one.

Parameters:
n - Name of the last tag to close
Returns:
the XmlGen object itself

closeTag

public XmlGen closeTag()
Closes the current tag , without having to name it

Returns:
the XmlGen object itself

closeAllTags

public XmlGen closeAllTags()
Closes all remaining opened tags As an XML file must be embedded in one main tag, and as this method closes ALL tags, including the main one, this method can be called only once during an XML file creation, and at its end !

Returns:
the XmlGen object itself

flush

public XmlGen flush()
Empties the current XML buffer. This is independant of the completeness of the tags. It can be done at any moment and allows to manage very large XML files Without flush(), an XML file may never be written on file. So at least, one such call is mandatory when ou are finished with the XML file, after having closed all the tags.

Returns:
the XmlGen object itself
See Also:
close()

close

public void close()
close : definitely closes the XML file after having closed all the tags and flushed the buffer.


,TÒ>