stylesheethandler.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,722 行 · 第 1/4 页

JAVA
1,722
字号
  }  ////////////////////////////////////////////////////////////////////  // Implementation of ContentHandler interface.  ////////////////////////////////////////////////////////////////////  /**   * Receive a Locator object for document events.   * This is called by the parser to push a locator for the   * stylesheet being parsed. The stack needs to be popped   * after the stylesheet has been parsed. We pop in   * popStylesheet.   *   * @param locator A locator for all SAX document events.   * @see org.xml.sax.ContentHandler#setDocumentLocator   * @see org.xml.sax.Locator   */  public void setDocumentLocator(Locator locator)  {    // System.out.println("pushing locator for: "+locator.getSystemId());    m_stylesheetLocatorStack.push(new SAXSourceLocator(locator));  }  /**   * The level of the stylesheet we are at.   */  private int m_stylesheetLevel = -1;  /**   * Receive notification of the beginning of the document.   *   * @see org.xml.sax.ContentHandler#startDocument   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void startDocument() throws org.xml.sax.SAXException  {    m_stylesheetLevel++;    pushSpaceHandling(false);  }  /** m_parsingComplete becomes true when the top-level stylesheet and all   * its included/imported stylesheets have been been fully parsed, as an   * indication that composition/optimization/compilation can begin.   * @see isStylesheetParsingComplete  */  private boolean m_parsingComplete = false;  /**   * Test whether the _last_ endDocument() has been processed.   * This is needed as guidance for stylesheet optimization   * and compilation engines, which generally don't want to start   * until all included and imported stylesheets have been fully   * parsed.   *   * @return true iff the complete stylesheet tree has been built.   */  public boolean isStylesheetParsingComplete()  {    return m_parsingComplete;  }  /**   * Receive notification of the end of the document.   *   * @see org.xml.sax.ContentHandler#endDocument   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void endDocument() throws org.xml.sax.SAXException  {    try    {      if (null != getStylesheetRoot())      {        if (0 == m_stylesheetLevel)          getStylesheetRoot().recompose();              }      else        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEETROOT, null)); //"Did not find the stylesheet root!");      XSLTElementProcessor elemProcessor = getCurrentProcessor();      if (null != elemProcessor)        elemProcessor.startNonText(this);      m_stylesheetLevel--;			            popSpaceHandling();      // WARNING: This test works only as long as stylesheets are parsed      // more or less recursively. If we switch to an iterative "work-list"      // model, this will become true prematurely. In that case,      // isStylesheetParsingComplete() will have to be adjusted to be aware      // of the worklist.      m_parsingComplete = (m_stylesheetLevel < 0);    }    catch (TransformerException te)    {      throw new org.xml.sax.SAXException(te);    }  }    private java.util.Vector m_prefixMappings = new java.util.Vector();  /**   * Receive notification of the start of a Namespace mapping.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions at the start of   * each element (such as allocating a new tree node or writing   * output to a file).</p>   *   * @param prefix The Namespace prefix being declared.   * @param uri The Namespace URI mapped to the prefix.   * @see org.xml.sax.ContentHandler#startPrefixMapping   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void startPrefixMapping(String prefix, String uri)          throws org.xml.sax.SAXException  {    // m_nsSupport.pushContext();    // this.getNamespaceSupport().declarePrefix(prefix, uri);    //m_prefixMappings.add(prefix); // JDK 1.2+ only -sc    //m_prefixMappings.add(uri); // JDK 1.2+ only -sc    m_prefixMappings.addElement(prefix); // JDK 1.1.x compat -sc    m_prefixMappings.addElement(uri); // JDK 1.1.x compat -sc  }  /**   * Receive notification of the end of a Namespace mapping.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions at the start of   * each element (such as allocating a new tree node or writing   * output to a file).</p>   *   * @param prefix The Namespace prefix being declared.   * @see org.xml.sax.ContentHandler#endPrefixMapping   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException  {    // m_nsSupport.popContext();  }  /**   * Flush the characters buffer.   *   * @throws org.xml.sax.SAXException   */  private void flushCharacters() throws org.xml.sax.SAXException  {    XSLTElementProcessor elemProcessor = getCurrentProcessor();    if (null != elemProcessor)      elemProcessor.startNonText(this);  }  /**   * Receive notification of the start of an element.   *   * @param name The element type name.   *   * @param uri The Namespace URI, or an empty string.   * @param localName The local name (without prefix), or empty string if not namespace processing.   * @param rawName The qualified name (with prefix).   * @param attributes The specified or defaulted attributes.   *   * @throws org.xml.sax.SAXException   */  public void startElement(          String uri, String localName, String rawName, Attributes attributes)            throws org.xml.sax.SAXException  {    NamespaceSupport nssupport = this.getNamespaceSupport();    nssupport.pushContext();        int n = m_prefixMappings.size();    for (int i = 0; i < n; i++)     {      String prefix = (String)m_prefixMappings.elementAt(i++);      String nsURI = (String)m_prefixMappings.elementAt(i);      nssupport.declarePrefix(prefix, nsURI);    }    //m_prefixMappings.clear(); // JDK 1.2+ only -sc    m_prefixMappings.removeAllElements(); // JDK 1.1.x compat -sc    m_elementID++;    // This check is currently done for all elements.  We should possibly consider    // limiting this check to xsl:stylesheet elements only since that is all it really    // applies to.  Also, it could be bypassed if m_shouldProcess is already true.    // In other words, the next two statements could instead look something like this:    // if (!m_shouldProcess)    // {    //   if (localName.equals(Constants.ELEMNAME_STYLESHEET_STRING) &&    //       url.equals(Constants.S_XSLNAMESPACEURL))    //   {    //     checkForFragmentID(attributes);    //     if (!m_shouldProcess)    //       return;    //   }    //   else    //     return;    // }     // I didn't include this code statement at this time because in practice     // it is a small performance hit and I was waiting to see if its absence    // caused a problem. - GLP    checkForFragmentID(attributes);    if (!m_shouldProcess)      return;    flushCharacters();        pushSpaceHandling(attributes);    XSLTElementProcessor elemProcessor = getProcessorFor(uri, localName,                                           rawName);    if(null != elemProcessor)  // defensive, for better multiple error reporting. -sb    {      this.pushProcessor(elemProcessor);      elemProcessor.startElement(this, uri, localName, rawName, attributes);    }    else    {      m_shouldProcess = false;      popSpaceHandling();    }                  }  /**   * Receive notification of the end of an element.   *   * @param name The element type name.   * @param attributes The specified or defaulted attributes.   *   * @param uri The Namespace URI, or an empty string.   * @param localName The local name (without prefix), or empty string if not namespace processing.   * @param rawName The qualified name (with prefix).   * @see org.xml.sax.ContentHandler#endElement   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void endElement(String uri, String localName, String rawName)          throws org.xml.sax.SAXException  {    m_elementID--;    if (!m_shouldProcess)      return;    if ((m_elementID + 1) == m_fragmentID)      m_shouldProcess = false;    flushCharacters();        popSpaceHandling();    XSLTElementProcessor p = getCurrentProcessor();    p.endElement(this, uri, localName, rawName);    this.popProcessor();    this.getNamespaceSupport().popContext();  }  /**   * Receive notification of character data inside an element.   *   * @param ch The characters.   * @param start The start position in the character array.   * @param length The number of characters to use from the   *               character array.   * @see org.xml.sax.ContentHandler#characters   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void characters(char ch[], int start, int length)          throws org.xml.sax.SAXException  {    if (!m_shouldProcess)      return;    XSLTElementProcessor elemProcessor = getCurrentProcessor();    XSLTElementDef def = elemProcessor.getElemDef();    if (def.getType() != XSLTElementDef.T_PCDATA)      elemProcessor = def.getProcessorFor(null, "text()");    if (null == elemProcessor)    {      // If it's whitespace, just ignore it, otherwise flag an error.      if (!XMLCharacterRecognizer.isWhiteSpace(ch, start, length))        error(          XSLMessages.createMessage(XSLTErrorResources.ER_NONWHITESPACE_NOT_ALLOWED_IN_POSITION, null),null);//"Non-whitespace text is not allowed in this position in the stylesheet!",              }    else      elemProcessor.characters(this, ch, start, length);  }  /**   * Receive notification of ignorable whitespace in element content.   *   * @param ch The whitespace characters.   * @param start The start position in the character array.   * @param length The number of characters to use from the   *               character array.   * @see org.xml.sax.ContentHandler#ignorableWhitespace   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void ignorableWhitespace(char ch[], int start, int length)          throws org.xml.sax.SAXException  {    if (!m_shouldProcess)      return;    getCurrentProcessor().ignorableWhitespace(this, ch, start, length);  }  /**   * Receive notification of a processing instruction.   *   * <p>The Parser will invoke this method once for each processing   * instruction found: note that processing instructions may occur   * before or after the main document element.</p>   *   * <p>A SAX parser should never report an XML declaration (XML 1.0,   * section 2.8) or a text declaration (XML 1.0, section 4.3.1)   * using this method.</p>   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions for each   * processing instruction, such as setting status variables or   * invoking other methods.</p>   *   * @param target The processing instruction target.   * @param data The processing instruction data, or null if   *             none is supplied.   * @see org.xml.sax.ContentHandler#processingInstruction   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void processingInstruction(String target, String data)          throws org.xml.sax.SAXException  {    if (!m_shouldProcess)      return;    // Recreating Scott's kluge:    // A xsl:for-each or xsl:apply-templates may have a special     // PI that tells us not to cache the document.  This PI     // should really be namespaced.    //    String localName = getLocalName(target);    //    String ns = m_stylesheet.getNamespaceFromStack(target);    //    // %REVIEW%: We need a better PI architecture        String prefix="",ns="", localName=target;    int colon=target.indexOf(':');    if(colon>=0)    {      ns=getNamespaceForPrefix(prefix=target.substring(0,colon));      localName=target.substring(colon+1);    }    try    {      // A xsl:for-each or xsl:apply-templates may have a special       // PI that tells us not to cache the document.  This PI       // should really be namespaced... but since the XML Namespaces      // spec never defined namespaces as applying to PI's, and since      // the testcase we're trying to support is inconsistant in whether      // it binds the prefix, I'm going to make this sloppy for      // testing purposes.      if(	 "xalan:doc-cache-off".equals(target) ||	   ("doc-cache-off".equals(localName) &&	    ns.equals("org.apache.xalan.xslt.extensions.Redirect") )	 )      {	if(!(m_elems.peek() instanceof ElemForEach))          throw new TransformerException	    ("xalan:doc-cache-off not allowed here!", 	     getLocator());        ElemForEach elem = (ElemForEach)m_elems.peek();        elem.m_doc_cache_off = true;	//System.out.println("JJK***** Recognized <? {"+ns+"}"+prefix+":"+localName+" "+data+"?>");      }    }    catch(Exception e)    {      // JJK: Officially, unknown PIs can just be ignored.      // Do we want to issue a warning?    }    flushCharacters();    getCurrentProcessor().processingInstruction(this, target, data);  }  /**   * Receive notification of a skipped entity.   *   * <p>By default, do nothing.  Application writers may override this

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?