You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Jean-Christophe Kermagoret <jc...@babelobjects.com> on 2003/11/19 13:20:24 UTC

InflatedFileGenerator

Hi List,
I wrote a very simple InflatedFileGenerator that takes a zip file and 
extract one item to use it as source for
FileGenerator.

It's useful if you want to use, for example, standart OpenOffice.org 
format .sxw instead of flat xml.
Of course you can use it with .zip format.

Usage example :
...
        <map:match pattern="process-ooo">
            <map:generate type="inflated" src="ooo/presentation.sxw">
                <map:parameter name="directory" value="ooo"/>
                <map:parameter name="file" value="content.xml"/>
            </map:generate>
            <map:transform src="test/ooo-content2sdbk.xsl"/>
            <map:serialize type="xml"/>
        </map:match>
...

where :
* directory is the dir where the file is going to be inflated
* file is the name of the file you want to get

Is there anybody interested ?
Where do I put it ?

Regards,

-- 

Jean-Christophe Kermagoret
jck@BabelObjects.Com




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: InflatedFileGenerator

Posted by Jean-Christophe Kermagoret <jc...@babelobjects.com>.
I didn't know the FileGenerator zipped capabilities ...
Here it is my source. It's pretty simple. It inherits from the 
FileGenerator.

You have to give 4 parameters :
* src : src archive path
* directory : dir where you want to store the unzipped content
* file : file to extract
* newfile : new filename

<pipeline>
        <map:match pattern="process-training-ooo-*">
            <map:generate type="inflated" src="training/{1}.sxw">
                <map:parameter name="directory" value="ooo"/>
                <map:parameter name="file" value="content.xml"/>
                <map:parameter name="newfile" value="{1}.xml"/>
            </map:generate>
            <map:transform src="stylesheets/tranform.xsl"/>
            <map:serialize type="xml"/>
        </map:match>
</pipeline>

Of course, don't forget :
<map:generators>
...
    <map:generator label="content" logger="sitemap.generator.inflated" 
name="inflated" pool-grow="4" pool-max="32" pool-min="8" 
src="org.apache.cocoon.generation.InflatedFileGenerator"/>
...
</map:generators>

Hope that helps.

/*
 * $Id$
 *
 * Copyright @ 2003 BABEL OBJECTS SARL. All Rights Reserved.
 *
 * This software is the proprietary information of BABEL OBJECTS SARL. 
 * Use is subject to license terms.
 *
 */
package org.apache.cocoon.generation;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.http.HttpEnvironment;
import org.apache.cocoon.environment.wrapper.MutableEnvironmentFacade;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * $Id$
 *
 * @author Jean-Christophe Kermagoret
 */
public class InflatedFileGenerator extends FileGenerator {
    /**
     * Setup the file generator.
     * Try to get the last modification date of the source for caching.
     */
    public void setup(SourceResolver resolver, Map objectModel, String 
src, Parameters par)
        throws ProcessingException, SAXException, IOException {
       
        String contextUrl = null;
       
        if (resolver instanceof HttpEnvironment) {
            contextUrl = ((HttpEnvironment) resolver).getContext();
        } else if (resolver instanceof MutableEnvironmentFacade) {   
            contextUrl = ((MutableEnvironmentFacade) resolver).getContext();
        }
        // We cut the first 6 characters corresponding to "file:/"
        String contextDir = 
contextUrl.substring(6,contextUrl.length());       
        super.setup(resolver, objectModel, retrieveFile(contextDir, src, 
par), par);
    }
   
    /*
     * Always retrieves the file, even :
     * if there is none
     * or if there is already one and its date is older than the archive
     * @TODO: cache implementation for extracted file
     */
    public String retrieveFile(String contextDir, String src, Parameters 
par) {
        String tmpDir = null;
        String fileToRetrieve = null;
        String newFileName = null;
        try {
            tmpDir = par.getParameter("directory");
            fileToRetrieve = par.getParameter("file");
            newFileName = par.getParameter("newfile");
        } catch (Exception ex) {
        }

        try {
            // Open the ZIP file
            String inFilename = contextDir + src;
           
            ZipInputStream in = new ZipInputStream(new 
FileInputStream(inFilename));

            // Lookup the right entry
            ZipEntry entry;
            do {
                entry = in.getNextEntry();
            } while (! fileToRetrieve.equals(entry.getName()));

            // Open the output file
            String outFilename = contextDir + tmpDir + "/" + newFileName;
            OutputStream out = new FileOutputStream(outFilename);

            // Transfer bytes from the ZIP file to the output file
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            // Close the streams
            out.close();
            in.close();
           
            return outFilename;
           
        } catch (IOException e) {
        }
       
        return null;
    }
}

Tony Edwards wrote:

> Hi Jean,
> We use the OpenOffice format a fair bit here for other projects. I did 
> write a simple generator to extract the 'content.xml' from the .sxw 
> file but it was a bit of a kludgey hack as my java skills are still a 
> bit infantile!
> I'd be interested in utilising your offering if that's possible.
>
> Thanks,
> tony
>
> Jean-Christophe Kermagoret wrote:
>
>> Hi List,
>> I wrote a very simple InflatedFileGenerator that takes a zip file and 
>> extract one item to use it as source for
>> FileGenerator.
>>
>> It's useful if you want to use, for example, standart OpenOffice.org 
>> format .sxw instead of flat xml.
>> Of course you can use it with .zip format.
>>
>> Usage example :
>> ...
>>        <map:match pattern="process-ooo">
>>            <map:generate type="inflated" src="ooo/presentation.sxw">
>>                <map:parameter name="directory" value="ooo"/>
>>                <map:parameter name="file" value="content.xml"/>
>>            </map:generate>
>>            <map:transform src="test/ooo-content2sdbk.xsl"/>
>>            <map:serialize type="xml"/>
>>        </map:match>
>> ...
>>
>> where :
>> * directory is the dir where the file is going to be inflated
>> * file is the name of the file you want to get
>>
>> Is there anybody interested ?
>> Where do I put it ?
>>
>> Regards,
>>
>>  
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>


-- 

Jean-Christophe Kermagoret
jck@BabelObjects.Com




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: InflatedFileGenerator

Posted by Conal Tuohy <co...@paradise.net.nz>.
Hi Jean-Christophe

As far as I know it's cacheable. The "jar:" protocol is a built-in Java
protocol, not a feature of Cocoon.

Con

> -----Original Message-----
> From: Jean-Christophe Kermagoret [mailto:jck@babelobjects.com]
> Sent: Thursday, 20 November 2003 19:31
> To: users@cocoon.apache.org
> Subject: Re: InflatedFileGenerator
>
>
> Is extracted file of zipped content cached ?
>
> Conal Tuohy wrote:
>
> >You can read zipped content with the regular FileGenerator,
> using the jar:
> >protocol.
> >See http://wiki.cocoondev.org/Wiki.jsp?page=JarProtocolExample


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: InflatedFileGenerator

Posted by Jean-Christophe Kermagoret <jc...@babelobjects.com>.
Is extracted file of zipped content cached ?

Conal Tuohy wrote:

>You can read zipped content with the regular FileGenerator, using the jar:
>protocol.
>See http://wiki.cocoondev.org/Wiki.jsp?page=JarProtocolExample
>
>  
>
>>-----Original Message-----
>>From: Tony Edwards [mailto:tedwards@civica.com.au]
>>Sent: Thursday, 20 November 2003 12:36
>>To: users@cocoon.apache.org
>>Subject: Re: InflatedFileGenerator
>>
>>
>>Hi Jean,
>>We use the OpenOffice format a fair bit here for other
>>projects. I did
>>write a simple generator to extract the 'content.xml' from
>>the .sxw file
>>but it was a bit of a kludgey hack as my java skills are still a bit
>>infantile!
>>I'd be interested in utilising your offering if that's possible.
>>
>>Thanks,
>>tony
>>
>>Jean-Christophe Kermagoret wrote:
>>
>>    
>>
>>>Hi List,
>>>I wrote a very simple InflatedFileGenerator that takes a zip
>>>      
>>>
>>file and
>>    
>>
>>>extract one item to use it as source for
>>>FileGenerator.
>>>
>>>It's useful if you want to use, for example, standart OpenOffice.org
>>>format .sxw instead of flat xml.
>>>Of course you can use it with .zip format.
>>>
>>>Usage example :
>>>...
>>>       <map:match pattern="process-ooo">
>>>           <map:generate type="inflated" src="ooo/presentation.sxw">
>>>               <map:parameter name="directory" value="ooo"/>
>>>               <map:parameter name="file" value="content.xml"/>
>>>           </map:generate>
>>>           <map:transform src="test/ooo-content2sdbk.xsl"/>
>>>           <map:serialize type="xml"/>
>>>       </map:match>
>>>...
>>>
>>>where :
>>>* directory is the dir where the file is going to be inflated
>>>* file is the name of the file you want to get
>>>
>>>Is there anybody interested ?
>>>Where do I put it ?
>>>
>>>Regards,
>>>
>>>
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>>For additional commands, e-mail: users-help@cocoon.apache.org
>>
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>  
>


-- 

Jean-Christophe Kermagoret
jck@BabelObjects.Com




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: InflatedFileGenerator

Posted by Conal Tuohy <co...@paradise.net.nz>.
You can read zipped content with the regular FileGenerator, using the jar:
protocol.
See http://wiki.cocoondev.org/Wiki.jsp?page=JarProtocolExample

> -----Original Message-----
> From: Tony Edwards [mailto:tedwards@civica.com.au]
> Sent: Thursday, 20 November 2003 12:36
> To: users@cocoon.apache.org
> Subject: Re: InflatedFileGenerator
>
>
> Hi Jean,
> We use the OpenOffice format a fair bit here for other
> projects. I did
> write a simple generator to extract the 'content.xml' from
> the .sxw file
> but it was a bit of a kludgey hack as my java skills are still a bit
> infantile!
> I'd be interested in utilising your offering if that's possible.
>
> Thanks,
> tony
>
> Jean-Christophe Kermagoret wrote:
>
> >Hi List,
> >I wrote a very simple InflatedFileGenerator that takes a zip
> file and
> >extract one item to use it as source for
> >FileGenerator.
> >
> >It's useful if you want to use, for example, standart OpenOffice.org
> >format .sxw instead of flat xml.
> >Of course you can use it with .zip format.
> >
> >Usage example :
> >...
> >        <map:match pattern="process-ooo">
> >            <map:generate type="inflated" src="ooo/presentation.sxw">
> >                <map:parameter name="directory" value="ooo"/>
> >                <map:parameter name="file" value="content.xml"/>
> >            </map:generate>
> >            <map:transform src="test/ooo-content2sdbk.xsl"/>
> >            <map:serialize type="xml"/>
> >        </map:match>
> >...
> >
> >where :
> >* directory is the dir where the file is going to be inflated
> >* file is the name of the file you want to get
> >
> >Is there anybody interested ?
> >Where do I put it ?
> >
> >Regards,
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: InflatedFileGenerator

Posted by Tony Edwards <te...@civica.com.au>.
Hi Jean,
We use the OpenOffice format a fair bit here for other projects. I did 
write a simple generator to extract the 'content.xml' from the .sxw file 
but it was a bit of a kludgey hack as my java skills are still a bit 
infantile!
I'd be interested in utilising your offering if that's possible.

Thanks,
tony

Jean-Christophe Kermagoret wrote:

>Hi List,
>I wrote a very simple InflatedFileGenerator that takes a zip file and 
>extract one item to use it as source for
>FileGenerator.
>
>It's useful if you want to use, for example, standart OpenOffice.org 
>format .sxw instead of flat xml.
>Of course you can use it with .zip format.
>
>Usage example :
>...
>        <map:match pattern="process-ooo">
>            <map:generate type="inflated" src="ooo/presentation.sxw">
>                <map:parameter name="directory" value="ooo"/>
>                <map:parameter name="file" value="content.xml"/>
>            </map:generate>
>            <map:transform src="test/ooo-content2sdbk.xsl"/>
>            <map:serialize type="xml"/>
>        </map:match>
>...
>
>where :
>* directory is the dir where the file is going to be inflated
>* file is the name of the file you want to get
>
>Is there anybody interested ?
>Where do I put it ?
>
>Regards,
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org