buildframework/helium/sf/java/antlint/src/com/nokia/helium/antlint/ant/types/CheckPropertyName.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     1
/*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     3
 * All rights reserved.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     8
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     9
 * Initial Contributors:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    11
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    12
 * Contributors:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    13
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    14
 * Description: 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    15
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    16
 */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    17
package com.nokia.helium.antlint.ant.types;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    18
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    19
import java.io.File;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    20
import java.util.ArrayList;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    21
import java.util.List;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    22
import java.util.regex.Matcher;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    23
import java.util.regex.Pattern;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    24
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    25
import org.dom4j.Document;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    26
import org.dom4j.DocumentException;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    27
import org.dom4j.Element;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    28
import org.dom4j.io.SAXReader;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    29
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    30
import com.nokia.helium.antlint.ant.AntlintException;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    31
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    32
/**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    33
 * <code>CheckPropertyName</code> is used to check the naming convention of
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    34
 * property names.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    35
 * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    36
 * <pre>
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    37
 * Usage:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    38
 * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    39
 *  &lt;antlint&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    40
 *       &lt;fileset id=&quot;antlint.files&quot; dir=&quot;${antlint.test.dir}/data&quot;&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    41
 *               &lt;include name=&quot;*.ant.xml&quot;/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    42
 *               &lt;include name=&quot;*build.xml&quot;/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    43
 *               &lt;include name=&quot;*.antlib.xml&quot;/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    44
 *       &lt;/fileset&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    45
 *       &lt;CheckPropertyName&quot; severity=&quot;error&quot; enabled=&quot;true&quot; regexp=&quot;([a-z0-9[\\d\\-]]*)&quot; /&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    46
 *  &lt;/antlint&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    47
 * </pre>
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    48
 * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    49
 * @ant.task name="CheckPropertyName" category="AntLint"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    50
 * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    51
 */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    52
public class CheckPropertyName extends AbstractCheck {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    53
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    54
    private ArrayList<String> propertiesVisited = new ArrayList<String>();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    55
    private String regExp;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    56
    private File antFile;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    57
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    58
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    59
     * {@inheritDoc}
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    60
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    61
    public void run(Element node) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    62
        if (node.getName().equals("property")) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    63
            String text = node.attributeValue("name");
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    64
            if (text != null && !text.isEmpty()) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    65
                checkPropertyName(text);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    66
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    67
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    68
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    69
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    70
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    71
     * Check the given property name.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    72
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    73
     * @param propertyName
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    74
     *            is the property name to check.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    75
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    76
    private void checkPropertyName(String propertyName) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    77
        Pattern p1 = Pattern.compile(getRegExp());
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    78
        Matcher m1 = p1.matcher(propertyName);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    79
        if (!m1.matches() && !isPropertyAlreadyVisited(propertyName)) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    80
            this.getReporter().report(this.getSeverity(),
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    81
                    "INVALID Property Name: " + propertyName,
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    82
                    this.getAntFile(), 0);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    83
            markPropertyAsVisited(propertyName);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    84
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    85
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    86
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    87
    /*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    88
     * (non-Javadoc)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    89
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    90
     * @see com.nokia.helium.antlint.ant.types.Check#run(java.io.File)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    91
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    92
    public void run(File antFilename) throws AntlintException {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    93
        List<Element> propertyNodes = new ArrayList<Element>();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    94
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    95
        this.antFile = antFilename;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    96
        SAXReader saxReader = new SAXReader();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    97
        Document doc;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    98
        try {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    99
            doc = saxReader.read(antFilename);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   100
            elementTreeWalk(doc.getRootElement(), "property", propertyNodes);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   101
        } catch (DocumentException e) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   102
            throw new AntlintException("Invalid XML file " + e.getMessage());
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   103
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   104
        for (Element propertyNode : propertyNodes) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   105
            run(propertyNode);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   106
        }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   107
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   108
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   109
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   110
     * Check whether the property is already visited or not.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   111
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   112
     * @param propertyName
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   113
     *            is the property to be checked.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   114
     * @return true, if already been visited; otherwise false
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   115
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   116
    private boolean isPropertyAlreadyVisited(String propertyName) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   117
        return propertiesVisited.contains(propertyName);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   118
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   119
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   120
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   121
     * Mark the given property as visited.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   122
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   123
     * @param propertyName
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   124
     *            is the property to be marked.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   125
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   126
    private void markPropertyAsVisited(String propertyName) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   127
        propertiesVisited.add(propertyName);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   128
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   129
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   130
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   131
     * @param regExp
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   132
     *            the regExp to set
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   133
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   134
    public void setRegExp(String regExp) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   135
        this.regExp = regExp;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   136
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   137
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   138
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   139
     * @return the regExp
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   140
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   141
    public String getRegExp() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   142
        return regExp;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   143
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   144
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   145
    /*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   146
     * (non-Javadoc)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   147
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   148
     * @see org.apache.tools.ant.types.DataType#toString()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   149
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   150
    public String toString() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   151
        return "CheckPropertyName";
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   152
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   153
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   154
    /*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   155
     * (non-Javadoc)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   156
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   157
     * @see com.nokia.helium.antlint.ant.types.Check#getAntFile()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   158
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   159
    public File getAntFile() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   160
        return this.antFile;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   161
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   162
}