ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/XMLFormatter.java
Revision: 1.2
Committed: Thu Dec 7 18:25:53 2000 UTC (23 years, 11 months ago) by ab11
Branch: MAIN
Changes since 1.1: +10 -2 lines
Log Message:
Added a addString() function.

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 ab11 1.2 * @version $Id: XMLFormatter.java,v 1.1 2000/11/27 19:44:00 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 ab11 1.2 public final String REVISION = "$Revision: 1.1 $";
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    
36     myStack = new Stack();
37     xmlData = new String();
38     xmlData = "<"+rootInfo+">";
39     hostInfo = rootInfo;
40     }
41    
42     /**
43     * Public no-arg Constructor for the class
44     * Does not enclose the XML in <data></data> where data is a wrapper
45     */
46     public XMLFormatter(){
47     myStack = new Stack();
48     xmlData = new String();
49     hostInfo = null;
50     }
51    
52    
53     //---PUBLIC METHODS---
54    
55     /**
56     * addEement, adds an element to the XML string
57     *
58     */
59     public void addElement(String name, String value){
60     // check that the strings contain valid data first
61     if (( name.length() != 0 ) && ( value.length() != 0 )){
62     xmlData += "<"+name+">"+value+"</"+name+">";
63     }
64     }
65    
66     /**
67     * addNest, adds a nest to the XML and all objects (elements or nests) that
68     * are added will be contained within this nest until closeNest() is called
69     *
70     */
71     public void addNest(String name){
72     xmlData += "<"+name+">";
73     myStack.push(name);
74     }
75 ab11 1.2
76     /**
77     * addString, adds a string into the XML packet no further processing is carried out
78     */
79     public void addString(String name){
80     xmlData += name;
81     }
82    
83 ab11 1.1
84     /**
85     * closeNest will close the currently opened nest (by writing </nest name> to
86     * the xml string.
87     * If already at the root nest then this command is ignored
88     *
89     */
90     public void closeNest(){
91    
92     if ( !myStack.empty() ){
93     String tmp = (String) myStack.pop();
94     xmlData += "</"+tmp+">";
95     }
96    
97     }
98    
99     /**
100     * ReturnXML returns a valid XML string made out of all elements and nests
101     * added since this objects construction.
102     * Will close any currently open nests before returning.
103     */
104     public String returnXML(){
105    
106     // remember to close any open nests first =)
107     while ( !myStack.empty() ){
108     closeNest();
109     }
110    
111     // add on HOSTinfo if it is defined
112     if ( hostInfo != null ){
113     xmlData += "</"+hostInfo+">";
114     }
115    
116     return xmlData;
117    
118     }
119    
120     //---PRIVATE METHODS---
121    
122     //---ACCESSOR/MUTATOR METHODS---
123    
124     //---ATTRIBUTES---
125    
126     // the string that will be returned in the end
127     private String xmlData;
128     // a stack object to hold the opened nest object
129     private Stack myStack;
130     // a string to hold the hostInfo
131     private String hostInfo;
132    
133    
134     //---STATIC ATTRIBUTES---
135    
136     } // class