Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/LineSeparatorOption.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2003  Oliver Burn
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  package com.puppycrawl.tools.checkstyle.checks;
20  
21  import java.util.Map;
22  import java.util.HashMap;
23  
24  /**
25   * Represents the options for line separator settings.
26   *
27   * @author lkuehne
28   * @see NewlineAtEndOfFileCheck
29   */
30  public final class LineSeparatorOption extends AbstractOption
31  {
32      /** maps from a string representation to an option */
33      private static final Map STR_TO_OPT = new HashMap();
34  
35      /** Windows-style line separators. **/
36      public static final LineSeparatorOption CRLF =
37          new LineSeparatorOption("crlf", "\r\n");
38  
39      /** Mac-style line separators. **/
40      public static final LineSeparatorOption CR =
41          new LineSeparatorOption("cr", "\r");
42  
43      /** Unix-style line separators. **/
44      public static final LineSeparatorOption LF =
45          new LineSeparatorOption("lf", "\n");
46  
47      /** System default line separators. **/
48      public static final LineSeparatorOption SYSTEM = new LineSeparatorOption(
49          "system", System.getProperty("line.separator"));
50  
51      /** the line separator representation */
52      private final String mLineSeparator;
53  
54      /**
55       * Creates a new <code>LineSeparatorOption</code> instance.
56       * @param aStrRep the string representation
57       * @param aSep the line separator, e.g. "\r\n"
58       */
59      private LineSeparatorOption(String aStrRep, String aSep)
60      {
61          super(aStrRep);
62          mLineSeparator = aSep;
63      }
64  
65      /**
66       * @param aBytes a bytes array to check
67       * @return if aBytes is equal to the byte representation
68       * of this line separator
69       */
70      public boolean matches(byte[] aBytes)
71      {
72          final String s = new String(aBytes);
73          return s.equals(mLineSeparator);
74      }
75  
76      /**
77       * @return the length of the file separator,
78       * e.g. 1 for CR, 2 for CRLF, ...
79       */
80      public int length()
81      {
82          return mLineSeparator.length();
83      }
84  
85      /** @see com.puppycrawl.tools.checkstyle.checks.AbstractOption */
86      protected Map getStrToOpt()
87      {
88          return STR_TO_OPT;
89      }
90  
91  }