You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by jh...@apache.org on 2008/11/10 13:09:57 UTC

svn commit: r712650 - in /ant/sandbox/javafront: src/main/org/apache/ant/javafront/TaskExec.java src/main/org/apache/ant/javafront/builder/Tag.java taskexec.bat taskexec.sh

Author: jhm
Date: Mon Nov 10 04:09:56 2008
New Revision: 712650

URL: http://svn.apache.org/viewvc?rev=712650&view=rev
Log:
Support for nested elements + more examples + comparison XML/CMD

Modified:
    ant/sandbox/javafront/src/main/org/apache/ant/javafront/TaskExec.java
    ant/sandbox/javafront/src/main/org/apache/ant/javafront/builder/Tag.java
    ant/sandbox/javafront/taskexec.bat
    ant/sandbox/javafront/taskexec.sh

Modified: ant/sandbox/javafront/src/main/org/apache/ant/javafront/TaskExec.java
URL: http://svn.apache.org/viewvc/ant/sandbox/javafront/src/main/org/apache/ant/javafront/TaskExec.java?rev=712650&r1=712649&r2=712650&view=diff
==============================================================================
--- ant/sandbox/javafront/src/main/org/apache/ant/javafront/TaskExec.java (original)
+++ ant/sandbox/javafront/src/main/org/apache/ant/javafront/TaskExec.java Mon Nov 10 04:09:56 2008
@@ -20,6 +20,7 @@
 import java.util.Hashtable;
 import java.util.Properties;
 import java.util.Map;
+import java.util.Stack;
 
 import org.apache.tools.ant.launch.*;
 import org.apache.tools.ant.*;
@@ -30,29 +31,92 @@
 
 public class TaskExec implements AntMain {
 
-    public void startAnt(String[] args, Properties additionalUserProperties, ClassLoader coreLoader) {
-        Map<String, String> attributes = new Hashtable<String, String>();
-
-        // Analyzing the command line arguments
-        String taskname = args[0];
-        for(int i=1; i<args.length; ) {
-            String attrName  = args[i++];
-            String attrValue = args[i++];
-            attributes.put(attrName, attrValue);
-        }
+    private enum NextStatementIs {
+        TAG, ATTRIBUTE, TEXT;
+    }
 
+    public void startAnt(String[] args, Properties additionalUserProperties, ClassLoader coreLoader) {
         // Initializing
         Project    project = initProject();
         TagBuilder builder = TagBuilder.forProject(project);
 
-        // Initializing the Task
-        Tag tag = builder.tag(taskname);
-        for (String key : attributes.keySet()) {
-            tag.withAttribute(key, attributes.get(key));
+        // Process the arguments
+        Stack<Tag> tags = new Stack<Tag>();
+        NextStatementIs nextIs = NextStatementIs.TAG;
+        Tag current = null;
+        StringBuilder text = new StringBuilder();
+        for(int i=0; i<args.length; i++) {
+            String arg = args[i];
+            if (arg.equals("+")) {
+                nextIs = NextStatementIs.TAG;
+            } else if (arg.equals("-")) {
+                debug("CLOSE: " + tags.pop());
+                current = tags.peek();
+                nextIs = NextStatementIs.ATTRIBUTE;
+            } else if (arg.equals("#")) {
+                nextIs = NextStatementIs.TEXT;
+            } else {
+                switch (nextIs) {
+                    case TAG :
+                         Tag newTag = builder.tag(arg);
+                         tags.push(newTag);
+                         if (current != null) {
+                             // This is not a root element so add it to its parent.
+                             current.withChild(newTag);
+                         }
+                         current = newTag;
+                         nextIs = NextStatementIs.ATTRIBUTE;
+                         debug("TAG  : " + arg);
+                         break;
+                    case ATTRIBUTE :
+                         String key = arg;
+                         String value = args[++i];
+                         current.withAttribute(key, value);
+                         debug("ATTR : " + key + "=" + value);
+                         break;
+                    case TEXT :
+                         // We have to add spaces becaused they are removed while passing
+                         // from the command line to Java. So first store the text word by word,
+                         // add spaces if nessessary and finally add the text if the last
+                         // text was found.
+                         if (text.length() > 0) {
+                            text.append(" ");
+                         }
+                         text.append(arg);
+                         debug("TEXT : += '" + arg + "'");
+                         break;
+                    default :
+                         // no-op
+                }
+                if (!(nextIs == NextStatementIs.TEXT) && text.length()>0) {
+                    // We have stored and no further text, so add it to the element.
+                    debug("XXSTORE: " + text.toString());
+                    current.withNestedText(text.toString());
+                    text = new StringBuilder();
+                }
+            }
+        }
+        
+        // Close text
+        if (text.length()>0) {
+            current.withNestedText(text.toString());
+            debug("STORE: " + text.toString());
         }
 
+        // Close all open nested elements
+        debug("Closing all open nested elements.");
+        for(int i=tags.size(); i>1; i--) {
+            debug("CLOSE: " + tags.peek().toString());
+            tags.pop().build();
+        }
+        
         // Run the task
-        tag.execute();
+        debug("RUN  : " + tags.firstElement());
+        debug("Current Configuration:");
+        debug(0, tags.firstElement().getUE());
+
+        tags.firstElement().execute();
+        
     }
 
     private Project initProject() {
@@ -67,5 +131,45 @@
 
         return rv;
     }
-
+    
+    
+    // ========== Debug facilities ==========
+    
+
+    private boolean debug = true;
+
+    private void debug(String s) {
+        if (debug) {
+            System.out.println(s);
+        }
+    }
+    private void debug(int indent, UnknownElement ue) {
+        if (debug) {
+            // Visualize the hierarchy.
+            for(int i=0; i<indent-1; i++) {
+                System.out.print("|  ");
+            }
+            if (indent > 0) {
+                System.out.print("+--");
+            }
+            // Print the information of the current UE.
+            System.out.println(debug(ue));
+            // Print the nested elements.
+            java.util.List<UnknownElement> children = ue.getChildren();
+            if (children != null) {
+                for(UnknownElement o : children) {
+                    debug(indent + 1, o);
+                }
+            }
+        }
+    }
+    private String debug(UnknownElement ue) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(ue.getTag());
+        Map m = ue.getWrapper().getAttributeMap();
+        for (java.util.Iterator i = m.entrySet().iterator(); i.hasNext();) {
+            sb.append(" ").append(i.next());
+        }
+        return sb.toString();
+    }
 }
\ No newline at end of file

Modified: ant/sandbox/javafront/src/main/org/apache/ant/javafront/builder/Tag.java
URL: http://svn.apache.org/viewvc/ant/sandbox/javafront/src/main/org/apache/ant/javafront/builder/Tag.java?rev=712650&r1=712649&r2=712650&view=diff
==============================================================================
--- ant/sandbox/javafront/src/main/org/apache/ant/javafront/builder/Tag.java (original)
+++ ant/sandbox/javafront/src/main/org/apache/ant/javafront/builder/Tag.java Mon Nov 10 04:09:56 2008
@@ -77,4 +77,12 @@
             ((Task) o).perform();
         }
     }
+    
+    // More for debugging ...
+    public String toString() {
+        return ue.getTag();
+    }
+    public UnknownElement getUE() {
+        return ue;
+    }
 }
\ No newline at end of file

Modified: ant/sandbox/javafront/taskexec.bat
URL: http://svn.apache.org/viewvc/ant/sandbox/javafront/taskexec.bat?rev=712650&r1=712649&r2=712650&view=diff
==============================================================================
--- ant/sandbox/javafront/taskexec.bat (original)
+++ ant/sandbox/javafront/taskexec.bat Mon Nov 10 04:09:56 2008
@@ -18,22 +18,83 @@
 cls
 set START=call ant -lib build\classes -main org.apache.ant.javafront.TaskExec
 
+
 echo ----- Build the library -----
 call ant
 
-echo ----- First Run: using ^<echo^> as Hello World
+
+
+
+echo ===============================================================================================
+echo XML: ^<echo message="Hello World"/^>
+echo CMD: echo message "Hello World"
+echo -----------------------------------------------------------------------------------------------
 %START% echo message "Hello World"
 
-echo ----- Second Run: using ^<echoproperties^> for printing all Ant related properties -----
+
+echo ===============================================================================================
+echo XML: ^<echo message="This is Ant version ${ant.version}"/^>
+echo CMD: echo message "This is Ant version ${ant.version}"
+echo -----------------------------------------------------------------------------------------------
+%START% echo message "This is Ant version ${ant.version}"
+
+
+echo ===============================================================================================
+echo XML: ^<echoproperties prefix="ant."/^>
+echo CMD: echoproperties prefix ant.
+echo -----------------------------------------------------------------------------------------------
 %START% echoproperties prefix ant.
 
-echo ----- Third Run: using ^<copy^> for copying one file -----
+
+echo ===============================================================================================
+echo XML: ^<copy file="build.xml" tofile="build.xml.bak"/^>
+echo CMD: copy file build.xml tofile build.xml.bak
+echo -----------------------------------------------------------------------------------------------
 %START% copy file build.xml tofile build.xml.bak
 
 echo ----- A 'build.xml.bak' should exist -----
 dir bu*.*
 
-echo ----- Delete that file again -----
+
+echo ===============================================================================================
+echo XML: ^<delete file="build.xml.bak"/^>
+echo CMD: delete file build.xml.bak
+echo -----------------------------------------------------------------------------------------------
 %START% delete file build.xml.bak
+
+echo ----- A 'build.xml.bak' should not exist -----
 dir bu*.*
 
+
+echo ===============================================================================================
+echo XML: ^<mkdir dir="test"/^>
+echo XML: ^<copy todir="test"^>
+echo          ^<fileset dir="src"/^>
+echo      ^</copy^>
+echo CMD: mkdir dir test
+echo CMD: copy todir test + fileset dir src
+echo -----------------------------------------------------------------------------------------------
+%START% mkdir dir test
+%START% copy todir test + fileset dir src
+
+
+echo ===============================================================================================
+echo XML: ^<echo^>Hello World^</echo^>
+echo CMD: echo # This is Ant version ${ant.version}
+echo -----------------------------------------------------------------------------------------------
+%START% echo # This is Ant version ${ant.version}
+
+
+echo ===============================================================================================
+echo XML: ^<concat^>
+echo          ^<fileset dir="src" includes="*.properties"/^>
+echo          ^<header^>Ant Version ${ant.version}^</header^>
+echo          ^<footer^>End of text^</footer^>
+echo      ^</concat^>
+echo CMD: concat + fileset dir src includes *.properties - + header # Ant Version ${ant.version} - + footer # End of text
+echo -----------------------------------------------------------------------------------------------
+%START% concat + fileset dir src includes *.properties - + header # Ant Version ${ant.version} - + footer # End of text
+
+
+
+:end
\ No newline at end of file

Modified: ant/sandbox/javafront/taskexec.sh
URL: http://svn.apache.org/viewvc/ant/sandbox/javafront/taskexec.sh?rev=712650&r1=712649&r2=712650&view=diff
==============================================================================
--- ant/sandbox/javafront/taskexec.sh (original)
+++ ant/sandbox/javafront/taskexec.sh Mon Nov 10 04:09:56 2008
@@ -25,18 +25,75 @@
 echo '----- Build the library -----'
 ant
 
-echo '----- First Run: using ^<echo^> as Hello World'
+
+
+
+echo '============================================================================================='
+echo XML: ^<echo message="Hello World"/^>
+echo CMD: echo message "Hello World"
+echo -----------------------------------------------------------------------------------------------
 StartAnt echo message "Hello World"
 
-echo '----- Second Run: using ^<echoproperties^> for printing all Ant related properties -----'
+
+echo ===============================================================================================
+echo XML: ^<echo message="This is Ant version ${ant.version}"/^>
+echo CMD: echo message "This is Ant version ${ant.version}"
+echo '---------------------------------------------------------------------------------------------'
+StartAnt echo message "This is Ant version ${ant.version}"
+
+
+echo '==============================================================================================='
+echo XML: ^<echoproperties prefix="ant."/^>
+echo CMD: echoproperties prefix ant.
+echo '---------------------------------------------------------------------------------------------'
 StartAnt echoproperties prefix ant.
 
-echo '----- Third Run: using ^<copy^> for copying one file -----'
+
+echo '==============================================================================================='
+echo XML: ^<copy file="build.xml" tofile="build.xml.bak"/^>
+echo CMD: copy file build.xml tofile build.xml.bak
+echo '---------------------------------------------------------------------------------------------'
 StartAnt copy file build.xml tofile build.xml.bak
 
-echo "----- A 'build.xml.bak' should exist -----"
+echo ----- A 'build.xml.bak' should exist -----
 ls bu*.*
 
-echo '----- Delete that file again -----'
+
+echo '==============================================================================================='
+echo XML: ^<delete file="build.xml.bak"/^>
+echo CMD: delete file build.xml.bak
+echo '---------------------------------------------------------------------------------------------'
 StartAnt delete file build.xml.bak
+
+echo ----- A 'build.xml.bak' should not exist -----
 ls bu*.*
+
+
+echo '==============================================================================================='
+echo XML: ^<mkdir dir="test"/^>
+echo XML: ^<copy todir="test"^>
+echo          ^<fileset dir="src"/^>
+echo      ^</copy^>
+echo CMD: mkdir dir test
+echo CMD: copy todir test + fileset dir src
+echo '---------------------------------------------------------------------------------------------'
+StartAnt mkdir dir test
+StartAnt copy todir test + fileset dir src
+
+
+echo '==============================================================================================='
+echo XML: ^<echo^>Hello World^</echo^>
+echo CMD: echo # This is Ant version ${ant.version}
+echo '---------------------------------------------------------------------------------------------'
+StartAnt echo # This is Ant version ${ant.version}
+
+
+echo '==============================================================================================='
+echo XML: ^<concat^>
+echo          ^<fileset dir="src" includes="*.properties"/^>
+echo          ^<header^>Ant Version ${ant.version}^</header^>
+echo          ^<footer^>End of text^</footer^>
+echo      ^</concat^>
+echo CMD: concat + fileset dir src includes *.properties - + header # Ant Version ${ant.version} - + footer # End of text
+echo '---------------------------------------------------------------------------------------------'
+StartAnt concat + fileset dir src includes *.properties - + header # Ant Version ${ant.version} - + footer # End of text