~ubuntu-branches/ubuntu/maverick/ant/maverick

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/ComponentHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-09-30 14:47:45 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080930144745-0x8uzivd9t15dua3
Tags: 1.7.1-0ubuntu1
* New upstream version (bug fix release).
  - mainly a bugfix release.
  - has extended support for Java6 features.
  - <script> now has support for JavaFX.
  - release notes: http://apache.linux-mirror.org/ant/README.html
* Remove debian/patches/05_ant-bug433444.patch. Obsoleted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
package org.apache.tools.ant;
20
20
 
21
 
import java.lang.reflect.Method;
22
21
import java.lang.reflect.Modifier;
23
22
import java.lang.reflect.InvocationTargetException;
24
23
import java.io.InputStream;
33
32
import java.util.Properties;
34
33
import java.util.Set;
35
34
import java.util.Stack;
 
35
import java.util.List;
 
36
import java.util.ArrayList;
36
37
 
37
38
import org.apache.tools.ant.taskdefs.Typedef;
38
39
import org.apache.tools.ant.taskdefs.Definer;
579
580
     */
580
581
    public static String getElementName(Project p, Object o, boolean brief) {
581
582
        if (p == null) {
582
 
            p = getProject(o);
 
583
            p = Project.getProject(o);
583
584
        }
584
585
        return p == null ? getUnmappedElementName(o.getClass(), brief)
585
586
                : getComponentHelper(p).getElementName(o, brief);
593
594
        return c.toString();
594
595
    }
595
596
 
596
 
    private static Project getProject(Object o) {
597
 
        if (o instanceof ProjectComponent) {
598
 
            return ((ProjectComponent) o).getProject();
599
 
        }
600
 
        try {
601
 
            Method m = o.getClass().getMethod("getProject", (Class[]) null);
602
 
            if (Project.class == m.getReturnType()) {
603
 
                return (Project) m.invoke(o, (Object[]) null);
604
 
            }
605
 
        } catch (Exception e) {
606
 
            //too bad
607
 
        }
608
 
        return null;
609
 
    }
610
 
 
611
597
    /**
612
598
     * Check if definition is a valid definition--it may be a
613
599
     * definition of an optional task that does not exist.
775
761
    /**
776
762
     * Called for each component name, check if the
777
763
     * associated URI has been examined for antlibs.
 
764
     * @param componentName the name of the component, which should include a URI
 
765
     *                      prefix if it is in a namespace
778
766
     */
779
767
    private synchronized void checkNamespace(String componentName) {
780
768
        String uri = ProjectHelper.extractUriFromComponentName(componentName);
853
841
        AntTypeDefinition def = getDefinition(componentName);
854
842
        if (def == null) {
855
843
            //not a known type
856
 
            boolean isAntlib = componentName.indexOf(MagicNames.ANTLIB_PREFIX) == 0;
857
 
            out.println("Cause: The name is undefined.");
858
 
            out.println("Action: Check the spelling.");
859
 
            out.println("Action: Check that any custom tasks/types have been declared.");
860
 
            out.println("Action: Check that any <presetdef>/<macrodef>"
861
 
                        + " declarations have taken place.");
862
 
            if (isAntlib) {
863
 
                out.println();
864
 
                out.println("This appears to be an antlib declaration. ");
865
 
                out.println("Action: Check that the implementing library exists in one of:");
866
 
                out.println(dirListing);
867
 
            }
 
844
            printUnknownDefinition(out, componentName, dirListing);
868
845
            definitions = true;
869
846
        } else {
870
847
            //we are defined, so it is an instantiation problem
878
855
            try {
879
856
                clazz = def.innerGetTypeClass();
880
857
            } catch (ClassNotFoundException e) {
881
 
                out.println("Cause: the class " + classname + " was not found.");
882
858
                jars = true;
883
 
                if (optional) {
884
 
                    out.println("        This looks like one of Ant's optional components.");
885
 
                    out.println("Action: Check that the appropriate optional JAR exists in");
886
 
                    out.println(dirListing);
887
 
                } else {
888
 
                    out.println("Action: Check that the component has been correctly declared");
889
 
                    out.println("        and that the implementing JAR is in one of:");
890
 
                    out.println(dirListing);
 
859
                if (!optional) {
891
860
                    definitions = true;
892
861
                }
 
862
                printClassNotFound(out, classname, optional, dirListing);
893
863
            } catch (NoClassDefFoundError ncdfe) {
894
864
                jars = true;
895
 
                out.println("Cause: Could not load a dependent class "
896
 
                        +  ncdfe.getMessage());
897
 
                if (optional) {
898
 
                    out.println("       It is not enough to have Ant's optional JARs");
899
 
                    out.println("       you need the JAR files that the"
900
 
                                + " optional tasks depend upon.");
901
 
                    out.println("       Ant's optional task dependencies are"
902
 
                                + " listed in the manual.");
903
 
                } else {
904
 
                    out.println("       This class may be in a separate JAR"
905
 
                                + " that is not installed.");
906
 
                }
907
 
                out.println("Action: Determine what extra JAR files are"
908
 
                            + " needed, and place them in one of:");
909
 
                out.println(dirListing);
 
865
                printNotLoadDependentClass(out, optional, ncdfe, dirListing);
910
866
            }
911
867
            //here we successfully loaded the class or failed.
912
868
            if (clazz != null) {
976
932
    }
977
933
 
978
934
    /**
 
935
     * Print unknown definition.forking
 
936
     */
 
937
    private void printUnknownDefinition(
 
938
        PrintWriter out, String componentName, String dirListing) {
 
939
        boolean isAntlib = componentName.indexOf(MagicNames.ANTLIB_PREFIX) == 0;
 
940
        String uri=ProjectHelper.extractUriFromComponentName(componentName);
 
941
        out.println("Cause: The name is undefined.");
 
942
        out.println("Action: Check the spelling.");
 
943
        out.println("Action: Check that any custom tasks/types have been declared.");
 
944
        out.println("Action: Check that any <presetdef>/<macrodef>"
 
945
                    + " declarations have taken place.");
 
946
        if(uri.length()>0) {
 
947
            List matches = antTypeTable.findMatches(uri);
 
948
            if(matches.size()>0) {
 
949
                out.println();
 
950
                out.println("The definitions in the namespace "+uri+" are:");
 
951
                for(Iterator it=matches.iterator();it.hasNext();) {
 
952
                    AntTypeDefinition def=(AntTypeDefinition) it.next();
 
953
                    String local = ProjectHelper.extractNameFromComponentName(def.getName());
 
954
                    out.println("    "+local);
 
955
                }
 
956
            } else {
 
957
                out.println("No types or tasks have been defined in this namespace yet");
 
958
                if (isAntlib) {
 
959
                    out.println();
 
960
                    out.println("This appears to be an antlib declaration. ");
 
961
                    out.println("Action: Check that the implementing library exists in one of:");
 
962
                    out.println(dirListing);
 
963
                }
 
964
            }
 
965
        }
 
966
    }
 
967
 
 
968
    /**
 
969
     * Print class not found.
 
970
     */
 
971
    private void printClassNotFound(
 
972
        PrintWriter out, String classname, boolean optional,
 
973
        String dirListing) {
 
974
        out.println("Cause: the class " + classname + " was not found.");
 
975
        if (optional) {
 
976
            out.println("        This looks like one of Ant's optional components.");
 
977
            out.println("Action: Check that the appropriate optional JAR exists in");
 
978
            out.println(dirListing);
 
979
        } else {
 
980
            out.println("Action: Check that the component has been correctly declared");
 
981
            out.println("        and that the implementing JAR is in one of:");
 
982
            out.println(dirListing);
 
983
        }
 
984
    }
 
985
 
 
986
    /**
 
987
     * Print could not load dependent class.
 
988
     */
 
989
    private void printNotLoadDependentClass(
 
990
        PrintWriter out, boolean optional, NoClassDefFoundError ncdfe,
 
991
        String dirListing) {
 
992
        out.println("Cause: Could not load a dependent class "
 
993
                    +  ncdfe.getMessage());
 
994
        if (optional) {
 
995
            out.println("       It is not enough to have Ant's optional JARs");
 
996
            out.println("       you need the JAR files that the"
 
997
                        + " optional tasks depend upon.");
 
998
            out.println("       Ant's optional task dependencies are"
 
999
                        + " listed in the manual.");
 
1000
        } else {
 
1001
            out.println("       This class may be in a separate JAR"
 
1002
                        + " that is not installed.");
 
1003
        }
 
1004
        out.println("Action: Determine what extra JAR files are"
 
1005
                    + " needed, and place them in one of:");
 
1006
        out.println(dirListing);
 
1007
    }
 
1008
 
 
1009
    /**
979
1010
     * Map that contains the component definitions.
980
1011
     */
981
1012
    private static class AntTypeTable extends Hashtable {
 
1013
        private static final long serialVersionUID = -3060442320477772028L;
 
1014
 
982
1015
        private Project project;
983
1016
 
984
1017
        AntTypeTable(Project project) {
1012
1045
            boolean found = false;
1013
1046
            if (clazz instanceof Class) {
1014
1047
                for (Iterator i = values().iterator(); i.hasNext() && !found;) {
1015
 
                    found |= (((AntTypeDefinition) (i.next())).getExposedClass(
 
1048
                    found = (((AntTypeDefinition) (i.next())).getExposedClass(
1016
1049
                        project) == clazz);
1017
1050
                }
1018
1051
            }
1022
1055
        public boolean containsValue(Object value) {
1023
1056
            return contains(value);
1024
1057
        }
 
1058
 
 
1059
        /**
 
1060
         * Create a list of all definitions that match a prefix, usually the URI
 
1061
         * of a library
 
1062
         * @param prefix prefix to match off
 
1063
         * @return the (possibly empty) list of definitions
 
1064
         */
 
1065
        public List/*<AntTypeDefinition>*/ findMatches(String prefix) {
 
1066
            ArrayList matches=new ArrayList();
 
1067
            for (Iterator i = values().iterator(); i.hasNext() ;) {
 
1068
                AntTypeDefinition def = (AntTypeDefinition) (i.next());
 
1069
                if(def.getName().startsWith(prefix)) {
 
1070
                    matches.add(def);
 
1071
                }
 
1072
            }
 
1073
            return matches;
 
1074
        }
1025
1075
    }
1026
 
 
1027
1076
}