You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Duncan Werner <du...@pagea.com> on 2000/02/27 21:38:29 UTC

dynamic file includes in xsp

Is there a way to do file includes in XSP pages? Specifically, I'd like to
include xml files in an xsp page based on parameters.

Thanks,

Duncan

Re: dynamic file includes in xsp

Posted by Rick Tyler <ri...@ticketweb.com>.
I got it to work in case anyone is interested.

The following "includes" the XML generated by an external
CGI program, invoked with the same arguments as were passed
to the .xml:

  <xsp:page language="java"
xmlns:xsp="http://www.apache.org/1999/XSP/Core">

    <xsp:structure>

      <xsp:include>java.net.*</xsp:include>
      <xsp:include>org.xml.sax.HandlerBase</xsp:include>
      <xsp:include>org.xml.sax.Parser</xsp:include>
      <xsp:include>org.xml.sax.helpers.ParserFactory</xsp:include>

    </xsp:structure>

    <page>

      <xsp:logic>

        class MyHandler extends HandlerBase
        {
          Document document;
          Stack xspNodeStack;
          Node xspCurrentNode, xspParentNode;

          public MyHandler (
            Document _document,
            Stack _xspNodeStack,
            Node _xspParentNode,
            Node _xspCurrentNode )
          {
            document = _document;
            xspNodeStack = _xspNodeStack;
            xspParentNode = _xspParentNode;
            xspCurrentNode = _xspCurrentNode;
          }

          public void startElement (
            String name,
            AttributeList attrList )
          {
            xspParentNode = xspCurrentNode;

            xspNodeStack.push( xspParentNode );

            xspCurrentNode = document.createElement( name );

            xspParentNode.appendChild( xspCurrentNode );

            if( attrList != null )
            {
              int numAttr = attrList.getLength();

              <![CDATA[
              for( int i = 0; i < numAttr; i++ )
              { ]]>
                ((Element) xspCurrentNode).setAttribute(
                  attrList.getName( i ),
                  attrList.getValue( i ) );
              }
            }
          }

          public void characters (
            char [] buf,
            int start,
            int length )
          {
            String text = new String( buf, start, length );

            xspCurrentNode.appendChild( document.createTextNode(
              text ) );
          }

          public void endElement ( String name )
          {
            xspCurrentNode = (Node) xspNodeStack.pop();
          }
        }

        MyHandler myHandler = new MyHandler(
          document, xspNodeStack, xspParentNode, xspCurrentNode );

        Parser parser = ParserFactory.makeParser(
          "org.apache.xerces.parsers.SAXParser" );

        parser.setDocumentHandler( myHandler );

        parser.setErrorHandler( myHandler );

        String url = "http://localhost/mycgi";

        String query = request.getQueryString();

        if( query != null )
          url += "?" + query;

        parser.parse( url );

      </xsp:logic>
    
    </page>

  </xsp:page>

- RT

Re: dynamic file includes in xsp

Posted by Mike Engelhart <me...@earthtrip.com>.
Rick Tyler wrote:

> Having explored a similar problem at great length over the past
> couple of weeks, I believe the only way to do what you want is
> to instantiate your own xml parser in order to parse the included
> XML and add to the DOM subtree which XSP must pass on to the next
> processing layer (i.e. XSLT).
I haven't tried this but I don't think you have to instantiate your own
parser as there is an "xspParser" variable implicitly defined in every XSP
page for your use.  Is that what you mean?

*****

Re: dynamic file includes in xsp

Posted by Rick Tyler <ri...@ticketweb.com>.
Duncan Werner wrote:
> 
> Is there a way to do file includes in XSP pages? Specifically, I'd like to
> include xml files in an xsp page based on parameters.

Having explored a similar problem at great length over the past
couple of weeks, I believe the only way to do what you want is
to instantiate your own xml parser in order to parse the included
XML and add to the DOM subtree which XSP must pass on to the next
processing layer (i.e. XSLT).

I have just started working on this myselft, but I don't have any
code to share yet.

Let me know if you want to collaborate.

- RT