You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Steve Amerige <St...@sas.com> on 2011/07/07 16:30:52 UTC

Re: Syntax for Names: target, property, macrodef, etc.

When I look at the 1.8.2 codebase, I see in:

     ant-1.8.2\src\main\org\apache\tools\ant\taskdefs\MacroDef.java

     /**
      * Check if a character is a valid character for an element or
      * attribute name.
      *
      * @param c the character to check
      * @return true if the character is a letter or digit or '.' or '-'
      *         attribute name
      */
     public static boolean isValidNameCharacter(char c) {
         // ? is there an xml api for this ?
         return Character.isLetterOrDigit(c) || c == '.' || c == '-';
     }

This seems to be at odds with the specification:

     http://www.w3.org/TR/xml/#sec-common-syn


          Names and Tokens

[4] 	|NameStartChar| 	   ::= 	|":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] 
| [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]|
[4a] 	|NameChar| 	   ::= 	|NameStartChar <http://www.w3.org/TR/xml/#NT-NameStartChar> | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | 
[#x203F-#x2040]|
[5] 	|Name| 	   ::= 	|NameStartChar <http://www.w3.org/TR/xml/#NT-NameStartChar> (NameChar <http://www.w3.org/TR/xml/#NT-NameChar>)*|
[6] 	|Names| 	   ::= 	|Name <http://www.w3.org/TR/xml/#NT-Name> (#x20 Name <http://www.w3.org/TR/xml/#NT-Name>)*|
[7] 	|Nmtoken| 	   ::= 	|(NameChar <http://www.w3.org/TR/xml/#NT-NameChar>)+|
[8] 	|Nmtokens| 	   ::= 	|Nmtoken <http://www.w3.org/TR/xml/#NT-Nmtoken> (#x20 Nmtoken <http://www.w3.org/TR/xml/#NT-Nmtoken>)*|


I've also looked at:

     ant-1.8.2\src\main\org\apache\tools\ant\taskdefs\AntStructure.java

         /**
          * Does this String match the XML-NMTOKEN production?
          * @param s the string to test
          * @return true if the string matches the XML-NMTOKEN
          */
         public static final boolean isNmtoken(String s) {
             final int length = s.length();
             for (int i = 0; i < length; i++) {
                 char c = s.charAt(i);
                 // XXX - we are committing CombiningChar and Extender here
                 if (!Character.isLetterOrDigit(c)
&& c != '.' && c != '-' && c != '_' && c != ':') {
                     return false;
                 }
             }
             return true;
         }

Maybe I'm looking at the wrong part in the code for where *element* names (such as macrodef and scriptdef) are validated and where 
other names (such as property name="...") are validated.  But, the Ant code doesn't match what is specified by the XML BNF.  Also, 
the XML BNF doesn't seem to allow spaces in a *Name* object.  Is there an Ant BNF specification that reflects the code?

Is there any formal specification for Ant syntax?  Where do I find it?

Sorry to be a bother!  But, I'm just trying to be very clear in understanding Ant and am hoping that a formal specification exists.

Many thanks!
Steve Amerige
SAS Institute, Deployment Software Developer


On 6/30/2011 9:31 AM, Dominique Devienne wrote:
> On Thu, Jun 30, 2011 at 6:51 AM, Steve Amerige<St...@sas.com>  wrote:
>> I'm looking for the authoritative specification within Ant for the value of
>> the name attribute as in:
>>
>> <property name="xxx" .../>
>> <macrodef name="xxx" ...>
>> <target name="xxx" ...>
>>
>> and so forth.  I can't find within the Ant manual any BNF that defines what
>> a valid name is allowed to be.
> Anything goes really, AFAIK. There is no specifications.
>
> Some names will create issues, for example<target name="-foo">  can't
> be called from the command line because -foo will be interpreted as a
> CLI switch and since not found Ant will error out. This "behavior" is
> relied upon to have "private" targets.
>
> Also remember this is XML land, so attribute whitespace normalization
> will apply (depending on the parser used possibly) which may result in
> some whitespaces being removed.
>
> But aside from these quirks, Ant does not put any restrictions on
> names in general. --DD
>
> PS: Also keep in mind that property expansion does occurs inside these
> names, but I assume you mean the names after expansion.


Re: Syntax for Names: target, property, macrodef, etc.

Posted by Stefan Bodewig <bo...@apache.org>.
On 2011-07-07, Steve Amerige wrote:

> When I look at the 1.8.2 codebase, I see in:

>     ant-1.8.2\src\main\org\apache\tools\ant\taskdefs\MacroDef.java

>     public static boolean isValidNameCharacter(char c) {
>         // ? is there an xml api for this ?
>         return Character.isLetterOrDigit(c) || c == '.' || c == '-';
>     }

This method is indeed used to validate attribute names and names of
nested elements of tasks defined as macrodefs (but not for the taks name
itself which is not formally restricted at all).

> This seems to be at odds with the specification:

>     http://www.w3.org/TR/xml/#sec-common-syn

Quite possible, we never really targeted full conformance but somethign
I'd call "good enough".

> I've also looked at:

>     ant-1.8.2\src\main\org\apache\tools\ant\taskdefs\AntStructure.java

>         public static final boolean isNmtoken(String s) {

AntStructure is a task that has been writen ages ago to write an
approximation of a DTD for a running Ant instance (each Ant run could in
fact produce a different DTD because new elements would be added by
taskdefs).  The task has never claimed to be useful 8-)

> Maybe I'm looking at the wrong part in the code for where *element*
> names (such as macrodef and scriptdef) are validated and where other
> names (such as property name="...") are validated.

You are.

In the end the XML parser decides what is a valid name and what is not.
Ant will not stop you from creating tasks with macro/script/taskdef that
are nor legal XML element names, you will just not be able to use them
because the parser will bail out.  We do have some checks in place but
they are - as you've found out - incomplete or even wrong.

For "normal" tasks - tasks written in Java - attribute names and names
of nested elements are determined by method names and thus are way more
restricted than their XML counterparts.  You can't have a "." or a "-"
in a method name and thus can't write a task in Java with an attribute
that would contain such a character.

I don't think we have any rules on property names at all.  Same for
target names.  Beyond the restrictions the XML parser will create, that
is (obviously you can't contains a 0 character in any XML document).

Some names will put you into trouble, though.  Target names that
contains commas (quite possible we prohibit it, not sure) would be
unusable in a depends attribute of a different target for example.
Properties whose names contain "$" or "}" will be difficult to use as
well.

> But, the Ant code doesn't match what is specified by the XML BNF.

This is true.  If you feel there is a real problem, feel free to provide
a patch 8-).  The difference between the formal specification and Ant's
code is likely to be in areas that haven't caused problems in practice
(or bugs would have been reported).

> Also, the XML BNF doesn't seem to allow spaces in a *Name* object.  Is
> there an Ant BNF specification that reflects the code?

> Is there any formal specification for Ant syntax?  Where do I find it?

None that I was aware of.

> Sorry to be a bother!  But, I'm just trying to be very clear in
> understanding Ant and am hoping that a formal specification exists.

I'm afraid there isn't.  The various IDE integration projects that
support writing Ant build files may have something, but even than you
can't be sure it will contain the full truth or rather "just work in
practice".

Sorry

        Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org