1 16 17 package com.sun.org.apache.xerces.internal.util; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.io.Reader ; 22 23 import com.sun.org.apache.xerces.internal.impl.ExternalSubsetResolver; 24 import com.sun.org.apache.xerces.internal.impl.XMLEntityDescription; 25 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 26 import com.sun.org.apache.xerces.internal.xni.XNIException; 27 import com.sun.org.apache.xerces.internal.xni.grammars.XMLDTDDescription; 28 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 29 30 import org.xml.sax.EntityResolver ; 31 import org.xml.sax.ext.EntityResolver2 ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 42 public class EntityResolver2Wrapper 43 implements ExternalSubsetResolver { 44 45 49 50 protected EntityResolver2 fEntityResolver; 51 52 56 57 public EntityResolver2Wrapper() {} 58 59 64 public EntityResolver2Wrapper(EntityResolver2 entityResolver) { 65 setEntityResolver(entityResolver); 66 } 68 72 77 public void setEntityResolver(EntityResolver2 entityResolver) { 78 fEntityResolver = entityResolver; 79 } 81 86 public EntityResolver2 getEntityResolver() { 87 return fEntityResolver; 88 } 90 94 105 public XMLInputSource getExternalSubset(XMLDTDDescription grammarDescription) 106 throws XNIException, IOException { 107 108 if (fEntityResolver != null) { 109 110 String name = grammarDescription.getRootName(); 111 String baseURI = grammarDescription.getBaseSystemId(); 112 113 try { 115 InputSource inputSource = fEntityResolver.getExternalSubset(name, baseURI); 116 return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null; 117 } 118 catch (SAXException e) { 120 Exception ex = e.getException(); 121 if (ex == null) { 122 ex = e; 123 } 124 throw new XNIException(ex); 125 } 126 } 127 128 return null; 130 131 } 133 137 147 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 148 throws XNIException, IOException { 149 150 if (fEntityResolver != null) { 151 152 String pubId = resourceIdentifier.getPublicId(); 153 String sysId = resourceIdentifier.getExpandedSystemId(); 154 String baseURI = resourceIdentifier.getBaseSystemId(); 155 String name = null; 156 if (resourceIdentifier instanceof XMLDTDDescription) { 157 name = "[dtd]"; 158 } 159 else if (resourceIdentifier instanceof XMLEntityDescription) { 160 name = ((XMLEntityDescription) resourceIdentifier).getEntityName(); 161 } 162 163 if (pubId == null && sysId == null && baseURI == null && name == null) { 169 return null; 170 } 171 172 try { 174 InputSource inputSource = 175 fEntityResolver.resolveEntity(name, pubId, baseURI, sysId); 176 return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null; 177 } 178 catch (SAXException e) { 180 Exception ex = e.getException(); 181 if (ex == null) { 182 ex = e; 183 } 184 throw new XNIException(ex); 185 } 186 } 187 188 return null; 190 191 } 193 196 private XMLInputSource createXMLInputSource(InputSource source, String baseURI) { 197 198 String publicId = source.getPublicId(); 199 String systemId = source.getSystemId(); 200 String baseSystemId = baseURI; 201 InputStream byteStream = source.getByteStream(); 202 Reader charStream = source.getCharacterStream(); 203 String encoding = source.getEncoding(); 204 XMLInputSource xmlInputSource = 205 new XMLInputSource(publicId, systemId, baseSystemId); 206 xmlInputSource.setByteStream(byteStream); 207 xmlInputSource.setCharacterStream(charStream); 208 xmlInputSource.setEncoding(encoding); 209 return xmlInputSource; 210 211 } 213 } | Popular Tags |