From 84b68cc9457b2353502494044aef647d00a3c614 Mon Sep 17 00:00:00 2001 From: glennm Date: Wed, 4 Jul 2001 19:02:45 +0000 Subject: [PATCH] Provide more descriptive error messages whenunknown attributes and elements are encountered in the build file.Bugzilla: 1722 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269269 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/IntrospectionHelper.java | 54 +++++++++++++++---- .../org/apache/tools/ant/ProjectHelper.java | 16 +++--- .../apache/tools/ant/RuntimeConfigurable.java | 2 +- .../org/apache/tools/ant/UnknownElement.java | 2 +- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index 5925e2e83..bc99c4a4f 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -219,8 +219,9 @@ public class IntrospectionHelper { throws BuildException { AttributeSetter as = (AttributeSetter) attributeSetters.get(attributeName); if (as == null) { - String msg = "Class " + element.getClass().getName() + - " doesn't support the \"" + attributeName + "\" attribute"; + String msg = getElementName(p, element) + + //String msg = "Class " + element.getClass().getName() + + " doesn't support the \"" + attributeName + "\" attribute."; throw new BuildException(msg); } try { @@ -240,10 +241,11 @@ public class IntrospectionHelper { /** * Adds PCDATA areas. */ - public void addText(Object element, String text) { + public void addText(Project project, Object element, String text) { if (addText == null) { - String msg = "Class " + element.getClass().getName() + - " doesn't support nested text elements"; + String msg = getElementName(project, element) + + //String msg = "Class " + element.getClass().getName() + + " doesn't support nested text data."; throw new BuildException(msg); } try { @@ -263,12 +265,13 @@ public class IntrospectionHelper { /** * Creates a named nested element. */ - public Object createElement(Object element, String elementName) + public Object createElement(Project project, Object element, String elementName) throws BuildException { NestedCreator nc = (NestedCreator) nestedCreators.get(elementName); if (nc == null) { - String msg = "Class " + element.getClass().getName() + - " doesn't support the nested \"" + elementName + "\" element"; + String msg = getElementName(project, element) + + //String msg = "Class " + element.getClass().getName() + + " doesn't support the nested \"" + elementName + "\" element."; throw new BuildException(msg); } try { @@ -296,7 +299,7 @@ public class IntrospectionHelper { Class nt = (Class) nestedTypes.get(elementName); if (nt == null) { String msg = "Class " + bean.getName() + - " doesn't support the nested \"" + elementName + "\" element"; + " doesn't support the nested \"" + elementName + "\" element."; throw new BuildException(msg); } return nt; @@ -310,7 +313,7 @@ public class IntrospectionHelper { Class at = (Class) attributeTypes.get(attributeName); if (at == null) { String msg = "Class " + bean.getName() + - " doesn't support the \"" + attributeName + "\" attribute"; + " doesn't support the \"" + attributeName + "\" attribute."; throw new BuildException(msg); } return at; @@ -500,6 +503,37 @@ public class IntrospectionHelper { return null; } + protected String getElementName(Project project, Object element) + { + Hashtable elements = project.getTaskDefinitions(); + String typeName = "task"; + if (!elements.contains( element.getClass() )) + { + elements = project.getDataTypeDefinitions(); + typeName = "data type"; + if (!elements.contains( element.getClass() )) + { + elements = null; + } + } + + if (elements != null) + { + Enumeration e = elements.keys(); + while (e.hasMoreElements()) + { + String elementName = (String) e.nextElement(); + Class elementClass = (Class) elements.get( elementName ); + if ( element.getClass().equals( elementClass ) ) + { + return "The <" + elementName + "> " + typeName; + } + } + } + + return "Class " + element.getClass().getName(); + } + /** * extract the name of a property from a method name - subtracting * a given prefix. diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index c909060ce..0a29860a9 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -490,7 +490,7 @@ public class ProjectHelper { public void characters(char[] buf, int start, int end) throws SAXParseException { if (wrapper == null) { try { - addText(task, buf, start, end); + addText(project, task, buf, start, end); } catch (BuildException exc) { throw new SAXParseException(exc.getMessage(), locator, exc); } @@ -536,7 +536,7 @@ public class ProjectHelper { child = new UnknownElement(propType.toLowerCase()); ((UnknownElement) target).addChild((UnknownElement) child); } else { - child = ih.createElement(target, propType.toLowerCase()); + child = ih.createElement(project, target, propType.toLowerCase()); } configureId(child, attrs); @@ -556,7 +556,7 @@ public class ProjectHelper { public void characters(char[] buf, int start, int end) throws SAXParseException { if (parentWrapper == null) { try { - addText(child, buf, start, end); + addText(project, child, buf, start, end); } catch (BuildException exc) { throw new SAXParseException(exc.getMessage(), locator, exc); } @@ -609,7 +609,7 @@ public class ProjectHelper { public void characters(char[] buf, int start, int end) throws SAXParseException { try { - addText(element, buf, start, end); + addText(project, element, buf, start, end); } catch (BuildException exc) { throw new SAXParseException(exc.getMessage(), locator, exc); } @@ -648,15 +648,15 @@ public class ProjectHelper { /** * Adds the content of #PCDATA sections to an element. */ - public static void addText(Object target, char[] buf, int start, int end) + public static void addText(Project project, Object target, char[] buf, int start, int end) throws BuildException { - addText(target, new String(buf, start, end)); + addText(project, target, new String(buf, start, end)); } /** * Adds the content of #PCDATA sections to an element. */ - public static void addText(Object target, String text) + public static void addText(Project project, Object target, String text) throws BuildException { if (text == null || text.trim().length() == 0) { @@ -666,7 +666,7 @@ public class ProjectHelper { if(target instanceof TaskAdapter) target = ((TaskAdapter) target).getProxy(); - IntrospectionHelper.getHelper(target.getClass()).addText(target, text); + IntrospectionHelper.getHelper(target.getClass()).addText(project, target, text); } diff --git a/src/main/org/apache/tools/ant/RuntimeConfigurable.java b/src/main/org/apache/tools/ant/RuntimeConfigurable.java index 881346a86..8a5584d54 100644 --- a/src/main/org/apache/tools/ant/RuntimeConfigurable.java +++ b/src/main/org/apache/tools/ant/RuntimeConfigurable.java @@ -135,7 +135,7 @@ public class RuntimeConfigurable { attributes = null; } if (characters.length() != 0) { - ProjectHelper.addText(wrappedObject, characters.toString()); + ProjectHelper.addText(p, wrappedObject, characters.toString()); characters.setLength(0); } Enumeration enum = children.elements(); diff --git a/src/main/org/apache/tools/ant/UnknownElement.java b/src/main/org/apache/tools/ant/UnknownElement.java index 20feb4c53..9383fc361 100644 --- a/src/main/org/apache/tools/ant/UnknownElement.java +++ b/src/main/org/apache/tools/ant/UnknownElement.java @@ -140,7 +140,7 @@ public class UnknownElement extends Task { for (int i=0; i