1 package org.codehaus.mojo.dashboard.report.plugin.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.SAXParseException;
35 import org.xml.sax.helpers.DefaultHandler;
36
37 import com.thoughtworks.xstream.XStream;
38 import com.thoughtworks.xstream.io.xml.DomDriver;
39
40
41
42
43
44
45 public class ConfigurationService implements IConfigurationService
46 {
47
48 private List warningsMsg = new ArrayList();
49
50 private boolean isValid = true;
51
52 private boolean isValidXSD = true;
53
54 private Configuration dashConfig;
55
56 static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
57
58 static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
59
60 static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
61
62
63
64
65 private String configfile = "";
66
67
68
69
70 public ConfigurationService( String configfile ) throws ConfigurationServiceException
71 {
72 this.configfile = configfile;
73 validateConfigFileWithXSD();
74 if ( isValidXSD )
75 {
76 dashConfig = readConfigFile( this.configfile );
77 validateConfigFile();
78 }
79 else
80 {
81 isValid = false;
82 throw new ConfigurationServiceException( "XML config validation with schema failed : " + this.configfile );
83 }
84
85 }
86
87 public Configuration getConfiguration()
88 {
89 return dashConfig;
90 }
91
92
93
94
95
96
97 public String getConfigFile()
98 {
99 return configfile;
100 }
101
102
103
104
105
106
107 public void setConfigFile( String configFile )
108 {
109 this.configfile = configFile;
110 }
111
112 public boolean isValidConfig()
113 {
114
115 return isValid;
116 }
117
118 private void validateConfigFileWithXSD()
119 {
120 InputStream stream =
121 Thread.currentThread().getContextClassLoader().getResourceAsStream( "config/maven-dashboard-config.xsd" );
122 try
123 {
124 InputSource xmlFile = new InputSource( new FileInputStream( this.configfile ) );
125
126 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
127 dbf.setIgnoringComments( true );
128 dbf.setNamespaceAware( true );
129 dbf.setValidating( true );
130 dbf.setAttribute( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA );
131 dbf.setAttribute( JAXP_SCHEMA_SOURCE, stream );
132 DocumentBuilder db = dbf.newDocumentBuilder();
133 db.setErrorHandler( new MyDefaultHandler() );
134 db.parse( xmlFile );
135
136 }
137 catch ( SAXException e )
138 {
139 isValidXSD = false;
140 e.printStackTrace();
141 }
142 catch ( IOException e )
143 {
144 isValidXSD = false;
145 e.printStackTrace();
146 }
147 catch ( ParserConfigurationException e )
148 {
149 isValidXSD = false;
150 e.printStackTrace();
151 }
152
153 System.out.println( isValidXSD );
154 }
155
156 private void validateConfigFile()
157 {
158 if ( dashConfig != null )
159 {
160 List sections = dashConfig.getSections();
161 Iterator iter = sections.iterator();
162 while ( iter.hasNext() )
163 {
164 Section section = (Section) iter.next();
165 if ( !section.isValidGraphs() )
166 {
167 isValid = false;
168 warningsMsg.addAll( section.getWarningMessages() );
169 break;
170 }
171 }
172 }
173 else
174 {
175 isValid = false;
176 }
177 }
178
179 public List getWarningMessages()
180 {
181 return warningsMsg;
182 }
183
184 private Configuration readConfigFile( String configFile ) throws ConfigurationServiceException
185 {
186 Configuration dashConfiguration = null;
187 try
188 {
189
190
191 XStream xstream = new XStream( new DomDriver() );
192
193 File fichier = new File( configFile );
194
195
196
197 FileInputStream fis = new FileInputStream( fichier );
198
199 try
200 {
201 xstream.setMode( XStream.NO_REFERENCES );
202
203 xstream.alias( "configuration", Configuration.class );
204 xstream.useAttributeFor( "version", String.class );
205 xstream.alias( "section", Section.class );
206 xstream.alias( "graph", Graph.class );
207 xstream.useAttributeFor( "id", String.class );
208 xstream.useAttributeFor( "title", String.class );
209 xstream.useAttributeFor( "timeUnit", String.class );
210 xstream.useAttributeFor( "groupId", String.class );
211 xstream.useAttributeFor( "artifactId", String.class );
212 xstream.useAttributeFor( "startPeriod", String.class );
213 xstream.useAttributeFor( "endPeriod", String.class );
214
215
216 dashConfiguration = (Configuration) xstream.fromXML( fis );
217
218 }
219 finally
220 {
221
222 fis.close();
223 }
224 }
225 catch ( FileNotFoundException e )
226 {
227 dashConfiguration = null;
228 throw new ConfigurationServiceException( "readConfigFile() failed : " + this.configfile, e );
229 }
230 catch ( IOException e )
231 {
232 dashConfiguration = null;
233 throw new ConfigurationServiceException( "readConfigFile() failed : " + this.configfile, e );
234 }
235 return dashConfiguration;
236 }
237
238 class MyDefaultHandler extends DefaultHandler
239 {
240 private String errMessage = "";
241
242
243
244
245
246
247 public void warning( SAXParseException e )
248 {
249 System.out.println( "Warning Line " + e.getLineNumber() + ": " + e.getMessage() + "\n" );
250 }
251
252 public void error( SAXParseException e )
253 {
254 errMessage = new String( "Error Line " + e.getLineNumber() + ": " + e.getMessage() + "\n" );
255 System.out.println( errMessage );
256 isValidXSD = false;
257 }
258
259 public void fatalError( SAXParseException e )
260 {
261 errMessage = new String( "Error Line " + e.getLineNumber() + ": " + e.getMessage() + "\n" );
262 System.out.println( errMessage );
263 isValidXSD = false;
264 }
265 }
266 }