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