sboag       99/12/13 00:19:32

  Modified:    src/org/apache/xalan/xslt Stylesheet.java
                        StylesheetRoot.java XSLTEngineImpl.java
  Log:
  Fixed bug where embedded stylesheets would explode, by fixing ability to do 
XPath search before the stylesheet init is called.
  
  Revision  Changes    Path
  1.13      +16 -2     xml-xalan/src/org/apache/xalan/xslt/Stylesheet.java
  
  Index: Stylesheet.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/Stylesheet.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Stylesheet.java   1999/12/03 08:43:14     1.12
  +++ Stylesheet.java   1999/12/13 08:19:31     1.13
  @@ -376,9 +376,11 @@
       while(tokenizer.hasMoreTokens ()) 
       {
         String prefix = tokenizer.nextToken ();
  +      if(prefix.equalsIgnoreCase("#default"))
  +        prefix="";
         String ns = getNamespaceForPrefixFromStack (prefix);
         if(null == ns)
  -        throw new 
SAXException(XSLMessages.createMessage(XSLTErrorResources.ERROR0063, new 
Object[]{prefix}));
  +        throw new 
SAXException(XSLMessages.createMessage(XSLTErrorResources.ERROR0095, new 
Object[]{prefix}));
         excludeResultPrefixes.put(prefix, ns);
       } 
       return excludeResultPrefixes;
  @@ -882,7 +884,7 @@
         String aname = atts.getName(i);
         String value = atts.getValue(i);
         boolean isPrefix = aname.startsWith("xmlns:");
  -      if (aname.equals("xmlns") || isPrefix) 
  +      if (isPrefix || aname.equals("xmlns")) 
         {
           String p = isPrefix ? aname.substring(6) : "";
           NameSpace ns = new NameSpace(p, value);
  @@ -1758,6 +1760,17 @@
     static XPathFactory m_xpathFactory = null;
     
     /**
  +   * Statically init anything to do with XPath.
  +   */
  +  static void initXPath(XSLTEngineImpl processor)
  +  {
  +    m_xpathFactory = 
processor.getXMLProcessorLiaison().getDefaultXPathFactory();
  +    
  +    // m_parserLiaison.setProcessorOwner(this);
  +    m_xpathProcessor = new 
XPathProcessorImpl(processor.getXMLProcessorLiaison());
  +  }
  +  
  +  /**
      * Init anything to do with XPath.
      */
     protected void initXPath(XSLTEngineImpl processor, XPathFactory 
xpathFactory)
  @@ -1773,6 +1786,7 @@
       m_xpath.installFunction("document", new FuncDocument());
       m_xpath.installFunction("format-number", new FuncFormatNumb());
     }
  +
   
     /**
      * The query/pattern-matcher object.
  
  
  
  1.13      +11 -3     xml-xalan/src/org/apache/xalan/xslt/StylesheetRoot.java
  
  Index: StylesheetRoot.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/StylesheetRoot.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- StylesheetRoot.java       1999/12/03 08:43:14     1.12
  +++ StylesheetRoot.java       1999/12/13 08:19:31     1.13
  @@ -564,15 +564,23 @@
     /**
      * The output method as specified in xsl:output.
      */
  -  String m_outputmethod = "xml";
  +  String m_outputmethod = null;
   
     /** 
      * Return the output method that was specified in the stylesheet. 
      * The returned value is one of Formatter.OUTPUT_METH_XML,
      * Formatter.OUTPUT_METH_HTML, or Formatter.OUTPUT_METH_TEXT.
      */
  -  public String getOutputMethod() { return m_outputmethod; }
  +  public String getOutputMethod() { return (null == m_outputmethod) ? "xml" 
: m_outputmethod; }
  +
  +  /** 
  +   * Return the output method that was specified in the stylesheet. 
  +   * The returned value is one of Formatter.OUTPUT_METH_XML,
  +   * Formatter.OUTPUT_METH_HTML, or Formatter.OUTPUT_METH_TEXT.
  +   */
  +  public boolean isOutputMethodSet() { return (null != m_outputmethod); }
     
  +
     /**
      * Set the output method.
      */
  @@ -758,7 +766,7 @@
           // XSLTEngineImpl.error(XSLTErrorResources.ERROR0002, new Object[] 
{name,aname}); //name+" has an illegal attribute: "+aname);
         }
       }
  -    if((m_outputmethod.equals("html")) &&
  +    if((null != m_outputmethod) && (m_outputmethod.equals("html")) &&
          (false == didSpecifyIndent))
       {
         m_indentResult = true;
  
  
  
  1.19      +145 -50   xml-xalan/src/org/apache/xalan/xslt/XSLTEngineImpl.java
  
  Index: XSLTEngineImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/XSLTEngineImpl.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- XSLTEngineImpl.java       1999/12/03 19:01:04     1.18
  +++ XSLTEngineImpl.java       1999/12/13 08:19:31     1.19
  @@ -206,6 +206,11 @@
      * can call startElement.
      */
     protected String m_pendingElementName = null;
  +  
  +  /**
  +   * Flag to tell if a StartDocument event is pending.
  +   */
  +  protected boolean m_pendingStartDoc = false;
   
     /**
      * The pending attributes.  We have to delay the call to
  @@ -848,6 +853,8 @@
         PrefixResolver nsNode = getExecContext().getNamespaceContext();
         // Try a bunch of really ugly stuff to find the fragment.
         // What's the right way to do this?
  +      if(null == Stylesheet.m_xpathFactory)
  +        Stylesheet.initXPath(this);
   
         XObject xobj = Stylesheet.evalXPathStr(getExecContext(), 
"id("+fragID+")",
                                                fragBase, nsNode);
  @@ -2113,7 +2120,7 @@
             Node attr = nnm.item(i);
             String aname = attr.getNodeName();
             boolean isPrefix = aname.startsWith("xmlns:");
  -          if (aname.equals("xmlns") || isPrefix)
  +          if (isPrefix || aname.equals("xmlns"))
             {
               int index = aname.indexOf(':');
               String namespaceOfPrefix = attr.getNodeValue();
  @@ -2152,7 +2159,7 @@
             Node attr = nnm.item(i);
             String aname = attr.getNodeName();
             boolean isPrefix = aname.startsWith("xmlns:");
  -          if (aname.equals("xmlns") || isPrefix)
  +          if (isPrefix || aname.equals("xmlns"))
             {
               String prefix = isPrefix ? aname.substring(6) : "";
               String desturi = getResultNamespaceForPrefix(prefix);
  @@ -2532,26 +2539,29 @@
             double highPreserveScore = XPath.MATCH_SCORE_NONE;
             double highStripScore = XPath.MATCH_SCORE_NONE;
   
  -          if(null != m_stylesheetRoot.m_whitespacePreservingElements)
  +          if(null != m_stylesheetRoot)
             {
  -            int nTests = 
m_stylesheetRoot.m_whitespacePreservingElements.size();
  -            for(int i = 0; i < nTests; i++)
  +            if(null != m_stylesheetRoot.m_whitespacePreservingElements)
               {
  -              XPath matchPat = 
(XPath)m_stylesheetRoot.m_whitespacePreservingElements.elementAt(i);
  -              double score = matchPat.getMatchScore(getExecContext(), 
parent);
  -              if(score > highPreserveScore)
  -                highPreserveScore = score;
  +              int nTests = 
m_stylesheetRoot.m_whitespacePreservingElements.size();
  +              for(int i = 0; i < nTests; i++)
  +              {
  +                XPath matchPat = 
(XPath)m_stylesheetRoot.m_whitespacePreservingElements.elementAt(i);
  +                double score = matchPat.getMatchScore(getExecContext(), 
parent);
  +                if(score > highPreserveScore)
  +                  highPreserveScore = score;
  +              }
               }
  -          }
  -          if(null != m_stylesheetRoot.m_whitespaceStrippingElements)
  -          {
  -            int nTests = 
m_stylesheetRoot.m_whitespaceStrippingElements.size();
  -            for(int i = 0; i < nTests; i++)
  +            if(null != m_stylesheetRoot.m_whitespaceStrippingElements)
               {
  -              XPath matchPat = 
(XPath)m_stylesheetRoot.m_whitespaceStrippingElements.elementAt(i);
  -              double score = matchPat.getMatchScore(getExecContext(), 
parent);
  -              if(score > highStripScore)
  -                highStripScore = score;
  +              int nTests = 
m_stylesheetRoot.m_whitespaceStrippingElements.size();
  +              for(int i = 0; i < nTests; i++)
  +              {
  +                XPath matchPat = 
(XPath)m_stylesheetRoot.m_whitespaceStrippingElements.elementAt(i);
  +                double score = matchPat.getMatchScore(getExecContext(), 
parent);
  +                if(score > highStripScore)
  +                  highStripScore = score;
  +              }
               }
             }
             if((highPreserveScore > XPath.MATCH_SCORE_NONE) || (highStripScore 
> XPath.MATCH_SCORE_NONE))
  @@ -3921,6 +3931,29 @@
       attList.removeAttribute(aname);
       attList.addAttribute(aname, "CDATA", value);
     }
  +  
  +  /**
  +   * Tell if the default namespace is listed in the pending 
  +   * attributes.  *Only* for use to see if the HTML element should 
  +   * be used to indicate a switch to the HTML formatter.
  +   */
  +  private boolean pendingHasDefaultNS()
  +  {
  +    if(null != m_pendingAttributes)
  +    {
  +      int n = m_pendingAttributes.getLength();
  +      for(int i = 0; i < n; i++)
  +      {
  +        if(m_pendingAttributes.getName(i).equals("xmlns"))
  +        {
  +          return true;
  +        }
  +      }
  +    }
  +    return false;
  +  }
  +  
  +  boolean m_mustFlushStartDoc = false;
   
     /**
      * Flush the pending element.
  @@ -3928,7 +3961,42 @@
     protected void flushPending()
       throws SAXException
     {
  -    if(null != m_pendingElementName)
  +    if(m_pendingStartDoc && (null != m_pendingElementName))
  +    {
  +      if(!m_stylesheetRoot.isOutputMethodSet())
  +      {
  +        if(m_pendingElementName.equalsIgnoreCase("html") && 
!pendingHasDefaultNS())
  +        {
  +          FormatterToXML xmlListener;
  +          try
  +          {
  +            xmlListener = (FormatterToXML)m_flistener;
  +            
  +          }
  +          catch(ClassCastException cce)
  +          {
  +            xmlListener = null;
  +          }
  +          if(null != xmlListener)
  +          {
  +            System.out.println("Setting the method automatically to HTML");
  +            m_stylesheetRoot.setOutputMethod("html");
  +            m_flistener = new FormatterToHTML(xmlListener);
  +          }
  +        }
  +      }
  +    }
  +
  +    if(m_pendingStartDoc && m_mustFlushStartDoc)
  +    {
  +      m_pendingStartDoc = false;
  +      m_flistener.startDocument();
  +      if(null != m_traceListeners)
  +        fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  +                                            
GenerateEvent.EVENTTYPE_STARTDOCUMENT));
  +    }
  +    
  +    if((null != m_pendingElementName) && m_mustFlushStartDoc)
       {
         if(null != m_stylesheetRoot.m_cdataSectionElems)
         {
  @@ -3957,7 +4025,6 @@
      */
     class ResultTreeHandler implements DocumentHandler, RawCharacterHandler
     {
  -
       /**
        * To fullfill the FormatterListener interface... not action
        * for the moment.
  @@ -3972,7 +4039,9 @@
       public void startDocument ()
         throws SAXException
       {
  -      m_flistener.startDocument();
  +      m_pendingStartDoc = true;
  +      m_mustFlushStartDoc = false;
  +      // m_flistener.startDocument();
         if(null != m_traceListeners)
           fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
                                               
GenerateEvent.EVENTTYPE_STARTDOCUMENT));
  @@ -3984,6 +4053,7 @@
       public void endDocument ()
         throws SAXException
       {
  +      m_mustFlushStartDoc = true;
         flushPending();
         m_flistener.endDocument();
         if(null != m_traceListeners)
  @@ -3991,7 +4061,7 @@
                                               
GenerateEvent.EVENTTYPE_ENDDOCUMENT));
         m_variableStacks.popCurrentContext();
       }
  -
  +    
       /**
        * Bottleneck the startElement event.
        */
  @@ -4001,6 +4071,7 @@
         flushPending();
         m_resultNameSpaces.push(m_emptyNamespace);
         m_pendingElementName = name;
  +      m_mustFlushStartDoc = true;
       }
   
       /**
  @@ -4019,6 +4090,7 @@
         }
         m_resultNameSpaces.push(m_emptyNamespace);
         m_pendingElementName = name;
  +      m_mustFlushStartDoc = true;
       }
   
       /**
  @@ -4044,31 +4116,46 @@
       public void characters (char ch[], int start, int length)
         throws SAXException
       {
  -      flushPending();
  -      if((null != m_stylesheetRoot.m_cdataSectionElems) &&
  -         !m_cdataStack.isEmpty() && (m_cdataStack.peek() == TRUE))
  +      if(!m_mustFlushStartDoc)
         {
  -        boolean isLexHandler = (m_flistener instanceof LexicalHandler);
  -        if(isLexHandler)
  -          ((LexicalHandler)m_flistener).startCDATA();
  -
  -        m_flistener.characters(ch, start, length);
  -        
  -        if(isLexHandler)
  -          ((LexicalHandler)m_flistener).endCDATA();
  -
  -        if(null != m_traceListeners)
  -          fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  -                                              GenerateEvent.EVENTTYPE_CDATA,
  -                                              ch, start, length));
  +        int n = ch.length;
  +        for(int i = 0; i < n; i++)
  +        {
  +          if(!Character.isSpaceChar(ch[i]))
  +          {
  +            m_mustFlushStartDoc = true;
  +            break;
  +          }
  +        }
         }
  -      else
  +      if(m_mustFlushStartDoc)
         {
  -        m_flistener.characters(ch, start, length);
  -        if(null != m_traceListeners)
  -          fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  -                                              
GenerateEvent.EVENTTYPE_CHARACTERS,
  -                                              ch, start, length));
  +        flushPending();
  +        if((null != m_stylesheetRoot.m_cdataSectionElems) &&
  +           !m_cdataStack.isEmpty() && (m_cdataStack.peek() == TRUE))
  +        {
  +          boolean isLexHandler = (m_flistener instanceof LexicalHandler);
  +          if(isLexHandler)
  +            ((LexicalHandler)m_flistener).startCDATA();
  +
  +          m_flistener.characters(ch, start, length);
  +          
  +          if(isLexHandler)
  +            ((LexicalHandler)m_flistener).endCDATA();
  +
  +          if(null != m_traceListeners)
  +            fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  +                                                
GenerateEvent.EVENTTYPE_CDATA,
  +                                                ch, start, length));
  +        }
  +        else
  +        {
  +          m_flistener.characters(ch, start, length);
  +          if(null != m_traceListeners)
  +            fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  +                                                
GenerateEvent.EVENTTYPE_CHARACTERS,
  +                                                ch, start, length));
  +        }
         }
       }
   
  @@ -4078,6 +4165,7 @@
       public void charactersRaw (char ch[], int start, int length)
         throws SAXException
       {
  +      m_mustFlushStartDoc = true;
         flushPending();
         /*
         if(m_flistener instanceof org.apache.xml.serialize.BaseSerializer)
  @@ -4110,12 +4198,15 @@
       public void ignorableWhitespace (char ch[], int start, int length)
         throws SAXException
       {
  -      flushPending();
  -      m_flistener.ignorableWhitespace(ch, start, length);
  -      if(null != m_traceListeners)
  -        fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  -                                            
GenerateEvent.EVENTTYPE_IGNORABLEWHITESPACE,
  -                                            ch, start, length));
  +      if(m_mustFlushStartDoc)
  +      {
  +        flushPending();
  +        m_flistener.ignorableWhitespace(ch, start, length);
  +        if(null != m_traceListeners)
  +          fireGenerateEvent(new GenerateEvent(XSLTEngineImpl.this,
  +                                              
GenerateEvent.EVENTTYPE_IGNORABLEWHITESPACE,
  +                                              ch, start, length));
  +      }
       }
   
       /**
  @@ -4124,6 +4215,7 @@
       public void processingInstruction (String target, String data)
         throws SAXException
       {
  +      m_mustFlushStartDoc = true;
         flushPending();
         m_flistener.processingInstruction(target, data);
         if(null != m_traceListeners)
  @@ -4137,6 +4229,7 @@
        */
       public void comment(String data) throws SAXException
       {
  +      m_mustFlushStartDoc = true;
         flushPending();
         if(m_flistener instanceof LexicalHandler)
         {
  @@ -4153,6 +4246,7 @@
        */
       public void entityReference(String name) throws SAXException
       {
  +      m_mustFlushStartDoc = true;
         flushPending();
         if(m_flistener instanceof LexicalHandler)
         {
  @@ -4171,6 +4265,7 @@
       public void cdata (char ch[], int start, int length)
         throws SAXException
       {
  +      m_mustFlushStartDoc = true;
         flushPending();
         if((null != m_stylesheetRoot.m_cdataSectionElems) &&
            !m_cdataStack.isEmpty() && (m_cdataStack.peek() == TRUE))
  
  
  

Reply via email to