cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject cvs commit: cocoon-2.1/src/blocks/scratchpad/samples/module-source form2.html form4.html sitemap.xmap test.xml
Date Thu, 08 Jan 2004 16:04:57 GMT
danielf     2004/01/08 08:04:57

  Modified:    src/blocks/scratchpad/samples scratchpad-samples.xml
  Added:       src/blocks/scratchpad/conf module-source.xconf
               src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl
                        ModuleSource.java ModuleSourceFactory.java
               src/blocks/scratchpad/samples/module-source form2.html
                        form4.html sitemap.xmap test.xml
  Log:
  Implementation, configuration and samples for ModuleSource. A module
  source is used for reading from a stream or a string that is found
  using an input module.
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/scratchpad/conf/module-source.xconf
  
  Index: module-source.xconf
  ===================================================================
  <?xml version="1.0"?>
  
  <xconf xpath="/cocoon/source-factories" unless="component-instance[@name='module']">
  
      <!-- module pseudo protocol -->
      <component-instance
          name="module"
          class="org.apache.cocoon.components.source.impl.ModuleSourceFactory"/>
  
  </xconf>
  
  
  
  1.1                  cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl/ModuleSource.java
  
  Index: ModuleSource.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.source.impl;
  
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.ByteArrayInputStream;
  import java.net.MalformedURLException;
  import java.util.Map;
  
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceSelector;
  
  import org.apache.excalibur.source.SourceException;
  import org.apache.excalibur.source.impl.AbstractSource;
  
  import org.apache.cocoon.components.modules.input.InputModule;
  
  import org.apache.commons.jxpath.JXPathContext;
  
  
  /**
   * A <code>Source</code> that takes its content from a
   * module.
   * <p>The URI syntax is
   * "module:<input-module>:<attribute-name>[#XPath]",
   * where :
   * <ul>
   * <li>an input-module name is used for finding an input-module for reading data from</li>,
   * <li>"attribute-name" is the name of the attribute found in the module</li>,
   * <li>"XPath" is an XPath that is aplied on the object in the
   * attribute, by using JXPath.</li>
   * </ul>
   * </p>
   *
   * @author <a href="mailto:danielf@nada.kth.se">Daniel Fagerstom</a>
   */
  
  public class ModuleSource
      extends AbstractSource {
  
      private final static String SCHEME = "module";
      private String attributeType;
      private String attributeName;
      private String xPath;
      private ServiceManager manager;
      private Map objectModel;
      private Logger logger;
      
      /**
       * Create a module source from a 'module:' uri and a the object model.
       * <p>The uri is of the form "module:attribute-type:attribute-name#xpath</p>
       */
      public ModuleSource( Map objectModel, String uri,
                           ServiceManager manager, Logger logger )
          throws MalformedURLException, SourceException {
  
          this.objectModel = objectModel;
          this.manager = manager;
          this.logger = logger;
  
          setSystemId( uri );
  
          // Scheme
          int start = 0;
          int end = uri.indexOf( ':' );
          if ( end == -1 )
              throw new MalformedURLException("Malformed uri for module source (cannot find
scheme) : " + uri);
  
          String scheme = uri.substring( start, end );
          if ( !SCHEME.equals( scheme ) )
              throw new MalformedURLException("Malformed uri for a module source : " + uri);
  
          setScheme( scheme );
  
          // Attribute type
          start = end + 1;
          end = uri.indexOf( ':', start );
          if ( end == -1 ) {
              throw new MalformedURLException("Malformed uri for module source (cannot find
attribute type) : " + uri);
          }
          this.attributeType = uri.substring( start, end );
  
          // Attribute name
          start = end + 1;
          end = uri.indexOf( '#', start );
  
          if ( end == -1 )
              end = uri.length();
  
          if ( end == start )
              throw new MalformedURLException("Malformed uri for module source (cannot find
attribute name) : " + uri);
  
          this.attributeName = uri.substring( start, end );
  
          // xpath
          start = end + 1;
          this.xPath = start < uri.length() ? uri.substring( start ) : "";
      }
      
      /**
       * Return an <code>InputStream</code> object to read from the source.
       *
       * @throws IOException if I/O error occured.
       */
      public InputStream getInputStream() throws IOException, SourceException {
          if ( this.logger.isDebugEnabled() ) {
              this.logger.debug( "Getting InputStream for " + getURI() );
          }
  
          Object obj = getInputAttribute( this.attributeType, this.attributeName );
          if ( obj == null )
              throw new SourceException( " The attribute: " + this.attributeName +
                                         " is empty" );
  
          if ( !(this.xPath.length() == 0 || this.xPath.equals( "/" )) ) {
              JXPathContext context = JXPathContext.newContext( obj );
              obj = context.getValue( this.xPath );
  
              if ( obj == null )
                  throw new SourceException( "the xpath: " + this.xPath +
                                             " applied on the attribute: " +
                                             this.attributeName +
                                             " returns null");
          }
  
          if ( obj instanceof InputStream ) {
              return (InputStream)obj;
          } else if ( obj instanceof String ) {
              return new ByteArrayInputStream( ((String)obj).getBytes() );
          } else {
              throw new SourceException( "The object type: " + obj.getClass() +
                                         " could not be serialized as a InputStream " + obj
);
          }
      }
  
      /**
       * Does this source actually exist ?
       *
       * @return true if the resource exists.
       *
       */
      public boolean exists() {
          boolean exists = false;
          try {
              exists = getInputAttribute( this.attributeType, this.attributeName ) != null;
          } catch ( SourceException e ) {
              exists = false;
          }
          return exists;
      }
  
      private Object getInputAttribute( String inputModuleName, String attributeName )
          throws  SourceException {
          Object obj;
          ServiceSelector selector = null;
          InputModule inputModule = null;
          try {
              selector = (ServiceSelector) this.manager.lookup( InputModule.ROLE + "Selector"
);
              inputModule = (InputModule) selector.select( inputModuleName );
              obj = inputModule.getAttribute( attributeName, null, this.objectModel );
  
          } catch ( ServiceException e ) {
              throw new SourceException( "Could not find an InputModule of the type " + 
                                         inputModuleName , e );
          } catch ( ConfigurationException e ) {
              throw new SourceException( "Could not find an attribute: " + attributeName +
                                         " from the InputModule " + inputModuleName, e );
          } finally {
              if ( inputModule != null ) selector.release( inputModule );
              this.manager.release( selector );
          }
  
          return obj;
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl/ModuleSourceFactory.java
  
  Index: ModuleSourceFactory.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.cocoon.components.source.impl;
  
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.util.Map;
  
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceFactory;
  
  import org.apache.cocoon.components.ContextHelper;
  
  /**
   * A factory for 'module:' sources (see {@link ModuleSource}). 
   *
   * @author <a href="mailto:danielf@nada.kth.se">Daniel Fagerstrom</a>
   */
  
  public class ModuleSourceFactory extends AbstractLogEnabled
    implements SourceFactory, Serviceable, Contextualizable, ThreadSafe {
      
      private ServiceManager manager;
      private Context context;
  
      /**
       * Servicable Interface
       */
      public void service( ServiceManager manager ) throws ServiceException {
          this.manager = manager;
      }
  
      /**
       * Contextualizable, get the object model
       */
      public void contextualize( Context context ) throws ContextException {
          this.context = context;
      }
      
  
      /**
       * Get a {@link ModuleSource} object.
       * 
       * @param location   The URI to resolve - this URI includes the scheme.
       * @param parameters this is optional and not used here
       */
      public Source getSource( String location, Map parameters )
          throws IOException, MalformedURLException {
  
          Map objectModel = ContextHelper.getObjectModel( this.context );
          return new ModuleSource( objectModel, location, this.manager, getLogger() );
      }
      
      /**
       * Release a {@link Source} object.
       */
      public void release( Source source ) {
          // Do nothing here
      }
  
  }
  
  
  
  1.9       +39 -1     cocoon-2.1/src/blocks/scratchpad/samples/scratchpad-samples.xml
  
  Index: scratchpad-samples.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/scratchpad/samples/scratchpad-samples.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- scratchpad-samples.xml	29 Dec 2003 16:24:44 -0000	1.8
  +++ scratchpad-samples.xml	8 Jan 2004 16:04:57 -0000	1.9
  @@ -65,4 +65,42 @@
       </sample>
     </group>  
   
  +  <group name="XModule Source">
  +    <sample name="Create"
  +            href="xmodule-source/insert?file=test.xml&amp;url=xmodule:session-attr:test">
  +        Populate a session attribute "test" with a dom tree created from the file "test.xml".
  +    </sample>
  +    <sample name="Read"
  +            href="xmodule-source/test-view?url=xmodule:session-attr:test&amp;xPathIn=page/body">
  +      Read xml from the session attribute "test" at the XPath position "page/body".
  +    </sample>
  +    <sample name="Insert"
  +            href="xmodule-source/insert?file=insert.xml&amp;url=xmodule:session-attr:test&amp;xPathOut=page/body/p">
  +        Insert a dom tree created from the file "insert.xml", at the
  +        XPath position "page/body/p" in the session attribute "test".
  +    </sample>
  +    <sample name="Delete"
  +            href="xmodule-source/delete?url=xmodule:session-attr:test&amp;xPath=page/body/p">
  +        Delete the content at theXPath position "page/body/p" in the
  +        session attribute "test".
  +    </sample>
  +  </group>
  +
  +  <group name="Module Source">
  +    <sample name="Request URI"
  +            href="module-source/test1">
  +      Use a source that reads from a request URI.
  +    </sample>
  +    <sample name="Text field"
  +            href="module-source/test2">
  +      Use a source that reads from a input text field.
  +    </sample>
  +    <sample name="XML text field"
  +            href="module-source/test4">
  +      Read XML data from a text field. Also demonstrates the use of
  +      input from multi part mime posts and require that uploads are
  +      enabled in web.xml.
  +    </sample>
  +  </group>
  +
   </samples>
  
  
  
  1.1                  cocoon-2.1/src/blocks/scratchpad/samples/module-source/form2.html
  
  Index: form2.html
  ===================================================================
  <html>
    <header>
      <title>Form</title>
    </header>
    <body>
      <form action="test2-post" method="post">
        <p>Write something</p>
        <textarea name="text" rows="20" cols="80">
  Some text.
  More text.
        </textarea>
        <input type="submit" value="Send"/>
      </form>
    </body>
  </html>
  
  
  
  1.1                  cocoon-2.1/src/blocks/scratchpad/samples/module-source/form4.html
  
  Index: form4.html
  ===================================================================
  <html>
    <header>
      <title>Form</title>
    </header>
    <body>
      <form action="test4-post" method="post" enctype="multipart/form-data">
        <p>Write an xml document</p>
        <textarea name="text" rows="20" cols="80">
  &lt;?xml version="1.0"?&gt;
  &lt;test&gt;
    &lt;element attr="foo"/&gt;
  &lt;/test&gt;
        </textarea>
        <input type="submit" value="Send"/>
      </form>
    </body>
  </html>
  
  
  
  1.1                  cocoon-2.1/src/blocks/scratchpad/samples/module-source/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  <?xml version="1.0"?>
  
  <!-- CVS $Id: sitemap.xmap,v 1.1 2004/01/08 16:04:57 danielf Exp $ -->
  
  <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
  
    <map:pipelines>
      <map:pipeline>
  
        <map:match pattern="test1">
          <map:generate type="text" src="module:request:requestURI"/>
          <map:transform src="context://stylesheets/system/xml2html.xslt"/>
          <map:serialize/>
        </map:match>
  
        <map:match pattern="test2">
          <map:generate src="form2.html"/>
          <map:serialize/>
        </map:match>
  
        <map:match pattern="test2-post">
          <map:generate type="text" src="module:request-param:text"/>
          <map:transform src="context://stylesheets/system/xml2html.xslt"/>
          <map:serialize/>
        </map:match>
  
        <!-- post an xml file to test3 with e.g.
             curl -T "test.xml" http://localhost:8888/samples/scratchpad/module-source/test3
-->
        <map:match pattern="test3">
          <map:generate src="module:request:inputStream"/>
          <map:serialize type="xml"/>
        </map:match>
  
        <map:match pattern="test4">
          <map:generate src="form4.html"/>
          <map:serialize/>
        </map:match>
  
        <map:match pattern="test4-post">
          <map:generate src="module:raw-request-param:text"/>
          <map:transform src="context://stylesheets/system/xml2html.xslt"/>
          <map:serialize/>
        </map:match>
  
      </map:pipeline>
    </map:pipelines>
  </map:sitemap>
  
  
  
  1.1                  cocoon-2.1/src/blocks/scratchpad/samples/module-source/test.xml
  
  Index: test.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <page>
    <header>
      <title>test</title>
    </header>
    <body>
      <header>test1</header>
      <p>Some text</p>
    </body>
  </page>
  
  
  

Mime
View raw message