001package org.apache.maven.toolchain.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 javax.inject.Named; 029import javax.inject.Singleton; 030 031import org.apache.maven.toolchain.model.PersistedToolchains; 032import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; 033import org.codehaus.plexus.util.IOUtil; 034import org.codehaus.plexus.util.ReaderFactory; 035import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 036 037/** 038 * Handles deserialization of toolchains from the default textual format. 039 * 040 * @author Robert Scholte 041 * @since 3.3.0 042 */ 043@Named 044@Singleton 045public class DefaultToolchainsReader 046 implements ToolchainsReader 047{ 048 049 @Override 050 public PersistedToolchains read( File input, Map<String, ?> options ) 051 throws IOException 052 { 053 if ( input == null ) 054 { 055 throw new IllegalArgumentException( "input file missing" ); 056 } 057 058 return read( ReaderFactory.newXmlReader( input ), options ); 059 } 060 061 @Override 062 public PersistedToolchains read( Reader input, Map<String, ?> options ) 063 throws IOException 064 { 065 if ( input == null ) 066 { 067 throw new IllegalArgumentException( "input reader missing" ); 068 } 069 070 try 071 { 072 MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader(); 073 return r.read( input, isStrict( options ) ); 074 } 075 catch ( XmlPullParserException e ) 076 { 077 throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 078 } 079 finally 080 { 081 IOUtil.close( input ); 082 } 083 } 084 085 @Override 086 public PersistedToolchains read( InputStream input, Map<String, ?> options ) 087 throws IOException 088 { 089 if ( input == null ) 090 { 091 throw new IllegalArgumentException( "input stream missing" ); 092 } 093 094 try 095 { 096 MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader(); 097 return r.read( input, isStrict( options ) ); 098 } 099 catch ( XmlPullParserException e ) 100 { 101 throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 102 } 103 finally 104 { 105 IOUtil.close( input ); 106 } 107 } 108 109 private boolean isStrict( Map<String, ?> options ) 110 { 111 Object value = ( options != null ) ? options.get( IS_STRICT ) : null; 112 return value == null || Boolean.parseBoolean( value.toString() ); 113 } 114 115}