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