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