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.impl.ExternalSubsetResolver; 24 import org.apache.xerces.impl.XMLEntityDescription; 25 import org.apache.xerces.xni.XMLResourceIdentifier; 26 import org.apache.xerces.xni.XNIException; 27 import org.apache.xerces.xni.grammars.XMLDTDDescription; 28 import org.apache.xerces.xni.parser.XMLInputSource; 29 30 import org.xml.sax.ext.EntityResolver2 ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 34 41 public class EntityResolver2Wrapper 42 implements ExternalSubsetResolver { 43 44 48 49 protected EntityResolver2 fEntityResolver; 50 51 55 56 public EntityResolver2Wrapper() {} 57 58 63 public EntityResolver2Wrapper(EntityResolver2 entityResolver) { 64 setEntityResolver(entityResolver); 65 } 67 71 76 public void setEntityResolver(EntityResolver2 entityResolver) { 77 fEntityResolver = entityResolver; 78 } 80 85 public EntityResolver2 getEntityResolver() { 86 return fEntityResolver; 87 } 89 93 104 public XMLInputSource getExternalSubset(XMLDTDDescription grammarDescription) 105 throws XNIException, IOException { 106 107 if (fEntityResolver != null) { 108 109 String name = grammarDescription.getRootName(); 110 String baseURI = grammarDescription.getBaseSystemId(); 111 112 try { 114 InputSource inputSource = fEntityResolver.getExternalSubset(name, baseURI); 115 return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null; 116 } 117 catch (SAXException e) { 119 Exception ex = e.getException(); 120 if (ex == null) { 121 ex = e; 122 } 123 throw new XNIException(ex); 124 } 125 } 126 127 return null; 129 130 } 132 136 146 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 147 throws XNIException, IOException { 148 149 if (fEntityResolver != null) { 150 151 String pubId = resourceIdentifier.getPublicId(); 152 String sysId = resourceIdentifier.getLiteralSystemId(); 153 String baseURI = resourceIdentifier.getBaseSystemId(); 154 String name = null; 155 if (resourceIdentifier instanceof XMLDTDDescription) { 156 name = "[dtd]"; 157 } 158 else if (resourceIdentifier instanceof XMLEntityDescription) { 159 name = ((XMLEntityDescription) resourceIdentifier).getEntityName(); 160 } 161 162 if (pubId == null && sysId == null) { 168 return null; 169 } 170 171 try { 173 InputSource inputSource = 174 fEntityResolver.resolveEntity(name, pubId, baseURI, sysId); 175 return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null; 176 } 177 catch (SAXException e) { 179 Exception ex = e.getException(); 180 if (ex == null) { 181 ex = e; 182 } 183 throw new XNIException(ex); 184 } 185 } 186 187 return null; 189 190 } 192 195 private XMLInputSource createXMLInputSource(InputSource source, String baseURI) { 196 197 String publicId = source.getPublicId(); 198 String systemId = source.getSystemId(); 199 String baseSystemId = baseURI; 200 InputStream byteStream = source.getByteStream(); 201 Reader charStream = source.getCharacterStream(); 202 String encoding = source.getEncoding(); 203 XMLInputSource xmlInputSource = 204 new XMLInputSource(publicId, systemId, baseSystemId); 205 xmlInputSource.setByteStream(byteStream); 206 xmlInputSource.setCharacterStream(charStream); 207 xmlInputSource.setEncoding(encoding); 208 return xmlInputSource; 209 210 } 212 } | Popular Tags |