1 package org.apache.maven.artifact.repository.metadata.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.util.Map;
27 import java.util.Objects;
28
29 import org.apache.maven.artifact.repository.metadata.Metadata;
30 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.util.ReaderFactory;
33 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34
35
36
37
38
39
40 @Component( role = MetadataReader.class )
41 public class DefaultMetadataReader
42 implements MetadataReader
43 {
44
45 public Metadata read( File input, Map<String, ?> options )
46 throws IOException
47 {
48 Objects.requireNonNull( input, "input cannot be null" );
49
50 Metadata metadata = read( ReaderFactory.newXmlReader( input ), options );
51
52 return metadata;
53 }
54
55 public Metadata read( Reader input, Map<String, ?> options )
56 throws IOException
57 {
58 Objects.requireNonNull( input, "input cannot be null" );
59
60 try ( final Reader in = input )
61 {
62 return new MetadataXpp3Reader().read( in, isStrict( options ) );
63 }
64 catch ( XmlPullParserException e )
65 {
66 throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
67 }
68 }
69
70 public Metadata read( InputStream input, Map<String, ?> options )
71 throws IOException
72 {
73 Objects.requireNonNull( input, "input cannot be null" );
74
75 try ( final InputStream in = input )
76 {
77 return new MetadataXpp3Reader().read( in, isStrict( options ) );
78 }
79 catch ( XmlPullParserException e )
80 {
81 throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
82 }
83 }
84
85 private boolean isStrict( Map<String, ?> options )
86 {
87 Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
88 return value == null || Boolean.parseBoolean( value.toString() );
89 }
90
91 }