This repository has been archived by the owner on Aug 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching ability for SAX EntityResolver.
- Loading branch information
furfurylic
committed
Mar 3, 2016
1 parent
08f8a89
commit 8cba783
Showing
15 changed files
with
247 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/* | ||
* Chionographis | ||
* | ||
* These codes are licensed under CC0. | ||
* https://creativecommons.org/publicdomain/zero/1.0/deed | ||
*/ | ||
|
||
package net.furfurylic.chionographis; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.WeakHashMap; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.transform.Source; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.URIResolver; | ||
import javax.xml.transform.dom.DOMSource; | ||
|
||
import org.w3c.dom.Document; | ||
import org.xml.sax.EntityResolver; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
|
||
final class CachingResolver implements EntityResolver, URIResolver { | ||
|
||
private static final Object LOCK = new Object(); | ||
|
||
private static Map<URI, URI> canonicalizedURIsForBytes_; | ||
private static Map<URI, Optional<byte[]>> bytes_; | ||
|
||
private static Map<URI, URI> canonicalizedURIsForSources_; | ||
private static Map<URI, Optional<Source>> sources_; | ||
|
||
Consumer<URI> listenStored_; | ||
Consumer<URI> listenHit_; | ||
|
||
public CachingResolver(Consumer<URI> listenStored, Consumer<URI> listenHit) { | ||
listenStored_ = listenStored; | ||
listenHit_ = listenHit; | ||
} | ||
|
||
@Override | ||
public InputSource resolveEntity(String publicId, String systemId) | ||
throws SAXException, IOException { | ||
if (systemId == null) { | ||
return null; | ||
} | ||
URI uri = URI.create(systemId); | ||
uri = uniquifyURI(uri); | ||
if (uri == null) { | ||
return null; | ||
} | ||
|
||
synchronized (LOCK) { | ||
if (bytes_ == null) { | ||
canonicalizedURIsForBytes_ = Collections.synchronizedMap(new WeakHashMap<URI, URI>()); | ||
bytes_ = Collections.synchronizedMap(new IdentityHashMap<>()); | ||
} | ||
} | ||
|
||
Optional<byte[]> cached = accessCache(uri, canonicalizedURIsForBytes_, bytes_, u -> { | ||
try { | ||
if (u.getScheme().toLowerCase().equals("file")) { | ||
File file = new File(u); | ||
long length = file.length(); | ||
if (length <= Integer.MAX_VALUE) { | ||
byte[] content = new byte[(int) length]; | ||
try (DataInputStream in = new DataInputStream(new FileInputStream(file))) { | ||
in.readFully(content); | ||
} | ||
return content; | ||
} | ||
} | ||
byte[] buffer = new byte[4096]; | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
try (InputStream in = u.toURL().openStream()) { | ||
int length; | ||
while ((length = in.read(buffer)) != -1) { | ||
bytes.write(buffer, 0, length); | ||
} | ||
} | ||
return bytes.toByteArray(); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
}); | ||
|
||
if (cached.isPresent()) { | ||
InputSource inputSource = new InputSource(new ByteArrayInputStream(cached.get())); | ||
inputSource.setSystemId(systemId); | ||
inputSource.setPublicId(publicId); | ||
return inputSource; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public Source resolve(String href, String base) throws TransformerException { | ||
URI uri; | ||
if (base == null) { | ||
uri = URI.create(href); | ||
} else { | ||
uri = URI.create(base).resolve(href); | ||
} | ||
uri = uniquifyURI(uri); | ||
if (uri == null) { | ||
return null; | ||
} | ||
|
||
synchronized (LOCK) { | ||
if (sources_ == null) { | ||
canonicalizedURIsForSources_ = Collections.synchronizedMap(new WeakHashMap<URI, URI>()); | ||
sources_ = Collections.synchronizedMap(new IdentityHashMap<>()); | ||
} | ||
} | ||
|
||
Optional<Source> cached = accessCache(uri, canonicalizedURIsForSources_, sources_, u -> { | ||
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); | ||
dbfac.setNamespaceAware(true); | ||
try { | ||
DocumentBuilder builder = dbfac.newDocumentBuilder(); | ||
builder.setEntityResolver(this); | ||
Document document = builder.parse(u.toString()); | ||
return new DOMSource(document, u.toString()); | ||
} catch (ParserConfigurationException | SAXException | IOException e) { | ||
return null; | ||
} | ||
}); | ||
return cached.orElse(null); | ||
} | ||
|
||
private static URI uniquifyURI(URI uri) { | ||
if (!uri.isAbsolute()) { | ||
return null; | ||
} | ||
if (uri.getScheme().toLowerCase().equals("file")) { | ||
// Afraid that omission of "xx/../" may break path meanings for symbolic links | ||
try { | ||
uri = Paths.get(uri).toRealPath().toUri(); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} else { | ||
uri = uri.normalize(); | ||
} | ||
return uri; | ||
} | ||
|
||
private static URI canonicalizeURI(URI uri, Map<URI, URI> canonicalURIs) { | ||
assert uri != null; | ||
URI existing = canonicalURIs.putIfAbsent(uri, uri); | ||
if (existing == null) { | ||
return uri; | ||
} else { | ||
return existing; | ||
} | ||
} | ||
|
||
private <T> Optional<T> accessCache(URI uri, Map<URI, URI> canonicalURIs, Map<URI, Optional<T>> cache, Function<URI, ? extends T> factory) { | ||
uri = canonicalizeURI(uri, canonicalURIs); | ||
synchronized (uri) { | ||
Optional<T> cached = cache.get(uri); | ||
if (cached != null) { | ||
if (!cached.isPresent()) { | ||
// Means that an error occurred in the privious try. | ||
return null; | ||
} else { | ||
// Cache hit. | ||
listenHit_.accept(URI.create(uri.toString())); | ||
} | ||
} else { | ||
cached = Optional.<T>ofNullable(factory.apply(uri)); | ||
if (cached.isPresent()) { | ||
listenStored_.accept(URI.create(uri.toString())); | ||
} | ||
cache.put(uri, cached); | ||
} | ||
return cached; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
X |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" standalone="no"?> | ||
<!DOCTYPE map SYSTEM "input.dtd"> | ||
<map> | ||
<a>&OneHundred;</a> | ||
<b>200</b> | ||
</map> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!ENTITY X SYSTEM "X.txt"> | ||
<!ENTITY OneHundred "100"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?xml version="1.0" standalone="no"?> | ||
<!DOCTYPE x SYSTEM "input.dtd"> | ||
<x>&X;</x> |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.