View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.maven.plugin.jboss.modules.domain.matching;
17  
18  import java.util.regex.MatchResult;
19  
20  import de.smartics.maven.plugin.jboss.modules.domain.MatchContext;
21  
22  /**
23   * Contains the result of two a regular expression matches. One groupId and one
24   * artifactId match.
25   */
26  public final class DoubleMatchContext implements MatchContext
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    // --- members --------------------------------------------------------------
33  
34    /**
35     * The result of the match.
36     */
37    private final boolean result;
38  
39    /**
40     * The match result to access group information of the groupId.
41     */
42    private final MatchResult groupMatchResult;
43  
44    /**
45     * The match result to access group information of the artifactId.
46     */
47    private final MatchResult artifactMatchResult;
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Constructor with a context.
55     *
56     * @param result the result of the match.
57     * @param groupIdContext the context of a match to derive from for groupIds.
58     * @param artifactIdContext the context of a match to derive from for
59     *          artifactIds.
60     */
61    public DoubleMatchContext(final boolean result,
62        final MatchContext groupIdContext, final MatchContext artifactIdContext)
63    {
64      this.result = result;
65      this.groupMatchResult =
66          groupIdContext != null && groupIdContext.isMatched() ? groupIdContext
67              .getMatchResult() : null;
68      this.artifactMatchResult =
69          artifactIdContext != null && artifactIdContext.isMatched()
70              ? artifactIdContext.getMatchResult() : null;
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    /**
82     * Checks if the match was successful.
83     *
84     * @return <code>true</code> if the match was successful, <code>false</code>
85     *         otherwise.
86     */
87    public boolean isMatched()
88    {
89      return result;
90    }
91  
92    /**
93     * {@inheritDoc}
94     * <p>
95     * Returns the artifactId match result.
96     * </p>
97     */
98    @Override
99    public MatchResult getMatchResult()
100   {
101     return artifactMatchResult;
102   }
103 
104   // --- business -------------------------------------------------------------
105 
106   @Override
107   public String translateName(final String input)
108   {
109     String translation = input;
110 
111     if (isMatched())
112     {
113       if (artifactMatchResult != null)
114       {
115         translation = translate(artifactMatchResult, "$", translation);
116       }
117 
118       if (groupMatchResult != null)
119       {
120         translation = translate(groupMatchResult, "$g", translation);
121       }
122     }
123 
124     return translation;
125   }
126 
127   private static String translate(final MatchResult matchResult,
128       final String delimiter, final String input)
129   {
130     String translation = input;
131     final int groupCount = matchResult.groupCount();
132     if (groupCount > 0)
133     {
134       for (int group = 1; group <= groupCount; group++)
135       {
136         final String replacement = matchResult.group(group);
137         translation = translation.replace(delimiter + group, replacement);
138       }
139     }
140     return translation;
141   }
142 
143   /**
144    * Checks if any match produced at least one group match.
145    *
146    * @return <code>true</code> if at least one group is matched,
147    *         <code>false</code> otherwise.
148    */
149   public boolean hasGroupMatch()
150   {
151     if (artifactMatchResult != null && isMatched())
152     {
153       final int groupCount = artifactMatchResult.groupCount();
154       if (groupCount > 0)
155       {
156         return true;
157       }
158     }
159 
160     if (groupMatchResult != null && isMatched())
161     {
162       final int groupCount = groupMatchResult.groupCount();
163       if (groupCount > 0)
164       {
165         return true;
166       }
167     }
168 
169     return false;
170   }
171 
172   // --- object basics --------------------------------------------------------
173 
174 }