1 16 17 package org.apache.xerces.util; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.io.Reader ; 22 23 import org.apache.xerces.xni.XNIException; 24 import org.apache.xerces.xni.XMLResourceIdentifier; 25 import org.apache.xerces.xni.parser.XMLEntityResolver; 26 import org.apache.xerces.xni.parser.XMLInputSource; 27 28 import org.xml.sax.EntityResolver ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 32 41 public class EntityResolverWrapper 42 implements XMLEntityResolver { 43 44 48 49 protected EntityResolver fEntityResolver; 50 51 55 56 public EntityResolverWrapper() {} 57 58 59 public EntityResolverWrapper(EntityResolver entityResolver) { 60 setEntityResolver(entityResolver); 61 } 63 67 68 public void setEntityResolver(EntityResolver entityResolver) { 69 fEntityResolver = entityResolver; 70 } 72 73 public EntityResolver getEntityResolver() { 74 return fEntityResolver; 75 } 77 81 91 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 92 throws XNIException, IOException { 93 94 String pubId = resourceIdentifier.getPublicId(); 100 String sysId = resourceIdentifier.getExpandedSystemId(); 101 if (pubId == null && sysId == null) 102 return null; 103 104 if (fEntityResolver != null && resourceIdentifier != null) { 106 try { 107 InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId); 108 if (inputSource != null) { 109 String publicId = inputSource.getPublicId(); 110 String systemId = inputSource.getSystemId(); 111 String baseSystemId = resourceIdentifier.getBaseSystemId(); 112 InputStream byteStream = inputSource.getByteStream(); 113 Reader charStream = inputSource.getCharacterStream(); 114 String encoding = inputSource.getEncoding(); 115 XMLInputSource xmlInputSource = 116 new XMLInputSource(publicId, systemId, baseSystemId); 117 xmlInputSource.setByteStream(byteStream); 118 xmlInputSource.setCharacterStream(charStream); 119 xmlInputSource.setEncoding(encoding); 120 return xmlInputSource; 121 } 122 } 123 124 catch (SAXException e) { 126 Exception ex = e.getException(); 127 if (ex == null) { 128 ex = e; 129 } 130 throw new XNIException(ex); 131 } 132 } 133 134 return null; 136 137 } } 139 | Popular Tags |