geirm       02/03/17 13:08:52

  Modified:    src/java/org/apache/tools/dvsl DVSL.java DVSLTask.java
                        TransformTool.java Transformer.java
  Log:
  Adding support for getAppValue() and putAppValue() to allow application-defined
  values to be accessed from w/in the stylesheet.  There are yecchy aspects, but
  this is needed and beats the prev solution, magic references in the context.
  
  The DVSLTask will put in "infilename" and "outfilename"
  
  Revision  Changes    Path
  1.10      +27 -1     jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSL.java
  
  Index: DVSL.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSL.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DVSL.java 15 Mar 2002 11:21:20 -0000      1.9
  +++ DVSL.java 17 Mar 2002 21:08:52 -0000      1.10
  @@ -106,6 +106,8 @@
       private File logFile;
       private LogSystem logger;
   
  +    private Map appVals = new HashMap();
  +
       private TemplateHandler templateHandler = new TemplateHandler();
   
       boolean validate = false;
  @@ -419,7 +421,8 @@
            *  velocity engine
            */
   
  -        transformer = new Transformer(ve, templateHandler,  baseContext, validate);
  +        transformer = new Transformer(ve, templateHandler,  baseContext, appVals,
  +                            validate);
       }
   
       /**
  @@ -607,6 +610,29 @@
           }
       }
   
  +
  +    /**
  +     *  Gets the application value for the specified key
  +     *
  +     *  @param key key to use to retrieve value
  +     *  @return value if found, null otherwise
  +     */
  +    public Object getAppValue(Object key)
  +    {
  +        return appVals.get(key);
  +    }
  +
  +    /**
  +     *  Sets the application value for the specified key
  +     *
  +     *  @param key key to use to store value
  +     *  @param value value to be stored
  +     *  @return old value if any, null otherwise
  +     */
  +    public Object putAppValue(Object key, Object value)
  +    {
  +        return appVals.put(key, value);
  +    }
   
       /**
        *  <p>
  
  
  
  1.7       +67 -48    
jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSLTask.java
  
  Index: DVSLTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/DVSLTask.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DVSLTask.java     22 Feb 2002 06:07:01 -0000      1.6
  +++ DVSLTask.java     17 Mar 2002 21:08:52 -0000      1.7
  @@ -126,6 +126,12 @@
   
   public class DVSLTask extends MatchingTask
   {
  +    /**
  +     *  Supported app values
  +     */
  +    public static final String INFILENAME = "infilename";
  +    public static final String OUTFILENAME = "outfilename";
  +
       private DVSL dvsl;
   
       private File destDir = null;
  @@ -188,16 +194,16 @@
            */
           dvsl = new DVSL();
   
  -        dvsl.setValidatingParser( validatingParser );
  +        dvsl.setValidatingParser(validatingParser);
   
           /*
            *  Create a new Classloader for loading the Toolbox and the Velocity
            *  properties class.
            */
  -        if ( classpath != null )
  +        if (classpath != null)
           {
  -            classLoader = new AntClassLoader( project, classpath );
  -            dvsl.setClassLoader( classLoader );
  +            classLoader = new AntClassLoader(project, classpath);
  +            dvsl.setClassLoader(classLoader);
           }
   
           /*
  @@ -206,25 +212,25 @@
            */
           Object velConfigObj = null;
   
  -        if ( velConfigClass != null )
  +        if (velConfigClass != null)
           {
               try
               {
  -                velConfigObj = Class.forName( velConfigClass, true, classLoader 
).newInstance();
  +                velConfigObj = Class.forName(velConfigClass, true, 
classLoader).newInstance();
   
  -                if ( velConfigObj instanceof Map  )
  +                if (velConfigObj instanceof Map)
                   {
                       velConfigMap = (Map) velConfigObj;
                   }
                   else
                   {
  -                    throw new BuildException("VelocityPropClass is not instanceof 
java.util.Map" );
  +                    throw new BuildException("VelocityPropClass is not instanceof 
java.util.Map");
                   }
               }
  -            catch( Exception ex )
  +            catch(Exception ex)
               {
                   throw new BuildException("Error instantiating VelocityPropClass : "
  -                        + ex );
  +                        + ex);
               }
           }
   
  @@ -232,9 +238,9 @@
            * If any nested Velocity Config elements have been specified, overlay any 
settings
            * in the Velocity Config object.
            */
  -        if ( !velConfigAttr.isEmpty() )
  +        if (!velConfigAttr.isEmpty())
           {
  -            if ( velConfigMap == null )
  +            if (velConfigMap == null)
               {
                  velConfigMap = new HashMap();
               }
  @@ -242,32 +248,32 @@
               /*
                * Now copy velocity config attributes into the Map
                */
  -            for ( Enumeration e = velConfigAttr.elements(); e.hasMoreElements(); )
  +            for (Enumeration e = velConfigAttr.elements(); e.hasMoreElements();)
               {
                   VelocityConfig p = (VelocityConfig)e.nextElement();
  -                velConfigMap.put( p.getName(), p.getValue() );
  +                velConfigMap.put(p.getName(), p.getValue());
               }
           }
   
           /*
            * Finally, set the Velocity Config object in DVSL if it's valid.
            */
  -        if ( velConfigMap != null)
  +        if (velConfigMap != null)
           {
  -            dvsl.setVelocityConfig( velConfigMap );
  +            dvsl.setVelocityConfig(velConfigMap);
           }
   
           /*
            * If a logfile attribute was specified, use that for the log file name,
            * otherwise use a Velocity to Ant logging adapter.
            */
  -        if ( logFile != null )
  +        if (logFile != null)
           {
  -            dvsl.setLogFile( logFile );
  +            dvsl.setLogFile(logFile);
           }
           else
           {
  -            dvsl.setLogSystem( new AntLogSystem( this ) );
  +            dvsl.setLogSystem(new AntLogSystem(this));
           }
   
           /*
  @@ -275,8 +281,8 @@
            */
           try
           {
  -            log( "Loading stylesheet " + stylesheet, Project.MSG_INFO);
  -            dvsl.setStylesheet( stylesheet );
  +            log("Loading stylesheet " + stylesheet, Project.MSG_INFO);
  +            dvsl.setStylesheet(stylesheet);
           }
           catch (Exception ex)
           {
  @@ -291,25 +297,25 @@
   
           try
           {
  -            if ( toolboxFile != null)
  +            if (toolboxFile != null)
               {
  -                toolboxProps.load( new FileInputStream( toolboxFile ) );
  +                toolboxProps.load(new FileInputStream(toolboxFile));
               }
   
               /*
                *  Overlay any parameters
                */
  -            for ( Enumeration e = toolAttr.elements(); e.hasMoreElements(); )
  +            for (Enumeration e = toolAttr.elements(); e.hasMoreElements();)
               {
                   Tool p = (Tool)e.nextElement();
  -                toolboxProps.setProperty( p.getName(), p.getValue() );
  +                toolboxProps.setProperty(p.getName(), p.getValue());
               }
   
  -            dvsl.setToolbox( toolboxProps );
  +            dvsl.setToolbox(toolboxProps);
           }
  -        catch( Exception ee )
  +        catch(Exception ee)
           {
  -            throw new BuildException( "Error loading the toolbox : " + ee );
  +            throw new BuildException("Error loading the toolbox : " + ee);
           }
   
           /*
  @@ -330,7 +336,7 @@
           /*
            *   make sure Source directory exists...
            */
  -        if (destDir == null )
  +        if (destDir == null)
           {
               String msg = "destdir attributes must be set!";
               throw new BuildException(msg);
  @@ -346,7 +352,7 @@
   
           for (int i = 0;i < list.length; ++i)
           {
  -            process( baseDir, list[i], destDir, stylesheet );
  +            process(baseDir, list[i], destDir, stylesheet);
           }
   
           /*
  @@ -360,7 +366,7 @@
   
               for (int i = 0;i < list.length;++i)
               {
  -                process( baseDir, list[i], destDir, stylesheet );
  +                process(baseDir, list[i], destDir, stylesheet);
               }
           }
       } //-- execute
  @@ -476,9 +482,9 @@
        * output is written with UTF-8 encoding.
        * @param encoding Output encoding
        */
  -    public void setOutputEncoding( String encoding )
  +    public void setOutputEncoding(String encoding)
       {
  -        if ( encoding != null )
  +        if (encoding != null)
               this.outputEncoding = encoding;
       }
   
  @@ -517,9 +523,9 @@
        *  Sets the flag to have DVSL use a validating parser for the
        *  input documents
        */
  -    public void setValidatingParser( boolean validating )
  +    public void setValidatingParser(boolean validating)
       {
  -        if ( validating == true )
  +        if (validating == true)
           {
               log("Parser is validating.");
           }
  @@ -546,20 +552,29 @@
               inFile = new File(baseDir,xmlFile);
               int dotPos = xmlFile.lastIndexOf('.');
   
  -            if ( dotPos > 0 )
  +            dvsl.putAppValue(INFILENAME, xmlFile);
  +
  +            String outfilename;
  +
  +            if (dotPos > 0)
               {
  -                outFile = new 
File(destDir,xmlFile.substring(0,xmlFile.lastIndexOf('.'))+fileExt);
  +                outfilename = xmlFile.substring(0, xmlFile.lastIndexOf('.'))
  +                         + fileExt;
  +                outFile = new File(destDir, outfilename);
               }
               else
               {
  -                outFile = new File(destDir,xmlFile+fileExt);
  +                outfilename = xmlFile + fileExt;
  +                outFile = new File(destDir, outfilename);
               }
   
  +            dvsl.putAppValue(OUTFILENAME, outfilename);
  +
               if (force ||
                   inFile.lastModified() > outFile.lastModified() ||
                   styleSheetLastModified > outFile.lastModified())
               {
  -                ensureDirectoryFor( outFile );
  +                ensureDirectoryFor(outFile);
                   log("Processing "+inFile+" to "+outFile, Project.MSG_INFO);
                   long time = transform(inFile, outFile);
                   log("Processed "+inFile+" in "+time+" ms.", Project.MSG_VERBOSE);
  @@ -589,15 +604,19 @@
           try
           {
               long styleSheetLastModified = stylesheet.lastModified();
  -            log("In file "+inFile+" time: " + inFile.lastModified() , 
Project.MSG_DEBUG);
  -            log("Out file "+outFile+" time: " + outFile.lastModified() , 
Project.MSG_DEBUG);
  -            log("Style file "+stylesheet+" time: " + styleSheetLastModified , 
Project.MSG_DEBUG);
  +
  +            log("In file "+inFile+" time: " + inFile.lastModified() ,
  +                    Project.MSG_DEBUG);
  +            log("Out file "+outFile+" time: " + outFile.lastModified() ,
  +                    Project.MSG_DEBUG);
  +            log("Style file "+stylesheet+" time: " + styleSheetLastModified ,
  +                    Project.MSG_DEBUG);
   
               if (force ||
                   inFile.lastModified() > outFile.lastModified() ||
                   styleSheetLastModified > outFile.lastModified())
               {
  -                ensureDirectoryFor( outFile );
  +                ensureDirectoryFor(outFile);
                   log("Processing " + inFile + " to " + outFile, Project.MSG_INFO);
                   long time = transform(inFile, outFile);
                   log("Processed "+inFile+" in "+time+" ms.", Project.MSG_VERBOSE);
  @@ -624,7 +643,7 @@
        * @param outFile File for transformed input
        * @return elapsed time in ms. for transformation
        */
  -    private long transform( File inFile, File outFile )
  +    private long transform(File inFile, File outFile)
           throws Exception
       {
           BufferedWriter writer =
  @@ -632,21 +651,21 @@
                                          new FileOutputStream(outFile),
                                              outputEncoding));
   
  -        long time = dvsl.transform( inFile, writer );
  +        long time = dvsl.transform(inFile, writer);
           writer.close();
           return time;
       }
   
  -    private void ensureDirectoryFor( File targetFile ) throws BuildException
  +    private void ensureDirectoryFor(File targetFile) throws BuildException
       {
  -        File directory = new File( targetFile.getParent() );
  +        File directory = new File(targetFile.getParent());
   
           if (!directory.exists())
           {
               if (!directory.mkdirs())
               {
                   throw new BuildException("Unable to create directory: "
  -                                         + directory.getAbsolutePath() );
  +                                         + directory.getAbsolutePath());
               }
           }
       }
  
  
  
  1.3       +6 -4      
jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/TransformTool.java
  
  Index: TransformTool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/TransformTool.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TransformTool.java        19 Dec 2001 04:27:21 -0000      1.2
  +++ TransformTool.java        17 Mar 2002 21:08:52 -0000      1.3
  @@ -70,16 +70,18 @@
        *  @param xpath  XPath expression to select nodes
        *  @return The rendered result
        */
  -    public String applyTemplates( String xpath ) throws Exception;
  +    public String applyTemplates(String xpath) throws Exception;
   
  -    public String applyTemplates( DVSLNode node ) throws Exception;
  +    public String applyTemplates(DVSLNode node) throws Exception;
   
  -    public String applyTemplates( DVSLNode node, String xpath ) throws Exception;
  +    public String applyTemplates(DVSLNode node, String xpath) throws Exception;
   
       public String applyTemplates() throws Exception;
   
       public String copy() throws Exception;
   
  -    public Object get( String key );
  +    public Object get(String key);
   
  +    public Object getAppValue(Object key);
  +    public Object putAppValue(Object key, Object value);
   }
  
  
  
  1.4       +51 -31    
jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/Transformer.java
  
  Index: Transformer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-dvsl/src/java/org/apache/tools/dvsl/Transformer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Transformer.java  20 Feb 2002 05:25:22 -0000      1.3
  +++ Transformer.java  17 Mar 2002 21:08:52 -0000      1.4
  @@ -56,6 +56,8 @@
   
   import java.util.List;
   import java.util.Stack;
  +import java.util.Map;
  +import java.util.HashMap;
   
   import java.io.Writer;
   import java.io.StringWriter;
  @@ -113,19 +115,28 @@
       private TemplateHandler templateHandler = null;
   
       /**
  +     *  HashMap to hold application values
  +     */
  +
  +    private Map appValue = new HashMap();
  +
  +    /**
        *  Sole public CTOR.  We rely on the caller to give us a
        *  VelocityEngine ready with all macros registered.
        *  The context is the callers context with all tools and 
        *  style drek.
        */
  -    public Transformer( VelocityEngine ve, TemplateHandler th, Context context,
  -                        boolean validate )
  +    public Transformer(VelocityEngine ve, TemplateHandler th,
  +                        Context context, Map applicationValues,
  +                        boolean validate)
       {
           this.ve = ve;
           this.baseContext = context;
           this.templateHandler = th;
   
  -        saxReader = new SAXReader( validate );
  +        appValue = applicationValues;
  +
  +        saxReader = new SAXReader(validate);
       }
   
       /**
  @@ -142,19 +153,19 @@
        *  @param reader XML document char stream
        *  @param writer Writer to output transformation to
        */
  -    long transform( Reader reader, Writer writer )
  +    long transform(Reader reader, Writer writer)
           throws Exception
       {
   
           /*
            *  parse the document
            */
  -        Document document = saxReader.read( reader );
  +        Document document = saxReader.read(reader);
   
  -        return transform( document, writer );
  +        return transform(document, writer);
       }
   
  -    long transform( Document dom4jdoc, Writer writer )
  +    long transform(Document dom4jdoc, Writer writer)
           throws Exception
       {
           /*
  @@ -163,12 +174,12 @@
            *  to do that
            */
   
  -        DVSLNode root = new Dom4jNodeImpl( dom4jdoc );
  +        DVSLNode root = new Dom4jNodeImpl(dom4jdoc);
   
  -        return transform( root, writer );
  +        return transform(root, writer);
       }
   
  -    protected long transform( DVSLNode root, Writer writer)
  +    protected long transform(DVSLNode root, Writer writer)
           throws Exception
       {
           /*
  @@ -176,7 +187,7 @@
             *  interacting with each other
             */
   
  -         currentContext = new DVSLNodeContext( baseContext );
  +         currentContext = new DVSLNodeContext(baseContext);
   
            long start = System.currentTimeMillis();
   
  @@ -185,9 +196,9 @@
            *  and invoke the transformation
            */
   
  -        currentContext.put( "context", this );
  +        currentContext.put("context", this);
   
  -        invoke(  root, writer );
  +        invoke(root, writer);
   
           long end = System.currentTimeMillis();
   
  @@ -195,52 +206,50 @@
       }
   
   
  -    private void invoke(  DVSLNode element, Writer writer )
  +    private void invoke(DVSLNode element, Writer writer)
           throws Exception
       {
           String[] arr = { };
   
  -        currentContext.pushNode( element );
  +        currentContext.pushNode(element);
   
  -        templateHandler.render( element, currentContext, writer );
  +        templateHandler.render(element, currentContext, writer);
   
           currentContext.popNode();
       }
   
  -    public Object get( String key )
  +    public Object get(String key)
       {
  -        return currentContext.get( key );
  +        return currentContext.get(key);
       }
           
  -    public String applyTemplates( DVSLNode node, String xpath )
  +    public String applyTemplates(DVSLNode node, String xpath)
           throws Exception
       {
           /*
            *  get the nodes that was asked for
            */
   
  -        List nodeset = node.selectNodes( xpath );
  +        List nodeset = node.selectNodes(xpath);
   
           StringWriter sw =  new StringWriter();
   
  -        for( int i = 0; i < nodeset.size(); i++)
  +        for (int i = 0; i < nodeset.size(); i++)
           {
  -            //            System.out.println("Apply (" + xpath + " : " + i + " : " 
+ ( (DVSLNode) nodeset.get(i)).getNodeImpl() );
  -
  -            DVSLNode n = (DVSLNode) nodeset.get( i );
  +            DVSLNode n = (DVSLNode) nodeset.get(i);
   
  -            invoke(  n, sw );
  +            invoke(n, sw);
           }
   
           return sw.toString();
       }
   
  -    public String applyTemplates( DVSLNode node )
  +    public String applyTemplates(DVSLNode node)
           throws Exception
       {
           StringWriter sw = new StringWriter();
   
  -        invoke(  node, sw );
  +        invoke(node, sw);
   
           return sw.toString();
       }
  @@ -248,16 +257,16 @@
       public String applyTemplates()
           throws Exception
       {        
  -        return applyTemplates( currentContext.peekNode(), 
  -                               "*|@*|text()|comment()|processing-instruction()" );
  +        return applyTemplates(currentContext.peekNode(),
  +                               "*|@*|text()|comment()|processing-instruction()");
       }
   
  -    public String applyTemplates( String path )
  +    public String applyTemplates(String path)
           throws Exception
       {
           DVSLNode node = currentContext.peekNode();
           
  -        return applyTemplates( node, path );
  +        return applyTemplates(node, path);
       }
   
       public String copy()
  @@ -270,4 +279,15 @@
   
           return node.copy();
       }
  +
  +    public Object getAppValue(Object key)
  +    {
  +        return appValue.get(key);
  +    }
  +
  +    public Object putAppValue(Object key, Object value)
  +    {
  +        return appValue.put(key, value);
  +    }
  +
   }
  
  
  

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

Reply via email to