1 package org.apache.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.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.
32 public class FilenameParser
36 private String extension;
40 private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" );
42 private static final Pattern extensionPattern =
43 Pattern.compile( "(\\.tar\\.gz$)|(\\.tar\\.bz2$)|(\\.[\\-a-z0-9]*$)", Pattern.CASE_INSENSITIVE );
45 private static final Pattern SNAPSHOT_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)$" );
47 private static final Pattern section = Pattern.compile( "([^-]*)" );
49 private Matcher matcher;
51 protected FilenameParser( String filename )
55 Matcher mat = extensionPattern.matcher( name );
58 extension = filename.substring( mat.start() + 1 );
59 name = name.substring( 0, name.length() - extension.length() - 1 );
62 matcher = section.matcher( name );
67 protected void reset()
72 protected String next()
74 // Past the end of the string.
75 if ( offset > name.length() )
80 // Return the next section.
81 if ( matcher.find( offset ) )
83 // Return found section.
84 offset = matcher.end() + 1;
85 return matcher.group();
92 protected String expect( String expected )
96 if ( name.startsWith( expected, offset ) )
100 else if ( VersionUtil.isGenericSnapshot( expected ) )
102 String version = name.substring( offset );
104 // check it starts with the same version up to the snapshot part
105 int leadingLength = expected.length() - 9;
106 if ( leadingLength > 0 && version.startsWith( expected.substring( 0, leadingLength ) ) &&
107 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 StringBuilder ver = new StringBuilder();
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();