1 16 17 package org.apache.xerces.util; 18 19 20 import org.apache.xerces.xni.XNIException; 21 import org.apache.xerces.xni.XMLResourceIdentifier; 22 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 23 import org.apache.xerces.xni.parser.XMLEntityResolver; 24 import org.apache.xerces.xni.parser.XMLInputSource; 25 26 import org.w3c.dom.ls.LSResourceResolver ; 27 import org.w3c.dom.ls.LSInput ; 28 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.io.Reader ; 32 import java.io.StringReader ; 33 34 35 45 public class DOMEntityResolverWrapper 46 implements XMLEntityResolver { 47 48 52 53 private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml"; 54 55 56 private static final String XSD_TYPE = "http://www.w3.org/2001/XMLSchema"; 57 58 59 protected LSResourceResolver fEntityResolver; 60 61 65 66 public DOMEntityResolverWrapper() {} 67 68 69 public DOMEntityResolverWrapper(LSResourceResolver entityResolver) { 70 setEntityResolver(entityResolver); 71 } 73 77 78 public void setEntityResolver(LSResourceResolver entityResolver) { 79 fEntityResolver = entityResolver; 80 } 82 83 public LSResourceResolver getEntityResolver() { 84 return fEntityResolver; 85 } 87 91 100 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 101 throws XNIException, IOException { 102 if (fEntityResolver != null) { 104 LSInput inputSource = 107 resourceIdentifier == null 108 ? fEntityResolver.resolveResource( 109 null, 110 null, 111 null, 112 null, 113 null) 114 : fEntityResolver.resolveResource( 115 getType(resourceIdentifier), 116 resourceIdentifier.getNamespace(), 117 resourceIdentifier.getPublicId(), 118 resourceIdentifier.getLiteralSystemId(), 119 resourceIdentifier.getBaseSystemId()); 120 if (inputSource != null) { 121 String publicId = inputSource.getPublicId(); 122 String systemId = inputSource.getSystemId(); 123 String baseSystemId = inputSource.getBaseURI(); 124 InputStream byteStream = inputSource.getByteStream(); 125 Reader charStream = inputSource.getCharacterStream(); 126 String encoding = inputSource.getEncoding(); 127 String data = inputSource.getStringData(); 128 129 134 XMLInputSource xmlInputSource = 135 new XMLInputSource(publicId, systemId, baseSystemId); 136 137 if (charStream != null) { 138 xmlInputSource.setCharacterStream(charStream); 139 } 140 else if (byteStream != null) { 141 xmlInputSource.setByteStream((InputStream ) byteStream); 142 } 143 else if (data != null && data.length() != 0) { 144 xmlInputSource.setCharacterStream(new StringReader (data)); 145 } 146 xmlInputSource.setEncoding(encoding); 147 return xmlInputSource; 148 } 149 } 150 151 return null; 153 154 } 156 157 private String getType(XMLResourceIdentifier resourceIdentifier) { 158 if (resourceIdentifier instanceof XMLGrammarDescription) { 159 XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier; 160 if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) { 161 return XSD_TYPE; 162 } 163 } 164 return XML_TYPE; 165 } 167 } | Popular Tags |