ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/XMLFormatter.java
Revision: 1.3
Committed: Thu Dec 7 23:22:17 2000 UTC (23 years, 9 months ago) by tdb
Branch: MAIN
Changes since 1.2: +12 -3 lines
Log Message:
Added a second constructor to take root attributes.

File Contents

# User Rev Content
1 ab11 1.1 //---PACKAGE DECLARATION---
2    
3     //---IMPORTS---
4     import java.util.*;
5    
6     /**
7     * Formats data into valid XML
8     * By calling either addElement() or addNest() you can use this
9     * class to create valid xml. When all elements have been added
10     * calling returnXML() will return a string containing the xml.
11     *
12     *
13     * @author $Author: ab11 $
14 tdb 1.3 * @version $Id: XMLFormatter.java,v 1.2 2000/12/07 18:25:53 ab11 Exp $
15 ab11 1.1 */
16     class XMLFormatter {
17    
18     //---FINAL ATTRIBUTES---
19    
20     /**
21     * The current CVS revision of this class
22     */
23 tdb 1.3 public final String REVISION = "$Revision: 1.2 $";
24 ab11 1.1
25     //---STATIC METHODS---
26    
27     //---CONSTRUCTORS---
28    
29     /**
30     * Public Constructor for the class
31     * Takes in one arguement which can contain any valid non xml character ( "<",">" )
32     * an example for rootInfo would be "Host"
33     */
34     public XMLFormatter(String rootInfo){
35     myStack = new Stack();
36     xmlData = new String();
37     xmlData = "<"+rootInfo+">";
38     hostInfo = rootInfo;
39     }
40    
41     /**
42     * Public no-arg Constructor for the class
43     * Does not enclose the XML in <data></data> where data is a wrapper
44     */
45     public XMLFormatter(){
46     myStack = new Stack();
47     xmlData = new String();
48     hostInfo = null;
49     }
50    
51 tdb 1.3 /**
52     * Public Constructor for the class
53     * Takes a second parameter containing the root attributes.
54     */
55     public XMLFormatter(String rootInfo, String rootAttributes){
56     myStack = new Stack();
57     xmlData = new String();
58     xmlData = "<"+rootInfo+" "+rootAttributes+">";
59     hostInfo = rootInfo;
60     }
61 ab11 1.1
62     //---PUBLIC METHODS---
63    
64     /**
65     * addEement, adds an element to the XML string
66     *
67     */
68     public void addElement(String name, String value){
69     // check that the strings contain valid data first
70     if (( name.length() != 0 ) && ( value.length() != 0 )){
71     xmlData += "<"+name+">"+value+"</"+name+">";
72     }
73     }
74    
75     /**
76     * addNest, adds a nest to the XML and all objects (elements or nests) that
77     * are added will be contained within this nest until closeNest() is called
78     *
79     */
80     public void addNest(String name){
81     xmlData += "<"+name+">";
82     myStack.push(name);
83     }
84 ab11 1.2
85     /**
86     * addString, adds a string into the XML packet no further processing is carried out
87     */
88     public void addString(String name){
89     xmlData += name;
90     }
91    
92 ab11 1.1
93     /**
94     * closeNest will close the currently opened nest (by writing </nest name> to
95     * the xml string.
96     * If already at the root nest then this command is ignored
97     *
98     */
99     public void closeNest(){
100    
101     if ( !myStack.empty() ){
102     String tmp = (String) myStack.pop();
103     xmlData += "</"+tmp+">";
104     }
105    
106     }
107    
108     /**
109     * ReturnXML returns a valid XML string made out of all elements and nests
110     * added since this objects construction.
111     * Will close any currently open nests before returning.
112     */
113     public String returnXML(){
114    
115     // remember to close any open nests first =)
116     while ( !myStack.empty() ){
117     closeNest();
118     }
119    
120     // add on HOSTinfo if it is defined
121     if ( hostInfo != null ){
122     xmlData += "</"+hostInfo+">";
123     }
124    
125     return xmlData;
126    
127     }
128    
129     //---PRIVATE METHODS---
130    
131     //---ACCESSOR/MUTATOR METHODS---
132    
133     //---ATTRIBUTES---
134    
135     // the string that will be returned in the end
136     private String xmlData;
137     // a stack object to hold the opened nest object
138     private Stack myStack;
139     // a string to hold the hostInfo
140     private String hostInfo;
141    
142    
143     //---STATIC ATTRIBUTES---
144    
145     } // class