001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.versionmigrator.internal;
014
015import java.util.Optional;
016import java.util.regex.Matcher;
017import java.util.regex.Pattern;
018
019import org.apache.tapestry5.versionmigrator.ClassRefactor;
020import org.apache.tapestry5.versionmigrator.FileRefactorCommitParser;
021
022/**
023 * Parses lines like this, in which just the package is changed:
024 * <code>commons/src/main/java/org/apache/tapestry5/{ioc =&gt; commons}/Messages.java (98%)</code>. 
025 */
026public class PackageChangeRefactorCommitParser implements FileRefactorCommitParser {
027
028    final public static String EXAMPLE = "commons/src/main/java/org/apache/tapestry5/{ioc => commons}/Messages.java (98%)";
029    
030    final private static Pattern PATTERN = 
031            Pattern.compile("([^/]*)/(.*)" + Pattern.quote("{") + "(.*)\\s=>\\s(.*)" + Pattern.quote("}/") + "([^\\.]*).*");
032    
033    @Override
034    public Optional<ClassRefactor> apply(String line) 
035    {
036        final Matcher matcher = PATTERN.matcher(line);
037        ClassRefactor move = null;
038        if (matcher.matches()) 
039        {
040            String newPackageNameSuffix = matcher.group(4);
041            String oldPackageNameSuffix = matcher.group(3);
042//                    System.out.printf("1(%s) 2(%s) 3(%s) 4(%s) 5(%s)\n", matcher.group(1), matcher.group(2), oldPackageNameSuffix, newPackageNameSuffix, matcher.group(5));
043            
044            final String rootPackageName = matcher.group(2)
045                    .replace("src/main/java/", "")
046                    .replace("/", ".");
047            final String className = matcher.group(5);
048            final String newClassName = FileRefactorCommitParser.buildClassName(rootPackageName, newPackageNameSuffix, className);
049            final String oldClassName = FileRefactorCommitParser.buildClassName(rootPackageName, oldPackageNameSuffix, className);
050            final String artifactName = matcher.group(1);
051            
052            move = new ClassRefactor(newClassName, oldClassName, artifactName, artifactName);
053        }
054        return Optional.ofNullable(move);
055    }
056
057}