View Javadoc

1   /*
2    * Copyright 2007-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.exceptions.conf;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import de.smartics.maven.exceptions.util.FileNameUtils;
25  
26  
27  /**
28   * This class provides utilities to discover source files.
29   */
30  public final class ConfigUtils
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    // --- members --------------------------------------------------------------
37  
38    // ****************************** Initializer *******************************
39  
40    // ****************************** Constructors ******************************
41  
42    /**
43     * Default constructor.
44     */
45    private ConfigUtils()
46    {
47    }
48  
49    // ****************************** Inner Classes *****************************
50  
51    // ********************************* Methods ********************************
52  
53    // --- init -----------------------------------------------------------------
54  
55    // --- get&set --------------------------------------------------------------
56  
57    // --- business -------------------------------------------------------------
58  
59    /**
60     * Checks each name if it references a library and if this is the case tries
61     * to find a source archive for the class archive.
62     *
63     * @param classRootDirectoryNames the root directory names for the class
64     *          archives (may be directories or files).
65     * @return the collection of source archives found for the given class
66     *         archives.
67     */
68    public static Collection<String> discoverSourceJars(
69        final Collection<String> classRootDirectoryNames)
70    {
71      final Collection<String> sourceRootDirectoryNames =
72          new ArrayList<String>(classRootDirectoryNames.size());
73  
74      for (String classRootDirectoryName : classRootDirectoryNames)
75      {
76        if (FileNameUtils.isArchive(classRootDirectoryName))
77        {
78          final String sourceRootDirectoryName =
79              discoverSourceDirectory(classRootDirectoryName);
80          if (sourceRootDirectoryName != null)
81          {
82            sourceRootDirectoryNames.add(sourceRootDirectoryName);
83          }
84        }
85      }
86  
87      return sourceRootDirectoryNames;
88    }
89  
90    /**
91     * Dispatches the given name and tries to find a source directory for it.
92     *
93     * @param classRootDirectoryName the name that references an archive with
94     *          classes.
95     * @return the name of an existing source archive or <code>null</code> if no
96     *         such archive has been found.
97     * @throws NullPointerException if the argument is <code>null</code>.
98     */
99    public static String discoverSourceDirectory(
100       final String classRootDirectoryName) throws NullPointerException
101   {
102     final int index = classRootDirectoryName.lastIndexOf('.');
103 
104     if (index >= 0 && index < classRootDirectoryName.length() - 1)
105     {
106       final String stem = classRootDirectoryName.substring(0, index);
107       File file = new File(stem + "-sources.jar");
108       if (file.exists())
109       {
110         return file.getAbsolutePath();
111       }
112 
113       file = new File(stem + "-sources.zip");
114       if (file.exists())
115       {
116         return file.getAbsolutePath();
117       }
118     }
119 
120     return null;
121   }
122 
123   /**
124    * Checks that the {@link String} value is provided. If not, the given message
125    * will be appended to the given buffer.
126    *
127    * @param buffer the buffer to append the <code>message</code> to, if
128    *          <code>value</code> is blank.
129    * @param value the value that must not be blank.
130    * @param message the validation failure message.
131    */
132   public static void checkStringValueProvided(
133       final StringBuilder buffer,
134       final String value,
135       final String message)
136   {
137     if (StringUtils.isBlank(value))
138     {
139       buffer.append('\n').append(message);
140     }
141   }
142 
143   /**
144    * Checks that the {@link Object} value is provided. If not, the given message
145    * will be appended to the given buffer.
146    *
147    * @param buffer the buffer to append the <code>message</code> to, if
148    *          <code>value</code> is <code>null</code>.
149    * @param value the value that must not be <code>null</code>.
150    * @param message the validation failure message.
151    */
152   public static void checkValueProvided(
153       final StringBuilder buffer,
154       final Object value,
155       final String message)
156   {
157     if (value == null)
158     {
159       buffer.append('\n').append(message);
160     }
161   }
162 
163   /**
164    * Checks that the {@link Collection} value is provided. If not, the given
165    * message will be appended to the given buffer.
166    *
167    * @param buffer the buffer to append the <code>message</code> to, if
168    *          <code>value</code> is <code>null</code> or empty.
169    * @param value the value that must not be <code>null</code> or empty.
170    * @param message the validation failure message.
171    */
172   public static void checkCollectionValueProvided(
173       final StringBuilder buffer,
174       final Collection<?> value,
175       final String message)
176   {
177     if (value == null || value.isEmpty())
178     {
179       buffer.append('\n').append(message);
180     }
181   }
182 
183   // --- object basics --------------------------------------------------------
184 
185 }