001: package org.acm.seguin.pmd;
002:
003: import java.io.OutputStream;
004: import java.io.PrintStream;
005: import java.util.Enumeration;
006: import java.util.Iterator;
007:
008: /**
009: * Writes an XML file containing information about rule set and each rule within the rule set.
010: *
011: * @author Donald A. Leckie
012: * @since August 30, 2002
013: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:58 $
014: */
015: public class RuleSetWriter {
016:
017: private PrintStream m_outputStream;
018: private StringBuffer m_line = new StringBuffer(500);
019: private int m_indent;
020:
021: /**
022: *******************************************************************************
023: *
024: * @param outputStream
025: */
026: public RuleSetWriter(OutputStream outputStream) {
027: m_outputStream = new PrintStream(outputStream);
028:
029: m_line.append("<?xml version=\"1.0\" ?>");
030: outputLine();
031: }
032:
033: /**
034: *******************************************************************************
035: *
036: * @param ruleSet
037: */
038: public void write(RuleSet ruleSet) {
039: // <ruleset name="xxxxxx"
040: indent();
041: setupNewLine();
042: m_line.append("<ruleset name=\"");
043: m_line.append(ruleSet.getName());
044: m_line.append('"');
045:
046: //
047: // include="true">
048: //
049: m_line.append(" include=\"");
050: m_line.append(ruleSet.include() ? "true" : "false");
051: m_line.append("\">");
052: outputLine();
053:
054: // <description>
055: // xxxxxxxxx
056: // </description>
057: indent();
058: writeDescription(ruleSet.getDescription());
059: outdent();
060:
061: //
062: // Write each rule.
063: //
064: Iterator rules = ruleSet.getRules().iterator();
065:
066: while (rules.hasNext()) {
067: write((Rule) rules.next());
068: }
069:
070: // </ruleset>
071: setupNewLine();
072: m_line.append("</ruleset>");
073: outputLine();
074: outdent();
075: }
076:
077: /**
078: *******************************************************************************
079: *
080: * @param rule
081: */
082: private void write(Rule rule) {
083: // Write a blank line to separate rules for easier reading.
084: m_outputStream.println("");
085:
086: // <rule name="xxxxxx"
087: indent();
088: setupNewLine();
089: m_line.append("<rule name=\"");
090: m_line.append(rule.getName());
091: m_line.append('"');
092: outputLine();
093:
094: // message="xxxxxx"
095: m_indent += 6;
096: setupNewLine();
097: m_line.append("message=\"");
098: m_line.append(rule.getMessage());
099: m_line.append('"');
100: outputLine();
101:
102: // class="xxxx"
103: setupNewLine();
104: m_line.append("class=\"");
105: m_line.append(rule.getClass().getName());
106: m_line.append('"');
107: outputLine();
108:
109: // include="true">
110: setupNewLine();
111: m_line.append("include=\"");
112: m_line.append(rule.include() ? "true" : "false");
113: m_line.append("\">");
114: outputLine();
115: m_indent -= 6;
116:
117: // <description>
118: // xxxxxxxxx
119: // </description>
120: indent();
121: writeDescription(rule.getDescription());
122: outdent();
123:
124: // <example>
125: // xxxxxxxxxxx
126: // </example>
127: indent();
128: writeExample(rule.getExample());
129: outdent();
130:
131: // <priority>
132: // xxx
133: // </priority>
134: indent();
135: writePriority(rule.getPriority());
136: outdent();
137:
138: // <properties>
139: // <property name="xxxx" value="yyyyy" />
140: // </properties>
141: indent();
142: writeProperties(rule);
143: outdent();
144:
145: // </ruleset>
146: setupNewLine();
147: m_line.append("</rule>");
148: outputLine();
149: outdent();
150: }
151:
152: /**
153: *******************************************************************************
154: *
155: * @param description
156: */
157: private void writeDescription(String description) {
158: // <description>
159: setupNewLine();
160: m_line.append("<description>");
161: outputLine();
162:
163: {
164: // xxxxxxxx
165: indent();
166: setupNewLine();
167: m_line.append(description);
168: outputLine();
169: outdent();
170: }
171:
172: // </description>
173: setupNewLine();
174: m_line.append("</description>");
175: outputLine();
176: }
177:
178: /**
179: *******************************************************************************
180: *
181: * @param example
182: */
183: private void writeExample(String example) {
184: // <example>
185: setupNewLine();
186: m_line.append("<example>");
187: outputLine();
188:
189: {
190: // xxxxxxxx
191: indent();
192: setupNewLineWithoutIndent();
193: m_line.append("<![CDATA[");
194: outputLine();
195: setupNewLineWithoutIndent();
196: m_line.append(example);
197: outputLine();
198: setupNewLineWithoutIndent();
199: m_line.append("]]>");
200: outputLine();
201: outdent();
202: }
203:
204: // </description>
205: setupNewLine();
206: m_line.append("</example>");
207: outputLine();
208: }
209:
210: /**
211: *******************************************************************************
212: *
213: * @param priority
214: */
215: private void writePriority(int priority) {
216: // <priority>
217: setupNewLine();
218: m_line.append("<priority>");
219: outputLine();
220:
221: {
222: // xx
223: indent();
224: setupNewLine();
225: m_line.append(String.valueOf(priority));
226: outputLine();
227: outdent();
228: }
229:
230: // </priority>
231: setupNewLine();
232: m_line.append("</priority>");
233: outputLine();
234: }
235:
236: /**
237: *******************************************************************************
238: *
239: * @param rule
240: */
241: private void writeProperties(Rule rule) {
242: // <properties>
243: setupNewLine();
244: m_line.append("<properties>");
245: outputLine();
246: indent();
247:
248: RuleProperties properties = rule.getProperties();
249: Enumeration keys = properties.keys();
250:
251: while (keys.hasMoreElements()) {
252: String name = (String) keys.nextElement();
253: String value = properties.getValue(name);
254: String valueType = properties.getValueType(name);
255:
256: // <property name="xxxxx" value="yyyyy" />
257: setupNewLine();
258: m_line.append("<property name=\"");
259: m_line.append(name);
260: m_line.append("\" value=\"");
261: m_line.append(value);
262: m_line.append("\" type=\"");
263: m_line.append(valueType);
264: m_line.append("\"/>");
265: outputLine();
266: }
267:
268: outdent();
269: setupNewLine();
270: m_line.append("</properties>");
271: outputLine();
272: }
273:
274: /**
275: *******************************************************************************
276: *
277: */
278: private void indent() {
279: m_indent += 3;
280: }
281:
282: /**
283: *******************************************************************************
284: *
285: */
286: private void outdent() {
287: m_indent -= 3;
288: }
289:
290: /**
291: *******************************************************************************
292: *
293: */
294: private void setupNewLine() {
295: m_line.setLength(0);
296:
297: for (int n = 0; n < m_indent; n++) {
298: m_line.append(' ');
299: }
300: }
301:
302: /**
303: *******************************************************************************
304: *
305: */
306: private void setupNewLineWithoutIndent() {
307: m_line.setLength(0);
308: }
309:
310: /**
311: *******************************************************************************
312: *
313: */
314: private void outputLine() {
315: m_outputStream.println(m_line.toString());
316: }
317: }
|