On Monday 03 June 2002 01:50 pm, you wrote:
> On 03.Jun.2002 -- 10:11 AM, daniel robinson wrote:
> > Hi,
> >
> > I've been poking around but I'm clueless (yes, generally as well as
> > specifically).

I have some code that simply uses embedded Java in an XSP and a Java 
temp file. This code processes a File that is submitted by the user's 
Applet simulating a FORM with method=PUT.  The user's FILE parameter is 
an XML document produced by the applet. I have to insert the DTD stuff 
because the Serializer I am generating this file from doesn't generate 
the DTD statements.

After the file is pulled into a string and written to a File, I add it 
to my PostgreSQL database as a BLOB.


<?xml version="1.0" encoding="ISO-8859-1"?>

<xsp:page
        language="java"
        create-session="yes"
        xmlns:xsp="http://apache.org/xsp";
        xmlns:session="http://apache.org/xsp/session/2.0";
        xmlns:xsp-request="http://apache.org/xsp/request/2.0";
        xmlns:xsp-response="http://apache.org/xsp/response/2.0";
        xmlns:log="http://apache.org/xsp/log/2.0";>

  <xsp:structure>
    <xsp:include>java.io.*</xsp:include>
    <xsp:include>java.sql.*</xsp:include>
  </xsp:structure>
    <page>
      <content>
              <xsp:logic>
                      File output = null;
    try {
                
        String username = (String)
                <session:get-attribute name="username" default="nobody" />;
                //System.err.println( "username=" + username );

        String filename = 
        <xsp-request:get-parameter name="FILENAME" />;
                //System.err.println( "filename=" + filename );

        String config_data = 
                <xsp-request:get-parameter name="FILE" />;
                //System.err.println( "config_data=" + config_data );

        int position = config_data.indexOf( <![CDATA["<product>"]]> );

        StringBuffer saveString = new StringBuffer( config_data.substring( 0, 
position ) );
        <![CDATA[
        saveString.append( "<!DOCTYPE product [\n" +
                        "<!ELEMENT product (page+)>\n" +
                        "<!ELEMENT children (page+)>\n" +
                        "<!ELEMENT Company EMPTY>\n" +
                        "<!ELEMENT page (#PCDATA | Company | children)*>\n" +
                        "<!ATTLIST page level (1 | 2 | 3 | 4 | 5 | 8 ) #REQUIRED\n" +
                        "       target CDATA #REQUIRED\n" +
                        "       checkbox (true | false) #REQUIRED\n" +
                        ">\n" +
                        "]>\n" );
                        ]]>
        saveString.append( config_data.substring( position) );

        output = File.createTempFile( "phoenix_", ".xml" );

        FileWriter fw   = new FileWriter( output );

        fw.write( saveString.toString() );
        
        fw.close();

    try {

        Class.forName( "org.postgresql.Driver" );

        Connection conn = 
        DriverManager.getConnection( 
                "jdbc:postgresql://192.168.1.100:5432/test", "postgres", "" );
            
        Statement stmt = conn.createStatement();
        
        String update_statement = "UPDATE user_products SET 
product_file=lo_import( \'" + output.getAbsolutePath() + "\') " +
                "  where name = \'" + username + "\'" +
                "       AND productname = \'" + filename + "\'";

        //System.err.println( "Update statement: " + update_statement );
        int rows =  stmt.executeUpdate( update_statement );

        //System.err.println( "Rows updated: " + rows );

        stmt.close();
        conn.close();
    }
    catch(ClassNotFoundException e ) {
        System.err.println( "Caught: " + e );
        e.printStackTrace();    // @@@ ToDo exit properly
    }
    catch(SQLException sqle ) {
        System.err.println( "Caught: " + sqle );
        sqle.printStackTrace(); // @@@ ToDo exit properly
    }
    }
    //output.delete();
    catch( Exception e ) {
        System.err.println( "Caught: " + e );
        e.printStackTrace();
    }
</xsp:logic>
        <session:set-attribute name="TEMPFILENAME">
                <xsp:logic><xsp:expr>output.getAbsolutePath()</xsp:expr></xsp:logic>
        </session:set-attribute>        
        <para>
        TEMPFILENAME=<session:get-attribute name="TEMPFILENAME" />
        </para>
</content>
</page>
</xsp:page>



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

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

Reply via email to