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 java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import org.apache.maven.wagon.TransferFailedException;
31
32 /**
33 * LSParser
34 *
35 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
36 * @version $Id: LSParser.java 682071 2008-08-02 22:21:46Z hboutemy $
37 */
38 public class LSParser
39 {
40 private final static Pattern PATTERN = Pattern.compile( ".+\\s+[0-9]+\\s+.+\\s+.+\\s+[0-9]+\\s+"
41 + "([0-9]{4}-[0-9]{2}-[0-9]{2}|.+\\s+[0-9]+)\\s+[0-9:]+\\s+(.+?)" );
42
43 /**
44 * Parse a raw "ls -la", and obtain the list of files.
45 *
46 * @todo use ls -1a and do away with the method all together
47 *
48 * @param rawLS the raw LS to parse.
49 * @return the list of files found.
50 * @throws TransferFailedException
51 */
52 public List parseFiles( String rawLS )
53 throws TransferFailedException
54 {
55 List ret = new ArrayList();
56 try
57 {
58 BufferedReader br = new BufferedReader( new StringReader( rawLS ) );
59
60 String line = br.readLine();
61
62 while ( line != null )
63 {
64 line = line.trim();
65
66 Matcher m = PATTERN.matcher( line );
67 if ( m.matches() )
68 {
69 ret.add( m.group( 2 ) );
70 }
71 line = br.readLine();
72 }
73 }
74 catch ( IOException e )
75 {
76 throw new TransferFailedException( "Error parsing file listing.", e );
77 }
78
79 return ret;
80 }
81 }