143107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller/*
243107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * Copyright (C) 2015 The Android Open Source Project
343107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller *
443107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * Licensed under the Apache License, Version 2.0 (the "License");
543107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * you may not use this file except in compliance with the License.
643107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * You may obtain a copy of the License at
743107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller *
843107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller *      http://www.apache.org/licenses/LICENSE-2.0
943107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller *
1043107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * Unless required by applicable law or agreed to in writing, software
1143107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * distributed under the License is distributed on an "AS IS" BASIS,
1243107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1343107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * See the License for the specific language governing permissions and
1443107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * limitations under the License.
1543107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller */
16ac1c676b1256aaa85c5cc22494ea56bd2d276b9fNeil Fullerpackage com.google.currysrc.processors;
17ac1c676b1256aaa85c5cc22494ea56bd2d276b9fNeil Fuller
18ac1c676b1256aaa85c5cc22494ea56bd2d276b9fNeil Fullerimport com.google.currysrc.api.process.Reporter;
1943107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller
2043107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fullerimport org.eclipse.jdt.core.dom.Comment;
2143107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller
2243107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller/**
2343107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller * Replaces all occurrences of {@code oldText} with {@code newText}.
2443107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller */
2543107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fullerpublic final class ReplaceTextCommentScanner extends BaseModifyCommentScanner {
2643107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  private final String oldText;
2743107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  private final String newText;
2843107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller
2943107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  public ReplaceTextCommentScanner(String oldText, String newText) {
3043107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller    this.oldText = oldText;
3143107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller    this.newText = newText;
3243107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  }
3343107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller
34ac1c676b1256aaa85c5cc22494ea56bd2d276b9fNeil Fuller  @Override
35ac1c676b1256aaa85c5cc22494ea56bd2d276b9fNeil Fuller  protected String processComment(Reporter reporter, Comment commentNode, String commentText) {
3643107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller    String newCommentText = commentText.replace(oldText, newText);
3743107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller    if (newCommentText.equals(commentText)) {
3843107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller      return null;
3943107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller    }
4043107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller    return newCommentText;
4143107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  }
4243107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller
4343107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  @Override
4443107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  public String toString() {
45905383184e9a27153fe4bd8b203775365b529900Neil Fuller    return "ReplaceTextCommentScanner{" +
4643107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller        "oldText='" + oldText + '\'' +
4743107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller        ", newText='" + newText + '\'' +
4843107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller        '}';
4943107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller  }
5043107a5a258dffdcc774ad6b7a88e87e996872f5Neil Fuller}
51