try { javax.xml.parsers.SAXParserFactory factory= javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware( true ); javax.xml.parsers.SAXParser jaxpParser= factory.newSAXParser(); reader=jaxpParser.getXMLReader();  } catch( javax.xml.parsers.ParserConfigurationException ex ) { throw new org.xml.sax.SAXException( ex ); } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { throw new org.xml.sax.SAXException( ex1.toString() ); } catch( NoSuchMethodError ex2 ) { }
  public Source getAssociatedStylesheet(    Source source, String media, String title, String charset)      throws TransformerConfigurationException  {    String baseID;    InputSource isource = null;    Node node = null;    XMLReader reader = null;    if(source instanceof DOMSource)    {      DOMSource dsource = (DOMSource)source;      node = dsource.getNode();      baseID = dsource.getSystemId();    }    else    {      isource = SAXSource.sourceToInputSource(source);      baseID = isource.getSystemId();    }        // What I try to do here is parse until the first startElement    // is found, then throw a special exception in order to terminate     // the parse.    StylesheetPIHandler handler = new StylesheetPIHandler(baseID, media,                                    title, charset);    try    {      if(null != node)      {        TreeWalker walker = new TreeWalker(handler);        walker.traverse(node);      }      else      {        if (null == reader)        {          reader = XMLReaderFactory.createXMLReader();        }        // Need to set options!        reader.setContentHandler(handler);        reader.parse(isource);      }    }    catch (StopParseException spe)    {      // OK, good.    }    catch (org.xml.sax.SAXException se)    {      throw new TransformerConfigurationException("getAssociatedStylesheets failed", se);    }    catch (IOException ioe)    {      throw new TransformerConfigurationException("getAssociatedStylesheets failed", ioe);    }    return handler.getAssociatedStylesheet();  }