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