1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.reader;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.function.Function;
24
25 import org.apache.maven.index.reader.Record.EntryKey;
26 import org.apache.maven.index.reader.Record.Type;
27
28 import static org.apache.maven.index.reader.Utils.FIELD_SEPARATOR;
29 import static org.apache.maven.index.reader.Utils.FS_PATTERN;
30 import static org.apache.maven.index.reader.Utils.INFO;
31 import static org.apache.maven.index.reader.Utils.NOT_AVAILABLE;
32 import static org.apache.maven.index.reader.Utils.UINFO;
33 import static org.apache.maven.index.reader.Utils.renvl;
34
35
36
37
38
39
40 public class RecordExpander implements Function<Map<String, String>, Record> {
41
42
43
44 @Override
45 public Record apply(final Map<String, String> recordMap) {
46 if (recordMap.containsKey("DESCRIPTOR")) {
47 return expandDescriptor(recordMap);
48 } else if (recordMap.containsKey("allGroups")) {
49 return expandAllGroups(recordMap);
50 } else if (recordMap.containsKey("rootGroups")) {
51 return expandRootGroups(recordMap);
52 } else if (recordMap.containsKey("del")) {
53 return expandDeletedArtifact(recordMap);
54 } else {
55
56 final String uinfo = recordMap.get(UINFO);
57 final String info = recordMap.get(INFO);
58 if (uinfo != null && !(info == null || info.isBlank())) {
59 final String[] splitInfo = FS_PATTERN.split(info);
60 if (splitInfo.length > 6) {
61 final String extension = splitInfo[6];
62 if (uinfo.endsWith(FIELD_SEPARATOR + NOT_AVAILABLE)) {
63 recordMap.put(UINFO, uinfo + FIELD_SEPARATOR + extension);
64 }
65 }
66 }
67 return expandAddedArtifact(recordMap);
68 }
69 }
70
71 private static Record expandDescriptor(final Map<String, String> raw) {
72 final Record result = new Record(Type.DESCRIPTOR, new HashMap<>());
73 String[] r = FS_PATTERN.split(raw.get("IDXINFO"));
74 result.put(Record.REPOSITORY_ID, r[1]);
75 return result;
76 }
77
78 private static Record expandAllGroups(final Map<String, String> raw) {
79 final Record result = new Record(Type.ALL_GROUPS, new HashMap<>());
80 putIfNotNullAsStringArray(raw, "allGroupsList", result, Record.ALL_GROUPS);
81 return result;
82 }
83
84 private static Record expandRootGroups(final Map<String, String> raw) {
85 final Record result = new Record(Type.ROOT_GROUPS, new HashMap<>());
86 putIfNotNullAsStringArray(raw, "rootGroupsList", result, Record.ROOT_GROUPS);
87 return result;
88 }
89
90 private static Record expandDeletedArtifact(final Map<String, String> raw) {
91 final Record result = new Record(Type.ARTIFACT_REMOVE, new HashMap<>());
92 putIfNotNullTS(raw, "m", result, Record.REC_MODIFIED);
93 if (raw.containsKey("del")) {
94 expandUinfo(raw.get("del"), result);
95 }
96 return result;
97 }
98
99
100
101
102 private static Record expandAddedArtifact(final Map<String, String> raw) {
103 final Record result = new Record(Type.ARTIFACT_ADD, new HashMap<>());
104
105
106 expandUinfo(raw.get(UINFO), result);
107 final String info = raw.get(INFO);
108 if (info != null) {
109 String[] r = FS_PATTERN.split(info);
110 result.put(Record.PACKAGING, renvl(r[0]));
111 result.put(Record.FILE_MODIFIED, Long.valueOf(r[1]));
112 result.put(Record.FILE_SIZE, Long.valueOf(r[2]));
113 result.put(Record.HAS_SOURCES, "1".equals(r[3]) ? Boolean.TRUE : Boolean.FALSE);
114 result.put(Record.HAS_JAVADOC, "1".equals(r[4]) ? Boolean.TRUE : Boolean.FALSE);
115 result.put(Record.HAS_SIGNATURE, "1".equals(r[5]) ? Boolean.TRUE : Boolean.FALSE);
116 if (r.length > 6) {
117 result.put(Record.FILE_EXTENSION, r[6]);
118 } else {
119 final String packaging = result.getString(Record.PACKAGING);
120 if (result.containsKey(Record.CLASSIFIER)
121 || "pom".equals(packaging)
122 || "war".equals(packaging)
123 || "ear".equals(packaging)) {
124 result.put(Record.FILE_EXTENSION, packaging);
125 } else {
126 result.put(Record.FILE_EXTENSION, "jar");
127 }
128 }
129 }
130 putIfNotNullTS(raw, "m", result, Record.REC_MODIFIED);
131 putIfNotNull(raw, "n", result, Record.NAME);
132 putIfNotNull(raw, "d", result, Record.DESCRIPTION);
133 putIfNotNull(raw, "1", result, Record.SHA1);
134
135
136 putIfNotNullAsStringArray(raw, "classnames", result, Record.CLASSNAMES);
137
138
139 putIfNotNull(raw, "px", result, Record.PLUGIN_PREFIX);
140 putIfNotNullAsStringArray(raw, "gx", result, Record.PLUGIN_GOALS);
141
142
143 putIfNotNull(raw, "Bundle-SymbolicName", result, Record.OSGI_BUNDLE_SYMBOLIC_NAME);
144 putIfNotNull(raw, "Bundle-Version", result, Record.OSGI_BUNDLE_VERSION);
145 putIfNotNull(raw, "Export-Package", result, Record.OSGI_EXPORT_PACKAGE);
146 putIfNotNull(raw, "Export-Service", result, Record.OSGI_EXPORT_SERVICE);
147 putIfNotNull(raw, "Bundle-Description", result, Record.OSGI_BUNDLE_DESCRIPTION);
148 putIfNotNull(raw, "Bundle-Name", result, Record.OSGI_BUNDLE_NAME);
149 putIfNotNull(raw, "Bundle-License", result, Record.OSGI_BUNDLE_LICENSE);
150 putIfNotNull(raw, "Bundle-DocURL", result, Record.OSGI_EXPORT_DOCURL);
151 putIfNotNull(raw, "Import-Package", result, Record.OSGI_IMPORT_PACKAGE);
152 putIfNotNull(raw, "Require-Bundle", result, Record.OSGI_REQUIRE_BUNDLE);
153 putIfNotNull(raw, "Provide-Capability", result, Record.OSGI_PROVIDE_CAPABILITY);
154 putIfNotNull(raw, "Require-Capability", result, Record.OSGI_REQUIRE_CAPABILITY);
155 putIfNotNull(raw, "Fragment-Host", result, Record.OSGI_FRAGMENT_HOST);
156 putIfNotNull(raw, "Bundle-RequiredExecutionEnvironment", result, Record.OSGI_BREE);
157 putIfNotNull(raw, "sha256", result, Record.SHA_256);
158
159 return result;
160 }
161
162
163
164
165 private static void expandUinfo(final String uinfo, final Record result) {
166 if (uinfo != null) {
167 String[] r = FS_PATTERN.split(uinfo);
168 result.put(Record.GROUP_ID, r[0]);
169 result.put(Record.ARTIFACT_ID, r[1]);
170 result.put(Record.VERSION, r[2]);
171 String classifier = renvl(r[3]);
172 if (classifier != null) {
173 result.put(Record.CLASSIFIER, classifier);
174 if (r.length > 4) {
175 result.put(Record.FILE_EXTENSION, r[4]);
176 }
177 } else if (r.length > 4) {
178 result.put(Record.PACKAGING, r[4]);
179 }
180 }
181 }
182
183
184
185
186 private static void putIfNotNull(
187 final Map<String, String> source, final String sourceName, final Record target, final EntryKey targetName) {
188 String value = source.get(sourceName);
189 if (value != null && !value.isBlank()) {
190 target.put(targetName, value);
191 }
192 }
193
194
195
196
197 private static void putIfNotNullTS(
198 final Map<String, String> source, final String sourceName, final Record target, final EntryKey targetName) {
199 String value = source.get(sourceName);
200 if (value != null && !value.isBlank()) {
201 target.put(targetName, Long.valueOf(value));
202 }
203 }
204
205
206
207
208 private static void putIfNotNullAsStringArray(
209 final Map<String, String> source, final String sourceName, final Record target, final EntryKey targetName) {
210 String value = source.get(sourceName);
211 if (value != null && !value.isBlank()) {
212 target.put(targetName, FS_PATTERN.split(value));
213 }
214 }
215 }