View Javadoc

1   package org.apache.maven.jcoveragereport;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.Reader;
21  
22  import org.xmlpull.v1.XmlPullParser;
23  import org.xmlpull.v1.XmlPullParserFactory;
24  
25  /**
26   * @author Emmanuel Venisse
27   * @version $Id: CoverageUnmarshaller.java 170200 2005-05-15 06:24:19Z brett $
28   */
29  public class CoverageUnmarshaller
30  {
31      public Coverage parse(Reader reader) throws Exception
32      {
33          Coverage coverage = new Coverage();
34  
35          XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
36          XmlPullParser parser = factory.newPullParser();
37          parser.setInput(reader);
38  
39          int eventType = parser.getEventType();
40  
41          while (eventType != XmlPullParser.END_DOCUMENT)
42          {
43              if (eventType == XmlPullParser.START_TAG)
44              {
45                  if (parser.getName().equals("coverage"))
46                  {
47                      coverage.setSrcDirectory(parser.getAttributeValue("", "src"));
48                  }
49                  if (parser.getName().equals("class"))
50                  {
51                      Clazz theClass = new Clazz(parser.getAttributeValue("", "name"));
52                      while (parser.nextTag() == XmlPullParser.START_TAG)
53                      {
54                          if (parser.getName().equals("file"))
55                          {
56                              String fileName = parser.getAttributeValue("", "name");
57                              theClass.setFile(fileName);
58                          }
59                          else if (parser.getName().equals("line")
60                                   && parser.getAttributeCount() == 1)
61                          {
62                              theClass.setLineRate(
63                                  parser.getAttributeValue("", "rate"));
64                          }
65                          else if (parser.getName().equals("line")
66                                   && parser.getAttributeCount() == 2)
67                          {
68                              Line line = new Line();
69                              line.setNumLine(
70                                  Integer.valueOf(
71                                      parser.getAttributeValue("", "number")
72                                  ).intValue());
73                              line.setNbHits(
74                                  Integer.valueOf(
75                                      parser.getAttributeValue("", "hits")
76                                  ).intValue());
77                              theClass.addLine(line);
78                          }
79                          else if (parser.getName().equals("branch"))
80                          {
81                              theClass.setBranchRate(
82                                  parser.getAttributeValue("", "rate"));
83                          }
84                          if (parser.getName().equals("methods"))
85                          {
86                              while (parser.nextTag() == XmlPullParser.START_TAG)
87                              {
88                                  if (parser.getName().equals("method"))
89                                  {
90                                      while (parser.nextTag() == XmlPullParser.START_TAG)
91                                      {
92                                          if (parser.getName().equals("line"))
93                                          {
94                                              //nothing
95                                          }
96                                          else if (parser.getName().equals("branch"))
97                                          {
98                                              //nothing
99                                          }
100                                         parser.next();
101                                     }
102                                 }
103                                 parser.next();
104                             }
105                         }
106                         else
107                         {
108                             parser.next();
109                         }
110                     }
111                     coverage.addClass(theClass);
112                 }
113             }
114 
115             eventType = parser.next();
116         }
117 
118         return coverage;
119     }
120 }