- SVGUniverse univ = SVGCache.getSVGUniverse();
- synchronized(univ) {
- // NOTE: hard-coded to assume all SVG data is encoded in UTF-8
- byte[] dataBytes = data.getBytes("UTF-8");
- dataHash = digest(dataBytes, assignments);
- if (diagramCache != null)
- univ.decRefCount(diagramCache.getXMLBase());
- URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), dataHash);
- diagramCache = univ.getDiagram(uri, false);
- if (diagramCache != null) {
- if (diagramCache.getRoot() == null) {
- diagramCache = univ.getDiagram(univ.loadSVG(BROKEN_SVG_DATA), false);
- dataHash = "broken";
- } else if (diagramCache.getRoot().getBoundingBox().isEmpty()) {
- diagramCache = univ.getDiagram(univ.loadSVG(EMPTY_SVG_DATA), false);
- dataHash = "empty";
- } else {
- for(SVGNodeAssignment ass : assignments) {
- SVGElement e = diagramCache.getElement(ass.elementId);
- if(e != null) {
- if("$text".equals(ass.attributeNameOrId)) {
- Tspan t = (Tspan)e;
- t.setText(ass.value);
- Text text = (Text)t.getParent();
- text.rebuild();
- } else {
- e.setAttribute(ass.attributeNameOrId, AnimationElement.AT_AUTO, ass.value);
- }
- }
- }
- if(!assignments.isEmpty())
- diagramCache.updateTime(0);
- }
- }
- documentCache = data;
- if (diagramCache != null) {
- setBounds((Rectangle2D) diagramCache.getRoot().getBoundingBox().clone());
- univ.incRefCount(diagramCache.getXMLBase());
- } else {
- setBounds(new Rectangle2D.Double());
- }
- }
+ Rectangle2D bbox = null;
+ synchronized (univ) {
+ // Relinquish reference to current element
+ if (diagramCache != null) {
+ univ.decRefCount(diagramCache.getXMLBase());
+ diagramCache = null;
+ }
+
+ // NOTE: hard-coded to assume all SVG data is encoded in UTF-8
+ byte[] dataBytes = data.getBytes("UTF-8");
+ dataHash = digest(dataBytes, assignments);
+ URI uri = univ.loadSVG(new ByteArrayInputStream(dataBytes), dataHash);
+ diagramCache = univ.getDiagram(uri, false);
+
+ if (diagramCache != null) {
+ univ.incRefCount(diagramCache.getXMLBase());
+
+ if (diagramCache.getRoot() == null) {
+ univ.decRefCount(diagramCache.getXMLBase());
+ diagramCache = univ.getDiagram(univ.loadSVG(BROKEN_SVG_DATA), false);
+ dataHash = "broken";
+ univ.incRefCount(diagramCache.getXMLBase());
+ bbox = (Rectangle2D) diagramCache.getRoot().getBoundingBox().clone();
+ } else {
+ bbox = diagramCache.getRoot().getBoundingBox();
+ if (bbox.isEmpty()) {
+ univ.decRefCount(diagramCache.getXMLBase());
+ diagramCache = univ.getDiagram(univ.loadSVG(EMPTY_SVG_DATA), false);
+ dataHash = "empty";
+ univ.incRefCount(diagramCache.getXMLBase());
+ bbox = (Rectangle2D) diagramCache.getRoot().getBoundingBox().clone();
+ } else {
+ if (applyAssignments(diagramCache, assignments)) {
+ bbox = (Rectangle2D) diagramCache.getRoot().getBoundingBox().clone();
+ } else {
+ bbox = (Rectangle2D) bbox.clone();
+ }
+ }
+ }
+ } else {
+ bbox = new Rectangle2D.Double();
+ }
+ }
+
+ documentCache = data;
+ setBounds(bbox);