1 /* 2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.xml.transform.sax; 27 28 import javax.xml.transform.Source; 29 import javax.xml.transform.stream.StreamSource; 30 31 import org.xml.sax.InputSource; 32 import org.xml.sax.XMLReader; 33 34 /** 35 * <p>Acts as an holder for SAX-style Source.</p> 36 * 37 * <p>Note that XSLT requires namespace support. Attempting to transform an 38 * input source that is not 39 * generated with a namespace-aware parser may result in errors. 40 * Parsers can be made namespace aware by calling the 41 * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p> 42 * 43 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a> 44 * @since 1.4 45 */ 46 public class SAXSource implements Source { 47 48 /** 49 * If {@link javax.xml.transform.TransformerFactory#getFeature} 50 * returns true when passed this value as an argument, 51 * the Transformer supports Source input of this type. 52 */ 53 public static final String FEATURE = 54 "http://javax.xml.transform.sax.SAXSource/feature"; 55 56 /** 57 * <p>Zero-argument default constructor. If this constructor is used, and 58 * no SAX source is set using 59 * {@link #setInputSource(InputSource inputSource)} , then the 60 * <code>Transformer</code> will 61 * create an empty source {@link org.xml.sax.InputSource} using 62 * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p> 63 * 64 * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget) 65 */ 66 public SAXSource() { } 67 68 /** 69 * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader} 70 * and a SAX InputSource. The {@link javax.xml.transform.Transformer} 71 * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself 72 * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call 73 * reader.parse(inputSource). 74 * 75 * @param reader An XMLReader to be used for the parse. 76 * @param inputSource A SAX input source reference that must be non-null 77 * and that will be passed to the reader parse method. 78 */ 79 public SAXSource(XMLReader reader, InputSource inputSource) { 80 this.reader = reader; 81 this.inputSource = inputSource; 82 } 83 84 /** 85 * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>. 86 * The {@link javax.xml.transform.Transformer} or 87 * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a 88 * reader (if setXMLReader is not used), sets itself as 89 * the reader's {@link org.xml.sax.ContentHandler}, and calls 90 * reader.parse(inputSource). 91 * 92 * @param inputSource An input source reference that must be non-null 93 * and that will be passed to the parse method of the reader. 94 */ 95 public SAXSource(InputSource inputSource) { 96 this.inputSource = inputSource; 97 } 98 99 /** 100 * Set the XMLReader to be used for the Source. 101 * 102 * @param reader A valid XMLReader or XMLFilter reference. 103 */ 104 public void setXMLReader(XMLReader reader) { 105 this.reader = reader; 106 } 107 108 /** 109 * Get the XMLReader to be used for the Source. 110 * 111 * @return A valid XMLReader or XMLFilter reference, or null. 112 */ 113 public XMLReader getXMLReader() { 114 return reader; 115 } 116 117 /** 118 * Set the SAX InputSource to be used for the Source. 119 * 120 * @param inputSource A valid InputSource reference. 121 */ 122 public void setInputSource(InputSource inputSource) { 123 this.inputSource = inputSource; 124 } 125 126 /** 127 * Get the SAX InputSource to be used for the Source. 128 * 129 * @return A valid InputSource reference, or null. 130 */ 131 public InputSource getInputSource() { 132 return inputSource; 133 } 134 135 /** 136 * Set the system identifier for this Source. If an input source 137 * has already been set, it will set the system ID or that 138 * input source, otherwise it will create a new input source. 139 * 140 * <p>The system identifier is optional if there is a byte stream 141 * or a character stream, but it is still useful to provide one, 142 * since the application can use it to resolve relative URIs 143 * and can include it in error messages and warnings (the parser 144 * will attempt to open a connection to the URI only if 145 * no byte stream or character stream is specified).</p> 146 * 147 * @param systemId The system identifier as a URI string. 148 */ 149 @Override 150 public void setSystemId(String systemId) { 151 152 if (null == inputSource) { 153 inputSource = new InputSource(systemId); 154 } else { 155 inputSource.setSystemId(systemId); 156 } 157 } 158 159 /** 160 * <p>Get the base ID (URI or system ID) from where URIs 161 * will be resolved.</p> 162 * 163 * @return Base URL for the <code>Source</code>, or <code>null</code>. 164 */ 165 @Override 166 public String getSystemId() { 167 168 if (inputSource == null) { 169 return null; 170 } else { 171 return inputSource.getSystemId(); 172 } 173 } 174 175 /** 176 * The XMLReader to be used for the source tree input. May be null. 177 */ 178 private XMLReader reader; 179 180 /** 181 * <p>The SAX InputSource to be used for the source tree input. 182 * Should not be <code>null</code>.</p> 183 */ 184 private InputSource inputSource; 185 186 /** 187 * Attempt to obtain a SAX InputSource object from a Source 188 * object. 189 * 190 * @param source Must be a non-null Source reference. 191 * 192 * @return An InputSource, or null if Source can not be converted. 193 */ 194 public static InputSource sourceToInputSource(Source source) { 195 196 if (source instanceof SAXSource) { 197 return ((SAXSource) source).getInputSource(); 198 } else if (source instanceof StreamSource) { 199 StreamSource ss = (StreamSource) source; 200 InputSource isource = new InputSource(ss.getSystemId()); 201 202 isource.setByteStream(ss.getInputStream()); 203 isource.setCharacterStream(ss.getReader()); 204 isource.setPublicId(ss.getPublicId()); 205 206 return isource; 207 } else { 208 return null; 209 } 210 } 211 212 /** 213 * Indicates whether the {@code SAXSource} object is empty. Empty is 214 * defined as follows: 215 * <ul> 216 * <li>if the system identifier and {@code InputSource} are {@code null}; 217 * </li> 218 * <li>if the system identifier is {@code null}, and the {@code InputSource} 219 * is empty. 220 * </li> 221 * </ul> 222 * 223 * @return true if the {@code SAXSource} object is empty, false otherwise 224 */ 225 @Override 226 public boolean isEmpty() { 227 return getSystemId() == null && (inputSource == null || inputSource.isEmpty()); 228 } 229 }