1 package org.apache.archiva.repository.maven.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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.VersionUtil;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
27 * Generic Filename Parser for use with layout routines.
31 public class FilenameParser
35 private String extension;
39 private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" );
41 private static final Pattern extensionPattern =
42 Pattern.compile( "(\\.tar\\.gz$)|(\\.tar\\.bz2$)|(\\.[\\-a-z0-9]*$)", Pattern.CASE_INSENSITIVE );
44 private static final Pattern SNAPSHOT_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)$" );
46 private static final Pattern section = Pattern.compile( "([^-]*)" );
48 private Matcher matcher;
50 public 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 );
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 )
95 if ( name.startsWith( expected, offset ) )
99 else if ( VersionUtil.isGenericSnapshot( expected ) )
101 String version = name.substring( offset );
103 // check it starts with the same version up to the snapshot part
104 int leadingLength = expected.length() - 9;
105 if ( leadingLength > 0 && version.startsWith( expected.substring( 0, leadingLength ) ) &&
106 version.length() > leadingLength )
108 // If we expect a non-generic snapshot - look for the timestamp
109 Matcher m = SNAPSHOT_PATTERN.matcher( version.substring( leadingLength + 1 ) );
112 value = version.substring( 0, leadingLength + 1 ) + m.group( 1 );
119 // Potential hit. check for '.' or '-' at end of expected.
120 int seperatorOffset = offset + value.length();
122 // Test for "out of bounds" first.
123 if ( seperatorOffset >= name.length() )
125 offset = name.length();
129 // Test for seperator char.
130 char seperatorChar = name.charAt( seperatorOffset );
131 if ( ( seperatorChar == '-' ) || ( seperatorChar == '.' ) )
133 offset = seperatorOffset + 1;
142 * Get the current seperator character.
144 * @return the seperator character (either '.' or '-'), or 0 if no seperator character available.
146 protected char seperator()
148 // Past the end of the string?
149 if ( offset >= name.length() )
154 // Before the start of the string?
160 return name.charAt( offset - 1 );
163 protected String getName()
168 public String getExtension()
173 public String remaining()
175 if ( offset >= name.length() )
180 String end = name.substring( offset );
181 offset = name.length();
185 public String nextNonVersion()
187 boolean done = false;
189 StringBuilder ver = new StringBuilder();
191 // Any text upto the end of a special case is considered non-version.
192 Matcher specialMat = mavenPluginPattern.matcher( name );
193 if ( specialMat.find() )
195 ver.append( name.substring( offset, specialMat.end() ) );
196 offset = specialMat.end() + 1;
201 int initialOffset = offset;
202 String section = next();
203 if ( section == null )
207 else if ( !VersionUtil.isVersion( section ) )
209 if ( ver.length() > 0 )
213 ver.append( section );
217 offset = initialOffset;
222 return ver.toString();
225 protected String nextVersion()
227 boolean done = false;
229 StringBuilder ver = new StringBuilder();
233 int initialOffset = offset;
234 String section = next();
235 if ( section == null )
239 else if ( VersionUtil.isVersion( section ) )
241 if ( ver.length() > 0 )
245 ver.append( section );
249 offset = initialOffset;
254 return ver.toString();