You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ni...@apache.org on 2002/12/19 00:41:46 UTC

cvs commit: xml-cocoon2/src/java/org/apache/cocoon/generation HTMLGenerator.java tidy.xmap

nicolaken    2002/12/18 15:41:46

  Added:       src/blocks/html/conf tidy.xmap
               src/blocks/html/java/org/apache/cocoon/generation
                        HTMLGenerator.java
  Removed:     src/java/org/apache/cocoon/generation HTMLGenerator.java
                        tidy.xmap
  Log:
  Html block.
  Now uses JTidy, in the future it could use the Necko HTML parser.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/blocks/html/conf/tidy.xmap
  
  Index: tidy.xmap
  ===================================================================
  <?xml version="1.0"?>
  
  <xmap xpath="/sitemap/components/generators"
        unless="generator[@name='html']">
      <map:generator name="html"
                     src="org.apache.cocoon.generation.HTMLGenerator"
                     label="content,data"/>
  </xmap>
  
  
  
  1.1                  xml-cocoon2/src/blocks/html/java/org/apache/cocoon/generation/HTMLGenerator.java
  
  Index: HTMLGenerator.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 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  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.generation;
  
  import org.apache.avalon.excalibur.xml.xpath.XPathProcessor;
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.ResourceNotFoundException;
  import org.apache.cocoon.caching.CacheableProcessingComponent;
  import org.apache.cocoon.components.source.SourceUtil;
  import org.apache.cocoon.components.url.URLFactory;
  import org.apache.cocoon.environment.ObjectModelHelper;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.environment.URLFactorySourceResolver;
  import org.apache.cocoon.xml.XMLUtils;
  import org.apache.cocoon.xml.dom.DOMStreamer;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceException;
  import org.apache.excalibur.source.SourceValidity;
  import org.w3c.dom.NodeList;
  import org.w3c.tidy.Tidy;
  import org.xml.sax.SAXException;
  
  import javax.xml.transform.OutputKeys;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.sax.SAXResult;
  
  import java.io.BufferedInputStream;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.util.Map;
  import java.util.Properties;
  
  /**
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @author <a href="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
   * @version CVS $Id: HTMLGenerator.java,v 1.1 2002/12/18 23:41:45 nicolaken Exp $
   */
  public class HTMLGenerator extends ComposerGenerator
  implements Configurable, CacheableProcessingComponent, Disposable {
  
      /** The  source */
      private Source inputSource;
  
      /** XPATH expression */
      private String xpath = null;
  
      /** XPath Processor */
      private XPathProcessor processor = null;
  
      /** JTidy properties */
      private Properties properties;
  
      public void compose(ComponentManager manager)
      throws ComponentException {
          super.compose( manager );
          this.processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
      }
      
      public void configure(Configuration config) throws ConfigurationException {
  
          String configUrl = config.getChild("jtidy-config").getValue(null);
  
          if(configUrl != null) {
              URLFactory urlFactory = null;
              org.apache.cocoon.environment.Source configSource = null;
              try {
                  urlFactory = (URLFactory)this.manager.lookup(URLFactory.ROLE);
                  URLFactorySourceResolver urlResolver = new URLFactorySourceResolver(urlFactory, this.manager);
                  configSource = urlResolver.resolve(configUrl);
                  if (getLogger().isDebugEnabled()) {
                      getLogger().debug("Loading configuration from " + configSource.getSystemId());
                  }
                  
                  this.properties = new Properties();
                  this.properties.load(configSource.getInputStream());
                  
              } catch (Exception e) {
                  getLogger().warn("Cannot load configuration from " + configUrl);
                  throw new ConfigurationException("Cannot load configuration from " + configUrl, e);
              } finally {
                  this.manager.release(urlFactory);
                  if (configSource != null) {
                      configSource.recycle();
                  }
              }
          }
      }
  
      /**
       * Recycle this component.
       * All instance variables are set to <code>null</code>.
       */
      public void recycle() {
          if (this.inputSource != null) {
              this.resolver.release( this.inputSource );
              this.inputSource = null;
          }
          this.xpath = null;
          super.recycle();
      }
  
      /**
       * Setup the html generator.
       * Try to get the last modification date of the source for caching.
       */
      public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
      throws ProcessingException, SAXException, IOException {
          super.setup(resolver, objectModel, src, par);
  
          Request request = ObjectModelHelper.getRequest(objectModel);
          xpath = request.getParameter("xpath");
          if(xpath == null)
              xpath = par.getParameter("xpath",null);
  
          // append the request parameter to the URL if necessary
          if (par.getParameterAsBoolean("copy-parameters", false)
                  && request.getQueryString() != null) {
              StringBuffer query = new StringBuffer(super.source);
              query.append(super.source.indexOf("?") == -1 ? '?' : '&');
              query.append(request.getQueryString());
              super.source = query.toString();
          }
  
          try {
              this.inputSource = resolver.resolveURI(super.source);
          } catch (SourceException se) {
              throw SourceUtil.handle("Unable to resolve " + super.source, se);
          }
      }
  
      /**
       * Generate the unique key.
       * This key must be unique inside the space of this component.
       * This method must be invoked before the generateValidity() method.
       *
       * @return The generated key or <code>0</code> if the component
       *              is currently not cacheable.
       */
      public java.io.Serializable generateKey() {
          if (this.xpath != null) {
              StringBuffer buffer = new StringBuffer(this.inputSource.getSystemId());
              buffer.append(':').append(this.xpath);
              return buffer.toString();
          } else {
              return this.inputSource.getSystemId();
          }
      }
  
      /**
       * Generate the validity object.
       * Before this method can be invoked the generateKey() method
       * must be invoked.
       *
       * @return The generated validity object or <code>null</code> if the
       *         component is currently not cacheable.
       */
      public SourceValidity generateValidity() {
          return this.inputSource.getValidity();
      }
  
      /**
       * Generate XML data.
       */
      public void generate()
      throws IOException, SAXException, ProcessingException {
          try
          {
              // Setup an instance of Tidy.
              Tidy tidy = new Tidy();
              tidy.setXmlOut(true);
              
              if (this.properties == null) {
              tidy.setXHTML(true);
              } else {
                  tidy.setConfigurationFromProps(this.properties);
              }
  
              //Set Jtidy warnings on-off
              tidy.setShowWarnings(getLogger().isWarnEnabled());
              //Set Jtidy final result summary on-off
              tidy.setQuiet(!getLogger().isInfoEnabled());
              //Set Jtidy infos to a String (will be logged) instead of System.out
              StringWriter stringWriter = new StringWriter();
              PrintWriter errorWriter = new PrintWriter(stringWriter);
              tidy.setErrout(errorWriter);
  
              // Extract the document using JTidy and stream it.
              org.w3c.dom.Document doc = tidy.parseDOM(new BufferedInputStream(this.inputSource.getInputStream()), null);
  
              // FIXME: Jtidy doesn't warn or strip duplicate attributes in same
              // tag; stripping.
              XMLUtils.stripDuplicateAttributes(doc, null);
  
              errorWriter.flush();
              errorWriter.close();
              if(getLogger().isWarnEnabled()){
                 getLogger().warn(stringWriter.toString());
              }
  
              if(xpath != null)
              {
                  Transformer serializer = TransformerFactory.newInstance().newTransformer();
                  serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  
                  NodeList nl = processor.selectNodeList(doc, xpath);
                  int length = nl.getLength();
                  for(int i=0;i<length;i++)
                  {
                      SAXResult result = new SAXResult(this.contentHandler);
                      result.setLexicalHandler(this.lexicalHandler);
                      serializer.transform(new DOMSource(nl.item(i)), result);
                  }
              } else {
                  DOMStreamer streamer = new DOMStreamer(this.contentHandler,this.lexicalHandler);
                  streamer.stream(doc);
              }
          } catch (IOException e){
              throw new ResourceNotFoundException("Could not get resource "
                  + this.inputSource.getSystemId(), e);
          } catch (SAXException e){
              throw e;
          } catch (Exception e){
              throw new ProcessingException("Exception in HTMLGenerator.generate()",e);
          }
      }
  
      public void dispose() {
          if (this.manager != null) {
              this.manager.release((Component)this.processor);
              this.manager = null;
          }
          this.processor = null;
          super.dispose();
      }
  }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org