How to assign variables like Ant?
This is a Ant config file named build.xml:
<property file="${basedir}/build.properties" />
<property name="Name" value="ProjectFullName"/> //setting some variables for later using...
<property name="name" value="project_name"/>
<property name="version" value="0.2"/>
<property name="year" value="2003"/>
<echo message="-- ${Name} ${version} [${year}] "/>//use the variables which has been set before...
I need do the same work like ant does.
Q1: Is there any component can solved this work? What name is it?
Q2: If the component exists, Can it be seperated to "local scope" and "global scope"?
Thank you and best regards!
[724 byte] By [
eingmarraa] at [2007-11-27 2:40:53]

> Q1: Is there any component can solved this work? What name is it?
Ant. Or any of several other templating language implementations in Java, such as StringTemplate or Velocity.
>Q2: If the component exists, Can it be seperated to "local scope" and "global scope"?
Velocity supports context chaining, which gives something like that. I'm not familiar with the other's support.
Check out class StrSubstitutor in Jakarta Commons Lang: http://jakarta.apache.org/commons/lang/[url] http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/text/StrSubstitutor.html[/url]
Lokoa at 2007-7-12 3:04:09 >

pm_kirkham and Loko , thank you very much!:)
And as pm_kirkham mentioned -- Velocity, it may be something which you are after:* http://velocity.apache.org/yc
yes , yclian, But I think velocity is too complex...
> yes , yclian, But I think velocity is too complex...Since you don't give any context in which you wish to use this it is difficult for anyone to suggest anything but general solutions.
ok, my English is not good, So I couldnt talk too much about I mind...I am so sorry for that.I think the StrSubstitutor is simple to learn...But it has no context to defined the scope varialbe ... :(
I don't understand what you are trying to do. I suspect that a java.util.Map plus some string substitution is all that you need but until you give more detail it is very difficult for anyone to advise.
I suspect that you are looking for something as simple as this (already published in this forum)
import e.util.*;
import java.util.*;
import java.util.regex.*;
/**
* For Rewriter see http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html
*/
class MacroProcessor extends Rewriter
{
public MacroProcessor()
{
super("\\$\\{([^}]+)\\}");
}
public String replacement()
{
final String newValue = macroReplacements.get(group(1));
return Matcher.quoteReplacement((newValue == null) ? group(0) : newValue);
}
public void putReplacement(String key, String value)
{
macroReplacements.put(key, value);
}
private final Map<String, String> macroReplacements = new HashMap<String, String>();
}
public class Fred906
{
public static void main(String[] args) throws Exception
{
String fileContent = "The quick \n${colour} \n${animal} jumps ${how} \nthe ${x} lazy dog.";
MacroProcessor macroProcessor = new MacroProcessor();
macroProcessor.putReplacement("colour","brown");
macroProcessor.putReplacement("how","over");
macroProcessor.putReplacement("animal","fox");
macroProcessor.putReplacement("x","${y}");
System.out.println(macroProcessor.rewrite(fileContent));
}
}
ok I think example is king:
your see this config xml which I made
<jothan-config>
...
<global variables>
<var name="abc" value="xm_cms" />
<var name="psw" value="mypassword" />
</global variables>
...
<datasources>
<datasource key="${abc}" rebuild="true">//I can use the variable "abc" here!
<connection driver="mysql">
<db-host>218.x.x.x</db-host>
<db-name>xmnext_cms</db-name>
<db-username>${psw+'_'+abc}</db-username>// it is a little bit complex: need concat them together!
<db-password>$psw</db-password>
</connection>
<db-fields table="xmcms_view_news_for_search">
<db-field key="title">
<name>title</name>
</db-field>
<db-field key="content">
<name>content</name>
</db-field>
<db-field key="pageurl">
<name>publish_url</name>
</db-field>
<db-field key="author">
<name>author</name>
</db-field>
<db-field key="date" intervalable="true">
<name>online_time</name>
</db-field>
<db-field key="type">
<name>type:news</name>
</db-field>
</db-fields>
</datasource>
<datasource key="xm_bbs" rebuild="false">
<connection driver="mysql">
<db-host>218.x.x.x</db-host>
<db-name>xmnext_bbs</db-name>
<db-username>abc</db-username>
<db-password>abc</db-password>
</connection>
...
<jothan>
1) Parse the xml file.2) Extract all the <var> elements and use them to build the macro replacement map.3) Visit all the other elements replacing macro values.
U mean there is no existed project or component to reuse for doing that?
> U mean there is no existed project or component to> reuse for doing that?I don't know. I have never seen one. It can't be more than 20 lines of code on top of my code plus Elliot's code.
I have studied your codes... thank you...:)
XML has that level of substitution built in using entities:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jothan-config [
><!ENTITY abc 'xm_cms'>
<!ENTITY psw 'mypassword'>
]>
<jothan-config>
...
<datasources>
<datasource key="&abc;" rebuild="true">//I can use the variable "abc" here!
<connection driver="mysql">
<db-host>218.x.x.x</db-host>
<db-name>xmnext_cms</db-name>
<db-username>&psw;_&abc;</db-username>
<db-password>&psw;</db-password>
</connection>
...
</datasource>
</datasources>
...
</jothan-config>
(the forum software is inserting a spurious greater-than before the first entity declaration)
Pete
> XML has that level of substitution built in using> entities::-) I had forgotten that! The OP will have to change his syntax but it will make it much better.
to pm_kirkham:
I am very appreciated you direct me another simple way... But it is not simple for my client programmer to learn... And I think EL expression is something which I expect mostly... Because it can do the work like "{$abc+'_'+$def}" ...
And I want to overide some variable by scope:
<global variables>
<var name="abc" value="xm_cms" />
<var name="psw" value="mypassword" />
</global variables>
...
<datasources>
<datasource key="${abc}" rebuild="true">//I can use the variable "abc" here!
<local variables>//define the local variable to override the variable "$abc"
<var name="abc" value="218.x.x.x" />
<local variables>
<connection driver="mysql">
<db-host>$abc</db-host> // reuse $abc here!
<db-name>xmnext_cms</db-name>
<db-username>${psw+'_'+abc}</db-username>
and thank you very much to sabre150 for your good and warmly answer...
BTW my xmlparser is not very much "Really XMLparser", it is a simple parser for xml elements to object which is named "digester", maybe it has been still used in struts by now.(I can solve the Local variable defined action by Digster :"When it is defining a xml elements to object I will define an symbol which is something like uri as "jothan.datasource[1]" , and the I will reuse the expression like this way: ${global.abc} which I defined in the "global variable" tags and ${jothan.datasource[1].abc} to reuse the varible which defined in the first "datasource" tags , yes there is too far from the xml which I gave before...But it's the terminal target which I expect to reach...)
If there no simple way to reach that...I must implement the simplest by using of "StrSubstitutor" or "EL expression"(If it can pear off from jsp2.0,yes,I just found it is in apache commons)... because of time shortly reasion, I need to begin the work this afternoon(now is 8:42am)
By now, I write the target which I had never said before because of my poor English.
Hoping for your deeply advise, thank you!
Best regards!
Message was edited by:
eingmarra
