bodewig     2004/04/16 01:36:01

  Modified:    .        WHATSNEW
               docs/manual/CoreTasks style.html
               src/etc/testcases/taskdefs/style build.xml
               src/main/org/apache/tools/ant/taskdefs XSLTProcess.java
               src/testcases/org/apache/tools/ant/taskdefs StyleTest.java
  Log:
  Add nested mappers to xslt, PR: 11249
  
  Revision  Changes    Path
  1.587     +2 -0      ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/ant/WHATSNEW,v
  retrieving revision 1.586
  retrieving revision 1.587
  diff -u -r1.586 -r1.587
  --- WHATSNEW  16 Apr 2004 07:48:49 -0000      1.586
  +++ WHATSNEW  16 Apr 2004 08:36:00 -0000      1.587
  @@ -141,6 +141,8 @@
   
   * <sshexec> now also captures stderr output.  Bugzilla Report 28349.
   
  +* <xslt> now supports a nested <mapper>.  Bugzilla Report 11249.
  +
   Changes from Ant 1.6.0 to Ant 1.6.1
   =============================================
   
  
  
  
  1.32      +19 -1     ant/docs/manual/CoreTasks/style.html
  
  Index: style.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/style.html,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- style.html        9 Feb 2004 21:50:05 -0000       1.31
  +++ style.html        16 Apr 2004 08:36:01 -0000      1.32
  @@ -56,7 +56,8 @@
     <tr>
       <td valign="top">extension</td>
       <td valign="top">desired file extension to be used for the targets. If 
not
  -      specified, the default is &quot;.html&quot;.</td>
  +      specified, the default is &quot;.html&quot;.  Will be ignored if
  +      a nested &lt;mapper&gt; has been specified.</td>
       <td align="center" valign="top">No</td>
     </tr>
     <tr>
  @@ -288,6 +289,16 @@
   </table>
   </blockquote>
   
  +<h4>mapper</h4>
  +
  +<p><em>since Ant 1.6.2</em></p>
  +
  +<p>You can define filename transformations by using a nested <a
  +href="../CoreTypes/mapper.html">mapper</a> element. The default mapper
  +used by <code>&lt;xslt&gt;</code> removes the file extension from the
  +source file and adds the extension specified via the extension
  +attribute.</p>
  +
   <h3>Examples</h3>
   <blockquote>
     <pre>
  @@ -334,6 +345,13 @@
       &lt;attribute 
name=&quot;http://xml.apache.org/xalan/features/optimize&quot; 
value=&quot;true&quot;/&gt;
     &lt;/factory&gt;
   &lt;/xslt&gt;</pre>
  +
  +  <h4>Using a mapper</h4>
  +<pre>&lt;xslt basedir=&quot;in&quot; destdir=&quot;out&quot;
  +      style=&quot;style/apache.xsl&quot;&gt;
  +  &lt;mapper type=&quot;glob&quot; from=&quot;*.xml.en&quot; 
to=&quot;*.html.en&quot;/&gt;
  +&lt;/xslt&gt;</pre>
  +
    </blockquote>
   <hr>
   <p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. 
All rights
  
  
  
  1.4       +15 -0     ant/src/etc/testcases/taskdefs/style/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/style/build.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- build.xml 12 Sep 2003 13:59:35 -0000      1.3
  +++ build.xml 16 Apr 2004 08:36:01 -0000      1.4
  @@ -42,6 +42,21 @@
         </style>
     </target>
   
  +  <target name="testDefaultMapper">
  +    <property name="value" value="myvalue"/>
  +    <style style="printParams.xsl" destDir="${out.dir}" basedir=".">
  +      <param name="set" expression="${value}"/>
  +    </style>
  +  </target>
  +
  +  <target name="testCustomMapper">
  +    <property name="value" value="myvalue"/>
  +    <style style="printParams.xsl" destDir="${out.dir}" basedir=".">
  +      <param name="set" expression="${value}"/>
  +      <mapper type="glob" from="data.*" to="out.*"/>
  +    </style>
  +  </target>
  +
     <target name="testNewerStylesheet">
         <antcall target="copyXsl">
           <param name="xsl.value" value="old-value"/>
  
  
  
  1.84      +64 -7     
ant/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
  
  Index: XSLTProcess.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java,v
  retrieving revision 1.83
  retrieving revision 1.84
  diff -u -r1.83 -r1.84
  --- XSLTProcess.java  16 Apr 2004 07:48:49 -0000      1.83
  +++ XSLTProcess.java  16 Apr 2004 08:36:01 -0000      1.84
  @@ -25,9 +25,11 @@
   import org.apache.tools.ant.DirectoryScanner;
   import org.apache.tools.ant.DynamicConfigurator;
   import org.apache.tools.ant.Project;
  +import org.apache.tools.ant.types.Mapper;
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.Reference;
   import org.apache.tools.ant.types.XMLCatalog;
  +import org.apache.tools.ant.util.FileNameMapper;
   import org.apache.tools.ant.util.FileUtils;
   
   /**
  @@ -133,6 +135,13 @@
       private AntClassLoader loader = null;
   
       /**
  +     * Mapper to use when a set of files gets processed.
  +     *
  +     * @since Ant 1.6.2
  +     */
  +    private Mapper mapperElement = null;
  +
  +    /**
        * Creates a new XSLTProcess Task.
        */
       public XSLTProcess() {
  @@ -163,6 +172,21 @@
       }
   
       /**
  +     * Defines the mapper to map source to destination files.
  +     * @return a mapper to be configured
  +     * @exception BuildException if more than one mapper is defined
  +     * @since Ant 1.6.2
  +     */
  +    public Mapper createMapper() throws BuildException {
  +        if (mapperElement != null) {
  +            throw new BuildException("Cannot define more than one mapper",
  +                                     getLocation());
  +        }
  +        mapperElement = new Mapper(getProject());
  +        return mapperElement;
  +    }
  +
  +    /**
        * Executes the task.
        *
        * @exception BuildException if there is an execution problem.
  @@ -437,7 +461,6 @@
                            File stylesheet)
           throws BuildException {
   
  -        String fileExt = targetExtension;
           File   outFile = null;
           File   inFile = null;
   
  @@ -451,13 +474,26 @@
                   return;
               }
   
  -            int dotPos = xmlFile.lastIndexOf('.');
  -            if (dotPos > 0) {
  -                outFile = new File(destDir,
  -                    xmlFile.substring(0, xmlFile.lastIndexOf('.')) + 
fileExt);
  +            FileNameMapper mapper = null;
  +            if (mapperElement != null) {
  +                mapper = mapperElement.getImplementation();
               } else {
  -                outFile = new File(destDir, xmlFile + fileExt);
  +                mapper = new StyleMapper();
  +            }
  +
  +            String[] outFileName = mapper.mapFileName(xmlFile);
  +            if (outFileName == null || outFileName.length == 0) {
  +                log("Skipping " + inFile + " it cannot get mapped to 
output.",
  +                    Project.MSG_VERBOSE);
  +                return;
  +            } else if (outFileName == null || outFileName.length > 1) {
  +                log("Skipping " + inFile + " its mapping is ambiguos.",
  +                    Project.MSG_VERBOSE);
  +                return;
               }
  +
  +            outFile = new File(destDir, outFileName[0]);
  +
               if (force
                   || inFile.lastModified() > outFile.lastModified()
                   || styleSheetLastModified > outFile.lastModified()) {
  @@ -921,5 +957,26 @@
           } // -- class Attribute
   
       } // -- class Factory
  +
  +    /**
  +     * Mapper implementation of the "traditional" way &lt;xslt&gt;
  +     * mapped filenames.
  +     *
  +     * <p>If the file has an extension, chop it off.  Append whatever
  +     * the user has specified as extension or ".html".</p>
  +     *
  +     * @since Ant 1.6.2
  +     */
  +    private class StyleMapper implements FileNameMapper {
  +        public void setFrom(String from) {}
  +        public void setTo(String to) {}
  +        public String[] mapFileName(String xmlFile) {
  +            int dotPos = xmlFile.lastIndexOf('.');
  +            if (dotPos > 0) {
  +                xmlFile = xmlFile.substring(0, dotPos);
  +            }
  +            return new String[] {xmlFile + targetExtension};
  +        }
  +    }
   
   }
  
  
  
  1.11      +14 -0     
ant/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
  
  Index: StyleTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- StyleTest.java    9 Mar 2004 16:48:57 -0000       1.10
  +++ StyleTest.java    16 Apr 2004 08:36:01 -0000      1.11
  @@ -82,6 +82,20 @@
       }
   
   
  +    public void testDefaultMapper() throws Exception {
  +        assertTrue(!getProject().resolveFile("out/data.html").exists());
  +        expectFileContains("testDefaultMapper",
  +                           "out/data.html",
  +                           "set='myvalue'");
  +    }
  +
  +    public void testCustomMapper() throws Exception {
  +        assertTrue(!getProject().resolveFile("out/out.xml").exists());
  +        expectFileContains("testCustomMapper",
  +                           "out/out.xml",
  +                           "set='myvalue'");
  +    }
  +
       // *************  copied from ConcatTest  *************
   
       // ------------------------------------------------------
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to