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