4040import java .io .IOException ;
4141import java .io .InputStreamReader ;
4242import java .io .Reader ;
43+ import java .io .StringReader ;
4344import java .io .StringWriter ;
4445import java .net .URL ;
4546import java .util .ArrayList ;
5960import net .sourceforge .pmd .eclipse .runtime .properties .PropertiesException ;
6061import net .sourceforge .pmd .eclipse .util .IOUtil ;
6162
63+ import org .apache .commons .io .IOUtils ;
6264import org .apache .log4j .Logger ;
6365import org .eclipse .core .resources .IFile ;
6466import org .eclipse .core .resources .IProject ;
@@ -167,26 +169,18 @@ private void loadRuleSetFromProject(IProjectProperties projectProperties) throws
167169 }
168170 }
169171
170- /**
171- * Read a project properties from properties file
172- *
173- * @param project a project
174- */
175- private ProjectPropertiesTO readProjectProperties (final IProject project ) throws PropertiesException {
172+ public ProjectPropertiesTO convertProjectPropertiesFromString (String properties )
173+ throws PropertiesException {
176174 ProjectPropertiesTO projectProperties = null ;
177175 Reader reader = null ;
178176 try {
179177 final Mapping mapping = new Mapping (this .getClass ().getClassLoader ());
180178 final URL mappingSpecUrl = this .getClass ().getResource (PROPERTIES_MAPPING );
181179 mapping .loadMapping (mappingSpecUrl );
182180
183- final IFile propertiesFile = project .getFile (PROPERTIES_FILE );
184- if (propertiesFile .exists () && propertiesFile .isAccessible ()) {
185- reader = new InputStreamReader (propertiesFile .getContents ());
186- final Unmarshaller unmarshaller = new Unmarshaller (mapping );
187- projectProperties = (ProjectPropertiesTO ) unmarshaller .unmarshal (reader );
188- reader .close ();
189- }
181+ reader = new StringReader (properties );
182+ final Unmarshaller unmarshaller = new Unmarshaller (mapping );
183+ projectProperties = (ProjectPropertiesTO ) unmarshaller .unmarshal (reader );
190184 } catch (MarshalException e ) {
191185 throw new PropertiesException (e );
192186 } catch (ValidationException e ) {
@@ -195,14 +189,35 @@ private ProjectPropertiesTO readProjectProperties(final IProject project) throws
195189 throw new PropertiesException (e );
196190 } catch (MappingException e ) {
197191 throw new PropertiesException (e );
198- } catch (CoreException e ) {
199- throw new PropertiesException (e );
200192 } finally {
201- IOUtil .closeQuietly (reader );
193+ IOUtil .closeQuietly (reader );
202194 }
203195
204196 return projectProperties ;
205197 }
198+ /**
199+ * Read a project properties from properties file
200+ *
201+ * @param project a project
202+ */
203+ private ProjectPropertiesTO readProjectProperties (final IProject project ) throws PropertiesException {
204+ ProjectPropertiesTO projectProperties = null ;
205+ try {
206+
207+ final IFile propertiesFile = project .getFile (PROPERTIES_FILE );
208+ if (propertiesFile .exists () && propertiesFile .isAccessible ()) {
209+ String properties = IOUtils .toString (propertiesFile .getContents ());
210+ projectProperties = convertProjectPropertiesFromString (properties );
211+ }
212+
213+ return projectProperties ;
214+
215+ } catch (IOException e ) {
216+ throw new PropertiesException (e );
217+ } catch (CoreException e ) {
218+ throw new PropertiesException (e );
219+ }
220+ }
206221
207222 /**
208223 * Fill a properties information structure from a transfer object
@@ -258,17 +273,9 @@ private void setRuleSetFromProperties(IProjectProperties projectProperties, Rule
258273 projectProperties .setProjectRuleSet (ruleSet );
259274 }
260275
261- /**
262- * Save project properties
263- *
264- * @param project a project
265- * @param projectProperties the project properties to save
266- * @param monitor a progress monitor
267- * @throws DAOException
268- */
269- private void writeProjectProperties (final IProject project , final ProjectPropertiesTO projectProperties )
276+ public String convertProjectPropertiesToString (ProjectPropertiesTO projectProperties )
270277 throws PropertiesException {
271- StringWriter writer = null ;
278+ StringWriter writer = null ;
272279 try {
273280 final Mapping mapping = new Mapping (getClass ().getClassLoader ());
274281 final URL mappingSpecUrl = getClass ().getResource (PROPERTIES_MAPPING );
@@ -282,12 +289,8 @@ private void writeProjectProperties(final IProject project, final ProjectPropert
282289 marshaller .marshal (projectProperties );
283290 writer .flush ();
284291
285- final IFile propertiesFile = project .getFile (PROPERTIES_FILE );
286- if (propertiesFile .exists () && propertiesFile .isAccessible ()) {
287- propertiesFile .setContents (new ByteArrayInputStream (writer .toString ().getBytes ()), false , false , null );
288- } else {
289- propertiesFile .create (new ByteArrayInputStream (writer .toString ().getBytes ()), false , null );
290- }
292+ return writer .toString ();
293+
291294 } catch (MarshalException e ) {
292295 throw new PropertiesException (e );
293296 } catch (ValidationException e ) {
@@ -296,10 +299,32 @@ private void writeProjectProperties(final IProject project, final ProjectPropert
296299 throw new PropertiesException (e );
297300 } catch (MappingException e ) {
298301 throw new PropertiesException (e );
302+ } finally {
303+ IOUtil .closeQuietly (writer );
304+ }
305+ }
306+
307+ /**
308+ * Save project properties
309+ *
310+ * @param project a project
311+ * @param projectProperties the project properties to save
312+ * @param monitor a progress monitor
313+ * @throws DAOException
314+ */
315+ private void writeProjectProperties (final IProject project , final ProjectPropertiesTO projectProperties )
316+ throws PropertiesException {
317+ try {
318+ String writer = convertProjectPropertiesToString (projectProperties );
319+
320+ final IFile propertiesFile = project .getFile (PROPERTIES_FILE );
321+ if (propertiesFile .exists () && propertiesFile .isAccessible ()) {
322+ propertiesFile .setContents (new ByteArrayInputStream (writer .getBytes ()), false , false , null );
323+ } else {
324+ propertiesFile .create (new ByteArrayInputStream (writer .getBytes ()), false , null );
325+ }
299326 } catch (CoreException e ) {
300327 throw new PropertiesException (e );
301- } finally {
302- IOUtil .closeQuietly (writer );
303328 }
304329 }
305330
0 commit comments