1 package net.sf.logdistiller.reports;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.IOException;
18 import java.io.Writer;
19 import java.util.Iterator;
20
21 import org.jdom.*;
22 import org.jdom.output.Format;
23 import org.jdom.output.XMLOutputter;
24
25 import net.sf.logdistiller.LogDistillation;
26 import net.sf.logdistiller.LogDistiller;
27 import net.sf.logdistiller.ReportFormat;
28
29
30
31
32
33
34 public class XmlReport
35 extends ReportFormat
36 {
37 public XmlReport()
38 {
39 super( "xml" );
40 }
41
42 public String getContentType()
43 {
44 return "text/xml";
45 }
46
47 public String getFileExtension()
48 {
49 return "xml";
50 }
51
52 public void report( LogDistillation ld, Writer output )
53 throws IOException
54 {
55 LogDistiller definition = ld.getDefinition();
56
57 Element report = new Element( "logdistiller-report" );
58 Element elmt = new Element( "logdistiller" );
59 elmt.setAttribute( "id", definition.getOutput().getId() );
60 elmt.setAttribute( "description", definition.getDescription() );
61 elmt.setAttribute( "logtype", definition.getLogType().getId() );
62 elmt.setAttribute( "version", ld.getVersion() );
63 report.addContent( elmt );
64
65 elmt = new Element( "logdistillation" );
66 if ( ld.getContent() != null )
67 {
68 elmt.setAttribute( "content", ld.getContent() );
69 }
70 String logsUrl = definition.getOutput().getUrl();
71 if ( logsUrl != null )
72 {
73 elmt.setAttribute( "logsUrl", logsUrl );
74 }
75 elmt.setAttribute( "beginTime", String.valueOf( ld.getBeginTime() ) );
76 elmt.setAttribute( "endTime", String.valueOf( ld.getEndTime() ) );
77 elmt.setAttribute( "eventCount", String.valueOf( ld.getEventCount() ) );
78 elmt.setAttribute( "eventBytes", String.valueOf( ld.getEventBytes() ) );
79 elmt.setAttribute( "skippedEventCount", String.valueOf( ld.getSkippedEventCount() ) );
80 elmt.setAttribute( "skippedEventBytes", String.valueOf( ld.getSkippedEventBytes() ) );
81 Iterator iter = definition.getWarnings().iterator();
82 while ( iter.hasNext() )
83 {
84 elmt.addContent( new Element( "warning" ).setText( (String) iter.next() ) );
85 }
86 report.addContent( elmt );
87
88
89 Iterator categoriesIterator = ld.listCategoriesToReport().iterator();
90 while ( categoriesIterator.hasNext() )
91 {
92 LogDistillation.Category category = (LogDistillation.Category) categoriesIterator.next();
93 elmt.addContent( newCategory( category ) );
94 }
95
96
97 Iterator groupsIterator = ld.listGroupsToReport().iterator();
98 while ( groupsIterator.hasNext() )
99 {
100 LogDistillation.Group group = (LogDistillation.Group) groupsIterator.next();
101 elmt.addContent( newGroup( group ) );
102 }
103
104 new XMLOutputter( Format.getPrettyFormat() ).output( new Document( report ), output );
105 }
106
107 private Element newCategory( LogDistillation.Category category )
108 {
109 Element elmt = new Element( "category" );
110 LogDistiller.Category definition = category.getDefinition();
111 elmt.setAttribute( "id", definition.getId() );
112 elmt.setAttribute( "description", definition.getDescription() );
113 elmt.setAttribute( "eventCount", String.valueOf( category.sumEventCount() ) );
114 elmt.setAttribute( "eventBytes", String.valueOf( category.sumBytes() ) );
115 Iterator iter = category.listGroupsToReport().iterator();
116 while ( iter.hasNext() )
117 {
118 LogDistillation.Group group = (LogDistillation.Group) iter.next();
119 elmt.addContent( newGroup( group ) );
120 }
121 return elmt;
122 }
123
124 private Element newGroup( LogDistillation.Group group )
125 {
126 Element elmt = new Element( "group" );
127 LogDistiller.Group definition = group.getDefinition();
128 elmt.setAttribute( "id", definition.getId() );
129 elmt.setAttribute( "description", definition.getDescription() );
130 elmt.setAttribute( "eventCount", String.valueOf( group.getEventCount() ) );
131 elmt.setAttribute( "eventBytes", String.valueOf( group.getBytes() ) );
132 if ( definition.continueProcessing() )
133 {
134 elmt.setAttribute( "continueProcessing", "true" );
135 }
136 addPluginsGlobalReport( group, new XmlPluginReport( elmt ) );
137 return elmt;
138 }
139
140 public void report( LogDistillation.Group group, Writer output )
141 throws IOException
142 {
143 LogDistillation ld = group.getLogdistillation();
144 LogDistiller.Group definition = group.getDefinition();
145 Element elmt;
146
147 Element report = new Element( "logdistiller-group-report" );
148 report.addContent( elmt = new Element( "group" ) );
149 elmt.setAttribute( "id", definition.getId() );
150 elmt.setAttribute( "description", definition.getDescription() );
151 elmt.setAttribute( "continueProcessing", definition.continueProcessing() ? "true" : "false" );
152 definition.dumpConditions( elmt );
153
154 report.addContent( elmt = new Element( "logdistillation" ) );
155 if ( ld.getContent() != null )
156 {
157 elmt.setAttribute( "content", ld.getContent() );
158 }
159 String logsUrl = ld.getDefinition().getOutput().getUrl();
160 if ( logsUrl != null )
161 {
162 elmt.setAttribute( "logsUrl", logsUrl );
163 }
164 elmt.setAttribute( "eventCount", String.valueOf( ld.getEventCount() ) );
165 elmt.setAttribute( "eventBytes", String.valueOf( ld.getEventBytes() ) );
166
167 if ( definition.getSave() )
168 {
169 Element save = new Element( "save" );
170 save.setAttribute( "logFile", group.getLogFile().getAbsolutePath() );
171 save.setAttribute( "savedEventCount", String.valueOf( group.getSavedEventCount() ) );
172 save.setAttribute( "maxSaveCount", String.valueOf( group.getMaxSaveCount() ) );
173 save.setAttribute( "maxSaveBytes", String.valueOf( group.getMaxSaveBytes() ) );
174 if ( logsUrl != null )
175 {
176 save.setAttribute( "logsUrl", ld.getDefinition().getOutput().getUrl( group.getLogFile().getName() ) );
177 }
178 elmt.addContent( save );
179 }
180 addPluginsGroupReport( group, new XmlPluginReport( elmt ) );
181
182 new XMLOutputter( Format.getPrettyFormat() ).output( new Document( report ), output );
183 }
184
185 private static class XmlPluginReport
186 extends PluginReport
187 {
188 private final Element elmt;
189
190 private Element pluginElmt;
191
192 public XmlPluginReport( Element elmt )
193 {
194 this.elmt = elmt;
195 pluginElmt = null;
196 }
197
198 public void beginPluginReport( LogDistillation.Plugin plugin, String description )
199 {
200 pluginElmt = new Element( "plugin" ).setAttribute( "type", plugin.getDefinition().getType() );
201 pluginElmt.addContent( new Element( "description" ).setText( description ) );
202 }
203
204 public void addLink( String filename, String description )
205 {
206 pluginElmt.addContent( new Element( "link" ).setAttribute( "filename", filename ).setText( description ) );
207 }
208
209 public void addParam( String param, String value )
210 {
211 pluginElmt.addContent( new Element( "param" ).setAttribute( "name", param ).setText( value ) );
212 }
213
214 public void addItem( int count, String description )
215 {
216 pluginElmt.addContent( new Element( "item" ).setAttribute( "count", String.valueOf( count ) ).setText(
217 description ) );
218 }
219
220 public void endPluginReport()
221 {
222 elmt.addContent( pluginElmt );
223 pluginElmt = null;
224 }
225 }
226 }