KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > xml > Dom4JXMLOutput


1 /*
2  * XML input/output support for FindBugs
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.xml;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25
26 import org.dom4j.Branch;
27 import org.dom4j.Element;
28
29 /**
30  * XMLOutput class to build all or part of a dom4j tree.
31  *
32  * @see XMLOutput
33  * @author David Hovemeyer
34  */

35 public class Dom4JXMLOutput implements XMLOutput {
36     private LinkedList JavaDoc<Branch> stack;
37
38     /**
39      * Constructor.
40      *
41      * @param topLevel the Document or Element that is the root of
42      * the tree to be built
43      */

44     public Dom4JXMLOutput(Branch topLevel) {
45         this.stack = new LinkedList JavaDoc<Branch>();
46         stack.addLast(topLevel);
47     }
48
49     public void beginDocument() {
50     }
51
52     public void openTag(String JavaDoc tagName) {
53         Branch top = stack.getLast();
54         Element element = top.addElement(tagName);
55         stack.addLast(element);
56     }
57
58     public void openTag(String JavaDoc tagName, XMLAttributeList attributeList) {
59         Branch top = stack.getLast();
60         Element element = top.addElement(tagName);
61         stack.addLast(element);
62
63         for (Iterator JavaDoc<XMLAttributeList.NameValuePair> i= attributeList.iterator();
64             i.hasNext(); ) {
65             XMLAttributeList.NameValuePair pair = i.next();
66             element.addAttribute(pair.getName(), pair.getValue());
67         }
68     }
69
70     public void openCloseTag(String JavaDoc tagName) {
71         openTag(tagName);
72         closeTag(tagName);
73     }
74
75     public void openCloseTag(String JavaDoc tagName, XMLAttributeList attributeList) {
76         openTag(tagName, attributeList);
77         closeTag(tagName);
78     }
79
80     public void startTag(String JavaDoc tagName) {
81         Branch top = stack.getLast();
82         Element element = top.addElement(tagName);
83         stack.addLast(element);
84     }
85     
86     public void addAttribute(String JavaDoc name, String JavaDoc value) {
87         Element element = (Element) stack.getLast();
88         element.addAttribute(name, value);
89     }
90     
91     public void stopTag(boolean close) {
92         if ( close ) {
93             closeTag(null);
94         }
95     }
96     
97     public void closeTag(String JavaDoc tagName) {
98         stack.removeLast();
99     }
100
101     public void writeText(String JavaDoc text) {
102         Element top = (Element) stack.getLast();
103         top.addText(text);
104     }
105
106     public void writeCDATA(String JavaDoc cdata) {
107         Element top = (Element) stack.getLast();
108         top.addCDATA(cdata);
109     }
110
111     /**
112      * Add a list of Strings to document as elements
113      * with given tag name to the tree.
114      *
115      * @param tagName the tag name
116      * @param listValues Collection of String values to add
117      */

118     public void writeElementList(String JavaDoc tagName, Collection JavaDoc<String JavaDoc> listValues) {
119         for (String JavaDoc listValue : listValues) {
120             openTag(tagName);
121             writeText(listValue);
122             closeTag(tagName);
123         }
124     }
125
126     /**
127      * Add given object to the tree.
128      *
129      * @param obj the object
130      */

131     public void write(XMLWriteable obj) {
132         try {
133             obj.writeXML(this);
134         } catch (java.io.IOException JavaDoc e) {
135             // Can't really happen
136
}
137     }
138
139     /**
140      * Add a Collection of XMLWriteable objects to the tree.
141      *
142      * @param collection Collection of XMLWriteable objects
143      */

144     public void writeCollection(Collection JavaDoc<? extends XMLWriteable> collection) {
145         for (XMLWriteable obj : collection) {
146             write(obj);
147         }
148     }
149
150     public void finish() {
151     }
152 }
153
154 // vim:ts=4
155
Popular Tags