View Javadoc
1   package org.apache.maven.artifact.repository.metadata.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Handles deserialization of metadata from some kind of textual format like XML.
37   *
38   * @author Benjamin Bentmann
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  }