You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Terry Anderson <ta...@highergear.com> on 2002/07/23 19:08:40 UTC

VELOCITY (XML) - COCOON (PDF)

Looking for help posting xml (created with velocity) to cocoon to create
a pdf. I successfully create the pdf with the file-generator (xml file
saved to disk), but having difficulty with the stream-generator. I've
attached my code for review (sorry for length).

Thanks for any assistance!   TA

JAVA CLASS (Posting velocity xml to cocoon stream-generator): package
velocityxml;

import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.net.*;
import org.apache.velocity.*;
import org.apache.cocoon.util.*;
import org.apache.velocity.app.VelocityEngine;

public class data2xml {
    public static void main(String[] args) throws Exception {
        VelocityEngine ve = new VelocityEngine();
        ve.init();

        ArrayList alHeaders = new ArrayList();
        ArrayList alRows = new ArrayList();
        ArrayList alFields;

        /*  create headers  */
        alHeaders.add("Name");
        alHeaders.add("City");
        alHeaders.add("State");

        /*  create row 1  */
        alFields = new ArrayList();
        alFields.add("Angelina");
        alFields.add("Carol Stream");
        alFields.add("IL");
        alRows.add(alFields);

        /*  create row 2  */
        alFields = new ArrayList();
        alFields.add("Ben");
        alFields.add("Wheaton");
        alFields.add("IL");
        alRows.add(alFields);

        /*  create row 3  */
        alFields = new ArrayList();
        alFields.add("Eric");
        alFields.add("Palatine");
        alFields.add("IL");
        alRows.add(alFields);


        VelocityContext context = new VelocityContext();
        context.put("hdrList", alHeaders);
        context.put("rowList", alRows);

        Template t = ve.getTemplate( "data2xml.vm" );
        ByteArrayOutputStream xml_os = null;
        Writer writer = null;
        try {
   xml_os = new ByteArrayOutputStream();
   writer = new OutputStreamWriter(xml_os);
   t.merge(context,writer);
  } catch(IOException ioe) {
            System.out.println("IOException Caught A:");
            ioe.printStackTrace();
  } finally {
   if (writer!=null) {
    writer.close();
   }
   if (xml_os!=null) {
    xml_os.close();
   }
  }

        URL url = new
URL("http://localhost:8080/cocoon/samples/genRpt/rpt.pdf2");
  HttpURLConnection httpConn = null;
  try {
   httpConn = (HttpURLConnection)url.openConnection();
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setRequestMethod("POST");
            httpConn.setUseCaches(false);
            httpConn.setDefaultUseCaches(false);
            httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
            PrintWriter pw = new
PrintWriter(httpConn.getOutputStream());
            String content = "Foo=" +
                URLEncoder.encode(new
String(xml_os.toByteArray()),"UTF-8");
            pw.println(content);
            pw.close();

            InputStream is = httpConn.getInputStream();
            BufferedReader br = new BufferedReader(new
InputStreamReader(is));
            String line = null;
            while((line=br.readLine())!=null) {
                System.out.println(line);
            }
            is.close();

  } catch(IOException ioe) {
            System.out.println("IOException Caught 2:");
            ioe.printStackTrace();
  } finally { }
    }
}

SITEMAP.XMAP:
  <map:match pattern="*.pdf">
      <map:generate type="stream">
          <map:parameter name="form-name" value="Foo"/>
      </map:generate>
      <map:transform type="xslt" src="doc2pdf.xsl"/>
      <map:serialize type="fo2pdf"/>
  </map:match>



---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>