ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/XMLFormatter.java
Revision: 1.7
Committed: Sat May 18 18:15:57 2002 UTC (22 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.6: +21 -2 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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