1 package org.apache.maven.wagon.providers.ssh;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.wagon.TransferFailedException;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 /**
33 * Parser for the output of <code>ls</code> command from any ssh server on any OS.
34 *
35 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
36 *
37 */
38 public class LSParser
39 {
40 /**
41 * output samples see LSParserTest:
42 * <ul></ul>
43 * <li>osx "-rw-r--r-- 1 joakim joakim 1194 Dec 11 09:25 pom.xml"</li>
44 * <li>osx fr : "-rw-r--r-- 1 olamy staff 19 21 sep 00:34 more-resources.dat"</li>
45 * <li>cygwin : "drwxr-xr-x+ 5 joakim None 0 Dec 11 10:30 pom.xml"</li>
46 * <li>linux : "-rw-r--r-- 1 joakim joakim 1194 2006-12-11 09:25 pom.xml"</li>
47 * </ul>
48 */
49 private static final Pattern PATTERN = Pattern.compile( ".+\\s+[0-9]+\\s+.+\\s+.+\\s+[0-9]+\\s+"
50 //2006-12-11
51 + "([0-9]{4}-[0-9]{2}-[0-9]{2}"
52 // Dec 11
53 + "|.+\\s+[0-9]+"
54 // 21 sep
55 + "|.+\\s+.+)"
56 // 09:25 pom.xml
57 + "\\s+[0-9:]+\\s+(.+?)" );
58
59 /**
60 * Parse a raw "ls -FlA", and obtain the list of files.
61 *
62 * @param rawLS the raw LS to parse.
63 * @return the list of files found.
64 * @throws TransferFailedException
65 * @todo use ls -1a and do away with the method all together
66 */
67 public List<String> parseFiles( String rawLS )
68 throws TransferFailedException
69 {
70 List<String> ret = new ArrayList<String>();
71 try
72 {
73 BufferedReader br = new BufferedReader( new StringReader( rawLS ) );
74
75 String line = br.readLine();
76
77 while ( line != null )
78 {
79 line = line.trim();
80
81 Matcher m = PATTERN.matcher( line );
82 if ( m.matches() )
83 {
84 ret.add( m.group( 2 ) );
85 }
86 line = br.readLine();
87 }
88 }
89 catch ( IOException e )
90 {
91 throw new TransferFailedException( "Error parsing file listing.", e );
92 }
93
94 return ret;
95 }
96 }