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.5 27/11/00 16:53 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.0 $"; |
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){ |
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 |
|
76 |
/** |
77 |
* closeNest will close the currently opened nest (by writing </nest name> to |
78 |
* the xml string. |
79 |
* If already at the root nest then this command is ignored |
80 |
* |
81 |
*/ |
82 |
public void closeNest(){ |
83 |
|
84 |
if ( !myStack.empty() ){ |
85 |
String tmp = (String) myStack.pop(); |
86 |
xmlData += "</"+tmp+">"; |
87 |
} |
88 |
|
89 |
} |
90 |
|
91 |
/** |
92 |
* ReturnXML returns a valid XML string made out of all elements and nests |
93 |
* added since this objects construction. |
94 |
* Will close any currently open nests before returning. |
95 |
*/ |
96 |
public String returnXML(){ |
97 |
|
98 |
// remember to close any open nests first =) |
99 |
while ( !myStack.empty() ){ |
100 |
closeNest(); |
101 |
} |
102 |
|
103 |
// add on HOSTinfo if it is defined |
104 |
if ( hostInfo != null ){ |
105 |
xmlData += "</"+hostInfo+">"; |
106 |
} |
107 |
|
108 |
return xmlData; |
109 |
|
110 |
} |
111 |
|
112 |
//---PRIVATE METHODS--- |
113 |
|
114 |
//---ACCESSOR/MUTATOR METHODS--- |
115 |
|
116 |
//---ATTRIBUTES--- |
117 |
|
118 |
// the string that will be returned in the end |
119 |
private String xmlData; |
120 |
// a stack object to hold the opened nest object |
121 |
private Stack myStack; |
122 |
// a string to hold the hostInfo |
123 |
private String hostInfo; |
124 |
|
125 |
|
126 |
//---STATIC ATTRIBUTES--- |
127 |
|
128 |
} // class |