001/* 002 * Copyright 2013 Chris Pheby 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.jadira.lang.io.core; 017 018import java.io.File; 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.Reader; 022import java.net.URL; 023 024import javax.xml.transform.sax.SAXSource; 025 026import org.springframework.core.io.InputStreamSource; 027import org.springframework.core.io.Resource; 028import org.xml.sax.InputSource; 029 030/** 031 * An InputSource suitable constructed from an accessible resource. It can be constructed from a stream, URL, file or Spring Framework resource. 032 */ 033public class ResourceInputSource extends InputSource { 034 035 private String fileName; 036 037 private File file; 038 039 private InputStreamSource streamSource; 040 041 private Resource resource; 042 043 /** 044 * Creates a ResourceInputSource for the given {@link URL} 045 * @param url The URL whose contents will be parsed 046 * @throws IOException Indicates a problem accessing the URL 047 */ 048 public ResourceInputSource(URL url) throws IOException { 049 050 if (url.getProtocol().equals("file")) { 051 doSetFile(new File(url.getFile())); 052 } 053 054 super.setByteStream(url.openStream()); 055 056 String urlName = url.getFile(); 057 058 if (urlName.lastIndexOf('/') != -1) { 059 this.fileName = urlName.substring(urlName.lastIndexOf('/') + 1); 060 } else { 061 this.fileName = urlName; 062 } 063 } 064 065 /** 066 * Creates a ResourceInputSource for the given {@link InputStreamSource} 067 * @param streamSource The InputStreamSource to be parsed 068 * @throws IOException Indicates a problem in accessing the underlying stream 069 */ 070 public ResourceInputSource(InputStreamSource streamSource) throws IOException { 071 setByteStream(streamSource.getInputStream()); 072 073 this.streamSource = streamSource; 074 } 075 076 /** 077 * Creates a ResourceInputSource for the given {@link Resource} 078 * @param resource The Resource to be parsed 079 * @throws IOException Indicates a problem in accessing the actual resource 080 */ 081 public ResourceInputSource(Resource resource) throws IOException { 082 083 try { 084 doSetFile(resource.getFile()); 085 } catch (IOException e) { 086 // Ignore - the resource will be accessed instead via its stream 087 } 088 setByteStream(resource.getInputStream()); 089 090 this.resource = resource; 091 } 092 093 /** 094 * Creates a ResourceInputSource for the given {@link InputStream} 095 * @param byteStream The byte stream to be parsed 096 */ 097 public ResourceInputSource(InputStream byteStream) { 098 setByteStream(byteStream); 099 } 100 101 /** 102 * Creates a ResourceInputSource for the given {@link InputStreamSource} and name 103 * @param byteStream The byte stream to be parsed 104 * @param fileName The file name for the file 105 */ 106 public ResourceInputSource(InputStream byteStream, String fileName) { 107 super.setByteStream(byteStream); 108 this.fileName = fileName; 109 } 110 111 /** 112 * Creates a ResourceInputSource for the given {@link File} 113 * @param file The file to be parsed 114 */ 115 public ResourceInputSource(File file) { 116 doSetFile(file); 117 } 118 119 private void doSetFile(File file) { 120 this.file = file; 121 } 122 123 public void setByteStream(InputStream byteStream) { 124 super.setByteStream(byteStream); 125 } 126 127 /** 128 * Returns the file name for this {@link InputSource} instance, if any 129 * @return The file's name 130 */ 131 public String getFileName() { 132 if (file != null) { 133 return file.getName(); 134 } else { 135 return fileName; 136 } 137 } 138 139 /** 140 * Return the associated file, if any 141 * @return The File 142 */ 143 public File getFile() { 144 return file; 145 } 146 147 /** 148 * Return the associated {@link InputStreamSource}, if any 149 * @return The InputStreamSource 150 */ 151 public InputStreamSource getStreamSource() { 152 return streamSource; 153 } 154 155 /** 156 * Return the associated Resource, if any 157 * @return The Resource 158 */ 159 public Resource getResource() { 160 return resource; 161 } 162 163 /** 164 * Returns this instance wrapped as a {@link SAXSource} 165 * @return The new {@link SAXSource} 166 */ 167 public SAXSource asSAXSource() { 168 return new SAXSource(this); 169 } 170 171 /** 172 * This method from {@link InputSource} is Unsupported 173 */ 174 @Override 175 public void setCharacterStream(Reader reader) { 176 throw new UnsupportedOperationException("CharacterStream is not supported"); 177 } 178 179 /** 180 * This method from {@link InputSource} is Unsupported 181 */ 182 @Override 183 public void setPublicId(String publicId) { 184 throw new UnsupportedOperationException("PublicId is not supported"); 185 } 186 187 /** 188 * This method from {@link InputSource} is Unsupported 189 */ 190 @Override 191 public void setSystemId(String systemId) { 192 throw new UnsupportedOperationException("SystemId is not supported"); 193 } 194}