buildframework/helium/sf/java/antlint/src/com/nokia/helium/antlint/ant/types/CheckTryCatchBlock.java
changeset 628 7c4a911dc066
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 package com.nokia.helium.antlint.ant.types;
       
    18 
       
    19 import java.io.File;
       
    20 import java.io.IOException;
       
    21 
       
    22 import javax.xml.parsers.ParserConfigurationException;
       
    23 import javax.xml.parsers.SAXParser;
       
    24 import javax.xml.parsers.SAXParserFactory;
       
    25 
       
    26 import org.xml.sax.Attributes;
       
    27 import org.xml.sax.Locator;
       
    28 import org.xml.sax.SAXException;
       
    29 import org.xml.sax.helpers.DefaultHandler;
       
    30 
       
    31 import com.nokia.helium.antlint.ant.AntlintException;
       
    32 
       
    33 /**
       
    34  * <code>CheckTryCatchBlock</code> is used to check for empty and more than one
       
    35  * catch elements in a given try-catch block.
       
    36  * 
       
    37  * <pre>
       
    38  * Usage:
       
    39  * 
       
    40  *  &lt;antlint&gt;
       
    41  *       &lt;fileset id=&quot;antlint.files&quot; dir=&quot;${antlint.test.dir}/data&quot;&gt;
       
    42  *               &lt;include name=&quot;*.ant.xml&quot;/&gt;
       
    43  *               &lt;include name=&quot;*build.xml&quot;/&gt;
       
    44  *               &lt;include name=&quot;*.antlib.xml&quot;/&gt;
       
    45  *       &lt;/fileset&gt;
       
    46  *       &lt;checkTryCatchBlock&quot; severity=&quot;error&quot; enabled=&quot;true&quot; /&gt;
       
    47  *  &lt;/antlint&gt;
       
    48  * </pre>
       
    49  * 
       
    50  * @ant.task name="checkTryCatchBlock" category="AntLint"
       
    51  * 
       
    52  */
       
    53 public class CheckTryCatchBlock extends AbstractCheck {
       
    54 
       
    55     private File antFile;
       
    56 
       
    57     /**
       
    58      * {@inheritDoc}
       
    59      */
       
    60     public File getAntFile() {
       
    61         return antFile;
       
    62     }
       
    63 
       
    64     /**
       
    65      * {@inheritDoc}
       
    66      */
       
    67     public void run(File antFilename) throws AntlintException {
       
    68         try {
       
    69             this.antFile = antFilename;
       
    70             SAXParserFactory saxFactory = SAXParserFactory.newInstance();
       
    71             saxFactory.setNamespaceAware(true);
       
    72             saxFactory.setValidating(true);
       
    73             SAXParser parser = saxFactory.newSAXParser();
       
    74             TryCatchBlockHandler handler = new TryCatchBlockHandler();
       
    75             parser.parse(antFilename, handler);
       
    76         } catch (ParserConfigurationException e) {
       
    77             throw new AntlintException("Not able to parse XML file "
       
    78                     + e.getMessage());
       
    79         } catch (SAXException e) {
       
    80             throw new AntlintException("Not able to parse XML file "
       
    81                     + e.getMessage());
       
    82         } catch (IOException e) {
       
    83             throw new AntlintException("Not able to find XML file "
       
    84                     + e.getMessage());
       
    85         }
       
    86     }
       
    87 
       
    88     public String toString() {
       
    89         return "CheckTryCatchBlock";
       
    90     }
       
    91 
       
    92     private class TryCatchBlockHandler extends DefaultHandler {
       
    93 
       
    94         private Locator locator;
       
    95         private int catchCounter;
       
    96 
       
    97         /**
       
    98          * {@inheritDoc}
       
    99          */
       
   100         public void setDocumentLocator(Locator locator) {
       
   101             this.locator = locator;
       
   102         }
       
   103 
       
   104         /**
       
   105          * {@inheritDoc}
       
   106          */
       
   107         public void startElement(String uri, String localName, String qName,
       
   108                 Attributes attributes) throws SAXException {
       
   109             if (localName.equals("trycatch")) {
       
   110                 catchCounter = 0;
       
   111             } else if (localName.equals("catch")) {
       
   112                 catchCounter++;
       
   113             }
       
   114         }
       
   115 
       
   116         /**
       
   117          * {@inheritDoc}
       
   118          */
       
   119         public void endElement(String uri, String localName, String qName)
       
   120             throws SAXException {
       
   121             if (localName.equals("trycatch") && catchCounter == 0) {
       
   122                 getReporter().report(getSeverity(),
       
   123                         "<trycatch> block found without <catch> element",
       
   124                         getAntFile(), locator.getLineNumber());
       
   125             } else if (localName.equals("trycatch") && catchCounter > 1) {
       
   126                 getReporter().report(
       
   127                         getSeverity(),
       
   128                         "<trycatch> block found with " + catchCounter
       
   129                                 + " <catch> elements.", getAntFile(),
       
   130                         locator.getLineNumber());
       
   131             }
       
   132         }
       
   133     }
       
   134 }