You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by sb...@apache.org on 2001/01/29 15:30:52 UTC

cvs commit: xml-xalan/java/src/org/apache/xalan/transformer TransformerIdentityImpl.java TransformerImpl.java

sboag       01/01/29 06:30:52

  Modified:    java/src/org/apache/xalan/transformer
                        TransformerIdentityImpl.java TransformerImpl.java
  Log:
  When Xalan creates a FileOutputStream, close the stream before exit.
  
  Revision  Changes    Path
  1.5       +156 -135  xml-xalan/java/src/org/apache/xalan/transformer/TransformerIdentityImpl.java
  
  Index: TransformerIdentityImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/transformer/TransformerIdentityImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TransformerIdentityImpl.java	2001/01/26 17:12:49	1.4
  +++ TransformerIdentityImpl.java	2001/01/29 14:30:50	1.5
  @@ -247,7 +247,8 @@
               fileURL = fileURL.substring(8);
             }
   
  -          serializer.setOutputStream(new java.io.FileOutputStream(fileURL));
  +          m_outputStream = new java.io.FileOutputStream(fileURL);
  +          serializer.setOutputStream(m_outputStream);
           }
           else
             throw new TransformerException("No output specified!");
  @@ -289,175 +290,190 @@
   
       createResultContentHandler(outputTarget);
   
  -    if (source instanceof DOMSource)
  +    try
       {
  -      DOMSource dsource = (DOMSource) source;
  -
  -      m_systemID = dsource.getSystemId();
  -
  -      Node dNode = dsource.getNode();
  -
  -      if (null != dNode)
  +      if (source instanceof DOMSource)
         {
  -        try
  +        DOMSource dsource = (DOMSource) source;
  +  
  +        m_systemID = dsource.getSystemId();
  +  
  +        Node dNode = dsource.getNode();
  +  
  +        if (null != dNode)
           {
  -          if(dNode.getNodeType() != Node.DOCUMENT_NODE)
  -            this.startDocument();
             try
             {
  -            if(dNode.getNodeType() == Node.ATTRIBUTE_NODE)
  +            if(dNode.getNodeType() != Node.DOCUMENT_NODE)
  +              this.startDocument();
  +            try
               {
  -              String data = dNode.getNodeValue();
  -              char[] chars = data.toCharArray();
  -              characters(chars, 0, chars.length);
  +              if(dNode.getNodeType() == Node.ATTRIBUTE_NODE)
  +              {
  +                String data = dNode.getNodeValue();
  +                char[] chars = data.toCharArray();
  +                characters(chars, 0, chars.length);
  +              }
  +              else
  +              {
  +                TreeWalker walker = new TreeWalker(this);
  +                walker.traverse(dNode);
  +              }
               }
  -            else
  +            finally
               {
  -              TreeWalker walker = new TreeWalker(this);
  -              walker.traverse(dNode);
  +              if(dNode.getNodeType() != Node.DOCUMENT_NODE)
  +                this.endDocument();
               }
             }
  -          finally
  +          catch (SAXException se)
             {
  -            if(dNode.getNodeType() != Node.DOCUMENT_NODE)
  -              this.endDocument();
  +            throw new TransformerException(se);
             }
  +  
  +          return;
           }
  -        catch (SAXException se)
  +        else
           {
  -          throw new TransformerException(se);
  +          String messageStr = XSLMessages.createMessage(
  +            XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
  +  
  +          throw new IllegalArgumentException(messageStr);
           }
  -
  -        return;
  -      }
  -      else
  -      {
  -        String messageStr = XSLMessages.createMessage(
  -          XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
  -
  -        throw new IllegalArgumentException(messageStr);
         }
  -    }
  -
  -    InputSource xmlSource = SAXSource.sourceToInputSource(source);
  -
  -    if (null == xmlSource)
  -    {
  -      throw new TransformerException("Can't transform a Source of type "
  -                                     + source.getClass().getName() + "!");
  -    }
  -
  -    if (null != xmlSource.getSystemId())
  -      m_systemID = xmlSource.getSystemId();
  -
  -    try
  -    {
  -      XMLReader reader = null;
  -
  -      if (source instanceof SAXSource)
  -        reader = ((SAXSource) source).getXMLReader();
  -
  -      if (null == reader)
  +  
  +      InputSource xmlSource = SAXSource.sourceToInputSource(source);
  +  
  +      if (null == xmlSource)
  +      {
  +        throw new TransformerException("Can't transform a Source of type "
  +                                       + source.getClass().getName() + "!");
  +      }
  +  
  +      if (null != xmlSource.getSystemId())
  +        m_systemID = xmlSource.getSystemId();
  +  
  +      try
         {
  -
  -        // Use JAXP1.1 ( if possible )      
  +        XMLReader reader = null;
  +  
  +        if (source instanceof SAXSource)
  +          reader = ((SAXSource) source).getXMLReader();
  +  
  +        if (null == reader)
  +        {
  +  
  +          // Use JAXP1.1 ( if possible )      
  +          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){}
  +        }
  +  
  +        if (null == reader)
  +        {
  +          reader = XMLReaderFactory.createXMLReader();
  +        }
  +  
           try
           {
  -          javax.xml.parsers.SAXParserFactory factory =
  -            javax.xml.parsers.SAXParserFactory.newInstance();
  -
  -          factory.setNamespaceAware(true);
  -
  -          javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
  -
  -          reader = jaxpParser.getXMLReader();
  +          reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
  +                            true);
  +          reader.setFeature("http://apache.org/xml/features/validation/dynamic",
  +                            true);
           }
  -        catch (javax.xml.parsers.ParserConfigurationException ex)
  +        catch (org.xml.sax.SAXException se)
           {
  -          throw new org.xml.sax.SAXException(ex);
  +  
  +          // We don't care.
           }
  -        catch (javax.xml.parsers.FactoryConfigurationError ex1)
  +  
  +        // Get the input content handler, which will handle the 
  +        // parse events and create the source tree. 
  +        ContentHandler inputHandler = this;
  +  
  +        reader.setContentHandler(inputHandler);
  +  
  +        if (inputHandler instanceof org.xml.sax.DTDHandler)
  +          reader.setDTDHandler((org.xml.sax.DTDHandler) inputHandler);
  +  
  +        try
           {
  -          throw new org.xml.sax.SAXException(ex1.toString());
  +          if (inputHandler instanceof org.xml.sax.ext.LexicalHandler)
  +            reader.setProperty("http://xml.org/sax/properties/lexical-handler",
  +                               inputHandler);
  +  
  +          if (inputHandler instanceof org.xml.sax.ext.DeclHandler)
  +            reader.setProperty(
  +              "http://xml.org/sax/properties/declaration-handler",
  +              inputHandler);
           }
  -        catch (NoSuchMethodError ex2){}
  -      }
  -
  -      if (null == reader)
  -      {
  -        reader = XMLReaderFactory.createXMLReader();
  -      }
  -
  -      try
  -      {
  -        reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
  -                          true);
  -        reader.setFeature("http://apache.org/xml/features/validation/dynamic",
  -                          true);
  +        catch (org.xml.sax.SAXException se){}
  +  
  +        try
  +        {
  +          if (inputHandler instanceof org.xml.sax.ext.LexicalHandler)
  +            reader.setProperty("http://xml.org/sax/handlers/LexicalHandler",
  +                               inputHandler);
  +  
  +          if (inputHandler instanceof org.xml.sax.ext.DeclHandler)
  +            reader.setProperty("http://xml.org/sax/handlers/DeclHandler",
  +                               inputHandler);
  +        }
  +        catch (org.xml.sax.SAXNotRecognizedException snre){}
  +  
  +        reader.parse(xmlSource);
  +      }
  +      catch (org.apache.xml.utils.WrappedRuntimeException wre)
  +      {
  +        Throwable throwable = wre.getException();
  +  
  +        while (throwable
  +               instanceof org.apache.xml.utils.WrappedRuntimeException)
  +        {
  +          throwable =
  +            ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
  +        }
  +  
  +        throw new TransformerException(wre.getException());
         }
         catch (org.xml.sax.SAXException se)
         {
  -
  -        // We don't care.
  +        throw new TransformerException(se);
         }
  -
  -      // Get the input content handler, which will handle the 
  -      // parse events and create the source tree. 
  -      ContentHandler inputHandler = this;
  -
  -      reader.setContentHandler(inputHandler);
  -
  -      if (inputHandler instanceof org.xml.sax.DTDHandler)
  -        reader.setDTDHandler((org.xml.sax.DTDHandler) inputHandler);
  -
  -      try
  -      {
  -        if (inputHandler instanceof org.xml.sax.ext.LexicalHandler)
  -          reader.setProperty("http://xml.org/sax/properties/lexical-handler",
  -                             inputHandler);
  -
  -        if (inputHandler instanceof org.xml.sax.ext.DeclHandler)
  -          reader.setProperty(
  -            "http://xml.org/sax/properties/declaration-handler",
  -            inputHandler);
  -      }
  -      catch (org.xml.sax.SAXException se){}
  -
  -      try
  +      catch (IOException ioe)
         {
  -        if (inputHandler instanceof org.xml.sax.ext.LexicalHandler)
  -          reader.setProperty("http://xml.org/sax/handlers/LexicalHandler",
  -                             inputHandler);
  -
  -        if (inputHandler instanceof org.xml.sax.ext.DeclHandler)
  -          reader.setProperty("http://xml.org/sax/handlers/DeclHandler",
  -                             inputHandler);
  +        throw new TransformerException(ioe);
         }
  -      catch (org.xml.sax.SAXNotRecognizedException snre){}
  -
  -      reader.parse(xmlSource);
       }
  -    catch (org.apache.xml.utils.WrappedRuntimeException wre)
  +    finally
       {
  -      Throwable throwable = wre.getException();
  -
  -      while (throwable
  -             instanceof org.apache.xml.utils.WrappedRuntimeException)
  +      if(null != m_outputStream)
         {
  -        throwable =
  -          ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
  +        try
  +        {
  +          m_outputStream.close();
  +        }
  +        catch(IOException ioe){}
  +        m_outputStream = null;
         }
  -
  -      throw new TransformerException(wre.getException());
  -    }
  -    catch (org.xml.sax.SAXException se)
  -    {
  -      throw new TransformerException(se);
       }
  -    catch (IOException ioe)
  -    {
  -      throw new TransformerException(ioe);
  -    }
     }
   
     /**
  @@ -1211,6 +1227,11 @@
       if (null != m_resultLexicalHandler)
         m_resultLexicalHandler.comment(ch, start, length);
     }
  +  
  +  /**
  +   * This is null unless we own the stream.
  +   */
  +  private java.io.FileOutputStream m_outputStream = null;
   
     /** The content handler where result events will be sent. */
     private ContentHandler m_resultContentHandler;
  
  
  
  1.78      +17 -1     xml-xalan/java/src/org/apache/xalan/transformer/TransformerImpl.java
  
  Index: TransformerImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/transformer/TransformerImpl.java,v
  retrieving revision 1.77
  retrieving revision 1.78
  diff -u -r1.77 -r1.78
  --- TransformerImpl.java	2001/01/29 11:52:54	1.77
  +++ TransformerImpl.java	2001/01/29 14:30:51	1.78
  @@ -176,6 +176,11 @@
     private Boolean m_reentryGuard = new Boolean(true);
     
     /**
  +   * This is null unless we own the stream.
  +   */
  +  private java.io.FileOutputStream m_outputStream = null;
  +  
  +  /**
      * True if the parser events should be on the main thread,
      * false if not.  Experemental.  Can not be set right now.
      */
  @@ -375,6 +380,16 @@
       if(!m_hasBeenReset)
       {
         m_hasBeenReset = true;
  +      
  +      if(this.m_outputStream != null)
  +      {
  +        try
  +        {
  +          m_outputStream.close();
  +        }
  +        catch(java.io.IOException ioe){}
  +      }
  +      m_outputStream = null;
     
         // I need to look more carefully at which of these really
         // needs to be reset.
  @@ -1053,7 +1068,8 @@
             {
               fileURL = fileURL.substring(8);
             }
  -          serializer.setOutputStream(new java.io.FileOutputStream(fileURL));
  +          m_outputStream = new java.io.FileOutputStream(fileURL);
  +          serializer.setOutputStream(m_outputStream);
           }
           else throw new TransformerException("No output specified!");