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 5306114
Showing
15 changed files
with
289 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,241 @@ | ||
/* | ||
* 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.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); | ||
} | ||
|
||
/** | ||
* Normalizes a URI in terms of its logical content. | ||
* | ||
* @param uri | ||
* a URI to normalize. | ||
* | ||
* @return | ||
* the normalized URI. | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* | ||
* @param uri | ||
* a URI. | ||
* @param canonicalURIs | ||
* @param cache | ||
* a possibly identity-based synchronized map. | ||
* @param factory | ||
* a factory function which make an object from a URI. | ||
* | ||
* @return | ||
* a possibly-empty resolved object. | ||
*/ | ||
private <T> Optional<T> accessCache(URI uri, Map<URI, URI> canonicalURIs, Map<URI, Optional<T>> cache, Function<URI, ? extends T> factory) { | ||
// Get the canonicalized form | ||
URI canonicalizedURI = canonicalizeURI(uri, canonicalURIs); | ||
|
||
// From here uri shall not be in the canonicalized form | ||
if (canonicalizedURI == uri) { | ||
uri = URI.create(uri.toString()); | ||
} | ||
|
||
// Lock with privately-canonicalized form | ||
synchronized (canonicalizedURI) { | ||
Optional<T> cached = cache.get(canonicalizedURI); | ||
if (cached != null) { | ||
if (!cached.isPresent()) { | ||
// Means that an error occurred in the previous try. | ||
return null; | ||
} else { | ||
// Cache hit. | ||
listenHit_.accept(uri); | ||
} | ||
} else { | ||
cached = Optional.<T>ofNullable(factory.apply(uri)); | ||
if (cached.isPresent()) { | ||
listenStored_.accept(uri); | ||
} | ||
cache.put(canonicalizedURI, cached); | ||
} | ||
return cached; | ||
} | ||
} | ||
|
||
/** | ||
* Canonicalizes a URI so that URIs which have the same logical content are one same object. | ||
* | ||
* @param uri | ||
* a URI to canonicalize. | ||
* @param canonicalURIs | ||
* a canonicalization mapping for URIs. | ||
* | ||
* @return | ||
* the canonicalized form of the URI, | ||
* which is different object from the parameter <i>uri</i> | ||
* if it did not come from this method. | ||
*/ | ||
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; | ||
} | ||
} | ||
} |
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.
Oops, something went wrong.