1 package org.apache.maven.model.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.Reader;
26 import java.nio.file.Path;
27 import java.util.Map;
28 import java.util.Objects;
29
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.inject.Singleton;
33
34 import org.apache.maven.api.model.InputSource;
35 import org.apache.maven.api.model.Model;
36 import org.apache.maven.model.building.ModelSourceTransformer;
37 import org.apache.maven.model.building.TransformerContext;
38 import org.apache.maven.model.v4.MavenXpp3Reader;
39 import org.apache.maven.model.v4.MavenXpp3ReaderEx;
40 import org.codehaus.plexus.util.ReaderFactory;
41 import org.codehaus.plexus.util.xml.XmlStreamReader;
42 import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
43 import org.codehaus.plexus.util.xml.pull.MXParser;
44 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
45 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
46
47
48
49
50
51
52 @Named
53 @Singleton
54 public class DefaultModelReader
55 implements ModelReader
56 {
57 private final ModelSourceTransformer transformer;
58
59 @Inject
60 public DefaultModelReader( ModelSourceTransformer transformer )
61 {
62 this.transformer = transformer;
63 }
64
65 @Override
66 public Model read( File input, Map<String, ?> options )
67 throws IOException
68 {
69 Objects.requireNonNull( input, "input cannot be null" );
70
71 try ( XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
72 {
73 Model model = read( in, input.toPath(), options );
74
75 return model.withPomFile( input.toPath() );
76 }
77 }
78
79 @Override
80 public Model read( Reader input, Map<String, ?> options )
81 throws IOException
82 {
83 Objects.requireNonNull( input, "input cannot be null" );
84
85 try ( Reader in = input )
86 {
87 return read( in, null, options );
88 }
89 }
90
91 @Override
92 public Model read( InputStream input, Map<String, ?> options )
93 throws IOException
94 {
95 Objects.requireNonNull( input, "input cannot be null" );
96
97 try ( XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
98 {
99 return read( in, null, options );
100 }
101 }
102
103 private boolean isStrict( Map<String, ?> options )
104 {
105 Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
106 return value == null || Boolean.parseBoolean( value.toString() );
107 }
108
109 private InputSource getSource( Map<String, ?> options )
110 {
111 Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
112 if ( value instanceof org.apache.maven.model.InputSource )
113 {
114 org.apache.maven.model.InputSource src = ( org.apache.maven.model.InputSource ) value;
115 return new InputSource( src.getModelId(), src.getLocation() );
116 }
117 return (InputSource) value;
118 }
119
120 private TransformerContext getTransformerContext( Map<String, ?> options )
121 {
122 Object value = ( options != null ) ? options.get( TRANSFORMER_CONTEXT ) : null;
123 return (TransformerContext) value;
124 }
125
126 private Model read( Reader reader, Path pomFile, Map<String, ?> options )
127 throws IOException
128 {
129 try
130 {
131 XmlPullParser parser = new MXParser( EntityReplacementMap.defaultEntityReplacementMap );
132 parser.setInput( reader );
133
134 TransformerContext context = getTransformerContext( options );
135 XmlPullParser transformingParser = context != null
136 ? transformer.transform( parser, pomFile, context ) : parser;
137
138 InputSource source = getSource( options );
139 boolean strict = isStrict( options );
140 if ( source != null )
141 {
142 return readModelEx( transformingParser, source, strict );
143 }
144 else
145 {
146 return readModel( transformingParser, strict );
147 }
148 }
149 catch ( XmlPullParserException e )
150 {
151 throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
152 }
153 catch ( IOException e )
154 {
155 throw e;
156 }
157 catch ( Exception e )
158 {
159 throw new IOException( "Unable to transform pom", e );
160 }
161 }
162
163 private Model readModel( XmlPullParser parser, boolean strict )
164 throws XmlPullParserException, IOException
165 {
166 MavenXpp3Reader mr = new MavenXpp3Reader();
167 return mr.read( parser, strict );
168 }
169
170 private Model readModelEx( XmlPullParser parser, InputSource source, boolean strict )
171 throws XmlPullParserException, IOException
172 {
173 MavenXpp3ReaderEx mr = new MavenXpp3ReaderEx();
174 return mr.read( parser, strict, source );
175 }
176
177 }