| 1 |
/* |
| 2 |
* i-scream central monitoring system |
| 3 |
* http://www.i-scream.org.uk |
| 4 |
* Copyright (C) 2000-2002 i-scream |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
//---PACKAGE DECLARATION--- |
| 22 |
|
| 23 |
//---IMPORTS--- |
| 24 |
import java.util.*; |
| 25 |
|
| 26 |
/** |
| 27 |
* Formats data into valid XML |
| 28 |
* By calling either addElement() or addNest() you can use |
| 29 |
* this class to create valid xml. When all elements have |
| 30 |
* been added calling returnXML() will return a string |
| 31 |
* containing the xml. |
| 32 |
* |
| 33 |
* |
| 34 |
* @author $Author: tdb $ |
| 35 |
* @version $Id: XMLFormatter.java,v 1.7 2002/05/18 18:15:57 tdb Exp $ |
| 36 |
*/ |
| 37 |
class XMLFormatter { |
| 38 |
|
| 39 |
//---FINAL ATTRIBUTES--- |
| 40 |
|
| 41 |
/** |
| 42 |
* The current CVS revision of this class |
| 43 |
*/ |
| 44 |
public final String REVISION = "$Revision: 1.7 $"; |
| 45 |
|
| 46 |
//---STATIC METHODS--- |
| 47 |
|
| 48 |
//---CONSTRUCTORS--- |
| 49 |
|
| 50 |
/** |
| 51 |
* Public Constructor for the class |
| 52 |
* Takes in one arguement which can contain any valid non xml character ( "<",">" ) |
| 53 |
* an example for rootInfo would be "Host" |
| 54 |
*/ |
| 55 |
public XMLFormatter(String rootInfo, String attributes){ |
| 56 |
|
| 57 |
myStack = new Stack(); |
| 58 |
xmlData = new String(); |
| 59 |
xmlData = "<"+rootInfo+" "+attributes+">"; |
| 60 |
hostInfo = rootInfo; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Public Constructor for the class |
| 65 |
* Takes in one arguement which can contain any valid non xml character ( "<",">" ) |
| 66 |
* an example for rootInfo would be "Host" |
| 67 |
*/ |
| 68 |
public XMLFormatter(String rootInfo){ |
| 69 |
myStack = new Stack(); |
| 70 |
xmlData = new String(); |
| 71 |
xmlData = "<"+rootInfo+">"; |
| 72 |
hostInfo = rootInfo; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Public no-arg Constructor for the class |
| 77 |
* Does not enclose the XML in <data></data> where data is a wrapper |
| 78 |
*/ |
| 79 |
public XMLFormatter(){ |
| 80 |
myStack = new Stack(); |
| 81 |
xmlData = new String(); |
| 82 |
hostInfo = null; |
| 83 |
} |
| 84 |
|
| 85 |
//---PUBLIC METHODS--- |
| 86 |
|
| 87 |
/** |
| 88 |
* addEement, adds an element to the XML string with attributes |
| 89 |
* |
| 90 |
*/ |
| 91 |
public void addElement(String name, String attributes, String value){ |
| 92 |
// check that the strings contain valid data first |
| 93 |
if (( name.length() != 0 ) && ( value.length() != 0 )){ |
| 94 |
xmlData += "<"+name+" "+attributes+">"+value+"</"+name+">"; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* addEement, adds an element to the XML string |
| 100 |
* |
| 101 |
*/ |
| 102 |
public void addElement(String name, String value){ |
| 103 |
// check that the strings contain valid data first |
| 104 |
if (( name.length() != 0 ) && ( value.length() != 0 )){ |
| 105 |
xmlData += "<"+name+">"+value+"</"+name+">"; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* addNest, adds a nest to the XML and all objects (elements or nests) that |
| 111 |
* are added will be contained within this nest until closeNest() is called |
| 112 |
* |
| 113 |
*/ |
| 114 |
public void addNest(String name){ |
| 115 |
xmlData += "<"+name+">"; |
| 116 |
myStack.push(name); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* addString, adds a string into the XML packet no further processing is carried out |
| 121 |
*/ |
| 122 |
public void addString(String name){ |
| 123 |
xmlData += name; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* closeNest will close the currently opened nest (by writing </nest name> to |
| 129 |
* the xml string. |
| 130 |
* If already at the root nest then this command is ignored |
| 131 |
* |
| 132 |
*/ |
| 133 |
public void closeNest(){ |
| 134 |
|
| 135 |
if ( !myStack.empty() ){ |
| 136 |
String tmp = (String) myStack.pop(); |
| 137 |
xmlData += "</"+tmp+">"; |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* ReturnXML returns a valid XML string made out of all elements and nests |
| 144 |
* added since this objects construction. |
| 145 |
* Will close any currently open nests before returning. |
| 146 |
*/ |
| 147 |
public String returnXML(){ |
| 148 |
|
| 149 |
// remember to close any open nests first =) |
| 150 |
while ( !myStack.empty() ){ |
| 151 |
closeNest(); |
| 152 |
} |
| 153 |
|
| 154 |
// add on HOSTinfo if it is defined |
| 155 |
if ( hostInfo != null ){ |
| 156 |
xmlData += "</"+hostInfo+">"; |
| 157 |
} |
| 158 |
|
| 159 |
return xmlData; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
//---PRIVATE METHODS--- |
| 164 |
|
| 165 |
//---ACCESSOR/MUTATOR METHODS--- |
| 166 |
|
| 167 |
//---ATTRIBUTES--- |
| 168 |
|
| 169 |
// the string that will be returned in the end |
| 170 |
private String xmlData; |
| 171 |
// a stack object to hold the opened nest object |
| 172 |
private Stack myStack; |
| 173 |
// a string to hold the hostInfo |
| 174 |
private String hostInfo; |
| 175 |
|
| 176 |
|
| 177 |
//---STATIC ATTRIBUTES--- |
| 178 |
|
| 179 |
} // class |