ref = loadedImages.get(imageURL);
if (ref == null)
{
return null;
}
BufferedImage img = ref.get();
//If image was cleared from memory, reload it
if (img == null)
{
try
{
img = ImageIO.read(imageURL);
} catch (Exception e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not load image", e);
}
ref = new SoftReference<>(img);
loadedImages.put(imageURL, ref);
}
return img;
}
/**
* Returns the element of the document at the given URI. If the document is
* not already loaded, it will be.
*/
public SVGElement getElement(URI path)
{
return getElement(path, true);
}
public SVGElement getElement(URL path)
{
try
{
URI uri = new URI(path.toString());
return getElement(uri, true);
} catch (Exception e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not parse url " + path, e);
}
return null;
}
/**
* Looks up a href within our universe. If the href refers to a document
* that is not loaded, it will be loaded. The URL #target will then be
* checked against the SVG diagram's index and the coresponding element
* returned. If there is no coresponding index, null is returned.
*/
public SVGElement getElement(URI path, boolean loadIfAbsent)
{
try
{
//Strip fragment from URI
URI xmlBase = new URI(path.getScheme(), path.getSchemeSpecificPart(), null);
SVGDiagram dia = loadedDocs.get(xmlBase);
if (dia == null && loadIfAbsent)
{
//System.err.println("SVGUnivserse: " + xmlBase.toString());
//javax.swing.JOptionPane.showMessageDialog(null, xmlBase.toString());
URL url = xmlBase.toURL();
loadSVG(url, false);
dia = loadedDocs.get(xmlBase);
if (dia == null)
{
return null;
}
}
String fragment = path.getFragment();
return fragment == null ? dia.getRoot() : dia.getElement(fragment);
} catch (Exception e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not parse path " + path, e);
return null;
}
}
public SVGDiagram getDiagram(URI xmlBase)
{
return getDiagram(xmlBase, true);
}
/**
* Returns the diagram that has been loaded from this root. If diagram is
* not already loaded, returns null.
*/
public SVGDiagram getDiagram(URI xmlBase, boolean loadIfAbsent)
{
if (xmlBase == null)
{
return null;
}
SVGDiagram dia = loadedDocs.get(xmlBase);
if (dia != null || !loadIfAbsent)
{
return dia;
}
//Load missing diagram
try
{
URL url;
if ("jar".equals(xmlBase.getScheme()) && xmlBase.getPath() != null && !xmlBase.getPath().contains("!/"))
{
//Workaround for resources stored in jars loaded by Webstart.
//http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6753651
url = SVGUniverse.class.getResource("xmlBase.getPath()");
}
else
{
url = xmlBase.toURL();
}
loadSVG(url, false);
dia = loadedDocs.get(xmlBase);
return dia;
} catch (Exception e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not parse", e);
}
return null;
}
/**
* Wraps input stream in a BufferedInputStream. If it is detected that this
* input stream is GZIPped, also wraps in a GZIPInputStream for inflation.
*
* @param is Raw input stream
* @return Uncompressed stream of SVG data
* @throws java.io.IOException
*/
private InputStream createDocumentInputStream(InputStream is) throws IOException
{
BufferedInputStream bin = new BufferedInputStream(is);
bin.mark(2);
int b0 = bin.read();
int b1 = bin.read();
bin.reset();
//Check for gzip magic number
if ((b1 << 8 | b0) == GZIPInputStream.GZIP_MAGIC)
{
GZIPInputStream iis = new GZIPInputStream(bin);
return iis;
} else
{
//Plain text
return bin;
}
}
public URI loadSVG(URL docRoot)
{
return loadSVG(docRoot, false);
}
/**
* Loads an SVG file and all the files it references from the URL provided.
* If a referenced file already exists in the SVG universe, it is not
* reloaded.
*
* @param docRoot - URL to the location where this SVG file can be found.
* @param forceLoad - if true, ignore cached diagram and reload
* @return - The URI that refers to the loaded document
*/
public URI loadSVG(URL docRoot, boolean forceLoad)
{
try
{
URI uri = new URI(docRoot.toString());
if (loadedDocs.containsKey(uri) && !forceLoad)
{
return uri;
}
InputStream is = docRoot.openStream();
return loadSVG(uri, new InputSource(createDocumentInputStream(is)));
} catch (URISyntaxException ex)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not parse", ex);
} catch (IOException e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not parse", e);
}
return null;
}
public URI loadSVG(InputStream is, String name) throws IOException
{
return loadSVG(is, name, false);
}
public URI loadSVG(InputStream is, String name, boolean forceLoad) throws IOException
{
URI uri = getStreamBuiltURI(name);
if (uri == null)
{
return null;
}
if (loadedDocs.containsKey(uri) && !forceLoad)
{
return uri;
}
return loadSVG(uri, new InputSource(createDocumentInputStream(is)));
}
public URI loadSVG(Reader reader, String name)
{
return loadSVG(reader, name, false);
}
/**
* This routine allows you to create SVG documents from data streams that
* may not necessarily have a URL to load from. Since every SVG document
* must be identified by a unique URL, Salamander provides a method to fake
* this for streams by defining it's own protocol - svgSalamander - for SVG
* documents without a formal URL.
*
* @param reader - A stream containing a valid SVG document
* @param name - A unique name for this document. It will be used to
* construct a unique URI to refer to this document and perform resolution
* with relative URIs within this document.
For example, a name of
* "/myScene" will produce the URI svgSalamander:/myScene.
* "/maps/canada/toronto" will produce svgSalamander:/maps/canada/toronto.
* If this second document then contained the href "../uk/london", it would
* resolve by default to svgSalamander:/maps/uk/london. That is, SVG
* Salamander defines the URI scheme svgSalamander for it's own internal use
* and uses it for uniquely identfying documents loaded by stream.
If
* you need to link to documents outside of this scheme, you can either
* supply full hrefs (eg, href="url(http://www.kitfox.com/index.html)") or
* put the xml:base attribute in a tag to change the defaultbase URIs are
* resolved against
If a name does not start with the character '/',
* it will be automatically prefixed to it.
* @param forceLoad - if true, ignore cached diagram and reload
*
* @return - The URI that refers to the loaded document
*/
public URI loadSVG(Reader reader, String name, boolean forceLoad)
{
//System.err.println(url.toString());
//Synthesize URI for this stream
URI uri = getStreamBuiltURI(name);
if (uri == null)
{
return null;
}
if (loadedDocs.containsKey(uri) && !forceLoad)
{
return uri;
}
return loadSVG(uri, new InputSource(reader));
}
/**
* Synthesize a URI for an SVGDiagram constructed from a stream.
*
* @param name - Name given the document constructed from a stream.
*/
public URI getStreamBuiltURI(String name)
{
if (name == null || name.length() == 0)
{
return null;
}
if (name.charAt(0) != '/')
{
name = '/' + name;
}
try
{
//Dummy URL for SVG documents built from image streams
return new URI(INPUTSTREAM_SCHEME, name, null);
} catch (Exception e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not parse", e);
return null;
}
}
private XMLReader getXMLReaderCached() throws SAXException, ParserConfigurationException
{
if (cachedReader == null)
{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
cachedReader = factory.newSAXParser().getXMLReader();
}
return cachedReader;
}
protected URI loadSVG(URI xmlBase, InputSource is)
{
// Use an instance of ourselves as the SAX event handler
SVGLoader handler = new SVGLoader(xmlBase, this, verbose);
//Place this docment in the universe before it is completely loaded
// so that the load process can refer to references within it's current
// document
loadedDocs.put(xmlBase, handler.getLoadedDiagram());
try
{
// Parse the input
XMLReader reader = getXMLReaderCached();
reader.setEntityResolver(
new EntityResolver()
{
public InputSource resolveEntity(String publicId, String systemId)
{
//Ignore all DTDs
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
reader.setContentHandler(handler);
reader.parse(is);
handler.getLoadedDiagram().updateTime(curTime);
return xmlBase;
} catch (SAXParseException sex)
{
System.err.println("Error processing " + xmlBase);
System.err.println(sex.getMessage());
loadedDocs.remove(xmlBase);
return null;
} catch (Throwable e)
{
Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
"Could not load SVG " + xmlBase, e);
}
return null;
}
/**
* Get list of uris of all loaded documents and subdocuments.
* @return
*/
public ArrayList getLoadedDocumentURIs()
{
return new ArrayList<>(loadedDocs.keySet());
}
/**
* Remove loaded document from cache.
* @param uri
*/
public void removeDocument(URI uri)
{
loadedDocs.remove(uri);
}
public boolean isVerbose()
{
return verbose;
}
public void setVerbose(boolean verbose)
{
this.verbose = verbose;
}
/**
* Uses serialization to duplicate this universe.
*/
public SVGUniverse duplicate() throws IOException, ClassNotFoundException
{
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bs);
os.writeObject(this);
os.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray());
ObjectInputStream is = new ObjectInputStream(bin);
SVGUniverse universe = (SVGUniverse) is.readObject();
is.close();
return universe;
}
/**
* @return the imageDataInlineOnly
*/
public boolean isImageDataInlineOnly()
{
return imageDataInlineOnly;
}
/**
* @param imageDataInlineOnly the imageDataInlineOnly to set
*/
public void setImageDataInlineOnly(boolean imageDataInlineOnly)
{
this.imageDataInlineOnly = imageDataInlineOnly;
}
public String statistics() {
return String.format("%s has loaded %d SVG images", this, loadedDocs.size());
}
}