1 23 24 package org.enhydra.xml.io; 25 26 import java.io.BufferedReader ; 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.Reader ; 32 import java.net.URL ; 33 34 import org.xml.sax.InputSource ; 35 36 39 public final class InputSourceOps { 40 43 private static String XML_DOCUMENT_HEADER_PREFIX = "<?xml"; 44 45 48 private InputSourceOps() { 49 } 50 51 54 private static boolean hasScheme(String systemId) { 55 int colonIdx = systemId.indexOf(':'); 58 return ((colonIdx >= 2) && (colonIdx < systemId.indexOf('/'))); 59 } 60 61 64 public static InputStream openSystemId(String systemId) throws IOException { 65 if (systemId == null) { 66 throw new IllegalArgumentException ("no open stream or system id specified in InputSource"); 67 } 68 69 if (hasScheme(systemId)) { 70 return new URL (systemId).openStream(); 71 } else { 72 return new FileInputStream (systemId); 73 } 74 } 75 76 83 public static Reader open(InputSource inputSource) throws IOException { 84 if (inputSource.getCharacterStream() != null) { 85 return inputSource.getCharacterStream(); 86 } 87 88 InputStream in = inputSource.getByteStream(); 89 if (in == null) { 90 in = openSystemId(inputSource.getSystemId()); 91 } 92 String encoding = inputSource.getEncoding(); 93 if (encoding != null) { 94 return new BufferedReader (new InputStreamReader (in, encoding)); 95 } else { 96 return new BufferedReader (new InputStreamReader (in)); 97 } 98 } 99 100 103 public static boolean isOpen(InputSource input) { 104 return (input.getCharacterStream() != null) 105 || (input.getByteStream() != null); 106 } 107 108 111 public static void close(InputSource input) throws IOException { 112 if (input.getCharacterStream() != null) { 113 input.getCharacterStream().close(); 114 } 115 if (input.getByteStream() != null) { 116 input.getByteStream().close(); 117 } 118 } 119 120 127 public static void closeIfOpened(InputSource inputSource, 128 Reader reader) throws IOException { 129 if ((inputSource.getCharacterStream() == null) 130 && (inputSource.getByteStream() == null)) { 131 reader.close(); 132 } 133 } 134 135 138 public static String getName(InputSource input) { 139 if (input == null) { 140 return "<null input source>"; 141 } 142 143 String name = null; 144 if (input.getSystemId() != null) { 145 name = input.getSystemId(); 146 } else if (input.getPublicId() != null) { 147 name = input.getPublicId(); 148 } 149 150 if (input.getByteStream() != null) { 152 if (name == null) { 153 return "<unnamed byte stream>"; 154 } else { 155 return name +"<byte stream>"; 156 } 157 } 158 if (input.getCharacterStream() != null) { 159 if (name == null) { 160 return "<unnamed character stream>"; 161 } else { 162 return name +"<character stream>"; 163 } 164 } 165 if (name != null) { 166 return name; 167 } else { 168 return "<empty input source>"; 169 } 170 } 171 172 179 public static boolean isXMLDocument(Reader reader) throws IOException { 180 char[] buf = new char[XML_DOCUMENT_HEADER_PREFIX.length()]; 181 reader.mark(XML_DOCUMENT_HEADER_PREFIX.length()+1); 182 if (reader.read(buf) != buf.length) { 183 return false; 184 } 185 return String.copyValueOf(buf).equals(XML_DOCUMENT_HEADER_PREFIX); 186 } 187 188 193 public static boolean isXMLDocument(InputSource inputSource) throws IOException { 194 Reader reader = open(inputSource); 195 try { 196 return isXMLDocument(reader); 197 } finally { 198 closeIfOpened(inputSource, reader); 199 } 200 } 201 } 202 | Popular Tags |