001package org.apache.maven.artifact.repository.metadata.io; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.Reader; 026import java.util.Map; 027 028import org.apache.commons.lang3.Validate; 029import org.apache.maven.artifact.repository.metadata.Metadata; 030import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; 031import org.codehaus.plexus.component.annotations.Component; 032import org.codehaus.plexus.util.IOUtil; 033import org.codehaus.plexus.util.ReaderFactory; 034import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 035 036/** 037 * Handles deserialization of metadata from some kind of textual format like XML. 038 * 039 * @author Benjamin Bentmann 040 */ 041@Component( role = MetadataReader.class ) 042public class DefaultMetadataReader 043 implements MetadataReader 044{ 045 046 public Metadata read( File input, Map<String, ?> options ) 047 throws IOException 048 { 049 Validate.notNull( input, "input cannot be null" ); 050 051 Metadata metadata = read( ReaderFactory.newXmlReader( input ), options ); 052 053 return metadata; 054 } 055 056 public Metadata read( Reader input, Map<String, ?> options ) 057 throws IOException 058 { 059 Validate.notNull( input, "input cannot be null" ); 060 061 try 062 { 063 MetadataXpp3Reader r = new MetadataXpp3Reader(); 064 return r.read( input, isStrict( options ) ); 065 } 066 catch ( XmlPullParserException e ) 067 { 068 throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 069 } 070 finally 071 { 072 IOUtil.close( input ); 073 } 074 } 075 076 public Metadata read( InputStream input, Map<String, ?> options ) 077 throws IOException 078 { 079 Validate.notNull( input, "input cannot be null" ); 080 081 try 082 { 083 MetadataXpp3Reader r = new MetadataXpp3Reader(); 084 return r.read( input, isStrict( options ) ); 085 } 086 catch ( XmlPullParserException e ) 087 { 088 throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 089 } 090 finally 091 { 092 IOUtil.close( input ); 093 } 094 } 095 096 private boolean isStrict( Map<String, ?> options ) 097 { 098 Object value = ( options != null ) ? options.get( IS_STRICT ) : null; 099 return value == null || Boolean.parseBoolean( value.toString() ); 100 } 101 102}