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  
28  import org.apache.commons.lang3.Validate;
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.IOUtil;
33  import org.codehaus.plexus.util.ReaderFactory;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * Handles deserialization of metadata from some kind of textual format like XML.
38   *
39   * @author Benjamin Bentmann
40   */
41  @Component( role = MetadataReader.class )
42  public class DefaultMetadataReader
43      implements MetadataReader
44  {
45  
46      public Metadata read( File input, Map<String, ?> options )
47          throws IOException
48      {
49          Validate.notNull( input, "input cannot be null" );
50  
51          Metadata metadata = read( ReaderFactory.newXmlReader( input ), options );
52  
53          return metadata;
54      }
55  
56      public Metadata read( Reader input, Map<String, ?> options )
57          throws IOException
58      {
59          Validate.notNull( input, "input cannot be null" );
60  
61          try
62          {
63              MetadataXpp3Reader r = new MetadataXpp3Reader();
64              return r.read( input, isStrict( options ) );
65          }
66          catch ( XmlPullParserException e )
67          {
68              throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
69          }
70          finally
71          {
72              IOUtil.close( input );
73          }
74      }
75  
76      public Metadata read( InputStream input, Map<String, ?> options )
77          throws IOException
78      {
79          Validate.notNull( input, "input cannot be null" );
80  
81          try
82          {
83              MetadataXpp3Reader r = new MetadataXpp3Reader();
84              return r.read( input, isStrict( options ) );
85          }
86          catch ( XmlPullParserException e )
87          {
88              throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
89          }
90          finally
91          {
92              IOUtil.close( input );
93          }
94      }
95  
96      private boolean isStrict( Map<String, ?> options )
97      {
98          Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
99          return value == null || Boolean.parseBoolean( value.toString() );
100     }
101 
102 }