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