001package net.sf.logdistiller.reports;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.io.IOException;
018import java.io.Writer;
019
020import org.jdom.*;
021import org.jdom.output.Format;
022import org.jdom.output.XMLOutputter;
023
024import net.sf.logdistiller.LogDistillation;
025import net.sf.logdistiller.LogDistiller;
026import net.sf.logdistiller.ReportFormat;
027
028/**
029 * XML report format.
030 *
031 * @since 0.8
032 */
033public class XmlReport
034    extends ReportFormat
035{
036    public XmlReport()
037    {
038        super( "xml" );
039    }
040
041    public String getContentType()
042    {
043        return "text/xml";
044    }
045
046    public String getFileExtension()
047    {
048        return "xml";
049    }
050
051    public void report( LogDistillation ld, Writer output )
052        throws IOException
053    {
054        LogDistiller definition = ld.getDefinition();
055
056        Element report = new Element( "logdistiller-report" );
057        Element elmt = new Element( "logdistiller" );
058        elmt.setAttribute( "id", definition.getOutput().getId() );
059        elmt.setAttribute( "description", definition.getDescription() );
060        elmt.setAttribute( "logtype", definition.getLogType().getId() );
061        elmt.setAttribute( "version", ld.getVersion() );
062        report.addContent( elmt );
063
064        elmt = new Element( "logdistillation" );
065        if ( ld.getContent() != null )
066        {
067            elmt.setAttribute( "content", ld.getContent() );
068        }
069        String logsUrl = definition.getOutput().getUrl();
070        if ( logsUrl != null )
071        {
072            elmt.setAttribute( "logsUrl", logsUrl );
073        }
074        elmt.setAttribute( "beginTime", String.valueOf( ld.getBeginTime() ) );
075        elmt.setAttribute( "endTime", String.valueOf( ld.getEndTime() ) );
076        elmt.setAttribute( "eventCount", String.valueOf( ld.getEventCount() ) );
077        elmt.setAttribute( "eventBytes", String.valueOf( ld.getEventBytes() ) );
078        elmt.setAttribute( "skippedEventCount", String.valueOf( ld.getSkippedEventCount() ) );
079        elmt.setAttribute( "skippedEventBytes", String.valueOf( ld.getSkippedEventBytes() ) );
080        for ( String warning : definition.getWarnings() )
081        {
082            elmt.addContent( new Element( "warning" ).setText( warning ) );
083        }
084        report.addContent( elmt );
085
086        // report for categories, and corresponding groups
087        for ( LogDistillation.Category category : ld.listCategoriesToReport() )
088        {
089            elmt.addContent( newCategory( category ) );
090        }
091
092        // report for groups without category
093        for ( LogDistillation.Group group : ld.listGroupsToReport() )
094        {
095            elmt.addContent( newGroup( group ) );
096        }
097
098        new XMLOutputter( Format.getPrettyFormat() ).output( new Document( report ), output );
099    }
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}