bdelacretaz    2004/04/15 01:39:30

  Modified:    src/blocks/bsf/java/org/apache/cocoon/generation
                        ScriptGenerator.java
  Log:
  Make ContentHandler available to scripts to
  write XML, in addition to existing StringBuffer
  (experimenting with GroovyMarkup, does not work yet)
  
  Revision  Changes    Path
  1.7       +29 -11    
cocoon-2.1/src/blocks/bsf/java/org/apache/cocoon/generation/ScriptGenerator.java
  
  Index: ScriptGenerator.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/bsf/java/org/apache/cocoon/generation/ScriptGenerator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ScriptGenerator.java      4 Apr 2004 04:44:09 -0000       1.6
  +++ ScriptGenerator.java      15 Apr 2004 08:39:30 -0000      1.7
  @@ -135,26 +135,42 @@
               }
               StringBuffer output = new StringBuffer();
   
  +            // make useful objects available to scripts
               mgr.registerBean("resolver", this.resolver);
               mgr.registerBean("source", super.source);
               mgr.registerBean("objectModel", this.objectModel);
               mgr.registerBean("parameters", this.parameters);
  -            mgr.registerBean("output", output);
               mgr.registerBean("logger", getLogger());
   
  -            getLogger().debug("BSFManager execution begining");
  +            // provide both a StringBuffer and ContentHandler to script,
  +            // so that it can provide XML either as a String or as SAX events
  +            mgr.registerBean("output", output);
  +            mgr.registerBean("contentHandler",contentHandler);
   
               // Execute the script
  +            if(getLogger().isDebugEnabled()) {
  +                getLogger().debug("BSFManager execution begining (" + 
inputSource.getURI() + ")");
  +            }
               
mgr.exec(BSFManager.getLangFromFilename(this.inputSource.getURI()),
                        this.inputSource.getURI(), 0, 0, 
IOUtils.getStringFromReader(in));
  +            if(getLogger().isDebugEnabled()) {
  +                getLogger().debug("BSFManager execution complete");
  +            }
   
  -            getLogger().debug("BSFManager execution complete");
  -            getLogger().debug("output = [" + output.toString() + "]");
  +            // If script wrote something to output buffer, use it
  +            if(output.length() > 0) {
  +                if(getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Using String output provided by 
script (" + output.toString() + ")");
  +                }
  +                InputSource xmlInput = new InputSource(new 
StringReader(output.toString()));
  +                parser = (SAXParser)(this.manager.lookup(SAXParser.ROLE));
  +                parser.parse(xmlInput, this.xmlConsumer);
  +            } else {
  +                if(getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Script provided no String output, 
content should have been written to contentHandler");
  +                }
  +            }
   
  -            // Extract the XML string from the BSFManager and parse it
  -            InputSource xmlInput = new InputSource(new 
StringReader(output.toString()));
  -            parser = (SAXParser)(this.manager.lookup(SAXParser.ROLE));
  -            parser.parse(xmlInput, this.xmlConsumer);
           } catch (SourceException se) {
               throw SourceUtil.handle(se);
           } catch (FileNotFoundException e) {
  @@ -162,12 +178,14 @@
                   "Could not load script " + this.inputSource.getURI(), e);
           } catch (BSFException e) {
               throw new ProcessingException(
  -                    "Exception in ScriptGenerator.generate()", e);
  +                    "BSFException in ScriptGenerator.generate()", e);
           } catch (Exception e) {
               throw new ProcessingException(
                       "Exception in ScriptGenerator.generate()", e);
           } finally {
  -            this.manager.release(parser);
  +            if(parser!=null) {
  +                this.manager.release(parser);
  +            }
           }
       }
   }
  
  
  

Reply via email to