1
1
package fr .adrienbrault .idea .symfony2plugin .dic ;
2
2
3
- import org .apache .commons .lang .StringUtils ;
3
+ import fr .adrienbrault .idea .symfony2plugin .dic .container .ServiceInterface ;
4
+ import fr .adrienbrault .idea .symfony2plugin .dic .container .XmlService ;
4
5
import org .w3c .dom .Document ;
5
6
import org .w3c .dom .Element ;
7
+ import org .w3c .dom .Node ;
6
8
import org .w3c .dom .NodeList ;
7
9
import org .xml .sax .SAXException ;
8
10
16
18
import java .util .Map ;
17
19
18
20
/**
21
+ * @author Daniel Espendiller <daniel@espendiller.net>
19
22
* @author Adrien Brault <adrien.brault@gmail.com>
20
23
*/
21
24
public class ServiceMapParser {
@@ -36,24 +39,47 @@ public ServiceMap parse(File file) throws IOException, SAXException {
36
39
}
37
40
38
41
public ServiceMap parse (Document document ) {
39
- Map <String , String > map = new HashMap <>();
40
- Map <String , String > publicMap = new HashMap <>();
41
-
42
42
NodeList servicesNodes = document .getElementsByTagName ("service" );
43
+
44
+ Map <String , ServiceInterface > services = new HashMap <>();
45
+ Map <String , ServiceInterface > aliases = new HashMap <>();
46
+
43
47
for (int i = 0 ; i < servicesNodes .getLength (); i ++) {
44
- Element node = ( Element ) servicesNodes .item (i );
45
- if ( node . hasAttribute ( "class" ) && node . hasAttribute ( "id" )) {
46
- map . put ( node . getAttribute ( "id" ), StringUtils . stripStart ( node . getAttribute ( "class" ), " \\ " )) ;
48
+ Node node = servicesNodes .item (i );
49
+ if (!( node instanceof Element )) {
50
+ continue ;
47
51
}
48
- if (!(node .hasAttribute ("public" ) && node .getAttribute ("public" ).equals ("false" ))) {
49
- publicMap .put (node .getAttribute ("id" ), StringUtils .stripStart (node .getAttribute ("class" ), "\\ " ));
52
+
53
+ // invalid service
54
+ XmlService service = XmlService .createFromXml ((Element ) node );
55
+ if (service == null ) {
56
+ continue ;
50
57
}
51
- if (node .hasAttribute ("alias" ) && publicMap .get (node .getAttribute ("alias" )) != null ) {
52
- map .put (node .getAttribute ("id" ), map .get (node .getAttribute ("alias" )));
53
- publicMap .put (node .getAttribute ("id" ), map .get (node .getAttribute ("alias" )));
58
+
59
+ if (service .getAlias () == null ) {
60
+ services .put (service .getId (), service );
61
+ } else {
62
+ aliases .put (service .getId (), service );
54
63
}
55
64
}
56
65
57
- return new ServiceMap (map , publicMap );
66
+ // resolve alias, as xml as a fully validated stated
67
+ // all alias are valid per file
68
+ aliases .values ().forEach (service -> {
69
+ ServiceInterface serviceAlias = services .get (service .getAlias ());
70
+ if (serviceAlias != null ) {
71
+ String className = serviceAlias .getClassName ();
72
+ if (className != null ) {
73
+ XmlService v = XmlService .create (
74
+ service .getId (),
75
+ className ,
76
+ service .isPublic ()
77
+ );
78
+ services .put (service .getId (), v );
79
+ }
80
+ }
81
+ });
82
+
83
+ return new ServiceMap (services .values ());
58
84
}
59
85
}
0 commit comments