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.maven.model.InputSource; 030import org.apache.maven.model.Model; 031import org.apache.maven.model.io.xpp3.MavenXpp3Reader; 032import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx; 033import org.codehaus.plexus.component.annotations.Component; 034import org.codehaus.plexus.util.IOUtil; 035import org.codehaus.plexus.util.ReaderFactory; 036import 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 ) 044public class DefaultModelReader 045 implements ModelReader 046{ 047 048 @Override 049 public Model read( File input, Map<String, ?> options ) 050 throws IOException 051 { 052 if ( input == null ) 053 { 054 throw new IllegalArgumentException( "input file missing" ); 055 } 056 057 Model model = read( new FileInputStream( input ), options ); 058 059 model.setPomFile( input ); 060 061 return model; 062 } 063 064 @Override 065 public Model read( Reader input, Map<String, ?> options ) 066 throws IOException 067 { 068 if ( input == null ) 069 { 070 throw new IllegalArgumentException( "input reader missing" ); 071 } 072 073 try 074 { 075 return read( input, isStrict( options ), getSource( options ) ); 076 } 077 finally 078 { 079 IOUtil.close( input ); 080 } 081 } 082 083 @Override 084 public Model 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 return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource( options ) ); 095 } 096 finally 097 { 098 IOUtil.close( input ); 099 } 100 } 101 102 private boolean isStrict( Map<String, ?> options ) 103 { 104 Object value = ( options != null ) ? options.get( IS_STRICT ) : null; 105 return value == null || Boolean.parseBoolean( value.toString() ); 106 } 107 108 private InputSource getSource( Map<String, ?> options ) 109 { 110 Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null; 111 return (InputSource) value; 112 } 113 114 private Model read( Reader reader, boolean strict, InputSource source ) 115 throws IOException 116 { 117 try 118 { 119 if ( source != null ) 120 { 121 return new MavenXpp3ReaderEx().read( reader, strict, source ); 122 } 123 else 124 { 125 return new MavenXpp3Reader().read( reader, strict ); 126 } 127 } 128 catch ( XmlPullParserException e ) 129 { 130 throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 131 } 132 } 133 134}