01: /*
02: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
03: *
04: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
05: *
06: * The contents of this file are subject to the terms of either the GNU General
07: * Public License Version 2 only ("GPL") or the Common Development and Distribution
08: * License("CDDL") (collectively, the "License"). You may not use this file except in
09: * compliance with the License. You can obtain a copy of the License at
10: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
11: * License for the specific language governing permissions and limitations under the
12: * License. When distributing the software, include this License Header Notice in
13: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
14: * designates this particular file as subject to the "Classpath" exception as
15: * provided by Sun in the GPL Version 2 section of the License file that
16: * accompanied this code. If applicable, add the following below the License Header,
17: * with the fields enclosed by brackets [] replaced by your own identifying
18: * information: "Portions Copyrighted [year] [name of copyright owner]"
19: *
20: * Contributor(s):
21: *
22: * The Original Software is NetBeans. The Initial Developer of the Original Software
23: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
24: * Rights Reserved.
25: *
26: * If you wish your version of this file to be governed by only the CDDL or only the
27: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
28: * this software in this distribution under the [CDDL or GPL Version 2] license." If
29: * you do not indicate a single choice of license, a recipient has the option to
30: * distribute your version of this file under either the CDDL, the GPL Version 2 or
31: * to extend the choice of license to its licensees as provided above. However, if
32: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
33: * the option applies only if the new code is made subject to such option by the
34: * copyright holder.
35: */
36:
37: package org.netbeans.installer.infra.utils.comment.handlers;
38:
39: import java.io.File;
40: import java.util.regex.Pattern;
41:
42: /**
43: * A {@link FileHandler} implementation capable of handling shell scripts.
44: *
45: * @author Kirill Sorokin
46: */
47: public class ShellFileHandler extends LineFileHandler {
48: /////////////////////////////////////////////////////////////////////////////////
49: // Instance
50: /**
51: * Creates a new instance of {@link ShellFileHandler}. The constuctor
52: * simply falls back to the
53: * {@link LineFileHandler#LineFileHandler(Pattern, Pattern, String)} passing in
54: * the parameters relevant to shell scripts.
55: */
56: public ShellFileHandler() {
57: super (COMMENT_PATTERN, IGNORE_PATTERN, COMMENT_PREFIX);
58: }
59:
60: /**
61: * {@inheritDoc}
62: */
63: public boolean accept(final File file) {
64: if (file == null) {
65: throw new IllegalArgumentException(
66: "The 'file' parameter cannot be null."); // NOI18N
67: }
68:
69: if (!file.isFile()) {
70: return false;
71: }
72:
73: return file.getName().endsWith(".sh"); // NOI18N
74: }
75:
76: /////////////////////////////////////////////////////////////////////////////////
77: // Constants
78: /**
79: * The regular expression pattern which matches the line which is a comment.
80: */
81: private static final Pattern COMMENT_PATTERN = Pattern
82: .compile("^\\s*#.*"); // NOI18N
83:
84: /**
85: * The regular expression pattern which matches the line which should be
86: * ignored.
87: */
88: private static final Pattern IGNORE_PATTERN = Pattern
89: .compile("^\\S*#!.*"); // NOI18N
90:
91: /**
92: * The prefix which should be used for each line in the comment.
93: */
94: private static final String COMMENT_PREFIX = "# "; // NOI18N
95: }
|