ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/XMLFormatter.java
Revision: 1.5
Committed: Fri Dec 8 12:24:00 2000 UTC (23 years, 11 months ago) by ab11
Branch: MAIN
Changes since 1.4: +3 -14 lines
Log Message:
woops. Removed tims original overridden constructor as I just added my own.

File Contents

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