ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Pawson <dave.paw...@gmail.com>
Subject Re: ant 1.7. Finding undefined variables
Date Thu, 21 May 2009 15:58:10 GMT
2009/5/21 Dominique Devienne <ddevienne@gmail.com>:

>> An example please Dominique?
>
> XML elements reading properties without the ${name} notation
> <target if/unless>
> <fail if/unless>
> <condition><isset property></condition>
> ...
>
> XML elements writing properties affecting the rest of the build:
> <condition property>
> <exec (output|error)property>
> <manifestclasspath property>

I've not seen any of those.
Perhaps you could provide me with an example build file I can experiment with?

Below, the xslt script I'm using if you want to play

java -cp saxon9.jar:/myjava:/myjava/xercesImpl.jar
net.sf.saxon.Transform  -x:org.apache.xerces.parsers.SAXParser -o $3
$1 $2  $4 $5 $6

regards


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dp="http://www.dpawson.co.uk/ns#"

 xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes=" xs"
  version="2.0">
  <xsl:strip-space elements="*"/>
  <d:doc xmlns:d="http://www.dpawson.co.uk/ns#">
    <d:revhistory>
      <d:purpose>
        <d:para>This stylesheet works with an ant build file file to produce a
         ant script which tests all variables within the build file</d:para>
      </d:purpose>
      <d:revision>
        <d:revnumber>1.0</d:revnumber>
        <d:date>2009-05-20T11:15:12Z</d:date>
        <d:authorinitials>DaveP</d:authorinitials>
        <d:revdescription>
          <d:para></d:para>
        </d:revdescription>
        <d:revremark></d:revremark>
      </d:revision>
    </d:revhistory>
  </d:doc>


  <xsl:output method="xml" indent="yes" encoding="utf-8"/>

  <xsl:template match="/">
    <project name="testvars.2" >
      <xsl:apply-templates select="//pathelement"/>

      <xsl:apply-templates select="//exec"/>

      <xsl:apply-templates select="//arg"/>

      <xsl:apply-templates select="//java"/>

      <xsl:apply-templates select="//fileset"/>

      <xsl:apply-templates select="//jvmarg"/>


      </project>
  </xsl:template>









<xsl:template match="pathelement[contains(@path,'$')]">
   <xsl:copy-of select="dp:property(@path,'')"/>
 </xsl:template>


 <xsl:template match="exec[contains(@executable,'$')]">
   <xsl:copy-of select="dp:property(@executable,'')"/>
 </xsl:template>


 <xsl:template match="arg[contains(@value,'$')]">
   <xsl:copy-of select="dp:property(@value,'')"/>
 </xsl:template>

 <xsl:template match="arg[contains(@line,'$')]">
   <xsl:copy-of select="dp:property(@line,'')"/>
 </xsl:template>

<xsl:template match="java">
  <xsl:if test="contains(@classname,'$')">
    <xsl:copy-of select="dp:property(@classname,'')"/>
  </xsl:if>
  <xsl:if test="contains(@dir,'$')">
    <xsl:copy-of select="dp:property(@dir,'')"/>
  </xsl:if>
 </xsl:template>

 <xsl:template match="fileset[contains(@dir,'$')]">
   <xsl:copy-of select="dp:property(@dir,'')"/>
 </xsl:template>

<xsl:template match="jvmarg[contains(@value,'$')]">
   <xsl:copy-of select="dp:property(@value,'')"/>
 </xsl:template>

  <xsl:template match="*"/>



<!--  -->
<!-- Function to output all properties for checking  -->
<!--  -->
<xsl:function name="dp:property">
  <xsl:param name="str" as="xs:string"/>
  <xsl:param name='res' as="item()*"/>
  <xsl:analyze-string select="$str" regex="\$\{{([^}}]+)\}}">
      <xsl:matching-substring>
      <xsl:variable name='part'>
 <property name="{{prop.msg}}" value="Must be defined, preferably in
${{basedir}}/${{properties.file}}"/>
  <fail message="{regex-group(1)} ${{prop.msg}}" unless="{regex-group(1)}"/>
</xsl:variable>
<xsl:choose>
  <xsl:when test="contains(regex-group(2), '$')">
     <xsl:copy-of select="dp:property(regex-group(2), $part)"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:copy-of select="$part"/>
   </xsl:otherwise>
 </xsl:choose>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <!--      <xsl:message>
        oops <xsl:value-of select="$str"/>
      </xsl:message>
      -->
    </xsl:non-matching-substring>
    </xsl:analyze-string>
    <xsl:value-of select="$res"/>

</xsl:function>



</xsl:stylesheet>



> ...
>
> In macros with params having default params (or lack thereof, what you
> try to catch), it uses @{name} to differentiate it from property using
> the ${name}. Of course the two can be combined ${foo@{bar}} so
> figuring out what property could be missing would replicate the
> running behavior.
>
> These are various reasons looking for ${name} may be insufficient for
> some builds. Nonetheless I agree than looking for ${name} covers a lot
> of ground.
>
> You need to watch out for property helpers though, as
> ${toString:some-ref-id} is a fake property name converting the Java
> object (Ant datatype typically like a path) with reference id
> "some-ref-id" to a string on the fly. (undocumented feature, but
> there's an official API to add custom property helpers for
> ${helper:rest} notation.
>
>> But yes, for clarity, I am assuming ${name} form. ?Best practice?
>
> Using ${name} is how you deref properties in the XML. All I'm saying
> is that property derefs happen in the code as well, and that even
> ${name} can have a different meaning with macros and property helpers
> (the latter is not very common, but the former more so).
>
> But as I said, ${name} covers 90% of the ground, if not more. I just
> wanted to make you aware of the 10% or 5% or 1% you could be missing.
> 90+ % is a good hit rate ;-) --DD
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>



-- 
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk

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


Mime
View raw message