cziegeler    2003/07/12 06:49:56

  Modified:    lib      jars.xml
               src/blocks/portal/java/org/apache/cocoon/portal/transformation
                        RSSTransformer.java
  Added:       src/blocks/html/lib jtidy-04aug2000r7-dev.jar
  Removed:     src/blocks/html/lib .cvsignore
               lib/optional jtidy-04aug2000r7-dev.jar
  Log:
  Removing direct dependency from portal block to jtidy
  
  Revision  Changes    Path
  1.3       +0 -0      cocoon-2.1/src/blocks/html/lib/jtidy-04aug2000r7-dev.jar
  
        <<Binary file>>
  
  
  1.65      +2 -2      cocoon-2.1/lib/jars.xml
  
  Index: jars.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/lib/jars.xml,v
  retrieving revision 1.64
  retrieving revision 1.65
  diff -u -r1.64 -r1.65
  --- jars.xml  11 Jul 2003 20:25:30 -0000      1.64
  +++ jars.xml  12 Jul 2003 13:49:56 -0000      1.65
  @@ -496,7 +496,7 @@
     <title>Transform HTML to XML</title>
     <description>Tidy is a HTML syntax checker and pretty printer.</description>
     <used-by>HTML generator (html block), RSSTransformer (Portal block)</used-by>
  -  <lib>optional/jtidy-04aug2000r7-dev.jar</lib>
  +  <lib>html/lib/jtidy-04aug2000r7-dev.jar</lib>
     <homepage>http://lempinen.net/sami/jtidy/</homepage>
    </file>
   
  
  
  
  1.2       +108 -34   
cocoon-2.1/src/blocks/portal/java/org/apache/cocoon/portal/transformation/RSSTransformer.java
  
  Index: RSSTransformer.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/portal/java/org/apache/cocoon/portal/transformation/RSSTransformer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RSSTransformer.java       11 Jul 2003 14:17:02 -0000      1.1
  +++ RSSTransformer.java       12 Jul 2003 13:49:56 -0000      1.2
  @@ -50,8 +50,22 @@
   */
   package org.apache.cocoon.portal.transformation;
   
  +import java.io.ByteArrayInputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.util.Map;
  +
  +import org.apache.avalon.framework.component.Component;
  +import org.apache.avalon.framework.component.ComponentException;
  +import org.apache.avalon.framework.parameters.Parameters;
  +import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.components.sax.XMLDeserializer;
  +import org.apache.cocoon.components.sax.XMLSerializer;
  +import org.apache.cocoon.environment.SourceResolver;
   import org.apache.cocoon.transformation.AbstractSAXTransformer;
  -import org.w3c.tidy.Tidy;
  +import org.apache.cocoon.xml.IncludeXMLConsumer;
  +import org.apache.cocoon.xml.XMLConsumer;
  +import org.apache.excalibur.xmlizer.XMLizer;
   import org.xml.sax.Attributes;
   import org.xml.sax.SAXException;
   
  @@ -66,6 +80,18 @@
   public final class RSSTransformer
   extends AbstractSAXTransformer {
   
  +    /** The xmlizer for converting html to xml */
  +    protected XMLizer xmlizer;
  +    
  +    /** The xml serializer */
  +    protected XMLSerializer serializer;
  +    
  +    /** The xml deserializer */
  +    protected XMLDeserializer deserializer;
  +    
  +    /** The filter */
  +    protected XMLConsumer filter;
  +    
       /**
        *  receive notification of start element event.
        **/
  @@ -85,47 +111,95 @@
           if ("description".equals(name)) {
               final String text = this.endTextRecording();
               final String html = "<html><body>"+text+"</body></html>";
  -
  +            
  +            boolean parsed = false;            
               try {
  -                final Tidy xhtmlconvert = new Tidy();
  -                xhtmlconvert.setXmlOut(true);
  -                xhtmlconvert.setXHTML(true);
  -                xhtmlconvert.setShowWarnings(false);
  -                org.w3c.dom.Document doc = xhtmlconvert.parseDOM(new 
java.io.ByteArrayInputStream(html.getBytes()), null);
  -                org.w3c.dom.NodeList node = 
org.apache.cocoon.xml.dom.DOMUtil.selectNodeList(doc, "/html/body/*");
  -                if (null != node) {
  -                    for(int i = 0; i < node.getLength(); i++) {
  -                        this.sendEvents(node.item(i));
  -                    }
  -                } else {
  -                    this.sendTextEvent(text);
  -                }
  -            } catch (Exception e) {
  +                InputStream inputStream = new ByteArrayInputStream(html.getBytes());
  +                this.xmlizer.toSAX(inputStream,
  +                                    "text/html",
  +                                    null,
  +                                    this.serializer);
  +                // if no exception occurs, everything is fine!
  +                parsed = true;
  +            } catch (Exception ignore) {
  +            }
  +            if ( parsed ) {
  +                this.deserializer.setConsumer( this.filter );
  +                this.deserializer.deserialize( this.serializer.getSAXFragment());
  +            } else {
                   this.sendTextEvent(text);
               }
           }
           super.endElement(uri,name,raw);
       }
   
  -    /**
  -     * Replace occurence of searchString in source with replacement. If
  -     * replacement is null remove the occurences.
  +    /* (non-Javadoc)
  +     * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
  +     */
  +    public void recycle() {
  +        this.manager.release( (Component) this.xmlizer );
  +        this.manager.release( this.serializer );
  +        this.manager.release( this.deserializer );
  +        this.xmlizer = null;
  +        this.serializer = null;
  +        this.deserializer = null;
  +        this.filter = null;
  +        super.recycle();
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see 
org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
 java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
        */
  -    public static String replace(String source,
  -                                 String searchString,
  -                                 String replacement) {
  -        if (source != null && searchString != null) {
  -            int pos;
  -            int l = searchString.length();
  -            if (replacement == null) replacement = "";
  -            do {
  -                pos = source.indexOf(searchString);
  -                if (pos != -1) {
  -                    source = source.substring(0, pos) + replacement + 
source.substring(pos + l);
  -                }
  -            } while (pos != -1);
  +    public void setup(SourceResolver resolver,
  +                       Map objectModel,
  +                       String src,
  +                       Parameters par)
  +    throws ProcessingException, SAXException, IOException {
  +        super.setup(resolver, objectModel, src, par);
  +        try {
  +            this.xmlizer = (XMLizer)this.manager.lookup(XMLizer.ROLE);
  +            this.serializer = 
(XMLSerializer)this.manager.lookup(XMLSerializer.ROLE);
  +            this.deserializer = 
(XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
  +        } catch (ComponentException ce) {
  +            throw new ProcessingException("Unable to lookup component.", ce);
           }
  -        return source;
  +    }
  +
  +   class HTMLFilter extends IncludeXMLConsumer {
  +       
  +       int bodyCount = 0;
  +       
  +       public HTMLFilter(XMLConsumer consumer) {
  +           super(consumer);
  +       }
  +
  +       public void startElement(String uri, String local, String qName, Attributes 
attr) throws SAXException {
  +           if (bodyCount > 0 ) {
  +               super.startElement(uri, local, qName, attr);
  +           } 
  +           if ("body".equalsIgnoreCase(local)) {
  +               bodyCount++;
  +           }
  +       }
  +
  +       public void endElement(String uri, String local, String qName) throws 
SAXException {
  +           if ("body".equalsIgnoreCase(local)) {
  +               bodyCount--;
  +           }
  +           if (bodyCount > 0 ) {
  +               super.endElement(uri, local, qName );
  +           } 
  +       }
  +
  +   }
  +
  +    /* (non-Javadoc)
  +     * @see 
org.apache.cocoon.transformation.AbstractSAXTransformer#setupTransforming()
  +     */
  +    public void setupTransforming()
  +        throws IOException, ProcessingException, SAXException {
  +        super.setupTransforming();
  +        this.filter = new HTMLFilter( this.xmlConsumer );
       }
   
   }
  
  
  

Reply via email to