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