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.commons.lang3.Validate; 032import org.apache.maven.toolchain.model.PersistedToolchains; 033import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; 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 toolchains from the default textual format. 040 * 041 * @author Robert Scholte 042 * @since 3.3.0 043 */ 044@Named 045@Singleton 046public class DefaultToolchainsReader 047 implements ToolchainsReader 048{ 049 050 @Override 051 public PersistedToolchains read( File input, Map<String, ?> options ) 052 throws IOException 053 { 054 Validate.notNull( input, "input cannot be null" ); 055 056 return read( ReaderFactory.newXmlReader( input ), options ); 057 } 058 059 @Override 060 public PersistedToolchains read( Reader input, Map<String, ?> options ) 061 throws IOException 062 { 063 Validate.notNull( input, "input cannot be null" ); 064 065 try 066 { 067 MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader(); 068 return r.read( input, isStrict( options ) ); 069 } 070 catch ( XmlPullParserException e ) 071 { 072 throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 073 } 074 finally 075 { 076 IOUtil.close( input ); 077 } 078 } 079 080 @Override 081 public PersistedToolchains read( InputStream input, Map<String, ?> options ) 082 throws IOException 083 { 084 Validate.notNull( input, "input cannot be null" ); 085 086 try 087 { 088 MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader(); 089 return r.read( input, isStrict( options ) ); 090 } 091 catch ( XmlPullParserException e ) 092 { 093 throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); 094 } 095 finally 096 { 097 IOUtil.close( input ); 098 } 099 } 100 101 private boolean isStrict( Map<String, ?> options ) 102 { 103 Object value = ( options != null ) ? options.get( IS_STRICT ) : null; 104 return value == null || Boolean.parseBoolean( value.toString() ); 105 } 106 107}