001package org.apache.maven.settings.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.maven.settings.Settings; 029import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; 030import org.codehaus.plexus.component.annotations.Component; 031import org.codehaus.plexus.util.IOUtil; 032import org.codehaus.plexus.util.ReaderFactory; 033import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 034 035/** 036 * Handles deserialization of settings from the default textual format. 037 * 038 * @author Benjamin Bentmann 039 */ 040@Component( role = SettingsReader.class ) 041public class DefaultSettingsReader 042 implements SettingsReader 043{ 044 045 @Override 046 public Settings read( File input, Map<String, ?> options ) 047 throws IOException 048 { 049 if ( input == null ) 050 { 051 throw new IllegalArgumentException( "input file missing" ); 052 } 053 054 Settings settings = read( ReaderFactory.newXmlReader( input ), options ); 055 056 return settings; 057 } 058 059 @Override 060 public Settings read( Reader input, Map<String, ?> options ) 061 throws IOException 062 { 063 if ( input == null ) 064 { 065 throw new IllegalArgumentException( "input reader missing" ); 066 } 067 068 try 069 { 070 SettingsXpp3Reader r = new SettingsXpp3Reader(); 071 return r.read( input, isStrict( options ) ); 072 } 073 catch ( XmlPullParserException e ) 074 { 075 throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 076 } 077 finally 078 { 079 IOUtil.close( input ); 080 } 081 } 082 083 @Override 084 public Settings read( InputStream input, Map<String, ?> options ) 085 throws IOException 086 { 087 if ( input == null ) 088 { 089 throw new IllegalArgumentException( "input stream missing" ); 090 } 091 092 try 093 { 094 SettingsXpp3Reader r = new SettingsXpp3Reader(); 095 return r.read( input, isStrict( options ) ); 096 } 097 catch ( XmlPullParserException e ) 098 { 099 throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 100 } 101 finally 102 { 103 IOUtil.close( input ); 104 } 105 } 106 107 private boolean isStrict( Map<String, ?> options ) 108 { 109 Object value = ( options != null ) ? options.get( IS_STRICT ) : null; 110 return value == null || Boolean.parseBoolean( value.toString() ); 111 } 112 113}