001 /* SAXSource.java -- 002 Copyright (C) 2004, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package javax.xml.transform.sax; 039 040 import java.io.InputStream; 041 import java.io.Reader; 042 import javax.xml.transform.Source; 043 import javax.xml.transform.stream.StreamSource; 044 import org.xml.sax.InputSource; 045 import org.xml.sax.XMLReader; 046 047 /** 048 * Specifies a SAX XML source. This is a tuple of input source and SAX 049 * parser. 050 * 051 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a) 052 */ 053 public class SAXSource 054 implements Source 055 { 056 057 /** 058 * Factory feature indicating that SAX sources are supported. 059 */ 060 public static final String FEATURE = 061 "http://javax.xml.transform.sax.SAXSource/feature"; 062 063 private XMLReader xmlReader; 064 private InputSource inputSource; 065 066 /** 067 * Default constructor. 068 */ 069 public SAXSource() 070 { 071 } 072 073 /** 074 * Constructor with a SAX parser and input source. 075 */ 076 public SAXSource(XMLReader reader, InputSource inputSource) 077 { 078 xmlReader = reader; 079 this.inputSource = inputSource; 080 } 081 082 /** 083 * Constructor with an input source. 084 * The SAX parser will be instantiated by the transformer. 085 */ 086 public SAXSource(InputSource inputSource) 087 { 088 this.inputSource = inputSource; 089 } 090 091 /** 092 * Sets the SAX parser to be used by this source. 093 * If null, the transformer will instantiate its own parser. 094 */ 095 public void setXMLReader(XMLReader reader) 096 { 097 xmlReader = reader; 098 } 099 100 /** 101 * Returns the SAX parser to be used by this source. 102 * If null, the transformer will instantiate its own parser. 103 */ 104 public XMLReader getXMLReader() 105 { 106 return xmlReader; 107 } 108 109 /** 110 * Sets the input source to parse. 111 */ 112 public void setInputSource(InputSource inputSource) 113 { 114 this.inputSource = inputSource; 115 } 116 117 /** 118 * Returns the input source to parse. 119 */ 120 public InputSource getInputSource() 121 { 122 return inputSource; 123 } 124 125 /** 126 * Sets the system ID for this source. 127 */ 128 public void setSystemId(String systemId) 129 { 130 if (inputSource != null) 131 { 132 inputSource.setSystemId(systemId); 133 } 134 } 135 136 /** 137 * Returns the system ID for this source. 138 */ 139 public String getSystemId() 140 { 141 if (inputSource != null) 142 { 143 return inputSource.getSystemId(); 144 } 145 return null; 146 } 147 148 /** 149 * Converts a source into a SAX input source. 150 * This method can use a StreamSource or the system ID. 151 * @return an input source or null 152 */ 153 public static InputSource sourceToInputSource(Source source) 154 { 155 InputSource in = null; 156 if (source instanceof SAXSource) 157 { 158 in = ((SAXSource) source).getInputSource(); 159 } 160 else if (source instanceof StreamSource) 161 { 162 StreamSource streamSource = (StreamSource) source; 163 InputStream inputStream = streamSource.getInputStream(); 164 if (inputStream != null) 165 { 166 in = new InputSource(inputStream); 167 } 168 else 169 { 170 Reader reader = streamSource.getReader(); 171 if (reader != null) 172 { 173 in = new InputSource(reader); 174 } 175 } 176 String publicId = streamSource.getPublicId(); 177 if (publicId != null && in != null) 178 { 179 in.setPublicId(publicId); 180 } 181 } 182 String systemId = source.getSystemId(); 183 if (systemId != null) 184 { 185 if (in == null) 186 { 187 in = new InputSource(systemId); 188 } 189 else 190 { 191 in.setSystemId(systemId); 192 } 193 } 194 return in; 195 } 196 197 }