JavaScript +蚂蚁问题

发布于 2024-12-18 02:30:17 字数 2134 浏览 3 评论 0原文

正在处理 ANT 构建文件,其中定义了两个宏,需要从另一个宏调用一个宏。

这是我需要做的(为了便于理解,我简化了整个场景)。我的主要目标使用两个参数调用 FirstMacro。第一个宏需要使用收到的两个参数执行“某些操作”并对 SecondMacro 进行嵌入调用。

传递给 FirstMacro 的参数之一是其中包含“反斜杠”() 的文件路径。当我使用 echo 在宏中打印它时,它打印得很好。但是,当我通过 JavaScript 将相同的参数传递给 SecondMacro 时,反斜杠消失了(我看到的是 C:Test,而不是 C:\Test)。 保留反斜杠或用正斜杠替换它们的解决方案是什么?

请注意,我正在使用 ANT 1.7,并且我已经尝试过 string.replace(/\/g,"/") 等。

另请注意,您可以复制粘贴下面的整个代码,将其另存为 build.xml 并尝试运行主要任务是查看运行中的问题。

<?xml version="1.0"?>
<project name="build" basedir="." default="main">
    <description>
    ==========================
    Macro: Second Macro
    ==========================
    </description>
    <macrodef name="secondMacro">
        <attribute name="param"/>

        <sequential>
            <echo>secondMacro param: @{param}</echo>
        </sequential>
    </macrodef>

    <description>
    ==========================
    Macro: FirstMacro
    ==========================
    </description>
    <macrodef name="firstMacro">
        <attribute name="param1"/>
        <attribute name="param2"/>

        <sequential>
            <echo>firstMacro first param: @{param1}</echo>
            <echo>firstMacro second param: @{param2}</echo>
            <script language="javascript">
                <![CDATA[
                    var ext = "@{param2}";
                    if ("".equals(ext)) {
                        ext = "out";
                    }

                    macrotask = project.createTask("secondMacro");
                    macrotask.setDynamicAttribute("param", "@{param1}" + "."+ext );
                    macrotask.perform();
                ]]>
            </script>
        </sequential>
    </macrodef>

    <description>
    ==========================
    Target: main target
    ==========================
    </description>
    <target name="main">

        <firstMacro param1="C:\TestFolder/TestFile" param2=""/>
    </target>
</project>

working on an ANT build file, where I have two macros defined and need to call one Macro from other.

Here is what I need to do (I have simplified the whole scenario for easy understanding). My main target calls FirstMacro with two params. The First Macro needs to do "something" with two params received and make an embedded call to SecondMacro.

One of the params passed to FirstMacro is a file path with "backslash" () in it. When I print it in the macro using echo it prints fine. But when I pass the same parameter to SecondMacro via JavaScript, the backslashes are gone (instead of C:\Test I see C:Test).
What is the solution to preserve the backslash or replace them with forward slash.

Note that I am using ANT 1.7 and I have already tried string.replace(/\/g,"/") etc.

Also note that, you can copy paste the entire code below, save it as build.xml and try running the main task to see the issue in running.

<?xml version="1.0"?>
<project name="build" basedir="." default="main">
    <description>
    ==========================
    Macro: Second Macro
    ==========================
    </description>
    <macrodef name="secondMacro">
        <attribute name="param"/>

        <sequential>
            <echo>secondMacro param: @{param}</echo>
        </sequential>
    </macrodef>

    <description>
    ==========================
    Macro: FirstMacro
    ==========================
    </description>
    <macrodef name="firstMacro">
        <attribute name="param1"/>
        <attribute name="param2"/>

        <sequential>
            <echo>firstMacro first param: @{param1}</echo>
            <echo>firstMacro second param: @{param2}</echo>
            <script language="javascript">
                <![CDATA[
                    var ext = "@{param2}";
                    if ("".equals(ext)) {
                        ext = "out";
                    }

                    macrotask = project.createTask("secondMacro");
                    macrotask.setDynamicAttribute("param", "@{param1}" + "."+ext );
                    macrotask.perform();
                ]]>
            </script>
        </sequential>
    </macrodef>

    <description>
    ==========================
    Target: main target
    ==========================
    </description>
    <target name="main">

        <firstMacro param1="C:\TestFolder/TestFile" param2=""/>
    </target>
</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2024-12-25 02:30:17
<firstMacro param1="C:\\TestFolder/TestFile" param2=""/>

印刷

主要:

<块引用>

[echo]firstMacro 第一个参数:C:\TestFolder/TestFile
[echo] 第一个宏第二个参数:
[echo] 第二个宏参数:C:\TestFolder/TestFile.out

应该是你的JS代码的问题。我对 JS 知之甚少,但反斜杠在大多数语言中意味着“这是一个转义字符”。所以当JS看到单个反斜杠时,它可能会将其视为转义字符并进行一些转换... 即使你使用beanshell或其他东西,也可能会发生类似的事情。

要解决这个问题,你可以尝试修改你的JS代码(抱歉,我无能为力)或尝试使用斜杠(/)而不是反斜杠

更新:您可以使用 ant-contrib 中的 if 任务。
这是 if-task 版本“firstMacro”的示例。

<macrodef name="firstMacro">
    <attribute name="param1"/>
    <attribute name="param2"/>
    <sequential>
        <echo>firstMacro first param: @{param1}</echo>
        <echo>firstMacro second param: @{param2}</echo>
        <if>
            <equals arg1="@{param2}" arg2="" />
            <then>
                <property name="param" value="@{param1}.out" />
            </then>
            <else>
                <property name="param" value="@{param1}.@{param2}" />
            </else>
        </if>
        <secondMacro param="${param}" />
    </sequential>
</macrodef>
<firstMacro param1="C:\\TestFolder/TestFile" param2=""/>

prints

main:

[echo] firstMacro first param: C:\TestFolder/TestFile
[echo] firstMacro second param:
[echo] secondMacro param: C:\TestFolder/TestFile.out

It should be your JS code's problem. I know little about JS, but backslash means "this is an escape character" in most languages. So when JS see a single backslash, it may regard it as an escape character and do some convertion... Even if you use beanshell or something else, similar thing may happen.

To solve the problem, you can try to modify your JS code (which I can't help, sorry) or try to use slash (/) instead of backslash.

Update: you can use the if task from ant-contrib.
Here is the example of the if-task version "firstMacro".

<macrodef name="firstMacro">
    <attribute name="param1"/>
    <attribute name="param2"/>
    <sequential>
        <echo>firstMacro first param: @{param1}</echo>
        <echo>firstMacro second param: @{param2}</echo>
        <if>
            <equals arg1="@{param2}" arg2="" />
            <then>
                <property name="param" value="@{param1}.out" />
            </then>
            <else>
                <property name="param" value="@{param1}.@{param2}" />
            </else>
        </if>
        <secondMacro param="${param}" />
    </sequential>
</macrodef>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文