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: tdb1 $ |
14 |
* @version $Id: XMLFormatter.java,v 1.3 2000/12/07 23:22:17 tdb1 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.3 $"; |
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 |
/** |
65 |
* Public Constructor for the class |
66 |
* Takes a second parameter containing the root attributes. |
67 |
*/ |
68 |
public XMLFormatter(String rootInfo, String rootAttributes){ |
69 |
myStack = new Stack(); |
70 |
xmlData = new String(); |
71 |
xmlData = "<"+rootInfo+" "+rootAttributes+">"; |
72 |
hostInfo = rootInfo; |
73 |
} |
74 |
|
75 |
//---PUBLIC METHODS--- |
76 |
|
77 |
/** |
78 |
* addEement, adds an element to the XML string with attributes |
79 |
* |
80 |
*/ |
81 |
public void addElement(String name, String attributes, String value){ |
82 |
// check that the strings contain valid data first |
83 |
if (( name.length() != 0 ) && ( value.length() != 0 )){ |
84 |
xmlData += "<"+name+" "+attributes+">"+value+"</"+name+">"; |
85 |
} |
86 |
} |
87 |
|
88 |
/** |
89 |
* addEement, adds an element to the XML string |
90 |
* |
91 |
*/ |
92 |
public void addElement(String name, String value){ |
93 |
// check that the strings contain valid data first |
94 |
if (( name.length() != 0 ) && ( value.length() != 0 )){ |
95 |
xmlData += "<"+name+">"+value+"</"+name+">"; |
96 |
} |
97 |
} |
98 |
|
99 |
/** |
100 |
* addNest, adds a nest to the XML and all objects (elements or nests) that |
101 |
* are added will be contained within this nest until closeNest() is called |
102 |
* |
103 |
*/ |
104 |
public void addNest(String name){ |
105 |
xmlData += "<"+name+">"; |
106 |
myStack.push(name); |
107 |
} |
108 |
|
109 |
/** |
110 |
* addString, adds a string into the XML packet no further processing is carried out |
111 |
*/ |
112 |
public void addString(String name){ |
113 |
xmlData += name; |
114 |
} |
115 |
|
116 |
|
117 |
/** |
118 |
* closeNest will close the currently opened nest (by writing </nest name> to |
119 |
* the xml string. |
120 |
* If already at the root nest then this command is ignored |
121 |
* |
122 |
*/ |
123 |
public void closeNest(){ |
124 |
|
125 |
if ( !myStack.empty() ){ |
126 |
String tmp = (String) myStack.pop(); |
127 |
xmlData += "</"+tmp+">"; |
128 |
} |
129 |
|
130 |
} |
131 |
|
132 |
/** |
133 |
* ReturnXML returns a valid XML string made out of all elements and nests |
134 |
* added since this objects construction. |
135 |
* Will close any currently open nests before returning. |
136 |
*/ |
137 |
public String returnXML(){ |
138 |
|
139 |
// remember to close any open nests first =) |
140 |
while ( !myStack.empty() ){ |
141 |
closeNest(); |
142 |
} |
143 |
|
144 |
// add on HOSTinfo if it is defined |
145 |
if ( hostInfo != null ){ |
146 |
xmlData += "</"+hostInfo+">"; |
147 |
} |
148 |
|
149 |
return xmlData; |
150 |
|
151 |
} |
152 |
|
153 |
//---PRIVATE METHODS--- |
154 |
|
155 |
//---ACCESSOR/MUTATOR METHODS--- |
156 |
|
157 |
//---ATTRIBUTES--- |
158 |
|
159 |
// the string that will be returned in the end |
160 |
private String xmlData; |
161 |
// a stack object to hold the opened nest object |
162 |
private Stack myStack; |
163 |
// a string to hold the hostInfo |
164 |
private String hostInfo; |
165 |
|
166 |
|
167 |
//---STATIC ATTRIBUTES--- |
168 |
|
169 |
} // class |