001package org.apache.maven.wagon.providers.ssh; 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 org.apache.maven.wagon.TransferFailedException; 023 024import java.io.BufferedReader; 025import java.io.IOException; 026import java.io.StringReader; 027import java.util.ArrayList; 028import java.util.List; 029import java.util.regex.Matcher; 030import java.util.regex.Pattern; 031 032/** 033 * Parser for the output of <code>ls</code> command from any ssh server on any OS. 034 * 035 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 036 * 037 */ 038public class LSParser 039{ 040 /** 041 * output samples see LSParserTest: 042 * <ul></ul> 043 * <li>osx "-rw-r--r-- 1 joakim joakim 1194 Dec 11 09:25 pom.xml"</li> 044 * <li>osx fr : "-rw-r--r-- 1 olamy staff 19 21 sep 00:34 more-resources.dat"</li> 045 * <li>cygwin : "drwxr-xr-x+ 5 joakim None 0 Dec 11 10:30 pom.xml"</li> 046 * <li>linux : "-rw-r--r-- 1 joakim joakim 1194 2006-12-11 09:25 pom.xml"</li> 047 * </ul> 048 */ 049 private static final Pattern PATTERN = Pattern.compile( ".+\\s+[0-9]+\\s+.+\\s+.+\\s+[0-9]+\\s+" 050 //2006-12-11 051 + "([0-9]{4}-[0-9]{2}-[0-9]{2}" 052 // Dec 11 053 + "|.+\\s+[0-9]+" 054 // 21 sep 055 + "|.+\\s+.+)" 056 // 09:25 pom.xml 057 + "\\s+[0-9:]+\\s+(.+?)" ); 058 059 /** 060 * Parse a raw "ls -FlA", and obtain the list of files. 061 * 062 * @param rawLS the raw LS to parse. 063 * @return the list of files found. 064 * @throws TransferFailedException 065 * @todo use ls -1a and do away with the method all together 066 */ 067 public List<String> parseFiles( String rawLS ) 068 throws TransferFailedException 069 { 070 List<String> ret = new ArrayList<String>(); 071 try 072 { 073 BufferedReader br = new BufferedReader( new StringReader( rawLS ) ); 074 075 String line = br.readLine(); 076 077 while ( line != null ) 078 { 079 line = line.trim(); 080 081 Matcher m = PATTERN.matcher( line ); 082 if ( m.matches() ) 083 { 084 ret.add( m.group( 2 ) ); 085 } 086 line = br.readLine(); 087 } 088 } 089 catch ( IOException e ) 090 { 091 throw new TransferFailedException( "Error parsing file listing.", e ); 092 } 093 094 return ret; 095 } 096}