    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 both artifact and package are changed:
    024 * <code>{tapestry-ioc/src/main/java/org/apache/tapestry5/ioc =&gt; commons/src/main/java/org/apache/tapestry5/commons}/ObjectProvider.java</code>.
    025 */
    026public class PackageAndArtifactChangeRefactorCommitParser implements FileRefactorCommitParser {
    027
    028    final public static String EXAMPLE = "{tapestry-ioc/src/main/java/org/apache/tapestry5/ioc => commons/src/main/java/org/apache/tapestry5/commons}/ObjectProvider.java";
    029
    030    final private static Pattern PATTERN =
    031            Pattern.compile("\\{([^\\/]+)\\/([^\\s]+)\\s=>\\s([^\\/]+)\\/([^}]+)}([^\\.]+).*");
    032
    033    @Override
    034    public Optional<ClassRefactor> apply(String line)
    035    {
    036        ClassRefactor move = null;
    037          final Matcher matcher = PATTERN.matcher(line);
    038          if (matcher.matches())
    039          {
    040//              System.out.printf("1(%s) 2(%s) 3(%s) 4(%s) 5(%s)\n", matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4), matcher.group(5));
    041              String newPackageNameSuffix = FileRefactorCommitParser.extractPackageOrClassName(matcher.group(4));
    042              String oldPackageNameSuffix = FileRefactorCommitParser.extractPackageOrClassName(matcher.group(2));
    043              final String className = matcher.group(5);
    044              final String newClassName = FileRefactorCommitParser.buildClassName("", newPackageNameSuffix, className);
    045              final String oldClassName = FileRefactorCommitParser.buildClassName("", oldPackageNameSuffix, className);
    046              final String sourceArtifactName = matcher.group(1);
    047              final String destinationArtifactName = matcher.group(3);
    048
    049              move  = new ClassRefactor(newClassName, oldClassName, sourceArtifactName, destinationArtifactName);
    050          }
    051        return Optional.ofNullable(move);
    052
    053    }
    054
    055}