View Javadoc

1   /*
2    * Copyright 2011-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.recode;
17  
18  import java.io.File;
19  
20  import org.codehaus.plexus.util.StringUtils;
21  
22  /**
23   * Describes a resource folder.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public class Resources
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The directory containing the resources.
38     */
39    private File dir;
40  
41    /**
42     * The source encoding of the resources.
43     */
44    private String sourceEncoding;
45  
46    /**
47     * The target encoding of the resources.
48     */
49    private String targetEncoding;
50  
51    /**
52     * The files within <code>dir</code> to be included. If not specified all
53     * files in that directory are included.
54     */
55    private String[] includes;
56  
57    /**
58     * The files within <code>dir</code> to be excluded. If not specified no file
59     * in that directory is excluded.
60     */
61    private String[] excludes;
62  
63    /**
64     * The flag indicates whether <code>\r\n</code> are to be translated to
65     * <code>\n</code>.
66     */
67    private Boolean fromDos;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    /**
74     * Default constructor.
75     */
76    public Resources()
77    {
78  
79    }
80  
81    /**
82     * Convenience constructor.
83     *
84     * @param dir the directory containing the resources.
85     */
86    public Resources(final File dir)
87    {
88      this.dir = dir;
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    // ********************************* Methods ********************************
94  
95    // --- init -----------------------------------------------------------------
96  
97    // --- get&set --------------------------------------------------------------
98  
99    /**
100    * Returns the directory containing the resources.
101    *
102    * @return the directory containing the resources.
103    */
104   public File getDir()
105   {
106     return dir;
107   }
108 
109   /**
110    * Returns the source encoding of the resources.
111    *
112    * @return the source encoding of the resources.
113    */
114   public String getSourceEncoding()
115   {
116     return sourceEncoding;
117   }
118 
119   /**
120    * Returns the target encoding of the resources.
121    *
122    * @return the target encoding of the resources.
123    */
124   public String getTargetEncoding()
125   {
126     return targetEncoding;
127   }
128 
129   /**
130    * Returns the files within <code>dir</code> to be included. If not specified
131    * all files in that directory are included.
132    *
133    * @return the files within <code>dir</code> to be included.
134    */
135   public String[] getIncludes()
136   {
137     return includes; // NOPMD
138   }
139 
140   /**
141    * Returns the files within <code>dir</code> to be excluded. If not specified
142    * no file in that directory is excluded.
143    *
144    * @return the files within <code>dir</code> to be excluded.
145    */
146   public String[] getExcludes()
147   {
148     return excludes; // NOPMD
149   }
150 
151   /**
152    * Returns the flag indicates whether <code>\r\n</code> are to be translated
153    * to <code>\n</code>. Defaults to <code>false</code>.
154    *
155    * @return the flag indicates whether <code>\r\n</code> are to be translated
156    *         to <code>\n</code>.
157    */
158   public boolean isFromDos()
159   {
160     return fromDos != null ? fromDos : false;
161   }
162 
163   // --- business -------------------------------------------------------------
164 
165   /**
166    * Sets default if not already set.
167    *
168    * @param sourceEncoding the source encoding of the resources.
169    * @param targetEncoding the target encoding of the resources.
170    * @param fromDos the flag indicates whether <code>\r\n</code> are to be
171    *          translated to <code>\n</code>.
172    */
173   public void setDefaults(final String sourceEncoding,
174       final String targetEncoding, final boolean fromDos)
175   {
176     if (StringUtils.isBlank(this.sourceEncoding))
177     {
178       this.sourceEncoding = sourceEncoding;
179     }
180     if (StringUtils.isBlank(this.targetEncoding))
181     {
182       this.targetEncoding = targetEncoding;
183     }
184     if (this.fromDos == null)
185     {
186       this.fromDos = fromDos;
187     }
188 
189     if (dir == null)
190     {
191       throw new IllegalStateException(
192           "Directory of resource descriptor is required.");
193     }
194   }
195 
196   // --- object basics --------------------------------------------------------
197 
198   /**
199    * Returns the string representation of the object.
200    *
201    * @return the string representation of the object.
202    */
203   @Override
204   public String toString()
205   {
206     final StringBuilder buffer = new StringBuilder();
207 
208     if (dir != null)
209     {
210       buffer.append(dir.getAbsolutePath());
211     }
212     append(buffer, "includes", includes);
213     append(buffer, "excludes", excludes);
214     buffer.append(": ").append(sourceEncoding).append("->")
215         .append(targetEncoding);
216     buffer.append(" (fromDos: ").append(fromDos).append(')');
217 
218     return buffer.toString();
219   }
220 
221   private static void append(final StringBuilder buffer, final String id,
222       final String[] items)
223   {
224     if (items != null)
225     {
226       buffer.append(" (").append(id).append(": ");
227       for (final String item : items)
228       {
229         buffer.append(item);
230       }
231       buffer.append(')');
232     }
233   }
234 }