View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.javagen.agile.core.visitor;
17  
18  import org.javagen.agile.core.model.Model;
19  import org.javagen.agile.core.util.RegexRenamer;
20  
21  /***
22   * Visits models applying regex to names, renaming them when a match is found.
23   * <p>
24   * Skips traversing the model If no regex have been set.
25   *
26   * @author Richard Easterling
27   */
28  public class RegexRenameVisitor extends DefaultVisitor {
29      
30      private RegexRenamer regexRenamer;
31      
32      ////////////////////////////////////////////////////////////////////////////
33      // Generator interface:
34      ////////////////////////////////////////////////////////////////////////////
35      
36      @Override
37      public Model gen(Model model) {
38          if (regexRenamer!=null && !regexRenamer.isEmpty()) {
39              GenericWalker.walk(model, this);
40          }
41          return model;
42      }
43  
44      ////////////////////////////////////////////////////////////////////////////
45      // Visitor interface:
46      ////////////////////////////////////////////////////////////////////////////
47  
48      @Override
49      public void visit(Model model) {
50          String newName = regexRenamer.rename(model.getName());
51          if (newName!=null)
52              model.setName(newName);
53      }
54  
55      ////////////////////////////////////////////////////////////////////////////
56      // getters and setters:
57      ////////////////////////////////////////////////////////////////////////////
58  
59      public RegexRenamer getRegexRename() {
60          return regexRenamer;
61      }
62  
63      public void setRegexRename(RegexRenamer regexRenamer) {
64          this.regexRenamer = regexRenamer;
65      }
66          
67  }