dims        01/07/13 13:01:06

  Modified:    src/org/apache/cocoon/generation Tag: cocoon_20_branch
                        HTMLGenerator.java
               webapp   Tag: cocoon_20_branch sitemap.xmap
               webapp/docs/samples Tag: cocoon_20_branch samples.xml
  Log:
  Added a sample for HTML Generator - It also shows how to get a subset of the HTML 
page using XPath.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.3   +38 -3     xml-cocoon2/src/org/apache/cocoon/generation/HTMLGenerator.java
  
  Index: HTMLGenerator.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/generation/HTMLGenerator.java,v
  retrieving revision 1.4.2.2
  retrieving revision 1.4.2.3
  diff -u -r1.4.2.2 -r1.4.2.3
  --- HTMLGenerator.java        2001/07/07 19:08:16     1.4.2.2
  +++ HTMLGenerator.java        2001/07/13 20:00:52     1.4.2.3
  @@ -18,9 +18,11 @@
   import org.apache.avalon.excalibur.pool.Recyclable;
   import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.parameters.Parameters;
  +import org.apache.cocoon.environment.Request;
   import org.apache.avalon.excalibur.pool.Poolable;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.ResourceNotFoundException;
  +import org.apache.cocoon.Constants;
   import org.apache.cocoon.caching.Cacheable;
   import org.apache.cocoon.caching.CacheValidity;
   import org.apache.cocoon.caching.TimeStampCacheValidity;
  @@ -30,17 +32,29 @@
   import org.apache.cocoon.xml.dom.DOMStreamer;
   import org.w3c.tidy.Tidy;
   import org.xml.sax.SAXException;
  +import javax.xml.transform.Transformer;
  +import javax.xml.transform.TransformerFactory;
  +import javax.xml.transform.dom.DOMSource;
  +import javax.xml.transform.sax.SAXResult;
  +import org.apache.xpath.XPathAPI;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.traversal.NodeIterator;
  +import org.xml.sax.InputSource;
  +import javax.xml.transform.OutputKeys;
   
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]";>Davanum Srinivas</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
  - * @version CVS $Revision: 1.4.2.2 $ $Date: 2001/07/07 19:08:16 $
  + * @version CVS $Revision: 1.4.2.3 $ $Date: 2001/07/13 20:00:52 $
    */
   public class HTMLGenerator extends ComposerGenerator implements Cacheable, 
Recyclable {
   
       /** The  source */
       private Source inputSource;
   
  +    /** XPATH expression */
  +    private String xpath = null;
  +
       /**
        * Recycle this component.
        * All instance variables are set to <code>null</code>.
  @@ -48,6 +62,7 @@
       public void recycle() {
           super.recycle();
           this.inputSource = null;
  +        this.xpath = null;
       }
   
       /**
  @@ -58,6 +73,11 @@
       throws ProcessingException, SAXException, IOException {
           super.setup(resolver, objectModel, src, par);
           this.inputSource = resolver.resolve(super.source);
  +        
  +        Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
  +        xpath = request.getParameter("xpath");
  +        if(xpath == null)
  +            xpath = par.getParameter("xpath",null);
       }
   
       /**
  @@ -104,8 +124,23 @@
   
               // Extract the document using JTidy and stream it.
               org.w3c.dom.Document doc = tidy.parseDOM(new 
BufferedInputStream(this.inputSource.getInputStream()), null);
  -            DOMStreamer streamer = new 
DOMStreamer(this.contentHandler,this.lexicalHandler);
  -            streamer.stream(doc);
  +            if(xpath != null)
  +            {
  +                Transformer serializer = 
TransformerFactory.newInstance().newTransformer();
  +                serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, 
"yes");
  +
  +                NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath);
  +                Node n;
  +                while ((n = nl.nextNode())!= null)
  +                {   
  +                    SAXResult result = new SAXResult(this.contentHandler);
  +                    result.setLexicalHandler(this.lexicalHandler);
  +                    serializer.transform(new DOMSource(n), result);
  +                }
  +            } else {
  +                DOMStreamer streamer = new 
DOMStreamer(this.contentHandler,this.lexicalHandler);
  +                streamer.stream(doc);
  +            }
           } catch (IOException e){
               getLogger().warn("HTMLGenerator.generate()", e);
               throw new ResourceNotFoundException("Could not get Resource for 
HTMLGenerator", e);
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.11.2.18 +10 -0     xml-cocoon2/webapp/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/webapp/sitemap.xmap,v
  retrieving revision 1.11.2.17
  retrieving revision 1.11.2.18
  diff -u -r1.11.2.17 -r1.11.2.18
  --- sitemap.xmap      2001/07/13 15:16:09     1.11.2.17
  +++ sitemap.xmap      2001/07/13 20:00:57     1.11.2.18
  @@ -570,6 +570,16 @@
       <map:serialize/>
      </map:match>
   
  +   <!-- ========================== Tidy Samples ============================== -->
  +   
  +   <map:match pattern="yahoo">
  +    <map:generate type="html" src="http://www.yahoo.com";>
  +      <map:parameter name="xpath" value="//form/table[3]/tr/td/table[2]"/>
  +    </map:generate>
  +    <map:transform src="stylesheets/news/news.xsl"/>
  +    <map:serialize type="html"/>
  +   </map:match>
  +
      <!-- ========================= Server ================================ -->
      <map:match pattern="generror">
       <map:generate src="docs/samples/error-giving-page.xml"/>
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.5   +3 -0      xml-cocoon2/webapp/docs/samples/samples.xml
  
  Index: samples.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/webapp/docs/samples/samples.xml,v
  retrieving revision 1.3.2.4
  retrieving revision 1.3.2.5
  diff -u -r1.3.2.4 -r1.3.2.5
  --- samples.xml       2001/07/13 13:40:19     1.3.2.4
  +++ samples.xml       2001/07/13 20:01:04     1.3.2.5
  @@ -121,6 +121,9 @@
      <sample name="Parameterised Redirect" 
href="redirect?dest=http://xml.apache.org/";>
       An example of redirecting a client based on a request parameter.
      </sample>
  +   <sample name="Yahoo Screen Scrape" href="yahoo">
  +    An example of HTML Generator scraping a part of the Yahoo main page.
  +   </sample>
     </group>
   
     <group name="Web Applications">
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to