Source Home >> Java Source 1.6.0 >> javax.xml.transform.sax.SAXSource V 0.09
  • 001/*
  • 002 * The contents of this file are subject to the terms
  • 003 * of the Common Development and Distribution License
  • 004 * (the "License"). You may not use this file except
  • 005 * in compliance with the License.
  • 006 *
  • 007 * You can obtain a copy of the license at
  • 008 * https://jaxp.dev.java.net/CDDLv1.0.html.
  • 009 * See the License for the specific language governing
  • 010 * permissions and limitations under the License.
  • 011 *
  • 012 * When distributing Covered Code, include this CDDL
  • 013 * HEADER in each file and include the License file at
  • 014 * https://jaxp.dev.java.net/CDDLv1.0.html
  • 015 * If applicable add the following below this CDDL HEADER
  • 016 * with the fields enclosed by brackets "[]" replaced with
  • 017 * your own identifying information: Portions Copyright
  • 018 * [year] [name of copyright owner]
  • 019 */
  • 020
  • 021/*
  • 022 * $Id: SAXSource.java,v 1.3 2005/11/03 19:34:26 jeffsuttor Exp $
  • 023 * @(#)SAXSource.java 1.24 06/04/07
  • 024 *
  • 025 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
  • 026 */
  • 027
  • 028package javax.xml.transform.sax;
  • 029
  • 030import javax.xml.transform.Source;
  • 031import javax.xml.transform.stream.StreamSource;
  • 032
  • 033import org.xml.sax.InputSource;
  • 034import org.xml.sax.XMLReader;
  • 035
  • 036/**
  • 037 * <p>Acts as an holder for SAX-style Source.</p>
  • 038 *
  • 039 * <p>Note that XSLT requires namespace support. Attempting to transform an
  • 040 * input source that is not
  • 041 * generated with a namespace-aware parser may result in errors.
  • 042 * Parsers can be made namespace aware by calling the
  • 043 * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
  • 044 *
  • 045 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  • 046 * @version $Revision: 1.3 $, $Date: 2005/11/03 19:34:26 $
  • 047 */
  • 048public class SAXSource implements Source {
  • 049
  • 050 /**
  • 051 * If {@link javax.xml.transform.TransformerFactory#getFeature}
  • 052 * returns true when passed this value as an argument,
  • 053 * the Transformer supports Source input of this type.
  • 054 */
  • 055 public static final String FEATURE =
  • 056 "http://javax.xml.transform.sax.SAXSource/feature";
  • 057
  • 058 /**
  • 059 * <p>Zero-argument default constructor. If this constructor is used, and
  • 060 * no SAX source is set using
  • 061 * {@link #setInputSource(InputSource inputSource)} , then the
  • 062 * <code>Transformer</code> will
  • 063 * create an empty source {@link org.xml.sax.InputSource} using
  • 064 * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
  • 065 *
  • 066 * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
  • 067 */
  • 068 public SAXSource() { }
  • 069
  • 070 /**
  • 071 * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
  • 072 * and a SAX InputSource. The {@link javax.xml.transform.Transformer}
  • 073 * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
  • 074 * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
  • 075 * reader.parse(inputSource).
  • 076 *
  • 077 * @param reader An XMLReader to be used for the parse.
  • 078 * @param inputSource A SAX input source reference that must be non-null
  • 079 * and that will be passed to the reader parse method.
  • 080 */
  • 081 public SAXSource(XMLReader reader, InputSource inputSource) {
  • 082 this.reader = reader;
  • 083 this.inputSource = inputSource;
  • 084 }
  • 085
  • 086 /**
  • 087 * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
  • 088 * The {@link javax.xml.transform.Transformer} or
  • 089 * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
  • 090 * reader via {@link org.xml.sax.helpers.XMLReaderFactory}
  • 091 * (if setXMLReader is not used), sets itself as
  • 092 * the reader's {@link org.xml.sax.ContentHandler}, and calls
  • 093 * reader.parse(inputSource).
  • 094 *
  • 095 * @param inputSource An input source reference that must be non-null
  • 096 * and that will be passed to the parse method of the reader.
  • 097 */
  • 098 public SAXSource(InputSource inputSource) {
  • 099 this.inputSource = inputSource;
  • 100 }
  • 101
  • 102 /**
  • 103 * Set the XMLReader to be used for the Source.
  • 104 *
  • 105 * @param reader A valid XMLReader or XMLFilter reference.
  • 106 */
  • 107 public void setXMLReader(XMLReader reader) {
  • 108 this.reader = reader;
  • 109 }
  • 110
  • 111 /**
  • 112 * Get the XMLReader to be used for the Source.
  • 113 *
  • 114 * @return A valid XMLReader or XMLFilter reference, or null.
  • 115 */
  • 116 public XMLReader getXMLReader() {
  • 117 return reader;
  • 118 }
  • 119
  • 120 /**
  • 121 * Set the SAX InputSource to be used for the Source.
  • 122 *
  • 123 * @param inputSource A valid InputSource reference.
  • 124 */
  • 125 public void setInputSource(InputSource inputSource) {
  • 126 this.inputSource = inputSource;
  • 127 }
  • 128
  • 129 /**
  • 130 * Get the SAX InputSource to be used for the Source.
  • 131 *
  • 132 * @return A valid InputSource reference, or null.
  • 133 */
  • 134 public InputSource getInputSource() {
  • 135 return inputSource;
  • 136 }
  • 137
  • 138 /**
  • 139 * Set the system identifier for this Source. If an input source
  • 140 * has already been set, it will set the system ID or that
  • 141 * input source, otherwise it will create a new input source.
  • 142 *
  • 143 * <p>The system identifier is optional if there is a byte stream
  • 144 * or a character stream, but it is still useful to provide one,
  • 145 * since the application can use it to resolve relative URIs
  • 146 * and can include it in error messages and warnings (the parser
  • 147 * will attempt to open a connection to the URI only if
  • 148 * no byte stream or character stream is specified).</p>
  • 149 *
  • 150 * @param systemId The system identifier as a URI string.
  • 151 */
  • 152 public void setSystemId(String systemId) {
  • 153
  • 154 if (null == inputSource) {
  • 155 inputSource = new InputSource(systemId);
  • 156 } else {
  • 157 inputSource.setSystemId(systemId);
  • 158 }
  • 159 }
  • 160
  • 161 /**
  • 162 * <p>Get the base ID (URI or system ID) from where URIs
  • 163 * will be resolved.</p>
  • 164 *
  • 165 * @return Base URL for the <code>Source</code>, or <code>null</code>.
  • 166 */
  • 167 public String getSystemId() {
  • 168
  • 169 if (inputSource == null) {
  • 170 return null;
  • 171 } else {
  • 172 return inputSource.getSystemId();
  • 173 }
  • 174 }
  • 175
  • 176 /**
  • 177 * The XMLReader to be used for the source tree input. May be null.
  • 178 */
  • 179 private XMLReader reader;
  • 180
  • 181 /**
  • 182 * <p>The SAX InputSource to be used for the source tree input.
  • 183 * Should not be <code>null</code>.</p>
  • 184 */
  • 185 private InputSource inputSource;
  • 186
  • 187 /**
  • 188 * Attempt to obtain a SAX InputSource object from a Source
  • 189 * object.
  • 190 *
  • 191 * @param source Must be a non-null Source reference.
  • 192 *
  • 193 * @return An InputSource, or null if Source can not be converted.
  • 194 */
  • 195 public static InputSource sourceToInputSource(Source source) {
  • 196
  • 197 if (source instanceof SAXSource) {
  • 198 return ((SAXSource) source).getInputSource();
  • 199 } else if (source instanceof StreamSource) {
  • 200 StreamSource ss = (StreamSource) source;
  • 201 InputSource isource = new InputSource(ss.getSystemId());
  • 202
  • 203 isource.setByteStream(ss.getInputStream());
  • 204 isource.setCharacterStream(ss.getReader());
  • 205 isource.setPublicId(ss.getPublicId());
  • 206
  • 207 return isource;
  • 208 } else {
  • 209 return null;
  • 210 }
  • 211 }
  • 212}
  • 213

文件:SAXSource.java
包名:javax.xml.transform.sax
类名:SAXSource
继承:
接口:[Source]