1 package org.eclipse.aether.internal.test.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30
31
32
33
34
35 class NodeDefinition
36 {
37
38 static final String ID = "\\(([-_a-zA-Z0-9]+)\\)";
39
40 static final String IDREF = "\\^([-_a-zA-Z0-9]+)";
41
42 static final String COORDS = "([^: \\(]+):([^: ]+)(?::([^: ]*)(?::([^: ]+))?)?:([^: \\[\\(<]+)";
43
44 private static final String COORDS_NC = NodeDefinition.COORDS.replaceAll( "\\((?=\\[)", "(?:" );
45
46 private static final String RANGE_NC = "[\\(\\[][^\\(\\)\\[\\]]+[\\)\\]]";
47
48 static final String RANGE = "(" + RANGE_NC + ")";
49
50 static final String SCOPE = "(?:scope\\s*=\\s*)?((?!optional)[-_a-zA-Z0-9]+)(?:<([-_a-zA-Z0-9]+))?";
51
52 static final String OPTIONAL = "(!?optional)";
53
54 static final String RELOCATIONS = "relocations\\s*=\\s*(" + COORDS_NC + "(?:\\s*,\\s*" + COORDS_NC + ")*)";
55
56 static final String KEY_VAL = "(?:[-_a-zA-Z0-9]+)\\s*:\\s*(?:[-_a-zA-Z0-9]*)";
57
58 static final String PROPS = "props\\s*=\\s*(" + KEY_VAL + "(?:\\s*,\\s*" + KEY_VAL + ")*)";
59
60 static final String COORDSX = "(" + COORDS_NC + ")" + RANGE + "?(?:<((?:" + RANGE_NC + ")|\\S+))?";
61
62 static final String NODE = COORDSX + "(?:\\s+" + PROPS + ")?" + "(?:\\s+" + SCOPE + ")?" + "(?:\\s+" + OPTIONAL
63 + ")?" + "(?:\\s+" + RELOCATIONS + ")?" + "(?:\\s+" + ID + ")?";
64
65 static final String LINE = "(?:" + IDREF + ")|(?:" + NODE + ")";
66
67 private static final Pattern PATTERN = Pattern.compile( LINE );
68
69 private final String def;
70
71 String coords;
72
73 Map<String, String> properties;
74
75 String range;
76
77 String premanagedVersion;
78
79 String scope;
80
81 String premanagedScope;
82
83 Boolean optional;
84
85 List<String> relocations;
86
87 String id;
88
89 String reference;
90
91 NodeDefinition( String definition )
92 {
93 def = definition.trim();
94
95 Matcher m = PATTERN.matcher( def );
96 if ( !m.matches() )
97 {
98 throw new IllegalArgumentException( "bad syntax: " + def );
99 }
100
101 reference = m.group( 1 );
102 if ( reference != null )
103 {
104 return;
105 }
106
107 coords = m.group( 2 );
108 range = m.group( 3 );
109 premanagedVersion = m.group( 4 );
110
111 String props = m.group( 5 );
112 if ( props != null )
113 {
114 properties = new LinkedHashMap<>();
115 for ( String prop : props.split( "\\s*,\\s*" ) )
116 {
117 int sep = prop.indexOf( ':' );
118 String key = prop.substring( 0, sep );
119 String val = prop.substring( sep + 1 );
120 properties.put( key, val );
121 }
122 }
123
124 scope = m.group( 6 );
125 premanagedScope = m.group( 7 );
126 optional = ( m.group( 8 ) != null ) ? !m.group( 8 ).startsWith( "!" ) : Boolean.FALSE;
127
128 String relocs = m.group( 9 );
129 if ( relocs != null )
130 {
131 relocations = new ArrayList<>();
132 Collections.addAll( relocations, relocs.split( "\\s*,\\s*" ) );
133 }
134
135 id = m.group( 10 );
136 }
137
138 @Override
139 public String toString()
140 {
141 return def;
142 }
143
144 }