1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the "License"). You may not use this file except * in compliance with the License. * * You can obtain a copy of the license at * https://jaxp.dev.java.net/CDDLv1.0.html. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * HEADER in each file and include the License file at * https://jaxp.dev.java.net/CDDLv1.0.html * If applicable add the following below this CDDL HEADER * with the fields enclosed by brackets "[]" replaced with * your own identifying information: Portions Copyright * [year] [name of copyright owner] */ /* * $Id: SAXSource.java,v 1.3 2005/11/03 19:34:26 jeffsuttor Exp $ * @(#)SAXSource.java 1.24 06/04/07 * * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. */ package javax.xml.transform.sax; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; /** * <p>Acts as an holder for SAX-style Source.</p> * * <p>Note that XSLT requires namespace support. Attempting to transform an * input source that is not * generated with a namespace-aware parser may result in errors. * Parsers can be made namespace aware by calling the * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p> * * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a> * @version $Revision: 1.3 $, $Date: 2005/11/03 19:34:26 $ */ public class SAXSource implements Source { /** * If {@link javax.xml.transform.TransformerFactory#getFeature} * returns true when passed this value as an argument, * the Transformer supports Source input of this type. */ public static final String FEATURE = "http://javax.xml.transform.sax.SAXSource/feature"; /** * <p>Zero-argument default constructor. If this constructor is used, and * no SAX source is set using * {@link #setInputSource(InputSource inputSource)} , then the * <code>Transformer</code> will * create an empty source {@link org.xml.sax.InputSource} using * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p> * * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget) */ public SAXSource() { } /** * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader} * and a SAX InputSource. The {@link javax.xml.transform.Transformer} * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call * reader.parse(inputSource). * * @param reader An XMLReader to be used for the parse. * @param inputSource A SAX input source reference that must be non-null * and that will be passed to the reader parse method. */ public SAXSource(XMLReader reader, InputSource inputSource) { this.reader = reader; this.inputSource = inputSource; } /** * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>. * The {@link javax.xml.transform.Transformer} or * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a * reader via {@link org.xml.sax.helpers.XMLReaderFactory} * (if setXMLReader is not used), sets itself as * the reader's {@link org.xml.sax.ContentHandler}, and calls * reader.parse(inputSource). * * @param inputSource An input source reference that must be non-null * and that will be passed to the parse method of the reader. */ public SAXSource(InputSource inputSource) { this.inputSource = inputSource; } /** * Set the XMLReader to be used for the Source. * * @param reader A valid XMLReader or XMLFilter reference. */ public void setXMLReader(XMLReader reader) { this.reader = reader; } /** * Get the XMLReader to be used for the Source. * * @return A valid XMLReader or XMLFilter reference, or null. */ public XMLReader getXMLReader() { return reader; } /** * Set the SAX InputSource to be used for the Source. * * @param inputSource A valid InputSource reference. */ public void setInputSource(InputSource inputSource) { this.inputSource = inputSource; } /** * Get the SAX InputSource to be used for the Source. * * @return A valid InputSource reference, or null. */ public InputSource getInputSource() { return inputSource; } /** * Set the system identifier for this Source. If an input source * has already been set, it will set the system ID or that * input source, otherwise it will create a new input source. * * <p>The system identifier is optional if there is a byte stream * or a character stream, but it is still useful to provide one, * since the application can use it to resolve relative URIs * and can include it in error messages and warnings (the parser * will attempt to open a connection to the URI only if * no byte stream or character stream is specified).</p> * * @param systemId The system identifier as a URI string. */ public void setSystemId(String systemId) { if (null == inputSource) { inputSource = new InputSource(systemId); } else { inputSource.setSystemId(systemId); } } /** * <p>Get the base ID (URI or system ID) from where URIs * will be resolved.</p> * * @return Base URL for the <code>Source</code>, or <code>null</code>. */ public String getSystemId() { if (inputSource == null) { return null; } else { return inputSource.getSystemId(); } } /** * The XMLReader to be used for the source tree input. May be null. */ private XMLReader reader; /** * <p>The SAX InputSource to be used for the source tree input. * Should not be <code>null</code>.</p> */ private InputSource inputSource; /** * Attempt to obtain a SAX InputSource object from a Source * object. * * @param source Must be a non-null Source reference. * * @return An InputSource, or null if Source can not be converted. */ public static InputSource sourceToInputSource(Source source) { if (source instanceof SAXSource) { return ((SAXSource) source).getInputSource(); } else if (source instanceof StreamSource) { StreamSource ss = (StreamSource) source; InputSource isource = new InputSource(ss.getSystemId()); isource.setByteStream(ss.getInputStream()); isource.setCharacterStream(ss.getReader()); isource.setPublicId(ss.getPublicId()); return isource; } else { return null; } } }