001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.svn; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.UncheckedIOException; 024import java.nio.charset.StandardCharsets; 025import java.nio.file.Files; 026import java.nio.file.Path; 027import java.util.ArrayList; 028import java.util.Iterator; 029import java.util.List; 030import java.util.stream.Collectors; 031 032import org.codehaus.plexus.util.Os; 033 034/** 035 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 036 */ 037public class SvnConfigFileReader { 038 private File configDirectory; 039 040 public File getConfigDirectory() { 041 if (configDirectory == null) { 042 if (Os.isFamily(Os.FAMILY_WINDOWS)) { 043 configDirectory = new File(System.getenv("APPDATA"), "Subversion"); 044 } else { 045 configDirectory = new File(System.getProperty("user.home"), ".subversion"); 046 } 047 } 048 049 return configDirectory; 050 } 051 052 public void setConfigDirectory(File configDirectory) { 053 this.configDirectory = configDirectory; 054 } 055 056 public String getProperty(String group, String propertyName) { 057 List<String> lines = getConfigLines(); 058 059 boolean inGroup = false; 060 for (Iterator<String> i = lines.iterator(); i.hasNext(); ) { 061 String line = i.next().trim(); 062 063 if (!inGroup) { 064 if (("[" + group + "]").equals(line)) { 065 inGroup = true; 066 } 067 } else { 068 if (line.startsWith("[") && line.endsWith("]")) { 069 // a new group start 070 return null; 071 } 072 073 if (line.startsWith("#")) { 074 continue; 075 } 076 if (line.indexOf('=') < 0) { 077 continue; 078 } 079 080 String property = line.substring(0, line.indexOf('=')).trim(); 081 082 if (!property.equals(propertyName)) { 083 continue; 084 } 085 086 String value = line.substring(line.indexOf('=') + 1); 087 return value.trim(); 088 } 089 } 090 091 return null; 092 } 093 094 /** 095 * Load the svn config file. 096 * 097 * @return the list of all non-comment, non-empty lines in the config file 098 */ 099 private List<String> getConfigLines() { 100 Path configPath = getConfigDirectory().toPath().resolve("config"); 101 if (Files.exists(configPath)) { 102 try { 103 return Files.lines(configPath, StandardCharsets.UTF_8) 104 .filter(line -> !line.isEmpty()) 105 .filter(line -> !line.startsWith("#")) 106 .collect(Collectors.toCollection(ArrayList::new)); 107 } catch (UncheckedIOException | IOException e) { 108 return new ArrayList<>(); 109 } 110 } 111 return new ArrayList<>(); 112 } 113}