1
+ //============================================================================
2
+ //
3
+ // Copyright (C) 2009 Lukas Frena
4
+ //
5
+ // This library is free software; you can redistribute it and/or
6
+ // modify it under the terms of the GNU Lesser General Public
7
+ // License as published by the Free Software Foundation; either
8
+ // version 2.1 of the License, or (at your option) any later version.
9
+ //
10
+ // This library is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ // Lesser General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Lesser General Public
16
+ // License along with this library; if not, write to the Free Software
17
+ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ //
19
+ //============================================================================
20
+
21
+ package eu .solven .cleanthat .language .java .eclipse .checkstyle ;
22
+
23
+ import java .io .InputStream ;
24
+ import java .util .Map ;
25
+
26
+ import javax .xml .parsers .ParserConfigurationException ;
27
+ import javax .xml .transform .TransformerException ;
28
+
29
+ import org .eclipse .core .resources .IFile ;
30
+ import org .eclipse .core .resources .IProject ;
31
+ import org .eclipse .core .runtime .CoreException ;
32
+ import org .slf4j .Logger ;
33
+ import org .slf4j .LoggerFactory ;
34
+
35
+ /**
36
+ * Class for writing a new eclipse-configuration-file. Gets used by class Transformer. Two eclipse-formatter-profile
37
+ * files gets written to the project root.
38
+ *
39
+ * @author Alexandros Karypidis
40
+ * @author Lukas Frena
41
+ * @author Lars Ködderitzsch
42
+ */
43
+ // https://github.com/checkstyle/eclipse-cs/blob/master/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java
44
+ public class FormatterConfigWriter {
45
+ private static final Logger LOGGER = LoggerFactory .getLogger (FormatterConfigWriter .class );
46
+
47
+ /** Constant for show generated code. */
48
+ private static final String CS_GENERATED = "CheckStyle-Generated " ;
49
+ /** A eclipse-configuration. */
50
+ private final FormatterConfiguration mConfiguration ;
51
+
52
+ private IProject mProject ;
53
+
54
+ /**
55
+ * Constructor to create a new instance of class FormatterConfigWriter.
56
+ *
57
+ * @param project
58
+ * the project whose formatter settings should be written
59
+ * @param settings
60
+ * A eclipse-configuration.
61
+ */
62
+ public FormatterConfigWriter (IProject project , final FormatterConfiguration settings ) {
63
+ mConfiguration = settings ;
64
+ mProject = project ;
65
+
66
+ writeSettings ();
67
+ }
68
+
69
+ /**
70
+ * Method for persisting all settings to files.
71
+ */
72
+ private void writeSettings () {
73
+ writeCleanupSettings (mConfiguration .getCleanupSettings ());
74
+ writeFormatterSettings (mConfiguration .getFormatterSettings ());
75
+ }
76
+
77
+ /**
78
+ * Method for writing all cleanup settings to disc.
79
+ *
80
+ * @param settings
81
+ * All the settings.
82
+ */
83
+ private void writeCleanupSettings (final Map <String , String > settings ) {
84
+ final IFile settingsFile = mProject .getFile (mProject .getName () + "-cs-cleanup.xml" );
85
+ try {
86
+ final InputStream stream =
87
+ XmlProfileWriter .writeCleanupProfileToStream (CS_GENERATED + mProject .getName (), settings );
88
+ createOrUpdateFile (settingsFile , stream );
89
+ } catch (CoreException | TransformerException | ParserConfigurationException exc ) {
90
+ LOGGER .warn ("Error saving cleanup profile" , exc );
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Method for writing all formatter settings to disc.
96
+ *
97
+ * @param settings
98
+ * All the settings.
99
+ */
100
+ private void writeFormatterSettings (final Map <String , String > settings ) {
101
+ final IFile settingsFile = mProject .getFile (mProject .getName () + "-cs-formatter.xml" );
102
+ try {
103
+ final InputStream stream =
104
+ XmlProfileWriter .writeFormatterProfileToStream (CS_GENERATED + mProject .getName (), settings );
105
+ createOrUpdateFile (settingsFile , stream );
106
+ } catch (CoreException | TransformerException | ParserConfigurationException exc ) {
107
+ LOGGER .warn ("Error saving formatter profile" , exc );
108
+ }
109
+ }
110
+
111
+ private static void createOrUpdateFile (IFile settingsFile , InputStream stream ) throws CoreException {
112
+ if (settingsFile .exists ()) {
113
+ settingsFile .setContents (stream , true , false , null );
114
+ } else {
115
+ settingsFile .create (stream , true , null );
116
+ }
117
+ }
118
+
119
+ }
0 commit comments