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 =
44 Pattern.compile( "(.tar.gz$)|(.tar.bz2$)|(.[a-z0-9]*$)", Pattern.CASE_INSENSITIVE );
46 private static final Pattern SNAPSHOT_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)$" );
48 private static final Pattern section = Pattern.compile( "([^-]*)" );
50 private Matcher matcher;
52 protected FilenameParser( String filename )
56 Matcher mat = extensionPattern.matcher( name );
59 extension = filename.substring( mat.start() + 1 );
60 name = name.substring( 0, name.length() - extension.length() - 1 );
63 matcher = section.matcher( name );
68 protected void reset()
73 protected String next()
75 // Past the end of the string.
76 if ( offset > name.length() )
81 // Return the next section.
82 if ( matcher.find( offset ) )
84 // Return found section.
85 offset = matcher.end() + 1;
86 return matcher.group();
93 protected String expect( String expected )
97 if ( name.startsWith( expected, offset ) )
101 else if ( VersionUtil.isGenericSnapshot( expected ) )
103 String version = name.substring( offset );
105 // check it starts with the same version up to the snapshot part
106 int leadingLength = expected.length() - 9;
107 if ( version.startsWith( expected.substring( 0, leadingLength ) ) && version.length() > leadingLength )
109 // If we expect a non-generic snapshot - look for the timestamp
110 Matcher m = SNAPSHOT_PATTERN.matcher( version.substring( leadingLength + 1 ) );
113 value = version.substring( 0, leadingLength + 1 ) + m.group( 1 );
120 // Potential hit. check for '.' or '-' at end of expected.
121 int seperatorOffset = offset + value.length();
123 // Test for "out of bounds" first.
124 if ( seperatorOffset >= name.length() )
126 offset = name.length();
130 // Test for seperator char.
131 char seperatorChar = name.charAt( seperatorOffset );
132 if ( ( seperatorChar == '-' ) || ( seperatorChar == '.' ) )
134 offset = seperatorOffset + 1;
143 * Get the current seperator character.
145 * @return the seperator character (either '.' or '-'), or 0 if no seperator character available.
147 protected char seperator()
149 // Past the end of the string?
150 if ( offset >= name.length() )
155 // Before the start of the string?
161 return name.charAt( offset - 1 );
164 protected String getName()
169 protected String getExtension()
174 protected String remaining()
176 if ( offset >= name.length() )
181 String end = name.substring( offset );
182 offset = name.length();
186 protected String nextNonVersion()
188 boolean done = false;
190 StringBuffer ver = new StringBuffer();
192 // Any text upto the end of a special case is considered non-version.
193 Matcher specialMat = mavenPluginPattern.matcher( name );
194 if ( specialMat.find() )
196 ver.append( name.substring( offset, specialMat.end() ) );
197 offset = specialMat.end() + 1;
202 int initialOffset = offset;
203 String section = next();
204 if ( section == null )
208 else if ( !VersionUtil.isVersion( section ) )
210 if ( ver.length() > 0 )
214 ver.append( section );
218 offset = initialOffset;
223 return ver.toString();
226 protected String nextVersion()
228 boolean done = false;
230 StringBuffer ver = new StringBuffer();
234 int initialOffset = offset;
235 String section = next();
236 if ( section == null )
240 else if ( VersionUtil.isVersion( section ) )
242 if ( ver.length() > 0 )
246 ver.append( section );
250 offset = initialOffset;
255 return ver.toString();