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]{1,4}$)",
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;
117 protected String getName()
122 protected String getExtension()
127 protected String remaining()
129 if ( offset >= name.length() )
134 String end = name.substring( offset );
135 offset = name.length();
139 protected String nextNonVersion()
141 boolean done = false;
143 StringBuffer ver = new StringBuffer();
145 // Any text upto the end of a special case is considered non-version.
146 Matcher specialMat = mavenPluginPattern.matcher( name );
147 if ( specialMat.find() )
149 ver.append( name.substring( offset, specialMat.end() ) );
150 offset = specialMat.end() + 1;
155 int initialOffset = offset;
156 String section = next();
157 if ( section == null )
161 else if ( !VersionUtil.isVersion( section ) )
163 if ( ver.length() > 0 )
167 ver.append( section );
171 offset = initialOffset;
176 return ver.toString();
179 protected String nextVersion()
181 boolean done = false;
183 StringBuffer ver = new StringBuffer();
187 int initialOffset = offset;
188 String section = next();
189 if ( section == null )
193 else if ( VersionUtil.isVersion( section ) )
195 if ( ver.length() > 0 )
199 ver.append( section );
203 offset = initialOffset;
208 return ver.toString();