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