# Headers

- About
- Checkstyle
- Documentation
- Release Notes
- Configuration
- Property Types
- Running
- Ant Task
- Command Line
- Available Checks
- Standard Checks
- Javadoc Comments
- Naming Conventions
- Headers
- Header
- RegexpHeader
- CrossLanguageRegexpHeader
- Imports
- Size Violations
- Whitespace
- Modifiers
- Block Checks
- Coding
- Class Design
- Duplicate Code
- Metrics
- Miscellaneous
- Optional Checks
- J2EE Checks
- Extending Checkstyle
- Writing checks
- Writing filters
- Writing listeners
- Javadoc
- SourceForge
- Mailing Lists
- Bug Database
- Feature Requests
- Developers
- Project Page
- SVN Repository
- Contributing

- Hosted by:

## Header

### Description

Checks that a source file begins with a specified header. Property headerFile specifies a file that contains the required header. Alternatively, the header specification can be set directly in the header property without the need for an external file.

Property ignoreLines specifies the line numbers to ignore when matching lines in a header file. This property is very useful for supporting headers that contain copyright dates. For example, consider the following header:

    line 1: ////////////////////////////////////////////////////////////////////
    line 2: // checkstyle:
    line 3: // Checks Java source code for adherence to a set of rules.
    line 4: // Copyright (C) 2002  Oliver Burn
    line 5: ////////////////////////////////////////////////////////////////////

Since the year information will change over time, you can tell Checkstyle to ignore line 4 by setting property ignoreLines to 4.

### Properties

name | description | type | default value
--- | --- | --- | ---
headerFile | name of the file containing the required header | string | null
header | the required header specified inline. Individual header lines must be separated by the string "\n" (even on platforms with a different line separator), see examples below. | string | null
ignoreLines | line numbers to ignore | list of integers | {}

### Example

To configure the check to use header file "java.header" and ignore lines 2, 3, and 4:

    <module name="Header">
        <property name="headerFile" value="java.header"/>
        <property name="ignoreLines" value="2, 3, 4"/>
    </module>

To configure the check to verify that each file starts with the header

    // Copyright (C) 2004 MyCompany
    // All rights reserved

without the need for an external header file:

    <module name="Header">
        <property name="header"
                  value="// Copyright (C) 2004 MyCompany\n// All rights reserved"/>
    </module>

### Package

com.puppycrawl.tools.checkstyle.checks.header

### Parent Module

TreeWalker

## RegexpHeader

### Description

Checks the header of a java source file against a header that contains a regular expression for each line of the source header.

Rationale: In some projects checking against a fixed header is not sufficient, e.g. the header might require a copyright line where the year information is not static.

For example, consider the following header:

    line  1: ^/{71}$
    line  2: ^// checkstyle:$
    line  3: ^// Checks Java source code for adherence to a set of rules\.$
    line  4: ^// Copyright \(C\) \d\d\d\d  Oliver Burn$
    line  5: ^// Last modification by \$Author.*\$$
    line  6: ^/{71}$
    line  7:
    line  8: ^package
    line  9:
    line 10: ^import
    line 11:
    line 12: ^/\*\*
    line 13: ^ \*([^/]|$)
    line 14: ^ \*/

Lines 1 and 6 demonstrate a more compact notation for 71 '/' characters. Line 4 enforces that the copyright notice includes a four digit year. Line 5 is an example how to enforce revision control keywords in a file header. Lines 12-14 is a template for javadoc (line 13 is so complicated to remove conflict with and of javadoc comment).

### Properties

name | description | type | default value
--- | --- | --- | ---
headerFile | name of the file containing the required header | string | null
header | the required header specified inline. Individual header lines must be separated by the string "\n" (even on platforms with a different line separator), and regular expressions must not span multiple lines. | string | null
multiLines | line numbers to repeat (zero or more times) | list of integers | {}

### Example

To configure the check to use header file "java.header" and 10 and 13 muli-lines:

    <module name="RegexpHeader">
        <property name="headerFile" value="java.header"/>
        <property name="multiLines" value="10, 13"/>
    </module>

To configure the check to verify that each file starts with the header

    ^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$
    ^// All rights reserved$

without the need for an external header file:

    <module name="RegexpHeader">
      <property
        name="header"
        value="^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$\n^// All rights reserved$"/>
    </module>

Note: ignoreLines property has been removed from this check to simplify it. To make some line optional use "^.*$" regexp for this line.

### Package

com.puppycrawl.tools.checkstyle.checks.header

### Parent Module

TreeWalker

## CrossLanguageRegexpHeader

### Description

Checks the header of each source file against a header that contains a regular expression for each line of the source header.

This check is very similar to the RegexpHeader check above. However it has Checker as it's parent module, so it is not limited to java source code, and allows checking headers of other file types as well.

Different programming languages have different comment synatx rules, but all of them start a comment with a non-word character. Hence you can often use the non-word character class to abstract away the concrete comment syntax and allow checking the header for different languages with a single header definition. For example, consider the following header specification (note that this is not the full Apache licence header):

    line 1: ^#!
    line 2: ^<\?xml.*>$
    line 3: ^\W*$
    line 4: ^\W*Copyright 2006 The Apache Software Foundation or its licensors, as applicable\.$
    line 5: ^\W*Licensed under the Apache License, Version 2\.0 \(the "License"\);$
    line 6: ^\W*$

Lines 1 and 2 leave room for technical header lines, e.g. the "#!/bin/sh" line in Unix shell scripts, or the xml file header of XML files. Set the multiline property to "1, 2" so these lines can be ignored for file types where they do no apply. Lines 3 throgh 6 define the actual header content. Note how lines 2, 4 and 5 use escapes for characters that have special regexp semantics.

### Properties

name | description | type | default value
--- | --- | --- | ---
charset | name of the file charset | String | System property "file.encoding"
fileExtensions | file type extensions to identify the file types to check. Must only contain text files, not binary files like bitmap graphics. | String Set | java, txt, properties, html, jsp, xml, wsdl, js, css, idl, c, cc, cpp, cxx, h, cs, sh, py, bat
headerFile | name of the file containing the required header | string | null
header | the required header specified inline. Individual header lines must be separated by the string "\n" (even on platforms with a different line separator), and regular expressions must not span multiple lines. | string | null
multiLines | line numbers to repeat (zero or more times) | list of integers | {}

### Example

To configure the check to use header file "apache.header" and 1 and 2 muli-lines:

    <module name="CrossLanguageRegexpHeader">
        <property name="headerFile" value="apache.header"/>
        <property name="multiLines" value="1, 2"/>
    </module>

To configure the check to verify that each file starts with the header

    ^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$
    ^// All rights reserved$

without the need for an external header file:

    <module name="CrossLanguageRegexpHeader">
      <property
        name="header"
        value="^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$\n^// All rights reserved$"/>
    </module>

### Package

com.puppycrawl.tools.checkstyle.checks.header

### Parent Module

Checker

Copyright © 2001-2007, Oliver Burn