You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/06/28 14:13:27 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/task JellyTask.java

jstrachan    2002/06/28 05:13:27

  Modified:    jelly/src/java/org/apache/commons/jelly/parser
                        XMLParser.java
               jelly/src/java/org/apache/commons/jelly/impl TagScript.java
                        BeanTagScript.java
               jelly/src/java/org/apache/commons/jelly JellyException.java
               jelly/src/java/org/apache/commons/jelly/task JellyTask.java
  Log:
  Changed the code along similar lines as Vinay's patch so that exceptions now carry the file name, the tag name and the line & column number of the error.
  This makes things much easier to debug when working wtih multiple files, such as Maven etc.
  
  One thing to watch though; right now this assumes that the Jelly script is parsed from a URI or File so that it know where the source is. Using a Reader or InputStream will not work as the parser doen't know where the script comes from.
  
  Revision  Changes    Path
  1.25      +19 -5     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java
  
  Index: XMLParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- XMLParser.java	25 Jun 2002 19:12:29 -0000	1.24
  +++ XMLParser.java	28 Jun 2002 12:13:27 -0000	1.25
  @@ -200,6 +200,12 @@
       protected HashMap namespaces = new HashMap();
   
       /**
  +     * The name of the file being parsed that is passed to the TagScript objects
  +     * for error reporting
  +     */
  +    private String fileName;
  +    
  +    /**
        * Do we want to use a validating parser?
        */
       protected boolean validating = false;
  @@ -257,6 +263,7 @@
        */
       public Script parse(File file) throws IOException, SAXException {
           ensureConfigured();
  +        this.fileName = file.toString();
           getXMLReader().parse(new InputSource(new FileReader(file)));
           return script;
       }
  @@ -272,6 +279,7 @@
        */
       public Script parse(InputSource input) throws IOException, SAXException {
           ensureConfigured();
  +        this.fileName = input.getSystemId();
           getXMLReader().parse(input);
           return script;
       }
  @@ -287,6 +295,7 @@
        */
       public Script parse(InputStream input) throws IOException, SAXException {
           ensureConfigured();
  +        this.fileName = null;
           getXMLReader().parse(new InputSource(input));
           return script;
       }
  @@ -302,6 +311,7 @@
        */
       public Script parse(Reader reader) throws IOException, SAXException {
           ensureConfigured();
  +        this.fileName = null;
           getXMLReader().parse(new InputSource(reader));
           return script;
       }
  @@ -317,6 +327,7 @@
        */
       public Script parse(String uri) throws IOException, SAXException {
           ensureConfigured();
  +        this.fileName = uri;
           getXMLReader().parse(uri);
           return script;
       }
  @@ -593,6 +604,9 @@
                   if ( locator != null ) {
                       tagScript.setLocator(locator);
                   }
  +                // sets the file name element names
  +                tagScript.setFileName(fileName);
  +                tagScript.setElementName(qName);
                   
                   // pop another tag onto the stack
                   if ( parentTag != null ) {
  
  
  
  1.14      +64 -6     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TagScript.java
  
  Index: TagScript.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TagScript.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- TagScript.java	27 Jun 2002 14:09:15 -0000	1.13
  +++ TagScript.java	28 Jun 2002 12:13:27 -0000	1.14
  @@ -106,6 +106,12 @@
       /** The attribute expressions that are created */
       protected Map attributes = new HashMap();
       
  +    /** the Jelly file which caused the problem */
  +    private String fileName;
  +
  +    /** the tag name which caused the problem */
  +    private String elementName;
  +
       /** the line number of the tag */
       private int lineNumber = -1;
       
  @@ -167,6 +173,34 @@
       }
       
       /** 
  +     * @return the Jelly file which caused the problem 
  +     */
  +    public String getFileName() {
  +        return fileName;
  +    }
  +
  +    /** 
  +     * Sets the Jelly file which caused the problem 
  +     */
  +    public void setFileName(String fileName) {
  +        this.fileName = fileName;
  +    }
  +    
  +
  +    /** 
  +     * @return the element name which caused the problem
  +     */
  +    public String getElementName() {
  +        return elementName;
  +    }
  +
  +    /** 
  +     * Sets the element name which caused the problem
  +     */
  +    public void setElementName(String elementName) {
  +        this.elementName = elementName;
  +    }
  +    /** 
        * @return the line number of the tag 
        */
       public int getLineNumber() {
  @@ -221,7 +255,25 @@
        */
       protected void handleException(Exception e) throws Exception {
           log.error( "Caught exception: " + e, e );
  -        throw new JellyException(e, columnNumber, lineNumber);            
  +        throw new JellyException(e, fileName, elementName, columnNumber, lineNumber);            
  +    }
  +    
  +    /**
  +     * Creates a new Jelly exception, adorning it with location information
  +     */
  +    protected JellyException createJellyException(String reason) {
  +        return new JellyException( 
  +            reason, fileName, elementName, columnNumber, lineNumber
  +        );
  +    }
  +    
  +    /**
  +     * Creates a new Jelly exception, adorning it with location information
  +     */
  +    protected JellyException createJellyException(String reason, Exception cause) {
  +        return new JellyException( 
  +            reason, cause, fileName, elementName, columnNumber, lineNumber
  +        );
       }
       
       /**
  @@ -233,6 +285,12 @@
           if (e.getLineNumber() == -1) {
               e.setColumnNumber(columnNumber);
               e.setLineNumber(lineNumber);
  +        }
  +        if ( e.getFileName() == null ) {
  +            e.setFileName( fileName );
  +        }
  +        if ( e.getElementName() == null ) {
  +            e.setElementName( elementName );
           }
           throw e;
       }
  
  
  
  1.12      +9 -10     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/BeanTagScript.java
  
  Index: BeanTagScript.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/BeanTagScript.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- BeanTagScript.java	27 Jun 2002 14:09:15 -0000	1.11
  +++ BeanTagScript.java	28 Jun 2002 12:13:27 -0000	1.12
  @@ -180,9 +180,8 @@
           for ( Iterator iter = attributes.keySet().iterator(); iter.hasNext(); ) {
               String name = (String) iter.next();
               if ( ! attributeSet.contains( name ) ) {
  -                throw new JellyException( 
  -                    "This tag does not understand the attribute '" + name + "'", 
  -                    getColumnNumber(), getLineNumber()  
  +                throw createJellyException( 
  +                    "This tag does not understand the attribute '" + name + "'"
                   );
               }
           }
  @@ -223,10 +222,10 @@
                       "Cannot call method: " + method.getName() + " as I cannot convert: " 
                       + value + " of type: " + valueTypeName + " into type: " + type.getName() 
                   );
  -                throw new JellyException( 
  +                throw createJellyException( 
                       "Cannot call method: " + method.getName() + " on tag of type: " 
                       + tag.getClass().getName() + " with value: " + value + " of type: " 
  -                    + valueTypeName + ". Exception: " + e, getColumnNumber(), getLineNumber()
  +                    + valueTypeName + ". Exception: " + e, e
                   );
               }
           }
  
  
  
  1.7       +51 -12    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyException.java
  
  Index: JellyException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyException.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- JellyException.java	27 Jun 2002 14:08:27 -0000	1.6
  +++ JellyException.java	28 Jun 2002 12:13:27 -0000	1.7
  @@ -77,13 +77,18 @@
       /** the underlying cause of the exception */
       private Throwable cause;
   
  +    /** the Jelly file which caused the problem */
  +    private String fileName;
  +
  +    /** the tag name which caused the problem */
  +    private String elementName;
  +
       /** the line number in the script of the error */
       private int lineNumber = -1;
       
       /** the column number in the script of the error */
       private int columnNumber = -1;
  -
  -
  +    
       public JellyException() {
       }
   
  @@ -101,19 +106,23 @@
           this.cause = cause;
       }
       
  -    public JellyException(Throwable cause, int columnNumber, int lineNumber) {
  -        this(cause.getLocalizedMessage(), cause, columnNumber, lineNumber);
  +    public JellyException(Throwable cause, String fileName, String elementName, int columnNumber, int lineNumber) {
  +        this(cause.getLocalizedMessage(), cause, fileName, elementName, columnNumber, lineNumber);
       }
       
  -    public JellyException(String reason, Throwable cause, int columnNumber, int lineNumber) {
  +    public JellyException(String reason, Throwable cause, String fileName, String elementName, int columnNumber, int lineNumber) {
           super(reason);
           this.cause = cause;
  +        this.fileName = fileName;
  +        this.elementName = elementName;
           this.columnNumber = columnNumber;
           this.lineNumber = lineNumber;
       }
       
  -    public JellyException(String reason, int columnNumber, int lineNumber) {
  +    public JellyException(String reason, String fileName, String elementName, int columnNumber, int lineNumber) {
           super(reason);
  +        this.fileName = fileName;
  +        this.elementName = elementName;
           this.columnNumber = columnNumber;
           this.lineNumber = lineNumber;
       }
  @@ -150,9 +159,39 @@
       public void setColumnNumber(int columnNumber) {
           this.columnNumber = columnNumber;
       }
  +
  +    /** 
  +     * @return the Jelly file which caused the problem 
  +     */
  +    public String getFileName() {
  +        return fileName;
  +    }
  +
  +    /** 
  +     * Sets the Jelly file which caused the problem 
  +     */
  +    public void setFileName(String fileName) {
  +        this.fileName = fileName;
  +    }
  +    
  +
  +    /** 
  +     * @return the element name which caused the problem
  +     */
  +    public String getElementName() {
  +        return elementName;
  +    }
  +
  +    /** 
  +     * Sets the element name which caused the problem
  +     */
  +    public void setElementName(String elementName) {
  +        this.elementName = elementName;
  +    }
  +    
       
       public String getMessage() {
  -        return super.getMessage() + " At line: " 
  +        return super.getMessage() + " File: " + fileName + " At tag <" + elementName + ">: line: " 
               + lineNumber + " column: " + columnNumber;
       }
       // #### overload the printStackTrace methods...
  
  
  
  1.6       +1 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/task/JellyTask.java
  
  Index: JellyTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/task/JellyTask.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JellyTask.java	30 May 2002 08:11:55 -0000	1.5
  +++ JellyTask.java	28 Jun 2002 12:13:27 -0000	1.6
  @@ -180,7 +180,7 @@
       protected Script compileScript() throws Exception {
           XMLParser parser = new XMLParser();
           parser.setContext(getJellyContext());
  -        Script script = parser.parse(getUrl().openStream());
  +        Script script = parser.parse(getUrl().toString());
           script = script.compile();
           if (log.isDebugEnabled()) {
               log.debug("Compiled script: " + getUrl());
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>