vgritsenko    02/01/21 15:14:27

  Modified:    src/java/org/apache/cocoon/generation HTMLGenerator.java
  Added:       src/java/org/apache/cocoon/xml XMLUtils.java
  Log:
  Bug 5916, Patch from [EMAIL PROTECTED]
  
  Revision  Changes    Path
  1.2       +24 -1     
xml-cocoon2/src/java/org/apache/cocoon/generation/HTMLGenerator.java
  
  Index: HTMLGenerator.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/generation/HTMLGenerator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HTMLGenerator.java        3 Jan 2002 12:31:16 -0000       1.1
  +++ HTMLGenerator.java        21 Jan 2002 23:14:26 -0000      1.2
  @@ -25,6 +25,7 @@
   import org.apache.cocoon.environment.SourceResolver;
   import org.apache.cocoon.util.HashUtil;
   import org.apache.cocoon.xml.dom.DOMStreamer;
  +import org.apache.cocoon.xml.XMLUtils;
   import org.w3c.dom.NodeList;
   import org.w3c.tidy.Tidy;
   import org.xml.sax.SAXException;
  @@ -35,6 +36,8 @@
   import javax.xml.transform.dom.DOMSource;
   import javax.xml.transform.sax.SAXResult;
   import java.io.BufferedInputStream;
  +import java.io.StringWriter;
  +import java.io.PrintWriter;
   import java.io.IOException;
   import java.util.Enumeration;
   import java.util.Map;
  @@ -42,7 +45,8 @@
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]";>Davanum Srinivas</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/01/03 12:31:16 $
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Nicola Ken Barozzi</a>
  + * @version CVS $Revision: 1.2 $ $Date: 2002/01/21 23:14:26 $
    */
   public class HTMLGenerator extends ComposerGenerator implements Cacheable, 
Recyclable, Composable, Disposable {
   
  @@ -166,9 +170,28 @@
               Tidy tidy = new Tidy();
               tidy.setXmlOut(true);
               tidy.setXHTML(true);
  +            //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();
  
  
  
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/xml/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.cocoon.xml;
  
  import java.util.ArrayList;
  
  import org.w3c.dom.NodeList;
  import org.w3c.dom.Node;
  import org.w3c.dom.Element;
  import org.w3c.dom.Attr;
  import org.w3c.dom.Document;
  import org.w3c.dom.NamedNodeMap;
  
  /**
   * XML utility methods
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Nicola Ken Barozzi</a>
   * @created 18 January 2002
   */
  public class XMLUtils{
  
     //using parent because some dom implementations like jtidy are bugged, 
     //cannot get parent or delete child
     public static void stripDuplicateAttributes(Node node, Node parent) {
          // The output depends on the type of the node
          switch(node.getNodeType()) {
          case Node.DOCUMENT_NODE: {       
              Document doc = (Document)node;
              Node child = doc.getFirstChild();   
              while(child != null) {              
                  stripDuplicateAttributes(child, node);          
                  child = child.getNextSibling(); 
              }
              break;
          } 
          case Node.ELEMENT_NODE: {        
              Element elt = (Element) node;
              NamedNodeMap attrs = elt.getAttributes();  
             
              ArrayList nodesToRemove = new ArrayList();
              int nodesToRemoveNum = 0;
              
              for(int i = 0; i < attrs.getLength(); i++) {  
                  Node a = attrs.item(i);
          
                for(int j = 0; j < attrs.getLength(); j++) {  
                    Node b = attrs.item(j);
                       
                    //if there are two attributes with same name
                    if(i!=j&&(a.getNodeName().equals(b.getNodeName())))
                    {
                      nodesToRemove.add(b);
                      nodesToRemoveNum++;
                    }
  
                }
     
              }
              
              for(int i=0;i<nodesToRemoveNum;i++)
              {
                org.w3c.dom.Attr nodeToDelete = (org.w3c.dom.Attr) 
nodesToRemove.get(i);
                org.w3c.dom.Element nodeToDeleteParent =  (org.w3c.dom.Element)node; 
//nodeToDelete.getParentNode();          
                nodeToDeleteParent.removeAttributeNode(nodeToDelete);
              }
              
              nodesToRemove.clear();
  
              Node child = elt.getFirstChild();        
              while(child != null) {                      
                  stripDuplicateAttributes(child, node);                
                  child = child.getNextSibling();        
              }
  
              break;
          }
          default:   
              //do nothing
              break;
          }
      }
  }
  
  
  

----------------------------------------------------------------------
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