3838import java .io .ByteArrayInputStream ;
3939import java .io .File ;
4040import java .io .IOException ;
41- import java .io .InputStreamReader ;
42- import java .io .Reader ;
4341import java .io .StringReader ;
4442import java .io .StringWriter ;
45- import java .net .URL ;
4643import java .util .ArrayList ;
4744import java .util .HashMap ;
4845import java .util .Iterator ;
4946import java .util .List ;
5047import java .util .Map ;
5148
49+ import javax .xml .bind .DataBindingException ;
50+ import javax .xml .bind .JAXBContext ;
51+ import javax .xml .bind .JAXBElement ;
52+ import javax .xml .bind .JAXBException ;
53+ import javax .xml .bind .Marshaller ;
54+ import javax .xml .transform .Source ;
55+ import javax .xml .transform .stream .StreamSource ;
56+
5257import net .sourceforge .pmd .Rule ;
5358import net .sourceforge .pmd .RuleSet ;
5459import net .sourceforge .pmd .RuleSetFactory ;
5863import net .sourceforge .pmd .eclipse .runtime .properties .IProjectProperties ;
5964import net .sourceforge .pmd .eclipse .runtime .properties .IProjectPropertiesManager ;
6065import net .sourceforge .pmd .eclipse .runtime .properties .PropertiesException ;
61- import net .sourceforge .pmd .eclipse .util .IOUtil ;
6266
6367import org .apache .commons .io .IOUtils ;
6468import org .apache .log4j .Logger ;
6771import org .eclipse .core .runtime .CoreException ;
6872import org .eclipse .ui .IWorkingSetManager ;
6973import org .eclipse .ui .PlatformUI ;
70- import org .exolab .castor .mapping .Mapping ;
71- import org .exolab .castor .mapping .MappingException ;
72- import org .exolab .castor .xml .MarshalException ;
73- import org .exolab .castor .xml .Marshaller ;
74- import org .exolab .castor .xml .Unmarshaller ;
75- import org .exolab .castor .xml .ValidationException ;
7674
7775/**
7876 * This class manages the persistence of the ProjectProperies information structure
@@ -84,10 +82,18 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager {
8482 private static final Logger log = Logger .getLogger (ProjectPropertiesManagerImpl .class );
8583
8684 private static final String PROPERTIES_FILE = ".pmd" ;
87- private static final String PROPERTIES_MAPPING = "/net/sourceforge/pmd/eclipse/runtime/properties/impl/mapping.xml" ;
8885
8986 private final Map <IProject , IProjectProperties > projectsProperties = new HashMap <IProject , IProjectProperties >();
9087
88+ private static final JAXBContext JAXB_CONTEXT = initJaxbContext ();
89+
90+ private static JAXBContext initJaxbContext () {
91+ try {
92+ return JAXBContext .newInstance (ProjectPropertiesTO .class );
93+ } catch (JAXBException e ) {
94+ throw new RuntimeException (e );
95+ }
96+ }
9197 /**
9298 * Load a project properties
9399 *
@@ -169,32 +175,16 @@ private void loadRuleSetFromProject(IProjectProperties projectProperties) throws
169175 }
170176 }
171177
172- public ProjectPropertiesTO convertProjectPropertiesFromString (String properties )
173- throws PropertiesException {
174- ProjectPropertiesTO projectProperties = null ;
175- Reader reader = null ;
178+ public ProjectPropertiesTO convertProjectPropertiesFromString (String properties ) {
176179 try {
177- final Mapping mapping = new Mapping (this .getClass ().getClassLoader ());
178- final URL mappingSpecUrl = this .getClass ().getResource (PROPERTIES_MAPPING );
179- mapping .loadMapping (mappingSpecUrl );
180-
181- reader = new StringReader (properties );
182- final Unmarshaller unmarshaller = new Unmarshaller (mapping );
183- projectProperties = (ProjectPropertiesTO ) unmarshaller .unmarshal (reader );
184- } catch (MarshalException e ) {
185- throw new PropertiesException (e );
186- } catch (ValidationException e ) {
187- throw new PropertiesException (e );
188- } catch (IOException e ) {
189- throw new PropertiesException (e );
190- } catch (MappingException e ) {
191- throw new PropertiesException (e );
192- } finally {
193- IOUtil .closeQuietly (reader );
180+ Source source = new StreamSource (new StringReader (properties ));
181+ JAXBElement <ProjectPropertiesTO > element = JAXB_CONTEXT .createUnmarshaller ().unmarshal (source , ProjectPropertiesTO .class );
182+ return element .getValue ();
183+ } catch (JAXBException e ) {
184+ throw new DataBindingException (e );
194185 }
195-
196- return projectProperties ;
197186 }
187+
198188 /**
199189 * Read a project properties from properties file
200190 *
@@ -273,34 +263,21 @@ private void setRuleSetFromProperties(IProjectProperties projectProperties, Rule
273263 projectProperties .setProjectRuleSet (ruleSet );
274264 }
275265
276- public String convertProjectPropertiesToString (ProjectPropertiesTO projectProperties )
277- throws PropertiesException {
278- StringWriter writer = null ;
266+ public String convertProjectPropertiesToString (ProjectPropertiesTO projectProperties ) {
279267 try {
280- final Mapping mapping = new Mapping (getClass ().getClassLoader ());
281- final URL mappingSpecUrl = getClass ().getResource (PROPERTIES_MAPPING );
282- mapping .loadMapping (mappingSpecUrl );
283-
284- writer = new StringWriter ();
285- final Marshaller marshaller = new Marshaller ();
286- marshaller .setProperty ("org.exolab.castor.indent" , "true" );
287- marshaller .setWriter (writer );
288- marshaller .setMapping (mapping );
289- marshaller .marshal (projectProperties );
290- writer .flush ();
268+ Marshaller marshaller = JAXB_CONTEXT .createMarshaller ();
269+ marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
270+ marshaller .setProperty (Marshaller .JAXB_ENCODING , "UTF-8" );
271+ marshaller .setProperty (Marshaller .JAXB_FRAGMENT , true );
291272
292- return writer .toString ();
273+ StringWriter writer = new StringWriter ();
274+ writer .write ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " );
275+ marshaller .marshal (projectProperties , writer );
276+ writer .write ("\n " );
293277
294- } catch (MarshalException e ) {
295- throw new PropertiesException (e );
296- } catch (ValidationException e ) {
297- throw new PropertiesException (e );
298- } catch (IOException e ) {
299- throw new PropertiesException (e );
300- } catch (MappingException e ) {
301- throw new PropertiesException (e );
302- } finally {
303- IOUtil .closeQuietly (writer );
278+ return writer .toString ();
279+ } catch (JAXBException e ) {
280+ throw new DataBindingException (e );
304281 }
305282 }
306283
0 commit comments