1 /**
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 package cppncss;
30
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.io.UnsupportedEncodingException;
34 import java.util.List;
35 import org.dom4j.DocumentHelper;
36 import org.dom4j.Element;
37 import org.dom4j.io.OutputFormat;
38 import org.dom4j.io.XMLWriter;
39
40 /**
41 * Implements an xml result output.
42 *
43 * @author Mathieu Champlon
44 */
45 public final class XmlResultOutput extends AbstractResultOutput
46 {
47 private final PrintStream stream;
48 private final Element root;
49 private Element node;
50
51 /**
52 * Create an xml result output.
53 *
54 * @param stream the stream to write to
55 */
56 public XmlResultOutput( final PrintStream stream )
57 {
58 this.stream = stream;
59 this.node = DocumentHelper.createDocument().addElement( "cppncss" );
60 this.root = node;
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 protected void printHeaders( final String type, final List<String> labels )
67 {
68 node = root.addElement( "measure" );
69 node.addAttribute( "type", type );
70 node = node.addElement( "labels" );
71 node.addElement( "label" ).addText( "Nr." );
72 for( String label : labels )
73 if( !label.startsWith( type ) )
74 node.addElement( "label" ).addText( label );
75 node = node.getParent();
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 protected void printItem( final String item )
82 {
83 node = node.getParent();
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 protected void printMeasurement( final String label, final int count )
90 {
91 node.addElement( "value" ).addText( Integer.toString( count ) );
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 protected void printIndex( final String item, final int index )
98 {
99 node = node.addElement( "item" );
100 node.addAttribute( "name", item );
101 node.addElement( "value" ).addText( Integer.toString( index ) );
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 public void notify( final String type, final String label, final float average )
108 {
109 if( !label.startsWith( type ) )
110 node.addElement( "average" ).addAttribute( "label", label )
111 .addAttribute( "value", Float.toString( average ) );
112
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 public void notify( final String type, final String label, final long sum )
119 {
120 node.addElement( "sum" ).addAttribute( "label", label ).addAttribute( "value", Long.toString( sum ) );
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public void flush()
127 {
128 try
129 {
130 new XMLWriter( stream, OutputFormat.createPrettyPrint() ).write( root );
131 }
132 catch( UnsupportedEncodingException e )
133 {
134 e.printStackTrace();
135 }
136 catch( IOException e )
137 {
138 e.printStackTrace();
139 }
140 }
141 }