1
2
3
4
5 package org.apache.maven.cli.internal.extension.io;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.Reader;
10 import java.text.DateFormat;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.LinkedHashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import org.apache.maven.api.annotations.Generated;
21 import org.apache.maven.cli.internal.extension.model.CoreExtensions;
22 import org.apache.maven.cli.internal.extension.model.CoreExtension;
23 import org.apache.maven.internal.xml.XmlNodeStaxBuilder;
24 import org.apache.maven.api.xml.XmlNode;
25 import javax.xml.stream.XMLInputFactory;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamReader;
28 import javax.xml.transform.stream.StreamSource;
29
30 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI;
31 import static javax.xml.XMLConstants.XML_NS_URI;
32
33 @Generated
34 public class CoreExtensionsStaxReader {
35
36 private static final Map<String, String> DEFAULT_ENTITIES;
37 static {
38 Map<String, String> entities = new HashMap<>();
39 entities.put("nbsp", "\u00a0");
40 entities.put("iexcl", "\u00a1");
41 entities.put("cent", "\u00a2");
42 entities.put("pound", "\u00a3");
43 entities.put("curren", "\u00a4");
44 entities.put("yen", "\u00a5");
45 entities.put("brvbar", "\u00a6");
46 entities.put("sect", "\u00a7");
47 entities.put("uml", "\u00a8");
48 entities.put("copy", "\u00a9");
49 entities.put("ordf", "\u00aa");
50 entities.put("laquo", "\u00ab");
51 entities.put("not", "\u00ac");
52 entities.put("shy", "\u00ad");
53 entities.put("reg", "\u00ae");
54 entities.put("macr", "\u00af");
55 entities.put("deg", "\u00b0");
56 entities.put("plusmn", "\u00b1");
57 entities.put("sup2", "\u00b2");
58 entities.put("sup3", "\u00b3");
59 entities.put("acute", "\u00b4");
60 entities.put("micro", "\u00b5");
61 entities.put("para", "\u00b6");
62 entities.put("middot", "\u00b7");
63 entities.put("cedil", "\u00b8");
64 entities.put("sup1", "\u00b9");
65 entities.put("ordm", "\u00ba");
66 entities.put("raquo", "\u00bb");
67 entities.put("frac14", "\u00bc");
68 entities.put("frac12", "\u00bd");
69 entities.put("frac34", "\u00be");
70 entities.put("iquest", "\u00bf");
71 entities.put("Agrave", "\u00c0");
72 entities.put("Aacute", "\u00c1");
73 entities.put("Acirc", "\u00c2");
74 entities.put("Atilde", "\u00c3");
75 entities.put("Auml", "\u00c4");
76 entities.put("Aring", "\u00c5");
77 entities.put("AElig", "\u00c6");
78 entities.put("Ccedil", "\u00c7");
79 entities.put("Egrave", "\u00c8");
80 entities.put("Eacute", "\u00c9");
81 entities.put("Ecirc", "\u00ca");
82 entities.put("Euml", "\u00cb");
83 entities.put("Igrave", "\u00cc");
84 entities.put("Iacute", "\u00cd");
85 entities.put("Icirc", "\u00ce");
86 entities.put("Iuml", "\u00cf");
87 entities.put("ETH", "\u00d0");
88 entities.put("Ntilde", "\u00d1");
89 entities.put("Ograve", "\u00d2");
90 entities.put("Oacute", "\u00d3");
91 entities.put("Ocirc", "\u00d4");
92 entities.put("Otilde", "\u00d5");
93 entities.put("Ouml", "\u00d6");
94 entities.put("times", "\u00d7");
95 entities.put("Oslash", "\u00d8");
96 entities.put("Ugrave", "\u00d9");
97 entities.put("Uacute", "\u00da");
98 entities.put("Ucirc", "\u00db");
99 entities.put("Uuml", "\u00dc");
100 entities.put("Yacute", "\u00dd");
101 entities.put("THORN", "\u00de");
102 entities.put("szlig", "\u00df");
103 entities.put("agrave", "\u00e0");
104 entities.put("aacute", "\u00e1");
105 entities.put("acirc", "\u00e2");
106 entities.put("atilde", "\u00e3");
107 entities.put("auml", "\u00e4");
108 entities.put("aring", "\u00e5");
109 entities.put("aelig", "\u00e6");
110 entities.put("ccedil", "\u00e7");
111 entities.put("egrave", "\u00e8");
112 entities.put("eacute", "\u00e9");
113 entities.put("ecirc", "\u00ea");
114 entities.put("euml", "\u00eb");
115 entities.put("igrave", "\u00ec");
116 entities.put("iacute", "\u00ed");
117 entities.put("icirc", "\u00ee");
118 entities.put("iuml", "\u00ef");
119 entities.put("eth", "\u00f0");
120 entities.put("ntilde", "\u00f1");
121 entities.put("ograve", "\u00f2");
122 entities.put("oacute", "\u00f3");
123 entities.put("ocirc", "\u00f4");
124 entities.put("otilde", "\u00f5");
125 entities.put("ouml", "\u00f6");
126 entities.put("divide", "\u00f7");
127 entities.put("oslash", "\u00f8");
128 entities.put("ugrave", "\u00f9");
129 entities.put("uacute", "\u00fa");
130 entities.put("ucirc", "\u00fb");
131 entities.put("uuml", "\u00fc");
132 entities.put("yacute", "\u00fd");
133 entities.put("thorn", "\u00fe");
134 entities.put("yuml", "\u00ff");
135
136
137
138
139
140 entities.put("OElig", "\u0152");
141 entities.put("oelig", "\u0153");
142 entities.put("Scaron", "\u0160");
143 entities.put("scaron", "\u0161");
144 entities.put("Yuml", "\u0178");
145 entities.put("circ", "\u02c6");
146 entities.put("tilde", "\u02dc");
147 entities.put("ensp", "\u2002");
148 entities.put("emsp", "\u2003");
149 entities.put("thinsp", "\u2009");
150 entities.put("zwnj", "\u200c");
151 entities.put("zwj", "\u200d");
152 entities.put("lrm", "\u200e");
153 entities.put("rlm", "\u200f");
154 entities.put("ndash", "\u2013");
155 entities.put("mdash", "\u2014");
156 entities.put("lsquo", "\u2018");
157 entities.put("rsquo", "\u2019");
158 entities.put("sbquo", "\u201a");
159 entities.put("ldquo", "\u201c");
160 entities.put("rdquo", "\u201d");
161 entities.put("bdquo", "\u201e");
162 entities.put("dagger", "\u2020");
163 entities.put("Dagger", "\u2021");
164 entities.put("permil", "\u2030");
165 entities.put("lsaquo", "\u2039");
166 entities.put("rsaquo", "\u203a");
167 entities.put("euro", "\u20ac");
168
169
170
171
172
173 entities.put("fnof", "\u0192");
174 entities.put("Alpha", "\u0391");
175 entities.put("Beta", "\u0392");
176 entities.put("Gamma", "\u0393");
177 entities.put("Delta", "\u0394");
178 entities.put("Epsilon", "\u0395");
179 entities.put("Zeta", "\u0396");
180 entities.put("Eta", "\u0397");
181 entities.put("Theta", "\u0398");
182 entities.put("Iota", "\u0399");
183 entities.put("Kappa", "\u039a");
184 entities.put("Lambda", "\u039b");
185 entities.put("Mu", "\u039c");
186 entities.put("Nu", "\u039d");
187 entities.put("Xi", "\u039e");
188 entities.put("Omicron", "\u039f");
189 entities.put("Pi", "\u03a0");
190 entities.put("Rho", "\u03a1");
191 entities.put("Sigma", "\u03a3");
192 entities.put("Tau", "\u03a4");
193 entities.put("Upsilon", "\u03a5");
194 entities.put("Phi", "\u03a6");
195 entities.put("Chi", "\u03a7");
196 entities.put("Psi", "\u03a8");
197 entities.put("Omega", "\u03a9");
198 entities.put("alpha", "\u03b1");
199 entities.put("beta", "\u03b2");
200 entities.put("gamma", "\u03b3");
201 entities.put("delta", "\u03b4");
202 entities.put("epsilon", "\u03b5");
203 entities.put("zeta", "\u03b6");
204 entities.put("eta", "\u03b7");
205 entities.put("theta", "\u03b8");
206 entities.put("iota", "\u03b9");
207 entities.put("kappa", "\u03ba");
208 entities.put("lambda", "\u03bb");
209 entities.put("mu", "\u03bc");
210 entities.put("nu", "\u03bd");
211 entities.put("xi", "\u03be");
212 entities.put("omicron", "\u03bf");
213 entities.put("pi", "\u03c0");
214 entities.put("rho", "\u03c1");
215 entities.put("sigmaf", "\u03c2");
216 entities.put("sigma", "\u03c3");
217 entities.put("tau", "\u03c4");
218 entities.put("upsilon", "\u03c5");
219 entities.put("phi", "\u03c6");
220 entities.put("chi", "\u03c7");
221 entities.put("psi", "\u03c8");
222 entities.put("omega", "\u03c9");
223 entities.put("thetasym", "\u03d1");
224 entities.put("upsih", "\u03d2");
225 entities.put("piv", "\u03d6");
226 entities.put("bull", "\u2022");
227 entities.put("hellip", "\u2026");
228 entities.put("prime", "\u2032");
229 entities.put("Prime", "\u2033");
230 entities.put("oline", "\u203e");
231 entities.put("frasl", "\u2044");
232 entities.put("weierp", "\u2118");
233 entities.put("image", "\u2111");
234 entities.put("real", "\u211c");
235 entities.put("trade", "\u2122");
236 entities.put("alefsym", "\u2135");
237 entities.put("larr", "\u2190");
238 entities.put("uarr", "\u2191");
239 entities.put("rarr", "\u2192");
240 entities.put("darr", "\u2193");
241 entities.put("harr", "\u2194");
242 entities.put("crarr", "\u21b5");
243 entities.put("lArr", "\u21d0");
244 entities.put("uArr", "\u21d1");
245 entities.put("rArr", "\u21d2");
246 entities.put("dArr", "\u21d3");
247 entities.put("hArr", "\u21d4");
248 entities.put("forall", "\u2200");
249 entities.put("part", "\u2202");
250 entities.put("exist", "\u2203");
251 entities.put("empty", "\u2205");
252 entities.put("nabla", "\u2207");
253 entities.put("isin", "\u2208");
254 entities.put("notin", "\u2209");
255 entities.put("ni", "\u220b");
256 entities.put("prod", "\u220f");
257 entities.put("sum", "\u2211");
258 entities.put("minus", "\u2212");
259 entities.put("lowast", "\u2217");
260 entities.put("radic", "\u221a");
261 entities.put("prop", "\u221d");
262 entities.put("infin", "\u221e");
263 entities.put("ang", "\u2220");
264 entities.put("and", "\u2227");
265 entities.put("or", "\u2228");
266 entities.put("cap", "\u2229");
267 entities.put("cup", "\u222a");
268 entities.put("int", "\u222b");
269 entities.put("there4", "\u2234");
270 entities.put("sim", "\u223c");
271 entities.put("cong", "\u2245");
272 entities.put("asymp", "\u2248");
273 entities.put("ne", "\u2260");
274 entities.put("equiv", "\u2261");
275 entities.put("le", "\u2264");
276 entities.put("ge", "\u2265");
277 entities.put("sub", "\u2282");
278 entities.put("sup", "\u2283");
279 entities.put("nsub", "\u2284");
280 entities.put("sube", "\u2286");
281 entities.put("supe", "\u2287");
282 entities.put("oplus", "\u2295");
283 entities.put("otimes", "\u2297");
284 entities.put("perp", "\u22a5");
285 entities.put("sdot", "\u22c5");
286 entities.put("lceil", "\u2308");
287 entities.put("rceil", "\u2309");
288 entities.put("lfloor", "\u230a");
289 entities.put("rfloor", "\u230b");
290 entities.put("lang", "\u2329");
291 entities.put("rang", "\u232a");
292 entities.put("loz", "\u25ca");
293 entities.put("spades", "\u2660");
294 entities.put("clubs", "\u2663");
295 entities.put("hearts", "\u2665");
296 entities.put("diams", "\u2666");
297 DEFAULT_ENTITIES = Collections.unmodifiableMap(entities);
298 }
299
300 private boolean addDefaultEntities = true;
301
302 private final ContentTransformer contentTransformer;
303
304 public CoreExtensionsStaxReader() {
305 this((s, f) -> s);
306 }
307
308 public CoreExtensionsStaxReader(ContentTransformer contentTransformer) {
309 this.contentTransformer = contentTransformer;
310 }
311
312
313
314
315
316
317 public boolean getAddDefaultEntities() {
318 return addDefaultEntities;
319 }
320
321
322
323
324
325
326 public void setAddDefaultEntities(boolean addDefaultEntities) {
327 this.addDefaultEntities = addDefaultEntities;
328 }
329
330
331 public CoreExtensions read(Reader reader) throws XMLStreamException {
332 return read(reader, true);
333 }
334
335
336
337
338
339
340
341
342 public CoreExtensions read(Reader reader, boolean strict) throws XMLStreamException {
343 XMLInputFactory factory = XMLInputFactory.newFactory();
344 factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
345 StreamSource streamSource = new StreamSource(reader);
346 XMLStreamReader parser = factory.createXMLStreamReader(streamSource);
347 return read(parser, strict);
348 }
349
350 public CoreExtensions read(InputStream in) throws XMLStreamException {
351 return read(in, true);
352 }
353
354
355
356
357
358
359
360
361
362
363 public CoreExtensions read(InputStream in, boolean strict) throws XMLStreamException {
364 XMLInputFactory factory = XMLInputFactory.newFactory();
365 factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
366 StreamSource streamSource = new StreamSource(in);
367 XMLStreamReader parser = factory.createXMLStreamReader(streamSource);
368 return read(parser, strict);
369 }
370
371
372
373
374
375
376
377
378
379
380 public CoreExtensions read(XMLStreamReader parser, boolean strict) throws XMLStreamException {
381 CoreExtensions coreExtensions = null;
382 int eventType = parser.getEventType();
383 boolean parsed = false;
384 while (eventType != XMLStreamReader.END_DOCUMENT) {
385 if (eventType == XMLStreamReader.START_ELEMENT) {
386 if (strict && ! "extensions".equals(parser.getLocalName())) {
387 throw new XMLStreamException("Expected root element 'extensions' but found '" + parser.getName() + "'", parser.getLocation(), null);
388 } else if (parsed) {
389
390 throw new XMLStreamException("Duplicated tag: 'extensions'", parser.getLocation(), null);
391 }
392 coreExtensions = parseCoreExtensions(parser, strict);
393 parsed = true;
394 }
395 eventType = parser.next();
396 }
397 if (parsed) {
398 return coreExtensions;
399 }
400 throw new XMLStreamException("Expected root element 'extensions' but found no element at all: invalid XML document", parser.getLocation(), null);
401 }
402
403 private CoreExtensions parseCoreExtensions(XMLStreamReader parser, boolean strict) throws XMLStreamException {
404 String tagName = parser.getLocalName();
405 CoreExtensions.Builder coreExtensions = CoreExtensions.newBuilder(true);
406 for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
407 String name = parser.getAttributeLocalName(i);
408 String ns = parser.getAttributeNamespace(i);
409 String value = parser.getAttributeValue(i);
410 if (W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(ns) || XML_NS_URI.equals(ns)) {
411
412 } else if ("xmlns".equals(name)) {
413
414 } else {
415 checkUnknownAttribute(parser, name, tagName, strict);
416 }
417 }
418 Set<String> parsed = new HashSet<>();
419 List<CoreExtension> extensions = new ArrayList<>();
420 while ((strict ? parser.nextTag() : nextTag(parser)) == XMLStreamReader.START_ELEMENT) {
421 String childName = checkDuplicate(parser.getLocalName(), parser, parsed);
422 switch (childName) {
423 case "extension": {
424 extensions.add(parseCoreExtension(parser, strict));
425 break;
426 }
427 default: {
428 checkUnknownElement(parser, strict);
429 break;
430 }
431 }
432 }
433 coreExtensions.extensions(extensions);
434 coreExtensions.namespaceUri(parser.getNamespaceURI());
435 coreExtensions.modelEncoding(parser.getEncoding());
436 return coreExtensions.build();
437 }
438
439 private CoreExtension parseCoreExtension(XMLStreamReader parser, boolean strict) throws XMLStreamException {
440 String tagName = parser.getLocalName();
441 CoreExtension.Builder coreExtension = CoreExtension.newBuilder(true);
442 for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
443 String name = parser.getAttributeLocalName(i);
444 String ns = parser.getAttributeNamespace(i);
445 String value = parser.getAttributeValue(i);
446 if (W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(ns) || XML_NS_URI.equals(ns)) {
447
448 } else {
449 checkUnknownAttribute(parser, name, tagName, strict);
450 }
451 }
452 Set<String> parsed = new HashSet<>();
453 while ((strict ? parser.nextTag() : nextTag(parser)) == XMLStreamReader.START_ELEMENT) {
454 String childName = checkDuplicate(parser.getLocalName(), parser, parsed);
455 switch (childName) {
456 case "groupId": {
457 coreExtension.groupId(interpolatedTrimmed(nextText(parser, strict), "groupId"));
458 break;
459 }
460 case "artifactId": {
461 coreExtension.artifactId(interpolatedTrimmed(nextText(parser, strict), "artifactId"));
462 break;
463 }
464 case "version": {
465 coreExtension.version(interpolatedTrimmed(nextText(parser, strict), "version"));
466 break;
467 }
468 case "classLoadingStrategy": {
469 coreExtension.classLoadingStrategy(interpolatedTrimmed(nextText(parser, strict), "classLoadingStrategy"));
470 break;
471 }
472 case "configuration": {
473 coreExtension.configuration(buildXmlNode(parser));
474 break;
475 }
476 default: {
477 checkUnknownElement(parser, strict);
478 break;
479 }
480 }
481 }
482 return coreExtension.build();
483 }
484
485
486 private String checkDuplicate(String tagName, XMLStreamReader parser, Set<String> parsed) throws XMLStreamException {
487 switch (tagName) {
488 case "extension":
489 break;
490 default:
491 if (!parsed.add(tagName)) {
492 throw new XMLStreamException("Duplicated tag: '" + tagName + "'", parser.getLocation(), null);
493 }
494 }
495 return tagName;
496 }
497
498
499
500
501
502
503
504
505
506
507
508
509 private void checkUnknownAttribute(XMLStreamReader parser, String attribute, String tagName, boolean strict) throws XMLStreamException {
510
511 if (strict) {
512 throw new XMLStreamException("Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser.getLocation(), null);
513 }
514 }
515
516
517
518
519
520
521
522
523
524
525 private void checkUnknownElement(XMLStreamReader parser, boolean strict) throws XMLStreamException {
526 if (strict) {
527 throw new XMLStreamException("Unrecognised tag: '" + parser.getName() + "'", parser.getLocation(), null);
528 }
529
530 for (int unrecognizedTagCount = 1; unrecognizedTagCount > 0;) {
531 int eventType = nextTag(parser);
532 if (eventType == XMLStreamReader.START_ELEMENT) {
533 unrecognizedTagCount++;
534 } else if (eventType == XMLStreamReader.END_ELEMENT) {
535 unrecognizedTagCount--;
536 }
537 }
538 }
539
540
541
542
543
544
545
546 private String getTrimmedValue(String s) {
547 if (s != null) {
548 s = s.trim();
549 }
550 return s;
551 }
552
553
554
555
556
557
558
559
560 private String interpolatedTrimmed(String value, String context) {
561 return getTrimmedValue(contentTransformer.transform(value, context));
562 }
563
564
565
566
567
568
569
570
571
572
573 private int nextTag(XMLStreamReader parser) throws XMLStreamException {
574 while (true) {
575 int next = parser.next();
576 switch (next) {
577 case XMLStreamReader.SPACE:
578 case XMLStreamReader.COMMENT:
579 case XMLStreamReader.PROCESSING_INSTRUCTION:
580 case XMLStreamReader.CDATA:
581 case XMLStreamReader.CHARACTERS:
582 continue;
583 case XMLStreamReader.START_ELEMENT:
584 case XMLStreamReader.END_ELEMENT:
585 return next;
586 }
587 }
588 }
589
590 private String nextText(XMLStreamReader parser, boolean strict) throws XMLStreamException {
591 int eventType = parser.getEventType();
592 if (eventType != XMLStreamReader.START_ELEMENT) {
593 throw new XMLStreamException("parser must be on START_ELEMENT to read next text", parser.getLocation(), null);
594 }
595 eventType = parser.next();
596 StringBuilder result = new StringBuilder();
597 while (true) {
598 if (eventType == XMLStreamReader.CHARACTERS || eventType == XMLStreamReader.CDATA) {
599 result.append(parser.getText());
600 } else if (eventType == XMLStreamReader.ENTITY_REFERENCE) {
601 String val = null;
602 if (strict) {
603 throw new XMLStreamException("Entities are not supported in strict mode", parser.getLocation(), null);
604 } else if (addDefaultEntities) {
605 val = DEFAULT_ENTITIES.get(parser.getLocalName());
606 }
607 if (val != null) {
608 result.append(val);
609 } else {
610 result.append("&").append(parser.getLocalName()).append(";");
611 }
612 } else if (eventType != XMLStreamReader.COMMENT) {
613 break;
614 }
615 eventType = parser.next();
616 }
617 if (eventType != XMLStreamReader.END_ELEMENT) {
618 throw new XMLStreamException(
619 "TEXT must be immediately followed by END_ELEMENT and not " + eventType , parser.getLocation(), null);
620 }
621 return result.toString();
622 }
623
624 private XmlNode buildXmlNode(XMLStreamReader parser) throws XMLStreamException {
625 return XmlNodeStaxBuilder.build(parser);
626 }
627
628 public static interface ContentTransformer {
629
630
631
632
633
634
635
636 String transform(String source, String fieldName);
637 }
638
639 }