jaxp/src/java.xml/share/classes/javax/xml/transform/stream/StreamSource.java
changeset 34983 cab976ee6f21
parent 25868 686eef1e7a79
equal deleted inserted replaced
34918:80f67512daa1 34983:cab976ee6f21
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package javax.xml.transform.stream;
    26 package javax.xml.transform.stream;
    27 
    27 
    28 import java.io.File;
    28 import java.io.File;
       
    29 import java.io.IOException;
    29 import java.io.InputStream;
    30 import java.io.InputStream;
    30 import java.io.Reader;
    31 import java.io.Reader;
       
    32 import javax.xml.transform.Result;
    31 
    33 
    32 import javax.xml.transform.Source;
    34 import javax.xml.transform.Source;
    33 
    35 
    34 /**
    36 /**
    35  * <p>Acts as an holder for a transformation Source in the form
    37  * <p>Acts as an holder for a transformation Source in the form
   231      * will attempt to open a connection to the URI only if
   233      * will attempt to open a connection to the URI only if
   232      * there is no byte stream or character stream specified).</p>
   234      * there is no byte stream or character stream specified).</p>
   233      *
   235      *
   234      * @param systemId The system identifier as a URL string.
   236      * @param systemId The system identifier as a URL string.
   235      */
   237      */
       
   238     @Override
   236     public void setSystemId(String systemId) {
   239     public void setSystemId(String systemId) {
   237         this.systemId = systemId;
   240         this.systemId = systemId;
   238     }
   241     }
   239 
   242 
   240     /**
   243     /**
   241      * Get the system identifier that was set with setSystemId.
   244      * Get the system identifier that was set with setSystemId.
   242      *
   245      *
   243      * @return The system identifier that was set with setSystemId, or null
   246      * @return The system identifier that was set with setSystemId, or null
   244      * if setSystemId was not called.
   247      * if setSystemId was not called.
   245      */
   248      */
       
   249     @Override
   246     public String getSystemId() {
   250     public String getSystemId() {
   247         return systemId;
   251         return systemId;
   248     }
   252     }
   249 
   253 
   250     /**
   254     /**
   257         //converts the URI to string as per rule specified in
   261         //converts the URI to string as per rule specified in
   258         //RFC 2396,
   262         //RFC 2396,
   259         this.systemId = f.toURI().toASCIIString();
   263         this.systemId = f.toURI().toASCIIString();
   260     }
   264     }
   261 
   265 
       
   266     /**
       
   267      * Indicates whether the {@code StreamSource} object is empty. Empty is
       
   268      * defined as follows:
       
   269      * <ul>
       
   270      * <li>All of the input sources, including the public identifier, system
       
   271      * identifier, byte stream, and character stream, are {@code null}.
       
   272      * </li>
       
   273      * <li>The public identifier and system identifier are {@code null}, and
       
   274      * byte and character stream are either {@code null} or contain no byte or
       
   275      * character.
       
   276      * <p>
       
   277      * Note that this method will reset the byte stream if it is provided, or
       
   278      * the character stream if the byte stream is not provided.
       
   279      * </li>
       
   280      * </ul>
       
   281      * <p>
       
   282      * In case of error while checking the byte or character stream, the method
       
   283      * will return false to allow the XML processor to handle the error.
       
   284      *
       
   285      * @return true if the {@code StreamSource} object is empty, false otherwise
       
   286      */
       
   287     @Override
       
   288     public boolean isEmpty() {
       
   289         return (publicId == null && systemId == null && isStreamEmpty());
       
   290     }
       
   291 
       
   292     private boolean isStreamEmpty() {
       
   293         boolean empty = true;
       
   294         try {
       
   295             if (inputStream != null) {
       
   296                 inputStream.reset();
       
   297                 int bytesRead = inputStream.available();
       
   298                 if (bytesRead > 0) {
       
   299                     return false;
       
   300                 }
       
   301             }
       
   302 
       
   303             if (reader != null) {
       
   304                 reader.reset();
       
   305                 int c = reader.read();
       
   306                 reader.reset();
       
   307                 if (c != -1) {
       
   308                     return false;
       
   309                 }
       
   310             }
       
   311         } catch (IOException ex) {
       
   312             //in case of error, return false
       
   313             return false;
       
   314         }
       
   315 
       
   316         return empty;
       
   317     }
       
   318 
   262     //////////////////////////////////////////////////////////////////////
   319     //////////////////////////////////////////////////////////////////////
   263     // Internal state.
   320     // Internal state.
   264     //////////////////////////////////////////////////////////////////////
   321     //////////////////////////////////////////////////////////////////////
   265 
   322 
   266     /**
   323     /**