grchiu      2003/06/18 07:59:46

  Modified:    java/src/org/apache/xml/serializer ToHTMLStream.java
                        ToStream.java ToTextStream.java
                        ToUnknownStream.java
  Log:
  Patch from myself and Brian Minchau ([EMAIL PROTECTED]):
  Added serializer trace mechanisms to flush trace pseudo characters for
  start tag before attributes are added. Fixed serializer trace to generate
  correct pseudo characters for html attributes.
  
  Revision  Changes    Path
  1.15      +21 -13    
xml-xalan/java/src/org/apache/xml/serializer/ToHTMLStream.java
  
  Index: ToHTMLStream.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToHTMLStream.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ToHTMLStream.java 9 Jun 2003 21:50:56 -0000       1.14
  +++ ToHTMLStream.java 18 Jun 2003 14:59:45 -0000      1.15
  @@ -790,7 +790,7 @@
                   // coming right away.  If you want to kill this optimization 
the corresponding
                   // optimization "OPTIMIZE-EMPTY in endElement() must be 
killed too.
                   if (m_tracer != null)
  -                    firePseudoElement(name);
  +                    firePseudoAttributes();
   
                   return;
               }
  @@ -803,7 +803,7 @@
               m_currentElemDepth++; // current element is one element deeper
   
               if (m_tracer != null)
  -                firePseudoElement(name);
  +                firePseudoAttributes();
   
               
               if (elemDesc.is(ElemDesc.HEADELEM))
  @@ -906,7 +906,11 @@
                   // so we need to process any gathered attributes NOW, before 
they go away.
                   int nAttrs = m_attributes.getLength();
                   if (nAttrs > 0)
  -                    processAttributes(nAttrs);
  +                {
  +                    processAttributes(m_writer, nAttrs);
  +                    // clear attributes object for re-use with next element
  +                    m_attributes.clear();
  +                }
                   if (!elemEmpty)
                   {
                       // As per Dave/Paul recommendation 12/06/2000
  @@ -978,6 +982,7 @@
   
       /**
        * Process an attribute.
  +     * @param   writer The writer to write the processed output to.
        * @param   name   The name of the attribute.
        * @param   value   The value of the attribute.
        * @param   elemDesc The description of the HTML element 
  @@ -986,13 +991,12 @@
        * @throws org.xml.sax.SAXException
        */
       protected void processAttribute(
  +        java.io.Writer writer,
           String name,
           String value,
           ElemDesc elemDesc)
           throws IOException
       {
  -
  -        final java.io.Writer writer = m_writer;
           writer.write(' ');
   
           if (   ((value.length() == 0) || value.equalsIgnoreCase(name))
  @@ -1652,17 +1656,17 @@
       }
   
       /**
  -     * If passed in via attribSAX, process the official SAX attributes
  -     * otherwise process the collected attributes from SAX-like
  -     * calls for an element from calls to 
  -     * attribute(String name, String value)
  +     * Process the attributes, which means to write out the currently
  +     * collected attributes to the writer. The attributes are not
  +     * cleared by this method
        * 
  +     * @param writer the writer to write processed attributes to.
        * @param nAttrs the number of attributes in m_attributes 
        * to be processed
        *
        * @throws org.xml.sax.SAXException
        */
  -    public void processAttributes(int nAttrs)
  +    public void processAttributes(java.io.Writer writer, int nAttrs)
           throws IOException,SAXException
       {
               /* 
  @@ -1671,11 +1675,11 @@
               for (int i = 0; i < nAttrs; i++)
               {
                   processAttribute(
  +                    writer,
                       m_attributes.getQName(i),
                       m_attributes.getValue(i),
                       m_elementDesc);
               }
  -            m_attributes.clear();
       }
   
       /**
  @@ -1694,8 +1698,12 @@
                   super.fireStartElem(m_elementName);  
               
               int nAttrs = m_attributes.getLength();   
  -            if (nAttrs>0) 
  -                processAttributes(nAttrs);
  +            if (nAttrs>0)
  +            {
  +                processAttributes(m_writer, nAttrs);
  +                // clear attributes object for re-use with next element
  +                m_attributes.clear();
  +            }
   
               m_writer.write('>');
   
  
  
  
  1.13      +58 -56    
xml-xalan/java/src/org/apache/xml/serializer/ToStream.java
  
  Index: ToStream.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToStream.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ToStream.java     12 Jun 2003 22:23:49 -0000      1.12
  +++ ToStream.java     18 Jun 2003 14:59:45 -0000      1.13
  @@ -1710,7 +1710,7 @@
               }
   
               m_needToOutputDocTypeDecl = false;
  -
  +        
               /* before we over-write the current elementLocalName etc.
                * lets close out the old one (if we still need to)
                */
  @@ -1754,9 +1754,9 @@
           m_startTagOpen = true;
           m_currentElemDepth++; // current element is one element deeper
           m_isprevtext = false;
  -        
  +
                if (m_tracer != null)
  -                     firePseudoElement(name);
  +                     firePseudoAttributes();
       }
   
       /**
  @@ -1858,15 +1858,19 @@
       }
   
       /**
  -     * Process the colleced attributes from SAX- like calls for an element 
from
  -     * calls to addattibute(String name, String value)
  -     * 
  -     * @param nAttrs the number of attributes in m_attributes to be processed
  +     * Process the attributes, which means to write out the currently
  +     * collected attributes to the writer. The attributes are not
  +     * cleared by this method
  +     * 
  +     * @param writer the writer to write processed attributes to.
  +     * @param nAttrs the number of attributes in m_attributes 
  +     * to be processed
  +     *
  +     * @throws java.io.IOException
        * @throws org.xml.sax.SAXException
        */
  -    public void processAttributes(int nAttrs) throws IOException, 
SAXException
  +    public void processAttributes(java.io.Writer writer, int nAttrs) throws 
IOException, SAXException
       {
  -
               /* real SAX attributes are not passed in, so process the 
                * attributes that were collected after the startElement call.
                * _attribVector is a "cheap" list for Stream serializer output
  @@ -1874,7 +1878,6 @@
                */
   
               String encoding = getEncoding();
  -            final java.io.Writer writer = m_writer;
               for (int i = 0; i < nAttrs; i++)
               {
                   // elementAt is JDK 1.1.8
  @@ -1886,12 +1889,6 @@
                   writeAttrString(m_writer, value, encoding);
                   writer.write('\"');
               }
  -
  -            /* The attributes are now processed so clear them out
  -             * so that they don't accumulate from element to element.
  -             * .removeAllElements() is used as it is from JDK 1.1.8
  -             */
  -            m_attributes.clear();
       }
   
       /**
  @@ -1973,7 +1970,11 @@
                       super.fireStartElem(m_elementName);
                   int nAttrs = m_attributes.getLength();
                   if (nAttrs > 0)
  -                    processAttributes(nAttrs);
  +                {
  +                    processAttributes(m_writer, nAttrs);
  +                    // clear attributes object for re-use with next element
  +                    m_attributes.clear();
  +                }
                   if (m_spaceBeforeClose)
                       writer.write(" />");
                   else
  @@ -2354,7 +2355,11 @@
                       super.fireStartElem(m_elementName);
                   int nAttrs = m_attributes.getLength();
                   if (nAttrs > 0)
  -                    processAttributes(nAttrs);
  +                {
  +                    processAttributes(m_writer, nAttrs);
  +                    // clear attributes object for re-use with next element
  +                    m_attributes.clear();
  +                }
                   m_writer.write('>');
               }
               catch (IOException e)
  @@ -2735,7 +2740,7 @@
                */
               m_attributes.setValue(index, value);
               if (old_value != null)
  -                firePseudoElement(m_elementName);
  +                firePseudoAttributes();
   
           }
           else
  @@ -2743,64 +2748,61 @@
               // the attribute doesn't exist yet, create it
               m_attributes.addAttribute(uri, localName, rawName, type, value);
               if (m_tracer != null)
  -                firePseudoElement(m_elementName);
  +                firePseudoAttributes();
           }
   
       }
   
       /**
  -     * To fire off the pseudo characters of elements, as they currently
  +     * To fire off the pseudo characters of attributes, as they currently
        * exist. This method should be called everytime an attribute is added,
        * or when an attribute value is changed, or an element is created.
        */
   
  -    protected void firePseudoElement(String elementName)
  +    protected void firePseudoAttributes()
       {
  -
  -        int nAttrs = m_attributes.getLength();
  -
           if (m_tracer != null)
           {
  -            String encoding = getEncoding();
  -            // make a StringBuffer to write the name="value" pairs to.
  -            StringBuffer sb = new StringBuffer();
  -            
  -            sb.append('<');
  -            sb.append(elementName);
  -
  -            // make a writer that internally appends to the same
  -            // StringBuffer
  -            java.io.Writer writer = new ToStream.WritertoStringBuffer(sb);
  -
               try
               {
  -                for (int i = 0; i < nAttrs; i++)
  +                // flush out the "<elemName" if not already flushed
  +                m_writer.flush();
  +
  +                int nAttrs = m_attributes.getLength();
  +                if (nAttrs > 0)
                   {
  +                    // make a StringBuffer to write the name="value" pairs 
to.
  +                    StringBuffer sb = new StringBuffer();
   
  -                    // for each attribute append the name="value"
  -                    // to the StringBuffer
  -                    final String name = m_attributes.getQName(i);
  -                    final String value = m_attributes.getValue(i);
  -                    sb.append(' ');
  -                    sb.append(name);
  -                    sb.append("=\"");
  -                    writeAttrString(writer, value, encoding);
  -                    sb.append('\"');
  +                    // make a writer that internally appends to the same
  +                    // StringBuffer
  +                    java.io.Writer writer =
  +                        new ToStream.WritertoStringBuffer(sb);
  +
  +                    processAttributes(writer, nAttrs);
  +                    // Don't clear the attributes! 
  +                    // We only want to see what would be written out
  +                    // at this point, we don't want to loose them.
  +
  +                    // convert the StringBuffer to a char array and
  +                    // emit the trace event that these characters "might"
  +                    // be written
  +                    char ch[] = sb.toString().toCharArray();
  +                    m_tracer.fireGenerateEvent(
  +                        SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
  +                        ch,
  +                        0,
  +                        ch.length);
                   }
               }
               catch (IOException ioe)
               {
  +                // ignore ?
  +            }
  +            catch (SAXException se)
  +            {
  +                // ignore ?
               }
  -
  -            // convert the StringBuffer to a char array and
  -            // emit the trace event that these characters "might"
  -            // be written
  -            char ch[] = sb.toString().toCharArray();
  -            m_tracer.fireGenerateEvent(
  -                SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
  -                ch,
  -                0,
  -                ch.length);
           }
       }
   
  
  
  
  1.6       +2 -2      
xml-xalan/java/src/org/apache/xml/serializer/ToTextStream.java
  
  Index: ToTextStream.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToTextStream.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ToTextStream.java 9 Jun 2003 20:49:35 -0000       1.5
  +++ ToTextStream.java 18 Jun 2003 14:59:45 -0000      1.6
  @@ -164,7 +164,7 @@
       // time to fire off startElement event
       if (m_tracer != null) {
           super.fireStartElem(name);
  -        this.firePseudoElement(name);
  +        this.firePseudoAttributes();
       }
       return;
     }
  @@ -588,7 +588,7 @@
                // time to fire off startlement event.
           if (m_tracer != null) {
               super.fireStartElem(elementName);
  -            this.firePseudoElement(elementName);
  +            this.firePseudoAttributes();
           }
           
           return;
  
  
  
  1.3       +33 -3     
xml-xalan/java/src/org/apache/xml/serializer/ToUnknownStream.java
  
  Index: ToUnknownStream.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToUnknownStream.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ToUnknownStream.java      8 Apr 2003 01:53:45 -0000       1.2
  +++ ToUnknownStream.java      18 Jun 2003 14:59:45 -0000      1.3
  @@ -501,14 +501,17 @@
                   
                   // null if not known
                   m_firstElementLocalName = localName;
  -                
  +
  +                if (m_tracer != null)
  +                    firePseudoElement(elementName);
  +                                     
                   /* we don't want to call our own addAttributes, which
                    * merely delegates to the wrapped handler, but we want to
                    * add these attributes to m_attributes. So me must call 
super.
                    * addAttributes() In this case m_attributes is only used 
for the
                    * first element, after that this class totally delegates to 
the
                    * wrapped handler which is either XML or HTML.
  -                 */                
  +                 */
                   if (atts != null)   
                       super.addAttributes(atts);
                   
  @@ -1260,8 +1263,14 @@
       }
   
       public void setTransformer(Transformer t)
  -    {
  +    {        
           m_handler.setTransformer(t);
  +             if ((t instanceof SerializerTrace) &&
  +                     (((SerializerTrace) t).hasTraceListeners())) {
  +                m_tracer = (SerializerTrace) t;
  +             } else {
  +                m_tracer = null;
  +             }        
       }
       public Transformer getTransformer()
       {
  @@ -1286,6 +1295,27 @@
       {
           m_handler.setSourceLocator(locator);
       }
  +
  +     protected void firePseudoElement(String elementName)
  +     {
  +             
  +             if (m_tracer != null) {
  +                     StringBuffer sb = new StringBuffer();
  +                 
  +                     sb.append('<');
  +                     sb.append(elementName);
  +                     
  +                     // convert the StringBuffer to a char array and
  +                     // emit the trace event that these characters "might"
  +                     // be written
  +                     char ch[] = sb.toString().toCharArray();
  +                     m_tracer.fireGenerateEvent(
  +                             
SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
  +                             ch,
  +                             0,
  +                             ch.length);
  +             }
  +     }
   
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to