001    package org.apache.maven.model.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.FileInputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.Reader;
027    import java.util.Map;
028    
029    import org.apache.maven.model.InputSource;
030    import org.apache.maven.model.Model;
031    import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
032    import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx;
033    import org.codehaus.plexus.component.annotations.Component;
034    import org.codehaus.plexus.util.IOUtil;
035    import org.codehaus.plexus.util.ReaderFactory;
036    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
037    
038    /**
039     * Handles deserialization of a model from some kind of textual format like XML.
040     * 
041     * @author Benjamin Bentmann
042     */
043    @Component( role = ModelReader.class )
044    public class DefaultModelReader
045        implements ModelReader
046    {
047    
048        public Model read( File input, Map<String, ?> options )
049            throws IOException
050        {
051            if ( input == null )
052            {
053                throw new IllegalArgumentException( "input file missing" );
054            }
055    
056            Model model = read( new FileInputStream( input ), options );
057    
058            model.setPomFile( input );
059    
060            return model;
061        }
062    
063        public Model read( Reader input, Map<String, ?> options )
064            throws IOException
065        {
066            if ( input == null )
067            {
068                throw new IllegalArgumentException( "input reader missing" );
069            }
070    
071            try
072            {
073                return read( input, isStrict( options ), getSource( options ) );
074            }
075            finally
076            {
077                IOUtil.close( input );
078            }
079        }
080    
081        public Model 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                return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource( options ) );
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        private InputSource getSource( Map<String, ?> options )
106        {
107            Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
108            return (InputSource) value;
109        }
110    
111        private Model read( Reader reader, boolean strict, InputSource source )
112            throws IOException
113        {
114            try
115            {
116                if ( source != null )
117                {
118                    return new MavenXpp3ReaderEx().read( reader, strict, source );
119                }
120                else
121                {
122                    return new MavenXpp3Reader().read( reader, strict );
123                }
124            }
125            catch ( XmlPullParserException e )
126            {
127                throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
128            }
129        }
130    
131    }