View Javadoc

1   package org.apache.continuum.purge.repository.utils;
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.archiva.common.utils.VersionUtil;
23  
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  /**
28   * Codes were taken from Archiva's FilenameParser
29   */
30  public class FilenameParser
31  {
32      private String name;
33  
34      private String extension;
35  
36      private int offset;
37  
38      private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" );
39  
40      private static final Pattern extensionPattern = Pattern.compile(
41          "(\\.tar\\.gz$)|(\\.tar\\.bz2$)|(\\.[\\-a-z0-9]*$)", Pattern.CASE_INSENSITIVE );
42  
43      private static final Pattern section = Pattern.compile( "([^-]*)" );
44  
45      private final Matcher matcher;
46  
47      public FilenameParser( String filename )
48      {
49          this.name = filename;
50  
51          Matcher mat = extensionPattern.matcher( name );
52          if ( mat.find() )
53          {
54              extension = filename.substring( mat.start() + 1 );
55              name = name.substring( 0, name.length() - extension.length() - 1 );
56          }
57  
58          matcher = section.matcher( name );
59  
60          reset();
61      }
62  
63      public void reset()
64      {
65          offset = 0;
66      }
67  
68      public String next()
69      {
70          // Past the end of the string.
71          if ( offset > name.length() )
72          {
73              return null;
74          }
75  
76          // Return the next section.
77          if ( matcher.find( offset ) )
78          {
79              // Return found section.
80              offset = matcher.end() + 1;
81              return matcher.group();
82          }
83  
84          // Nothing to return.
85          return null;
86      }
87  
88      protected String remaining()
89      {
90          if ( offset >= name.length() )
91          {
92              return null;
93          }
94  
95          String end = name.substring( offset );
96          offset = name.length();
97          return end;
98      }
99  
100     protected String nextNonVersion()
101     {
102         boolean done = false;
103 
104         StringBuffer ver = new StringBuffer();
105 
106         // Any text upto the end of a special case is considered non-version. 
107         Matcher specialMat = mavenPluginPattern.matcher( name );
108         if ( specialMat.find() )
109         {
110             ver.append( name.substring( offset, specialMat.end() ) );
111             offset = specialMat.end() + 1;
112         }
113 
114         while ( !done )
115         {
116             int initialOffset = offset;
117             String section = next();
118             if ( section == null )
119             {
120                 done = true;
121             }
122             else if ( !VersionUtil.isVersion( section ) )
123             {
124                 if ( ver.length() > 0 )
125                 {
126                     ver.append( '-' );
127                 }
128                 ver.append( section );
129             }
130             else
131             {
132                 offset = initialOffset;
133                 done = true;
134             }
135         }
136 
137         return ver.toString();
138     }
139 
140     public String getExtension()
141     {
142         return extension;
143     }
144 }