1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 61 import com.sun.org.apache.xerces.internal.xni.XNIException; 62 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 63 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 64 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 65 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 66 67 import org.w3c.dom.ls.LSResourceResolver ; 68 import org.w3c.dom.ls.LSInput ; 69 70 import java.io.InputStream ; 71 import java.io.IOException ; 72 import java.io.Reader ; 73 import java.io.StringReader ; 74 75 76 86 public class DOMEntityResolverWrapper 87 implements XMLEntityResolver { 88 89 93 94 private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml"; 95 96 97 private static final String XSD_TYPE = "http://www.w3.org/2001/XMLSchema"; 98 99 100 protected LSResourceResolver fEntityResolver; 101 102 106 107 public DOMEntityResolverWrapper() {} 108 109 110 public DOMEntityResolverWrapper(LSResourceResolver entityResolver) { 111 setEntityResolver(entityResolver); 112 } 114 118 119 public void setEntityResolver(LSResourceResolver entityResolver) { 120 fEntityResolver = entityResolver; 121 } 123 124 public LSResourceResolver getEntityResolver() { 125 return fEntityResolver; 126 } 128 132 141 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 142 throws XNIException, IOException { 143 if (fEntityResolver != null) { 145 LSInput inputSource = 148 resourceIdentifier == null 149 ? fEntityResolver.resolveResource( 150 null, 151 null, 152 null, 153 null, 154 null) 155 : fEntityResolver.resolveResource( 156 getType(resourceIdentifier), 157 resourceIdentifier.getNamespace(), 158 resourceIdentifier.getPublicId(), 159 resourceIdentifier.getLiteralSystemId(), 160 resourceIdentifier.getBaseSystemId()); 161 if (inputSource != null) { 162 String publicId = inputSource.getPublicId(); 163 String systemId = inputSource.getSystemId(); 164 String baseSystemId = inputSource.getBaseURI(); 165 InputStream byteStream = inputSource.getByteStream(); 166 Reader charStream = inputSource.getCharacterStream(); 167 String encoding = inputSource.getEncoding(); 168 String data = inputSource.getStringData(); 169 XMLInputSource xmlInputSource = 170 new XMLInputSource(publicId, systemId, baseSystemId); 171 172 if (charStream != null) { 173 xmlInputSource.setCharacterStream(charStream); 174 } 175 if (byteStream != null) { 176 xmlInputSource.setByteStream((InputStream ) byteStream); 177 } 178 if (data != null && data.length() != 0) { 179 xmlInputSource.setCharacterStream( 180 new StringReader (data)); 181 } 182 xmlInputSource.setEncoding(encoding); 183 return xmlInputSource; 184 } 185 } 186 187 return null; 189 190 } 192 193 private String getType(XMLResourceIdentifier resourceIdentifier) { 194 if (resourceIdentifier instanceof XMLGrammarDescription) { 195 XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier; 196 if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) { 197 return XSD_TYPE; 198 } 199 } 200 return XML_TYPE; 201 } 203 } | Popular Tags |