1 /*------------------------------------------------------------------------------
2 Name: JSVGCanvasExtended.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: The JSVGCanvas extended to fit the application specific requirements
6 Version: $Id: JSVGCanvasExtended.java 16476 2007-09-06 22:36:52Z laghi $
7 ------------------------------------------------------------------------------*/
8 package javaclients.svg.batik;
9
10 import org.apache.batik.swing.JSVGCanvas;
11 // import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
12
13 import java.util.logging.Logger;
14 import java.util.logging.Level;
15 import org.xmlBlaster.util.Global;
16 import org.xmlBlaster.util.XmlBlasterException;
17
18 import java.io.IOException;
19 import java.io.Reader;
20 import java.io.InputStream;
21 import java.io.StringReader;
22 import java.io.ByteArrayInputStream;
23 import java.util.Hashtable;
24
25 import org.w3c.dom.Element;
26
27
28 /**
29 * @author $Author: laghi $ (michele@laghi.eu)
30 */
31
32 public class JSVGCanvasExtended extends JSVGCanvas
33 {
34 private final static String ME = "JSVGCanvasExtended";
35 private static Logger log = Logger.getLogger(JSVGCanvasExtended.class.getName());
36 // private final static String PARSER_CLASSNAME = "org.xml.sax.parser";
37 // private final static String PARSER_CLASSNAME = "org.apache.crimson.parser.Parser2";
38 // private final static String PARSER_CLASSNAME = "org.apache.crimson.parser.XMLReaderImpl";
39
40 private Interactor specificInteractor = null;
41 // this object takes care of the communication to xmlBlaster
42 private Transceiver transceiver = null;
43 private Hashtable idTable = null;
44
45
46 public JSVGCanvasExtended(Global glob)
47 {
48 super();
49
50 this.specificInteractor = new Interactor();
51 /* Initializes this object. It sets all necessary stuff for the special
52 * interactor (the one which takes care of the application specific stuff)
53 * adds the specific interactor to the list of interactors of this
54 * canvas-inherited object. */
55 this.specificInteractor.setBridgeContext(this.bridgeContext);
56 this.specificInteractor.setCanvas(this);
57 this.transceiver = new Transceiver(glob, this);
58 this.specificInteractor.setTransceiver(this.transceiver);
59 }
60
61
62 public void loadDocumentFromReader (Reader reader)
63 throws IOException
64 {
65 this.setSVGDocument(SvgUtility.createDocument(reader,"file://dummy.svg"));
66 }
67
68
69 public void loadDocumentFromInputStream (InputStream inputStream)
70 throws IOException
71 {
72 this.setSVGDocument(SvgUtility.createDocument(inputStream, "file://dummy.svg"));
73 }
74
75
76 public void loadDocumentFromXmlString (String xmlString) throws IOException
77 {
78 loadDocumentFromReader(new StringReader(xmlString));
79 }
80
81 public void loadDocumentFromByteArray (byte[] byteArray) throws IOException
82 {
83 loadDocumentFromInputStream(new ByteArrayInputStream(byteArray));
84 }
85
86
87 /**
88 * gets the element with the given elementId. If no table has been created,
89 * then it returns null. If the id is not found in the table, null is
90 * returned.
91 */
92 public Element getElement (String elementId)
93 {
94 if (this.idTable == null) return null;
95 Object obj = this.idTable.get(elementId);
96 if (obj == null) return null;
97 return (Element)obj;
98 }
99
100
101 /**
102 * This method should be called each time a new document has been successfully
103 * and completelty loaded.
104 */
105 public void updateDocument ()
106 {
107 try {
108 // a new contextBridge has been created
109 // a new grepahicsNode has been created
110 this.specificInteractor.setBridgeContext(this.bridgeContext);
111 this.specificInteractor.setGraphicsNode();
112 this.transceiver.setBridgeContext(this.bridgeContext);
113 SvgIdMapper mapper = new SvgIdMapper();
114 this.idTable = mapper.createIdTable(this.svgDocument);
115 this.transceiver.subscribeElements();
116 }
117 catch (XmlBlasterException ex) {
118 log.severe(".updateDocument: graphicsNode was null");
119 }
120 }
121
122 public Interactor getSpecificInteractor ()
123 {
124 return this.specificInteractor;
125 }
126
127 }
syntax highlighted by Code2HTML, v. 0.9.1