2020
2121package net .sf .eclipsecs .core .transformer ;
2222
23- import java .util .ArrayList ;
24- import java .util .List ;
23+ import java .io .InputStream ;
2524import java .util .Map ;
2625
26+ import javax .xml .parsers .ParserConfigurationException ;
27+ import javax .xml .transform .TransformerException ;
28+
2729import net .sf .eclipsecs .core .util .CheckstyleLog ;
2830
31+ import org .eclipse .core .resources .IFile ;
2932import org .eclipse .core .resources .IProject ;
3033import org .eclipse .core .runtime .CoreException ;
31- import org .eclipse .core .runtime .preferences .IScopeContext ;
32- import org .eclipse .jdt .core .JavaCore ;
33- import org .eclipse .jdt .internal .corext .fix .CleanUpConstants ;
34- import org .eclipse .jdt .internal .corext .fix .CleanUpPreferenceUtil ;
35- import org .eclipse .jdt .internal .ui .preferences .PreferencesAccess ;
36- import org .eclipse .jdt .internal .ui .preferences .cleanup .CleanUpProfileManager ;
37- import org .eclipse .jdt .internal .ui .preferences .cleanup .CleanUpProfileVersioner ;
38- import org .eclipse .jdt .internal .ui .preferences .formatter .FormatterProfileManager ;
39- import org .eclipse .jdt .internal .ui .preferences .formatter .FormatterProfileStore ;
40- import org .eclipse .jdt .internal .ui .preferences .formatter .IProfileVersioner ;
41- import org .eclipse .jdt .internal .ui .preferences .formatter .ProfileManager ;
42- import org .eclipse .jdt .internal .ui .preferences .formatter .ProfileManager .CustomProfile ;
43- import org .eclipse .jdt .internal .ui .preferences .formatter .ProfileManager .Profile ;
44- import org .eclipse .jdt .internal .ui .preferences .formatter .ProfileStore ;
45- import org .eclipse .jdt .internal .ui .preferences .formatter .ProfileVersioner ;
46- import org .osgi .service .prefs .BackingStoreException ;
4734
4835/**
49- * Class for writing a new eclipse-configuration-file. Gets used by class Transformer. A new
50- * eclipse-formatter-profile gets added .
36+ * Class for writing a new eclipse-configuration-file. Gets used by class Transformer. Two
37+ * eclipse-formatter-profile files gets written to the project root .
5138 *
39+ * @author Alexandros Karypidis
5240 * @author Lukas Frena
5341 * @author Lars Ködderitzsch
5442 */
55- @ SuppressWarnings ("restriction" )
5643public class FormatterConfigWriter {
5744
58- private static final String JDT_UI_PLUGINID = "org.eclipse.jdt.ui" ;
59-
45+ /** Constant for show generated code. */
46+ private static final String CS_GENERATED = "CheckStyle-Generated " ;
6047 /** A eclipse-configuration. */
6148 private final FormatterConfiguration mConfiguration ;
6249
63- /** Name of new createt profile. */
64- private final String mNewProfileName ;
65-
6650 private IProject mProject ;
6751
6852 /**
@@ -77,135 +61,58 @@ public FormatterConfigWriter(IProject project, final FormatterConfiguration sett
7761 mConfiguration = settings ;
7862 mProject = project ;
7963
80- mNewProfileName = "eclipse-cs " + mProject .getName ();
8164 writeSettings ();
8265 }
8366
8467 /**
85- * Method for writing all settings to disc. Also activates new profile .
68+ * Method for persisting all settings to files .
8669 */
8770 private void writeSettings () {
88- // read the Eclipse-Preferences for manipulation
8971 writeCleanupSettings (mConfiguration .getCleanupSettings ());
9072 writeFormatterSettings (mConfiguration .getFormatterSettings ());
9173 }
9274
75+ /**
76+ * Method for writing all cleanup settings to disc.
77+ *
78+ * @param settings
79+ * All the settings.
80+ */
9381 private void writeCleanupSettings (final Map <String , String > settings ) {
94-
95- PreferencesAccess access = PreferencesAccess .getOriginalPreferences ();
96-
97- IScopeContext instanceScope = access .getInstanceScope ();
98- IScopeContext scope = access .getProjectScope (mProject );
99-
100- IProfileVersioner versioner = new CleanUpProfileVersioner ();
101- ProfileStore profilesStore = new ProfileStore (CleanUpConstants .CLEANUP_PROFILES , versioner );
82+ final IFile settingsFile = mProject .getFile (mProject .getName () + "-cs-cleanup.xml" );
10283 try {
103-
104- List <Profile > profiles = profilesStore .readProfiles (instanceScope );
105-
106- if (profiles == null ) {
107- profiles = new ArrayList <>();
108- }
109- profiles .addAll (CleanUpPreferenceUtil .getBuiltInProfiles ());
110-
111- ProfileManager manager = new CleanUpProfileManager (profiles , scope , access , versioner );
112-
113- CustomProfile myProfile = (CustomProfile ) manager
114- .getProfile (ProfileManager .ID_PREFIX + mNewProfileName );
115-
116- if (myProfile == null ) {
117- // take current settings and create new profile
118- Profile current = manager .getSelected ();
119- myProfile = new CustomProfile (mNewProfileName , current .getSettings (),
120- versioner .getCurrentVersion (), versioner .getProfileKind ());
121- manager .addProfile (myProfile );
122- }
123-
124- Map <String , String > joinedSettings = myProfile .getSettings ();
125- joinedSettings .putAll (settings );
126-
127- myProfile .setSettings (joinedSettings );
128- manager .setSelected (myProfile );
129-
130- // writes profiles to the workspace profile store
131- profilesStore .writeProfiles (manager .getSortedProfiles (), instanceScope );
132-
133- // commits changes to the project profile settings
134- manager .commitChanges (scope );
135-
136- scope .getNode (JDT_UI_PLUGINID ).flush ();
137- scope .getNode (JavaCore .PLUGIN_ID ).flush ();
138- if (scope != instanceScope ) {
139- instanceScope .getNode (JDT_UI_PLUGINID ).flush ();
140- instanceScope .getNode (JavaCore .PLUGIN_ID ).flush ();
141- }
142-
143- } catch (CoreException e ) {
144- CheckstyleLog .log (e , "Error storing cleanup profile" );
145- } catch (BackingStoreException e ) {
146- CheckstyleLog .log (e , "Error storing cleanup profile" );
84+ final InputStream stream = XmlProfileWriter .writeCleanupProfileToStream (
85+ CS_GENERATED + mProject .getName (), settings );
86+ createOrUpdateFile (settingsFile , stream );
87+ } catch (CoreException | TransformerException | ParserConfigurationException exc ) {
88+ CheckstyleLog .log (exc , "Error saving cleanup profile" );
14789 }
14890 }
14991
15092 /**
151- * Method for writing all formatter- settings to disc.
93+ * Method for writing all formatter settings to disc.
15294 *
15395 * @param settings
15496 * All the settings.
15597 */
15698 private void writeFormatterSettings (final Map <String , String > settings ) {
157-
158- PreferencesAccess access = PreferencesAccess .getOriginalPreferences ();
159-
160- IScopeContext instanceScope = access .getInstanceScope ();
161- IScopeContext scope = access .getProjectScope (mProject );
162-
163- IProfileVersioner versioner = new ProfileVersioner ();
164- ProfileStore profilesStore = new FormatterProfileStore (versioner );
99+ final IFile settingsFile = mProject .getFile (mProject .getName () + "-cs-formatter.xml" );
165100 try {
101+ final InputStream stream = XmlProfileWriter .writeFormatterProfileToStream (
102+ CS_GENERATED + mProject .getName (), settings );
103+ createOrUpdateFile (settingsFile , stream );
104+ } catch (CoreException | TransformerException | ParserConfigurationException exc ) {
105+ CheckstyleLog .log (exc , "Error saving formatter profile" );
106+ }
107+ }
166108
167- List <Profile > profiles = profilesStore .readProfiles (instanceScope );
168-
169- if (profiles == null ) {
170- profiles = new ArrayList <>();
171- }
172-
173- ProfileManager manager = new FormatterProfileManager (profiles , scope , access , versioner );
174-
175- CustomProfile myProfile = (CustomProfile ) manager
176- .getProfile (ProfileManager .ID_PREFIX + mNewProfileName );
177-
178- if (myProfile == null ) {
179- // take current settings and create new profile
180- Profile current = manager .getSelected ();
181- myProfile = new CustomProfile (mNewProfileName , current .getSettings (),
182- versioner .getCurrentVersion (), versioner .getProfileKind ());
183- manager .addProfile (myProfile );
184- }
185-
186- Map <String , String > joinedSettings = myProfile .getSettings ();
187- joinedSettings .putAll (settings );
188-
189- myProfile .setSettings (joinedSettings );
190- manager .setSelected (myProfile );
191-
192- // writes profiles to the workspace profile store
193- profilesStore .writeProfiles (manager .getSortedProfiles (), instanceScope );
194-
195- // commits changes to the project profile settings
196- manager .commitChanges (scope );
197-
198- scope .getNode (JDT_UI_PLUGINID ).flush ();
199- scope .getNode (JavaCore .PLUGIN_ID ).flush ();
200- if (scope != instanceScope ) {
201- instanceScope .getNode (JDT_UI_PLUGINID ).flush ();
202- instanceScope .getNode (JavaCore .PLUGIN_ID ).flush ();
203- }
204-
205- } catch (CoreException e ) {
206- CheckstyleLog .log (e , "Error storing formatter profile" );
207- } catch (BackingStoreException e ) {
208- CheckstyleLog .log (e , "Error storing formatter profile" );
109+ private static void createOrUpdateFile (IFile settingsFile , InputStream stream )
110+ throws CoreException {
111+ if (settingsFile .exists ()) {
112+ settingsFile .setContents (stream , true , false , null );
113+ } else {
114+ settingsFile .create (stream , true , null );
209115 }
210116 }
117+
211118}
0 commit comments