001 package 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 017 import java.io.IOException; 018 import java.io.Writer; 019 import java.util.Iterator; 020 021 import org.jdom.*; 022 import org.jdom.output.Format; 023 import org.jdom.output.XMLOutputter; 024 025 import net.sf.logdistiller.LogDistillation; 026 import net.sf.logdistiller.LogDistiller; 027 import net.sf.logdistiller.ReportFormat; 028 029 /** 030 * XML report format. 031 * 032 * @since 0.8 033 */ 034 public class XmlReport 035 extends ReportFormat 036 { 037 public XmlReport() 038 { 039 super( "xml" ); 040 } 041 042 public String getContentType() 043 { 044 return "text/xml"; 045 } 046 047 public String getFileExtension() 048 { 049 return "xml"; 050 } 051 052 public void report( LogDistillation ld, Writer output ) 053 throws IOException 054 { 055 LogDistiller definition = ld.getDefinition(); 056 057 Element report = new Element( "logdistiller-report" ); 058 Element elmt = new Element( "logdistiller" ); 059 elmt.setAttribute( "id", definition.getOutput().getId() ); 060 elmt.setAttribute( "description", definition.getDescription() ); 061 elmt.setAttribute( "logtype", definition.getLogType().getId() ); 062 elmt.setAttribute( "version", ld.getVersion() ); 063 report.addContent( elmt ); 064 065 elmt = new Element( "logdistillation" ); 066 if ( ld.getContent() != null ) 067 { 068 elmt.setAttribute( "content", ld.getContent() ); 069 } 070 String logsUrl = definition.getOutput().getUrl(); 071 if ( logsUrl != null ) 072 { 073 elmt.setAttribute( "logsUrl", logsUrl ); 074 } 075 elmt.setAttribute( "beginTime", String.valueOf( ld.getBeginTime() ) ); 076 elmt.setAttribute( "endTime", String.valueOf( ld.getEndTime() ) ); 077 elmt.setAttribute( "eventCount", String.valueOf( ld.getEventCount() ) ); 078 elmt.setAttribute( "eventBytes", String.valueOf( ld.getEventBytes() ) ); 079 elmt.setAttribute( "skippedEventCount", String.valueOf( ld.getSkippedEventCount() ) ); 080 elmt.setAttribute( "skippedEventBytes", String.valueOf( ld.getSkippedEventBytes() ) ); 081 Iterator iter = definition.getWarnings().iterator(); 082 while ( iter.hasNext() ) 083 { 084 elmt.addContent( new Element( "warning" ).setText( (String) iter.next() ) ); 085 } 086 report.addContent( elmt ); 087 088 // report for categories, and corresponding groups 089 Iterator categoriesIterator = ld.listCategoriesToReport().iterator(); 090 while ( categoriesIterator.hasNext() ) 091 { 092 LogDistillation.Category category = (LogDistillation.Category) categoriesIterator.next(); 093 elmt.addContent( newCategory( category ) ); 094 } 095 096 // report for groups without category 097 Iterator groupsIterator = ld.listGroupsToReport().iterator(); 098 while ( groupsIterator.hasNext() ) 099 { 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 }