1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings.io.xpp3;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.Reader;
24 import org.apache.maven.settings.Settings;
25 import org.codehaus.plexus.util.ReaderFactory;
26 import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
27 import org.codehaus.plexus.util.xml.pull.MXParser;
28 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30
31 public class SettingsXpp3Reader {
32 private boolean addDefaultEntities = true;
33
34 private final ContentTransformer contentTransformer;
35
36 public SettingsXpp3Reader() {
37 this((source, fieldName) -> source);
38 }
39
40 public SettingsXpp3Reader(ContentTransformer contentTransformer) {
41 this.contentTransformer = contentTransformer;
42 }
43
44
45
46
47
48
49 public boolean getAddDefaultEntities() {
50 return addDefaultEntities;
51 }
52
53
54
55
56
57
58
59
60
61
62
63 public Settings read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
64 XmlPullParser parser =
65 addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser();
66 parser.setInput(reader);
67 return read(parser, strict);
68 }
69
70
71
72
73
74
75
76
77
78
79 public Settings read(Reader reader) throws IOException, XmlPullParserException {
80 return read(reader, true);
81 }
82
83
84
85
86
87
88
89
90
91
92
93 public Settings read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
94 return read(ReaderFactory.newXmlReader(in), strict);
95 }
96
97
98
99
100
101
102
103
104
105
106 public Settings read(InputStream in) throws IOException, XmlPullParserException {
107 return read(ReaderFactory.newXmlReader(in));
108 }
109
110
111
112
113
114
115
116
117
118
119
120 public Settings read(XmlPullParser parser, boolean strict) throws IOException, XmlPullParserException {
121 org.apache.maven.settings.v4.SettingsXpp3Reader reader = contentTransformer != null
122 ? new org.apache.maven.settings.v4.SettingsXpp3Reader(contentTransformer::transform)
123 : new org.apache.maven.settings.v4.SettingsXpp3Reader();
124 reader.setAddDefaultEntities(addDefaultEntities);
125 org.apache.maven.api.settings.Settings settings = reader.read(parser, strict);
126 return new Settings(settings);
127 }
128
129
130
131
132
133
134 public void setAddDefaultEntities(boolean addDefaultEntities) {
135 this.addDefaultEntities = addDefaultEntities;
136 }
137
138 public interface ContentTransformer {
139
140
141
142
143
144
145
146 String transform(String source, String fieldName);
147 }
148 }