View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.utils;
2   
3   /*
4    * Copyright 2008 David Vicente
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  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.util.Date;
26  
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.mojo.dashboard.report.plugin.beans.CheckstyleError;
29  import org.codehaus.mojo.dashboard.report.plugin.beans.DashBoardMavenProject;
30  import org.codehaus.mojo.dashboard.report.plugin.beans.XRefPackageBean;
31  
32  import com.thoughtworks.xstream.XStream;
33  import com.thoughtworks.xstream.io.xml.DomDriver;
34  
35  /**
36   * @author A315941
37   * @version %version : 1 %
38   */
39  
40  public class DashXStreamUtils
41  {
42      private static DashXStreamUtils xstreamUtils = null;
43      
44      //Instanciation de la classe XStream
45      XStream xstream = new XStream( new DomDriver() );
46      
47      /**
48       * Creation forbidden...
49       */
50      private DashXStreamUtils()
51      {
52          super();
53          xstream.setMode( XStream.NO_REFERENCES );
54          // Convertion du contenu de l'objet DashBoardReportBean en XML
55          xstream.alias( "MavenProject", DashBoardMavenProject.class );
56          xstream.alias( "xrefpackage", XRefPackageBean.class );
57          xstream.alias( "checkstyleerror", CheckstyleError.class );
58          xstream.useAttributeFor( "id", Long.class );
59          xstream.useAttributeFor( "artifactId", String.class );
60          xstream.useAttributeFor( "groupId", String.class );
61          xstream.useAttributeFor( "version", String.class );
62          xstream.useAttributeFor( "projectName", String.class );
63          xstream.useAttributeFor( "generatedDate", Date.class );
64          xstream.useAttributeFor( "averageAfferentCoupling", String.class );
65          xstream.useAttributeFor( "nbPackages", String.class );
66          xstream.useAttributeFor( "sumAC", String.class );
67      }
68      
69      public static DashXStreamUtils getInstance()
70      {
71          if (xstreamUtils == null){
72              xstreamUtils = new DashXStreamUtils();
73          }
74          return xstreamUtils;
75      }
76      
77      /**
78       * 
79       * @param project
80       * @param dashboardDataFile
81       * @return
82       * @throws DashXStreamUtilsException
83       */
84      public DashBoardMavenProject readXMLDashBoardReport( MavenProject project, String dashboardDataFile ) throws DashXStreamUtilsException
85      {
86          DashBoardMavenProject mavenProject = null;
87          try
88          {
89  
90              
91              // Instanciation d'un fichier
92              File fichier = new File( project.getBuild().getDirectory(), dashboardDataFile );
93  
94              // Redirection du fichier /target/dashboard-report.xml vers un flux
95              // d'entr�e fichier
96              FileInputStream fis = new FileInputStream( fichier );
97  
98              try
99              { 
100                 // D�s�rialisation du fichier /target/dashboard-multi-report.xml vers un nouvel
101                 // objet DashBoardReportBean
102                 mavenProject = (DashBoardMavenProject) xstream.fromXML( fis );
103 
104             }
105             finally
106             {
107                 // On s'assure de fermer le flux quoi qu'il arrive
108                 fis.close();
109             }
110         }
111         catch ( FileNotFoundException e )
112         {
113             System.out.println( "readXMLDashBoardReport() for project " + project.getName() + " failed :"
114                             + e.getMessage() );
115             throw new DashXStreamUtilsException("readXMLDashBoardReport() for project " + project.getName() + " failed .",e.fillInStackTrace());
116         }
117         catch ( IOException ioe )
118         {
119             throw new DashXStreamUtilsException("readXMLDashBoardReport() for project " + project.getName() + " failed .",ioe.fillInStackTrace());
120         }
121         return mavenProject;
122     }
123 
124     /**
125      * 
126      * @param project
127      * @param mavenProject
128      * @param dashboardDataFile
129      * @throws DashXStreamUtilsException
130      */
131     public void saveXMLDashBoardReport( MavenProject project, DashBoardMavenProject mavenProject,
132                                            String dashboardDataFile )throws DashXStreamUtilsException
133     {
134         try
135         {
136             // Instanciation d'un fichier
137             File dir = new File( project.getBuild().getDirectory() );
138             if ( !dir.exists() )
139             {
140                 dir.mkdirs();
141             }
142             File fichier = new File( dir, dashboardDataFile );
143             // Instanciation d'un flux de sortie fichier vers le xml
144             FileOutputStream fos = new FileOutputStream( fichier );
145             OutputStreamWriter output = new OutputStreamWriter( fos, "UTF-8" );
146             try
147             {
148                 output.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
149                 // S�rialisation de l'objet dashBoardReport dans /target/dashboard-report.xml
150                 xstream.toXML( mavenProject, output );
151             }
152             finally
153             {
154                 // On s'assure de fermer le flux quoi qu'il arrive
155                 fos.close();
156             }
157 
158         }
159         catch ( FileNotFoundException e )
160         {
161             throw new DashXStreamUtilsException("saveXMLDashBoardReport() for project " + project.getName() + " failed .",e.fillInStackTrace());
162         }
163         catch ( IOException ioe )
164         {
165             throw new DashXStreamUtilsException("saveXMLDashBoardReport() for project " + project.getName() + " failed .",ioe.fillInStackTrace());
166         }
167     }
168 }