KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * XML input/output support for FindBugs
3  * Copyright (C) 2004,2005 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.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.nio.charset.Charset JavaDoc;
27
28 /**
29  * Write XML to an output stream.
30  *
31  * @author David Hovemeyer
32  */

33 public class OutputStreamXMLOutput implements XMLOutput {
34     private static final String JavaDoc OPENING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
35     private static String JavaDoc getStylesheetCode(String JavaDoc stylesheet) {
36         if (stylesheet == null) return "";
37         return "<?xml-stylesheet type=\"text/xsl\" HREF=\"" + stylesheet + "\"?>\n";
38     }
39
40     private static final MetaCharacterMap textMetaCharacterMap = new MetaCharacterMap();
41     static {
42         textMetaCharacterMap.addMeta('<', "&lt;");
43         textMetaCharacterMap.addMeta('>', "&gt;");
44         textMetaCharacterMap.addMeta('&', "&amp;");
45     }
46
47     private class WriterQuoteMetaCharacters extends QuoteMetaCharacters {
48         public WriterQuoteMetaCharacters(String JavaDoc text) {
49             super(text, textMetaCharacterMap);
50         }
51
52         @Override JavaDoc
53                  public void emitLiteral(String JavaDoc s) throws IOException JavaDoc {
54             out.write(s);
55             newLine = s.endsWith("\n");
56         }
57     }
58
59     private Writer JavaDoc out;
60     private int nestingLevel;
61     private boolean newLine;
62     private String JavaDoc stylesheet;
63     /**
64      * Constructor.
65      * @param os OutputStream to write XML output to
66      */

67     public OutputStreamXMLOutput(OutputStream JavaDoc os) {
68         this(os, null);
69     }
70
71     public OutputStreamXMLOutput(OutputStream JavaDoc os, String JavaDoc stylesheet) {
72         this.out = new OutputStreamWriter JavaDoc(os, Charset.forName("UTF-8"));
73         this.nestingLevel = 0;
74         this.newLine = true;
75     }
76     public void beginDocument() throws IOException JavaDoc {
77         out.write(OPENING);
78         out.write(getStylesheetCode(stylesheet));
79         out.write("\n");
80         newLine = true;
81     }
82
83     public void openTag(String JavaDoc tagName) throws IOException JavaDoc {
84         emitTag(tagName, false);
85     }
86
87     public void openTag(String JavaDoc tagName, XMLAttributeList attributeList) throws IOException JavaDoc {
88         emitTag(tagName, attributeList.toString(), false);
89     }
90
91     public void openCloseTag(String JavaDoc tagName) throws IOException JavaDoc {
92         emitTag(tagName, true);
93     }
94
95     public void openCloseTag(String JavaDoc tagName, XMLAttributeList attributeList) throws IOException JavaDoc {
96         emitTag(tagName, attributeList.toString(), true);
97     }
98
99     public void startTag(String JavaDoc tagName) throws IOException JavaDoc {
100         indent();
101         ++nestingLevel;
102         out.write("<" + tagName);
103     }
104     
105     public void addAttribute(String JavaDoc name, String JavaDoc value) throws IOException JavaDoc {
106         out.write(' ');
107         out.write(name);
108         out.write('=');
109         out.write(XMLAttributeList.getQuotedAttributeValue(value));
110     }
111     
112     public void stopTag(boolean close) throws IOException JavaDoc {
113         if (close) {
114             out.write("/>\n");
115             --nestingLevel;
116             newLine = true;
117         } else {
118             out.write(">");
119             newLine = false;
120         }
121     }
122     
123     private void emitTag(String JavaDoc tagName, boolean close) throws IOException JavaDoc {
124         startTag(tagName);
125         stopTag(close);
126     }
127
128     private void emitTag(String JavaDoc tagName, String JavaDoc attributes, boolean close) throws IOException JavaDoc {
129         startTag(tagName);
130         attributes = attributes.trim();
131         if (attributes.length() > 0) {
132             out.write(" ");
133             out.write(attributes);
134         }
135         stopTag(close);
136     }
137
138     public void closeTag(String JavaDoc tagName) throws IOException JavaDoc {
139         --nestingLevel;
140         if (newLine)
141             indent();
142         out.write("</" + tagName + ">\n");
143         newLine = true;
144     }
145
146     public void writeText(String JavaDoc text) throws IOException JavaDoc {
147         new WriterQuoteMetaCharacters(text).process();
148     }
149
150     public void writeCDATA(String JavaDoc cdata) throws IOException JavaDoc {
151         // We just trust fate that the characters being written
152
// don't contain the string "]]>"
153
out.write("<![CDATA[");
154         out.write(cdata);
155         out.write("]]>");
156         newLine = false;
157     }
158
159     public void finish() throws IOException JavaDoc {
160         out.close();
161     }
162
163     private void indent() throws IOException JavaDoc {
164         if (!newLine)
165             out.write("\n");
166         for (int i = 0; i < nestingLevel; ++i) {
167             out.write(" ");
168         }
169     }
170 }
171
172 // vim:ts=4
173
Popular Tags