2
2
3
3
import com .intellij .openapi .ui .Messages ;
4
4
import com .squareup .javapoet .*;
5
+ import com .techconative .actions .DozerTOMapperStructPlugin ;
5
6
import com .techconative .actions .utilities .Utilities ;
6
7
import org .mapstruct .Mapper ;
7
8
import org .mapstruct .Mapping ;
14
15
import org .xml .sax .SAXException ;
15
16
16
17
import javax .lang .model .element .Modifier ;
18
+ import javax .swing .text .BadLocationException ;
17
19
import javax .xml .parsers .DocumentBuilder ;
18
20
import javax .xml .parsers .DocumentBuilderFactory ;
19
21
import javax .xml .parsers .ParserConfigurationException ;
20
22
import java .io .IOException ;
21
23
import java .io .StringReader ;
24
+ import java .nio .file .FileSystems ;
22
25
import java .nio .file .Paths ;
23
26
import java .util .*;
27
+ import java .util .concurrent .atomic .AtomicBoolean ;
24
28
import java .util .stream .IntStream ;
25
29
26
30
public class GenerateMappings {
27
31
28
32
static private TypeSpec .Builder person ;
29
33
static private boolean alreadyExecuted = false ;
30
34
private static Document finalDocument ;
35
+ private static int length ;
31
36
32
- public static String generateMappings ( String selectedText , String path , boolean generate ,
33
- String className , String mapperName ) throws IOException {
37
+
38
+ public static Integer check ( String selectedText ) {
34
39
DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance ();
35
- Map <String , String > map = new HashMap <>();
36
40
DocumentBuilder dBuilder = null ;
41
+ length = 0 ;
37
42
try {
38
43
dBuilder = dbFactory .newDocumentBuilder ();
39
44
} catch (ParserConfigurationException ex ) {
40
45
Messages .showMessageDialog (String .valueOf (ex ), "ERROR" , Messages .getErrorIcon ());
41
- return null ;
46
+ return 0 ;
42
47
}
43
48
finalDocument = null ;
44
49
try {
45
50
finalDocument = dBuilder .parse (new InputSource (new StringReader (selectedText )));
46
51
} catch (SAXException | IOException | NullPointerException ex ) {
47
52
Messages .showMessageDialog (String .valueOf (ex ), "ERROR" , Messages .getErrorIcon ());
48
- return null ;
53
+ return 0 ;
49
54
}
50
55
finalDocument .getDocumentElement ().normalize ();
56
+ return finalDocument .getElementsByTagName ("mapping" ).getLength ();
57
+ }
51
58
52
- int length = finalDocument .getElementsByTagName ("mapping" ).getLength ();
59
+ public static boolean CheckXml (String selectedText ) {
60
+ length = check (selectedText );
61
+ if (length == 1 && finalDocument .getElementsByTagName ("mappings" ).getLength () == 0 ) {
62
+ alreadyExecuted = true ;
63
+ return true ;
64
+ } else {
65
+ alreadyExecuted = false ;
66
+ return false ;
67
+ }
68
+ }
69
+
70
+ public static String generateMappings (String selectedText , String path , boolean generate ,
71
+ String className , String mapperName ) throws IOException , BadLocationException {
72
+ if (length == 0 ) {
73
+ return null ;
74
+ }
75
+ Map <String , String > map = new HashMap <>();
76
+ person = null ;
77
+ AtomicBoolean partialMapping = new AtomicBoolean (false );
53
78
54
79
if (length != finalDocument .getElementsByTagName ("class-a" ).getLength () &&
55
80
length != finalDocument .getElementsByTagName ("class-b" ).getLength ()) {
56
81
Messages .showMessageDialog ("Wrong xml structure" , "ERROR" , Messages .getErrorIcon ());
57
82
return null ;
58
83
}
59
84
60
-
61
85
IntStream .range (0 , length ).forEachOrdered (x -> {
62
86
map .clear ();
63
87
NodeList nodeList = finalDocument .getElementsByTagName ("mapping" ).item (x ).getChildNodes ();
@@ -99,13 +123,26 @@ public static String generateMappings(String selectedText, String path, boolean
99
123
if (finalDocument .getElementsByTagName ("mapping" ).item (x ).getAttributes ().getNamedItem ("map-id" ) != null ) {
100
124
String mapId = finalDocument .getElementsByTagName ("mapping" ).item (x ).getAttributes ()
101
125
.getNamedItem ("map-id" ).getTextContent ();
102
- person .addAnnotation (AnnotationSpec .builder (Named .class )
103
- .addMember ("value" , "$S" , Utilities .apply (mapId )).build ());
126
+ map .put ("methodMapId" , mapId );
127
+ }
128
+ if (finalDocument .getElementsByTagName ("mappings" ).getLength () == 0 && length >= 1 && !generate ) {
129
+ try {
130
+ DozerTOMapperStructPlugin .getJTextPlane (generateMethod (map , annotationSpecList , true )
131
+ .replaceAll ("@org.mapstruct." , "@" ));
132
+ partialMapping .set (true );
133
+ } catch (BadLocationException e ) {
134
+ throw new RuntimeException (e );
135
+ }
136
+ } else {
137
+ generateMethod (map , annotationSpecList , false );
104
138
}
105
- generateMethod (map , annotationSpecList );
106
139
});
107
-
108
- return generateJavaClass (path , generate );
140
+ if (partialMapping .get ()) {
141
+ alreadyExecuted = false ;
142
+ return null ;
143
+ } else {
144
+ return generateJavaClass (path , generate );
145
+ }
109
146
}
110
147
111
148
@@ -119,7 +156,7 @@ private static String getClassName(String value) {
119
156
return strings [strings .length - 1 ];
120
157
}
121
158
122
- static void generateMethod (Map <String , String > map , List <AnnotationSpec > annotationSpecList ) {
159
+ static String generateMethod (Map <String , String > map , List <AnnotationSpec > annotationSpecList , boolean partialMapping ) {
123
160
124
161
ClassName classTypeB = ClassName .get (map .get ("packageB" ), map .get ("ClassBName" ));
125
162
ClassName classTypeA = ClassName .get (map .get ("packageA" ), map .get ("ClassAName" ));
@@ -128,16 +165,24 @@ static void generateMethod(Map<String, String> map, List<AnnotationSpec> annotat
128
165
.methodBuilder ("to" + map .get ("ClassBName" ))
129
166
.addParameter (classTypeA , Utilities .getObjectNameForClassName (map .get ("ClassAName" )))
130
167
.returns (classTypeB ).addModifiers (Modifier .PUBLIC , Modifier .ABSTRACT );
131
- AnnotationSpec .Builder anno = AnnotationSpec .builder (Mappings .class );
132
- IntStream .range (0 , annotationSpecList .size ()).forEachOrdered (x ->
133
- anno .addMember ("value" , "$L" , annotationSpecList .get (x ))
134
- );
135
- method .addAnnotation (anno .build ());
168
+ if (!annotationSpecList .isEmpty () && annotationSpecList != null ) {
169
+ AnnotationSpec .Builder anno = AnnotationSpec .builder (Mappings .class );
170
+ IntStream .range (0 , annotationSpecList .size ()).forEachOrdered (x ->
171
+ anno .addMember ("value" , "$L" , annotationSpecList .get (x ))
172
+ );
173
+ method .addAnnotation (anno .build ());
174
+ }
136
175
if (map .containsKey ("methodMapId" )) {
137
176
method .addAnnotation (AnnotationSpec .builder (Named .class )
138
177
.addMember ("value" , "$S" , Utilities .findAndApply (map .get ("methodMapId" ))).build ());
139
178
}
140
- person .addMethod (method .build ());
179
+ if (partialMapping ) {
180
+ return method .build ().toString ().replaceAll (map .get ("packageB" ) + "." , "" )
181
+ .replaceAll (map .get ("packageA" ) + "." , "" );
182
+ } else {
183
+ person .addMethod (method .build ());
184
+ return null ;
185
+ }
141
186
}
142
187
143
188
static private void buildJavaClass (String path , Map <String , String > map , String className , String mapperName ) {
@@ -177,10 +222,12 @@ static private String generateJavaClass(String path, boolean generate) throws IO
177
222
return code ;
178
223
}
179
224
180
- static private String [] getPath (String path ) {
181
- String str = path .replace (path .charAt (2 ), '.' );
182
- String [] strings = str .split ("src.main.java." );
183
- strings [0 ] = str .replaceAll ("." + strings [1 ], "" ).trim ().replace ('.' , path .charAt (2 ));
225
+ private static String [] getPath (String path ) {
226
+ String fileSeparator = FileSystems .getDefault ().getSeparator ();
227
+ String str = path .replace (fileSeparator , "." );
228
+ String [] strings = new String [2 ];
229
+ strings [1 ] = str .split (".src.main.java." )[1 ];
230
+ strings [0 ] = path .replace (strings [1 ].replace ("." , fileSeparator ), "" );
184
231
return strings ;
185
232
}
186
233
0 commit comments