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