1 package org.apache.maven.archiva.repository.content;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 * Generic Filename Parser for use with layout routines.
30 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
33 public class FilenameParser
37 private String extension;
41 private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" );
43 private static final Pattern extensionPattern = Pattern.compile( "(.tar.gz$)|(.tar.bz2$)|(.[a-z0-9]*$)",
44 Pattern.CASE_INSENSITIVE );
46 private static final Pattern section = Pattern.compile( "([^-]*)" );
48 private Matcher matcher;
50 protected FilenameParser( String filename )
54 Matcher mat = extensionPattern.matcher( name );
57 extension = filename.substring( mat.start() + 1 );
58 name = name.substring( 0, name.length() - extension.length() - 1 );
61 matcher = section.matcher( name );
66 protected void reset()
71 protected String next()
73 // Past the end of the string.
74 if ( offset > name.length() )
79 // Return the next section.
80 if ( matcher.find( offset ) )
82 // Return found section.
83 offset = matcher.end() + 1;
84 return matcher.group();
91 protected String expect( String expected )
93 if ( name.startsWith( expected, offset ) )
95 // Potential hit. check for '.' or '-' at end of expected.
96 int seperatorOffset = offset + expected.length();
98 // Test for "out of bounds" first.
99 if ( seperatorOffset >= name.length() )
101 offset = name.length();
105 // Test for seperator char.
106 char seperatorChar = name.charAt( seperatorOffset );
107 if ( ( seperatorChar == '-' ) || ( seperatorChar == '.' ) )
109 offset = seperatorOffset + 1;
118 * Get the current seperator character.
120 * @return the seperator character (either '.' or '-'), or 0 if no seperator character available.
122 protected char seperator()
124 // Past the end of the string?
125 if ( offset >= name.length() )
130 // Before the start of the string?
136 return name.charAt( offset - 1 );
139 protected String getName()
144 protected String getExtension()
149 protected String remaining()
151 if ( offset >= name.length() )
156 String end = name.substring( offset );
157 offset = name.length();
161 protected String nextNonVersion()
163 boolean done = false;
165 StringBuffer ver = new StringBuffer();
167 // Any text upto the end of a special case is considered non-version.
168 Matcher specialMat = mavenPluginPattern.matcher( name );
169 if ( specialMat.find() )
171 ver.append( name.substring( offset, specialMat.end() ) );
172 offset = specialMat.end() + 1;
177 int initialOffset = offset;
178 String section = next();
179 if ( section == null )
183 else if ( !VersionUtil.isVersion( section ) )
185 if ( ver.length() > 0 )
189 ver.append( section );
193 offset = initialOffset;
198 return ver.toString();
201 protected String nextVersion()
203 boolean done = false;
205 StringBuffer ver = new StringBuffer();
209 int initialOffset = offset;
210 String section = next();
211 if ( section == null )
215 else if ( VersionUtil.isVersion( section ) )
217 if ( ver.length() > 0 )
221 ver.append( section );
225 offset = initialOffset;
230 return ver.toString();