You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by jbmdharris <jb...@hotmail.com> on 2008/11/09 22:49:59 UTC

Inserting different relative paths into multiple files using the same token

I have many different HTML files in different directories.  I'm already using
the <copy> task with a <filterset> to replace tokens in these files.  I'm
trying to figure out how to create a token like @ROOTPATH@ that will always
resolve to the proper number of "../" patterns to get from the current
directory to the root of the directory tree.  This would be used for
example, like this:

&lt;a href="@ROOTPATH@/foo/bar.html"&gt;some link&lt;/a&gt;

In a file that then gets included into lots of other files in lots of
different directories.  So the value of @ROOTPATH@ needs to be different
depending upon the directory in which file containing it is located.  I am
unable to use absolute directory paths because this HTML is only read off of
a user's local disk instead of a webserver and an absolute path beginning at
the user's C: drive (or / if on Unix) isn't acceptable.  My ant build tasks
use <replaceregexp> to simulate SSI <!--#include --> to get the contents of
these files (like the banner) into all the other files.

I've tried using this:
<for param="currentDir">
   <path>
      <dirset dir="${rootPath}"/>
   </path>
   <sequential>
      <propertyregex property="currentDirForwardSlashes"
input="@{currentDir}" regexp="\\" replace="/" defaultvalue="@{currentDir}"
override="true"/>
      <echo message="Path: ${currentDirForwardSlashes}"/>
   </sequential>
</for>

But the path always comes back fully-qualified and I can't figure out how to
truncate the ${rootPath} poriton of the path off the front of the path and
then change the remaining directories in the path to ".."

I've also tried using a recursive technique with <dirset includes="*"> so I
only get one level at a time and then recursively call my task appending
"../" to a parameter.  However, I'm getting an error that <antcall> can't
call tasks recursively.

Does anyone have any ideas?
-- 
View this message in context: http://www.nabble.com/Inserting-different-relative-paths-into-multiple-files-using-the-same-token-tp20411245p20411245.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


AW: AW: AW: Inserting different relative paths into multiple files using the same token

Posted by Ja...@rzf.fin-nrw.de.
a) You could hold the (java written) task in your project
   <taskdef name="relpath" classname="..." classpath="${build.classes}"/>
b) You could hold that in your buildfile
   1. Write the java source into a temp directory with <echo>
   2. compile that using <javac>
   3. taskdef from that directory
c) Hold the class on webserver, <get> it into a temp dir and <taskdef> from there

Jan

>-----Ursprüngliche Nachricht-----
>Von: jbmdharris [mailto:jbmdharris@hotmail.com] 
>Gesendet: Montag, 10. November 2008 15:20
>An: user@ant.apache.org
>Betreff: Re: AW: AW: Inserting different relative paths into 
>multiple files using the same token
>
>
>I could implement this as a custom task written in Java, but 
>that would also
>require modifying the build environment... which is sometimes 
>OK if it's
>really necessary.  In my case, my build environment is part of 
>my corporate
>environment and it's used by hundreds of people.  Getting 
>something new in
>the environment requires work and proof that it's really 
>necessary.  So, I'm
>trying to use the built-in capabilities of the environment 
>provided to me. 
>At least I have ant-contrib available...
>
>-- 
>View this message in context: 
>http://www.nabble.com/Inserting-different-relative-paths-into-m
>ultiple-files-using-the-same-token-tp20411245p20420777.html
>Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

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


Re: AW: AW: Inserting different relative paths into multiple files using the same token

Posted by jbmdharris <jb...@hotmail.com>.
I could implement this as a custom task written in Java, but that would also
require modifying the build environment... which is sometimes OK if it's
really necessary.  In my case, my build environment is part of my corporate
environment and it's used by hundreds of people.  Getting something new in
the environment requires work and proof that it's really necessary.  So, I'm
trying to use the built-in capabilities of the environment provided to me. 
At least I have ant-contrib available...

-- 
View this message in context: http://www.nabble.com/Inserting-different-relative-paths-into-multiple-files-using-the-same-token-tp20411245p20420777.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


AW: AW: Inserting different relative paths into multiple files using the same token

Posted by Ja...@rzf.fin-nrw.de.
You could rewrite that in Java and <taskdef> that class.

Jan 

>-----Ursprüngliche Nachricht-----
>Von: jbmdharris [mailto:jbmdharris@hotmail.com] 
>Gesendet: Montag, 10. November 2008 13:03
>An: user@ant.apache.org
>Betreff: Re: AW: Inserting different relative paths into 
>multiple files using the same token
>
>
>My build environment doesn't include Rhino for executing 
>JavaScript in Java
>and it's unlikely that I would be allowed to add it.
>-- 
>View this message in context: 
>http://www.nabble.com/Inserting-different-relative-paths-into-m
>ultiple-files-using-the-same-token-tp20411245p20418710.html
>Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

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


Re: AW: Inserting different relative paths into multiple files using the same token

Posted by jbmdharris <jb...@hotmail.com>.
My build environment doesn't include Rhino for executing JavaScript in Java
and it's unlikely that I would be allowed to add it.
-- 
View this message in context: http://www.nabble.com/Inserting-different-relative-paths-into-multiple-files-using-the-same-token-tp20411245p20418710.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


AW: Inserting different relative paths into multiple files using the same token

Posted by Ja...@rzf.fin-nrw.de.
Would that be ok?
        <scriptdef name="relpath" language="javascript">
            <attribute name="property"/>
            <attribute name="base"/>
            <attribute name="file"/>
            <![CDATA[
                // Imports: Helper classes for Files
                importClass(Packages.org.apache.tools.ant.util.FileUtils);
                importClass(Packages.java.io.File);
                
                // Access to the attributes
                propName = attributes.get("property");
                baseFile = new File(attributes.get("base"));
                file     = new File(attributes.get("file"));
                
                // The relative
                relPath = FileUtils.getRelativePath(file, baseFile);
                if (!file.isDirectory()) {
                    relPath = (relPath.length() >= 3) ? relPath.substring(3) : relPath;
                }
                relPath += "/";

                // Store the result in a property
                project.setNewProperty(propName, relPath);
            ]]>
        </scriptdef>
        <ac:for param="file">
            <fileset dir="." includes="**/file.txt"/>
            <sequential>
                <relpath property="rel" base="x/a/b" file="@{file}"/>
                <echo>
                    File: @{file}
                    Base: x/a/b
                    rel : ${rel}
                </echo>
                <ac:var name="rel" unset="true"/>
            </sequential>
        </ac:for>


Jan 

>-----Ursprüngliche Nachricht-----
>Von: jbmdharris [mailto:jbmdharris@hotmail.com] 
>Gesendet: Sonntag, 9. November 2008 22:50
>An: user@ant.apache.org
>Betreff: Inserting different relative paths into multiple 
>files using the same token
>
>
>I have many different HTML files in different directories.  
>I'm already using
>the <copy> task with a <filterset> to replace tokens in these 
>files.  I'm
>trying to figure out how to create a token like @ROOTPATH@ 
>that will always
>resolve to the proper number of "../" patterns to get from the current
>directory to the root of the directory tree.  This would be used for
>example, like this:
>
>&lt;a href="@ROOTPATH@/foo/bar.html"&gt;some link&lt;/a&gt;
>
>In a file that then gets included into lots of other files in lots of
>different directories.  So the value of @ROOTPATH@ needs to be 
>different
>depending upon the directory in which file containing it is 
>located.  I am
>unable to use absolute directory paths because this HTML is 
>only read off of
>a user's local disk instead of a webserver and an absolute 
>path beginning at
>the user's C: drive (or / if on Unix) isn't acceptable.  My 
>ant build tasks
>use <replaceregexp> to simulate SSI <!--#include --> to get 
>the contents of
>these files (like the banner) into all the other files.
>
>I've tried using this:
><for param="currentDir">
>   <path>
>      <dirset dir="${rootPath}"/>
>   </path>
>   <sequential>
>      <propertyregex property="currentDirForwardSlashes"
>input="@{currentDir}" regexp="\\" replace="/" 
>defaultvalue="@{currentDir}"
>override="true"/>
>      <echo message="Path: ${currentDirForwardSlashes}"/>
>   </sequential>
></for>
>
>But the path always comes back fully-qualified and I can't 
>figure out how to
>truncate the ${rootPath} poriton of the path off the front of 
>the path and
>then change the remaining directories in the path to ".."
>
>I've also tried using a recursive technique with <dirset 
>includes="*"> so I
>only get one level at a time and then recursively call my task 
>appending
>"../" to a parameter.  However, I'm getting an error that 
><antcall> can't
>call tasks recursively.
>
>Does anyone have any ideas?
>-- 
>View this message in context: 
>http://www.nabble.com/Inserting-different-relative-paths-into-m
ultiple-files-using-the-same-token-tp20411245p20411245.html
>Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

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


Re: Inserting different relative paths into multiple files using the same token

Posted by jbmdharris <jb...@hotmail.com>.
After trying many things (including the foreach task which also cannot call
the task in which it is written recursively), I went back to trying to strip
the absolute paths down instead of building up the relative path
recursively.  This is what I have so far and it seems to work... but I'm
still looking for better ideas on how to do this.

    <!-- Computes root relative paths and substitutes them into the output
for the @DOCROOTPATH@ keyword -->
    <target name="processRootPath">
        <!-- param passed in: name="keyword" value="DOCROOTPATH"
               param passed in: name="rootPath" value="${dir.tmphtml}/doc" 
-->
        <!-- compute the fully-qualified root path. This changes all slashes
to platform versions so we have to change 
            them all back to forward slash. rootPath may be fully qualified,
but we shouldn't assume this. -->
        <property name="rootPathFQ" location="${rootPath}"/>
        <propertyregex property="rootPathFQForwardSlashes"
input="${rootPathFQ}" regexp="\\" replace="/" defaultValue=""
override="true"/>
        <echo message="rootPath=${rootPath}"/>
        <echo message="rootPathFQ=${rootPathFQ}"/>
        <echo
message="rootPathFQForwardSlashes=${rootPathFQForwardSlashes}"/>

        <!-- Iterate through all directories, compute a relative upPath back
to the docRoot and substitute that value into every file in that directory
in place of the keyword -->
        <for param="currentDir">
            <path>
                <dirset dir="${rootPath}"/>
            </path>
            <sequential>
                <!-- currentDir comes back fully qualified with all slashes
in platform versions.  So change them back to forward slash to compare with
the doc root -->
                <propertyregex property="currentDirForwardSlashes"
input="@{currentDir}" regexp="\\" replace="/" defaultValue=""
override="true"/>
                <echo
message="currentDirForwardSlashes=${currentDirForwardSlashes}"/>

                <!-- strip the fully-qualified root path off of the
fully-qualified current directory... this assumes that the root path is
normalized and matches exactly. -->
                <propertyregex property="relativePath"
input="${currentDirForwardSlashes}"
regexp="${rootPathFQForwardSlashes}(.*?)" replace="\1" defaultValue=""
override="true"/>
                <echo message="relativePath=${relativePath}"/>

                <!-- strip everything out of the relative path except for
the slashes -->
                <propertyregex property="relativePathSlashesOnly"
input="${relativePath}" regexp="[^/]" replace="" defaultValue=""
override="true"/>
                <echo
message="relativePathSlashesOnly=${relativePathSlashesOnly}"/>

                <!-- change / to ../ -->
                <propertyregex property="upPathToRoot"
input="${relativePathSlashesOnly}" regexp="/" replace="../" defaultValue=""
override="true"/>
                <echo message="upPathToRoot=${upPathToRoot}"/>

                <!-- replace the keyword in all files in the current
directory -->
                <replaceregexp match="@${keyword}@"
replace="${upPathToRoot}" flags="sg" byline="false">
                    <fileset dir="@{currentDir}" includes="*"/>
                </replaceregexp>
            </sequential>
        </for>

    </target>

-- 
View this message in context: http://www.nabble.com/Inserting-different-relative-paths-into-multiple-files-using-the-same-token-tp20411245p20414294.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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