You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by tk...@apache.org on 2001/03/30 16:31:15 UTC
cvs commit: xml-batik/sources/org/apache/batik/swing/svg JSVGComponent.java SVGDocumentLoader.java
tkormann 01/03/30 06:31:15
Modified: sources/org/apache/batik/bridge DocumentLoader.java
GVTBuilder.java URIResolver.java
sources/org/apache/batik/swing JSVGCanvasBeanInfo.java
sources/org/apache/batik/swing/svg JSVGComponent.java
SVGDocumentLoader.java
Log:
No more cache policy, as I don't have enough informations to determine
whether or not a document is in its build phase
Add line number in error
- Works with internal references
- Works with external <image>
- Does not work yet with external <use> as the bridge is analyzing a clone.
Will be fixed after 1.0 (or after the linking support)
Revision Changes Path
1.9 +20 -128 xml-batik/sources/org/apache/batik/bridge/DocumentLoader.java
Index: DocumentLoader.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/bridge/DocumentLoader.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DocumentLoader.java 2001/03/08 12:39:21 1.8
+++ DocumentLoader.java 2001/03/30 14:31:08 1.9
@@ -10,31 +10,29 @@
import java.io.IOException;
import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
+
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDocumentFactory;
+
import org.apache.batik.dom.util.DocumentDescriptor;
+
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.svg.SVGDocument;
+
import org.xml.sax.SAXException;
/**
- * This class is responsible on loading an SVG document.
+ * This class is responsible on loading an SVG document and
+ * maintaining a cache.
*
* @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
- * @version $Id: DocumentLoader.java,v 1.8 2001/03/08 12:39:21 tkormann Exp $
+ * @version $Id: DocumentLoader.java,v 1.9 2001/03/30 14:31:08 tkormann Exp $
*/
public class DocumentLoader {
/**
- * The default size of the cache in terms of number of nodes.
- */
- public static final int DEFAULT_MAX_CACHED_NODE_COUNT = 2000;
-
- /**
* The document factory used to create the document according a
* DOM implementation.
*/
@@ -45,32 +43,10 @@
*
* WARNING: tagged private as no element of this Map should be
* referenced outise of this class
- */
- private HashMap documentMap = new HashMap();
-
- /**
- * A list of the cached documents that can be removed from the
- * cache at any time.
*/
- private List cachedDocs = new LinkedList();
+ protected HashMap cacheMap = new HashMap();
/**
- * A list of the <tt>DocumentState</tt> that represents the
- * documents in progress.
- */
- private List currentDocs = new LinkedList();
-
- /**
- * The current number of cached nodes.
- */
- private int currentCachedNodeCount = 0;
-
- /**
- * The size of the cache.
- */
- private int size;
-
- /**
* Constructs a new <tt>DocumentLoader</tt>.
*/
protected DocumentLoader() { }
@@ -80,17 +56,7 @@
* @param parser The SAX2 parser classname.
*/
public DocumentLoader(String parser) {
- this(parser, DEFAULT_MAX_CACHED_NODE_COUNT);
- }
-
- /**
- * Constructs a new <tt>DocumentLoader</tt> with the specified XML parser.
- * @param parser The SAX2 parser classname.
- * @param size the size of the cache
- */
- public DocumentLoader(String parser, int size) {
- this.documentFactory = new SAXSVGDocumentFactory(parser, true);
- this.size = size;
+ documentFactory = new SAXSVGDocumentFactory(parser, true);
}
/**
@@ -103,76 +69,24 @@
if (n != -1) {
uri = uri.substring(0, n);
}
- Document document = (Document) documentMap.get(uri);
- if (document != null) {
- //System.out.println("reusing: "+uri);
- DocumentState state = getDocumentState(cachedDocs, document);
- // move the state if the document is cached and not in progress
- if (state != null) {
- cachedDocs.remove(state);
- cachedDocs.add(0, state);
- }
- } else {
+ DocumentState state = (DocumentState)cacheMap.get(uri);
+ if (state == null) {
//System.out.println("loading: "+uri);
// load the document
- document = documentFactory.createDocument(uri);
+ Document document = documentFactory.createDocument(uri);
DocumentDescriptor desc = documentFactory.getDocumentDescriptor();
- // update the cache
- int num = desc.getNumberOfElements();
- while ((currentCachedNodeCount + num) > size &&
- cachedDocs.size() > 0) {
- // remove the oldest document loaded
- int i = cachedDocs.size()-1;
- DocumentState state = (DocumentState)cachedDocs.get(i);
- cachedDocs.remove(i);
- documentMap.remove(state.uri);
- currentCachedNodeCount -= state.nodeCount;
- }
- currentCachedNodeCount += num;
- // add the new loaded document to the cache
- DocumentState state = new DocumentState(uri, document, num, desc);
- currentDocs.add(0, state);
- documentMap.put(uri, document);
- }
- return document;
- }
-
- /**
- * Disposes and releases all resources allocated for the specified
- * document. It's the document loader's responsability to
- * physically removed the specified document from the cache when
- * needed. The specified document is in fact just tagged as no
- * more in progress.
- *
- * @param document the document to dispose
- */
- public void dispose(Document document) {
- DocumentState state = getDocumentState(currentDocs, document);
- if (state != null) {
- //System.out.println("disposing: "+state.uri);
- // allow GC of the DocumentDescriptor
- state.desc = null;
- // remove the state from the 'in progress' list
- currentDocs.remove(state);
- // add the state to the cached document list. The document
- // is tagged as no more in progress and can be removed
- // from the cache at any time
- cachedDocs.add(0, state);
+ state = new DocumentState(uri, document, desc);
+ cacheMap.put(uri, state);
}
+ return state.document;
}
/**
* Disposes and releases all resources allocated by this document loader.
*/
public void dispose() {
- if (currentDocs.size() > 0) {
- System.err.println(
- "WARNING: The loader still has "+currentDocs.size()+
- " documents marked in progress.");
- }
//System.out.println("purge the cache");
- documentMap.clear();
- cachedDocs.clear();
+ cacheMap.clear();
}
/**
@@ -184,10 +98,9 @@
* been loaded by this document loader.
*/
public int getLineNumber(Element e) {
- DocumentState state = getDocumentState(currentDocs,
- e.getOwnerDocument());
- if (state == null || state.desc == null) {
- System.err.println("line number not available.");
+ String uri = ((SVGDocument)e.getOwnerDocument()).getURL();
+ DocumentState state = (DocumentState)cacheMap.get(uri);
+ if (state == null) {
return -1;
} else {
return state.desc.getLocationLine(e);
@@ -195,36 +108,19 @@
}
/**
- * Returns the <tt>DocumentState</tt> of the specified Document.
- * @param document the document
- */
- private DocumentState getDocumentState(List l, Document document) {
- for (Iterator i = l.iterator(); i.hasNext();) {
- DocumentState state = (DocumentState) i.next();
- if (state.document == document) {
- return state;
- }
- }
- return null;
- }
-
- /**
* A simple class that contains a Document and its number of nodes.
*/
private static class DocumentState {
private String uri;
private Document document;
- private int nodeCount;
private DocumentDescriptor desc;
public DocumentState(String uri,
Document document,
- int nodeCount,
DocumentDescriptor desc) {
this.uri = uri;
this.document = document;
- this.nodeCount = nodeCount;
this.desc = desc;
}
@@ -238,10 +134,6 @@
public Document getDocument() {
return document;
- }
-
- public int getNodeCount() {
- return nodeCount;
}
}
}
1.6 +1 -4 xml-batik/sources/org/apache/batik/bridge/GVTBuilder.java
Index: GVTBuilder.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/bridge/GVTBuilder.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- GVTBuilder.java 2001/03/15 20:02:59 1.5
+++ GVTBuilder.java 2001/03/30 14:31:09 1.6
@@ -29,7 +29,7 @@
* This class is responsible for creating a GVT tree using an SVG DOM tree.
*
* @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
- * @version $Id: GVTBuilder.java,v 1.5 2001/03/15 20:02:59 tkormann Exp $
+ * @version $Id: GVTBuilder.java,v 1.6 2001/03/30 14:31:09 tkormann Exp $
*/
public class GVTBuilder implements SVGConstants {
@@ -66,15 +66,12 @@
if (topNode != null) {
rootNode.getChildren().add(topNode);
}
- ctx.getDocumentLoader().dispose(document);
}
-
// <!> FIXME: TO BE REMOVED
if (ctx.isDynamic()) {
BridgeEventSupport.loadScripts(ctx, document);
BridgeEventSupport.addGVTListener(ctx, svgElement);
}
-
return rootNode;
}
1.6 +1 -2 xml-batik/sources/org/apache/batik/bridge/URIResolver.java
Index: URIResolver.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/bridge/URIResolver.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- URIResolver.java 2001/03/30 11:46:08 1.5
+++ URIResolver.java 2001/03/30 14:31:09 1.6
@@ -25,7 +25,7 @@
* This class is used to resolve the URI that can be found in a SVG document.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: URIResolver.java,v 1.5 2001/03/30 11:46:08 tkormann Exp $
+ * @version $Id: URIResolver.java,v 1.6 2001/03/30 14:31:09 tkormann Exp $
*/
public class URIResolver {
/**
@@ -98,7 +98,6 @@
URL url = new URL(((SVGOMDocument)document).getURLObject(), uri);
Document doc = documentLoader.loadDocument(url.toString());
- documentLoader.dispose(doc);
String ref = url.getRef();
if (url.getRef() == null) {
return doc;
1.2 +2 -2 xml-batik/sources/org/apache/batik/swing/JSVGCanvasBeanInfo.java
Index: JSVGCanvasBeanInfo.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/JSVGCanvasBeanInfo.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JSVGCanvasBeanInfo.java 2001/03/26 16:13:30 1.1
+++ JSVGCanvasBeanInfo.java 2001/03/30 14:31:12 1.2
@@ -12,10 +12,10 @@
import java.beans.SimpleBeanInfo;
/**
- * This class represents a general-purpose SVG component.
+ * A <tt>BeanInfo</tt> for the <tt>JSVGCanvas</tt>.
*
* @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
- * @version $Id: JSVGCanvasBeanInfo.java,v 1.1 2001/03/26 16:13:30 tkormann Exp $
+ * @version $Id: JSVGCanvasBeanInfo.java,v 1.2 2001/03/30 14:31:12 tkormann Exp $
*/
public class JSVGCanvasBeanInfo extends SimpleBeanInfo {
1.5 +21 -5 xml-batik/sources/org/apache/batik/swing/svg/JSVGComponent.java
Index: JSVGComponent.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/svg/JSVGComponent.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- JSVGComponent.java 2001/03/16 09:45:53 1.4
+++ JSVGComponent.java 2001/03/30 14:31:13 1.5
@@ -27,11 +27,11 @@
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.BridgeMutationEvent;
+import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GraphicsNodeBridge;
import org.apache.batik.bridge.ViewBox;
import org.apache.batik.bridge.UserAgent;
-import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.svg.SVGOMDocument;
@@ -55,7 +55,7 @@
* This class represents a Swing component which can display SVG.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: JSVGComponent.java,v 1.4 2001/03/16 09:45:53 hillion Exp $
+ * @version $Id: JSVGComponent.java,v 1.5 2001/03/30 14:31:13 tkormann Exp $
*/
public class JSVGComponent extends JGVTComponent {
@@ -65,6 +65,11 @@
protected SVGDocumentLoader documentLoader;
/**
+ * The concrete bridge document loader.
+ */
+ protected DocumentLoader loader;
+
+ /**
* The GVT tree builder.
*/
protected GVTTreeBuilder gvtTreeBuilder;
@@ -157,8 +162,8 @@
url = newURI.toString();
}
- documentLoader = new SVGDocumentLoader(url,
- new SAXSVGDocumentFactory(userAgent.getXMLParserClassName()));
+ loader = new DocumentLoader(userAgent.getXMLParserClassName());
+ documentLoader = new SVGDocumentLoader(url, loader);
documentLoader.setPriority(Thread.MIN_PRIORITY);
Iterator it = svgDocumentLoaderListeners.iterator();
@@ -199,7 +204,9 @@
* Creates a new bridge context.
*/
protected BridgeContext createBridgeContext() {
- return new BridgeContext(userAgent, rendererFactory.getRenderContext());
+ return new BridgeContext(userAgent,
+ rendererFactory.getRenderContext(),
+ loader);
}
/**
@@ -331,6 +338,9 @@
* Called when a build was completed.
*/
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
+ loader.dispose(); // purge loader cache
+ loader = null;
+
gvtTreeBuilder = null;
setGraphicsNode(e.getGVTRoot(), false);
Dimension2D dim = bridgeContext.getDocumentSize();
@@ -342,6 +352,9 @@
* Called when a build was cancelled.
*/
public void gvtBuildCancelled(GVTTreeBuilderEvent e) {
+ loader.dispose(); // purge loader cache
+ loader = null;
+
gvtTreeBuilder = null;
image = null;
repaint();
@@ -351,6 +364,9 @@
* Called when a build failed.
*/
public void gvtBuildFailed(GVTTreeBuilderEvent e) {
+ loader.dispose(); // purge loader cache
+ loader = null;
+
gvtTreeBuilder = null;
image = null;
repaint();
1.2 +9 -9 xml-batik/sources/org/apache/batik/swing/svg/SVGDocumentLoader.java
Index: SVGDocumentLoader.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/svg/SVGDocumentLoader.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SVGDocumentLoader.java 2001/03/08 01:21:09 1.1
+++ SVGDocumentLoader.java 2001/03/30 14:31:13 1.2
@@ -16,7 +16,7 @@
import java.util.LinkedList;
import java.util.List;
-import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
+import org.apache.batik.bridge.DocumentLoader;
import org.w3c.dom.svg.SVGDocument;
@@ -26,19 +26,19 @@
* This class represents an object which loads asynchroneaously a SVG document.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: SVGDocumentLoader.java,v 1.1 2001/03/08 01:21:09 hillion Exp $
+ * @version $Id: SVGDocumentLoader.java,v 1.2 2001/03/30 14:31:13 tkormann Exp $
*/
public class SVGDocumentLoader extends Thread {
-
+
/**
* The URL of the document,
*/
protected String url;
/**
- * The document factory.
+ * The document loader.
*/
- protected SAXSVGDocumentFactory documentFactory;
+ protected DocumentLoader loader;
/**
* The exception thrown.
@@ -53,11 +53,11 @@
/**
* Creates a new SVGDocumentLoader.
* @param u The URL of the document.
- * @param df The document factory to use to create the document.
+ * @param l The document loader to use
*/
- public SVGDocumentLoader(String u, SAXSVGDocumentFactory df) {
+ public SVGDocumentLoader(String u, DocumentLoader l) {
url = u;
- documentFactory = df;
+ loader = l;
}
/**
@@ -66,7 +66,7 @@
public void run() {
try {
fireStartedEvent();
- SVGDocument svgDocument = documentFactory.createDocument(url);
+ SVGDocument svgDocument = (SVGDocument)loader.loadDocument(url);
fireCompletedEvent(svgDocument);
} catch (InterruptedIOException e) {
fireCancelledEvent();
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org