01: /*
02: * {START_JAVA_COPYRIGHT_NOTICE
03: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
04: * SUN PROPRIETARY/CONFIDENTIAL.
05: * Use is subject to license terms.
06: * END_COPYRIGHT_NOTICE}
07: */
08: package org.apache.batik.css.engine;
09:
10: import java.net.URL;
11: import java.util.HashMap;
12: import java.util.Iterator;
13:
14: import org.apache.batik.css.engine.StyleSheet;
15:
16: /**
17: * Style sheet cache, to avoid repeated loading and parsing of the
18: * stylesheet, and also to avoid introducing excessive memory overhead
19: * such that multiple pages referring to the same stylesheet can use
20: * the same instance. This is important since stylesheets can take up
21: * a lot of memory when they are large. (The default stylesheet in Creator
22: * took up about 5 megabytes for each usage, which quickly adds up when you
23: * have many pages!)
24: *
25: * @todo Clear the cache when stylesheets are updated!
26: *
27: * @author Tor Norbye, tor.norbye@sun.com
28: */
29: public class StyleSheetCache {
30: private static StyleSheetCache instance;
31: HashMap sheets;
32:
33: /** Construct a new cache */
34: public StyleSheetCache() {
35: }
36:
37: public static StyleSheetCache getInstance() {
38: if (instance == null) {
39: instance = new StyleSheetCache();
40: }
41:
42: return instance;
43: }
44:
45: /** Return number of entries in this cache */
46: public int size() {
47: return (sheets != null) ? sheets.size() : 0;
48: }
49:
50: /** Get a style sheet by a particular URL */
51: public StyleSheet get(URL url) {
52: if ((sheets == null) || (url == null)) {
53: return null;
54: }
55:
56: StyleSheet result = (StyleSheet) sheets.get(url);
57:
58: return result;
59: }
60:
61: /** Put a style sheet into the cache */
62: public void put(URL url, StyleSheet sheet) {
63: if (sheets == null) {
64: sheets = new HashMap(); // TODO - initial size?
65: }
66:
67: sheets.put(url, sheet);
68: }
69:
70: /** Clear out the cache */
71: public void flush() {
72: sheets = null;
73: }
74:
75: public String toString() {
76: return "StyleSheetCache: " + sheets.toString();
77: }
78: }
|