Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LSParser |
|
| 5.0;5 |
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 | * LSParser | |
34 | * | |
35 | * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> | |
36 | * @version $Id: LSParser.java 1174063 2011-09-22 11:22:43Z olamy $ | |
37 | */ | |
38 | 5 | 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 | 1 | 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 | 5 | List<String> ret = new ArrayList<String>(); |
71 | try | |
72 | { | |
73 | 5 | BufferedReader br = new BufferedReader( new StringReader( rawLS ) ); |
74 | ||
75 | 5 | String line = br.readLine(); |
76 | ||
77 | 46 | while ( line != null ) |
78 | { | |
79 | 41 | line = line.trim(); |
80 | ||
81 | 41 | Matcher m = PATTERN.matcher( line ); |
82 | 41 | if ( m.matches() ) |
83 | { | |
84 | 36 | ret.add( m.group( 2 ) ); |
85 | } | |
86 | 41 | line = br.readLine(); |
87 | 41 | } |
88 | } | |
89 | 0 | catch ( IOException e ) |
90 | { | |
91 | 0 | throw new TransferFailedException( "Error parsing file listing.", e ); |
92 | 5 | } |
93 | ||
94 | 5 | return ret; |
95 | } | |
96 | } |