
org.apache.batik.bridge.ScriptingEnvironment Maven / Gradle / Ivy
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.batik.bridge;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.dom.events.NodeEventTarget;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGOMDocument;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.dom.util.SAXDocumentFactory;
import org.apache.batik.dom.util.XLinkSupport;
import org.apache.batik.script.Interpreter;
import org.apache.batik.script.InterpreterException;
import org.apache.batik.script.ScriptEventWrapper;
import org.apache.batik.util.EncodingUtilities;
import org.apache.batik.util.ParsedURL;
import org.apache.batik.util.RunnableQueue;
import org.apache.batik.util.SVGConstants;
import org.apache.batik.util.XMLConstants;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.MutationEvent;
import org.w3c.dom.svg.SVGDocument;
/**
* This class contains the informations needed by the SVG scripting.
*
* @author Stephane Hillion
* @version $Id: ScriptingEnvironment.java 594367 2007-11-13 00:40:53Z cam $
*/
public class ScriptingEnvironment extends BaseScriptingEnvironment {
public static final String [] SVG_EVENT_ATTRS = {
"onabort", // SVG element
"onerror", // SVG element
"onresize", // SVG element
"onscroll", // SVG element
"onunload", // SVG element
"onzoom", // SVG element
"onbegin", // SMIL
"onend", // SMIL
"onrepeat", // SMIL
"onfocusin", // UI Events
"onfocusout", // UI Events
"onactivate", // UI Events
"onclick", // UI Events
"onmousedown", // UI Events
"onmouseup", // UI Events
"onmouseover", // UI Events
"onmouseout", // UI Events
"onmousemove", // UI Events
"onkeypress", // UI Events
"onkeydown", // UI Events
"onkeyup" // UI Events
};
public static final String [] SVG_DOM_EVENT = {
"SVGAbort", // SVG element
"SVGError", // SVG element
"SVGResize", // SVG element
"SVGScroll", // SVG element
"SVGUnload", // SVG element
"SVGZoom", // SVG element
"beginEvent", // SMIL
"endEvent", // SMIL
"repeatEvent", // SMIL
"DOMFocusIn", // UI Events
"DOMFocusOut", // UI Events
"DOMActivate", // UI Events
"click", // UI Events
"mousedown", // UI Events
"mouseup", // UI Events
"mouseover", // UI Events
"mouseout", // UI Events
"mousemove", // UI Events
"keypress", // UI Events
"keydown", // UI Events
"keyup" // UI Events
};
/**
* The timer for periodic or delayed tasks.
*/
protected Timer timer = new Timer(true);
/**
* The update manager.
*/
protected UpdateManager updateManager;
/**
* The update runnable queue.
*/
protected RunnableQueue updateRunnableQueue;
/**
* The DOMNodeInserted event listener.
*/
protected EventListener domNodeInsertedListener;
/**
* The DOMNodeRemoved event listener.
*/
protected EventListener domNodeRemovedListener;
/**
* The DOMAttrModified event listener.
*/
protected EventListener domAttrModifiedListener;
/**
* The SVGAbort event listener.
*/
protected EventListener svgAbortListener =
new ScriptingEventListener("onabort");
/**
* The SVGError event listener.
*/
protected EventListener svgErrorListener =
new ScriptingEventListener("onerror");
/**
* The SVGResize event listener.
*/
protected EventListener svgResizeListener =
new ScriptingEventListener("onresize");
/**
* The SVGScroll event listener.
*/
protected EventListener svgScrollListener =
new ScriptingEventListener("onscroll");
/**
* The SVGUnload event listener.
*/
protected EventListener svgUnloadListener =
new ScriptingEventListener("onunload");
/**
* The SVGZoom event listener.
*/
protected EventListener svgZoomListener =
new ScriptingEventListener("onzoom");
/**
* The begin event listener.
*/
protected EventListener beginListener =
new ScriptingEventListener("onbegin");
/**
* The end event listener.
*/
protected EventListener endListener =
new ScriptingEventListener("onend");
/**
* The repeat event listener.
*/
protected EventListener repeatListener =
new ScriptingEventListener("onrepeat");
/**
* The focusin event listener.
*/
protected EventListener focusinListener =
new ScriptingEventListener("onfocusin");
/**
* The focusout event listener.
*/
protected EventListener focusoutListener =
new ScriptingEventListener("onfocusout");
/**
* The activate event listener.
*/
protected EventListener activateListener =
new ScriptingEventListener("onactivate");
/**
* The click event listener.
*/
protected EventListener clickListener =
new ScriptingEventListener("onclick");
/**
* The mousedown event listener.
*/
protected EventListener mousedownListener =
new ScriptingEventListener("onmousedown");
/**
* The mouseup event listener.
*/
protected EventListener mouseupListener =
new ScriptingEventListener("onmouseup");
/**
* The mouseover event listener.
*/
protected EventListener mouseoverListener =
new ScriptingEventListener("onmouseover");
/**
* The mouseout event listener.
*/
protected EventListener mouseoutListener =
new ScriptingEventListener("onmouseout");
/**
* The mousemove event listener.
*/
protected EventListener mousemoveListener =
new ScriptingEventListener("onmousemove");
/**
* The keypress event listener.
*/
protected EventListener keypressListener =
new ScriptingEventListener("onkeypress");
/**
* The keydown event listener.
*/
protected EventListener keydownListener =
new ScriptingEventListener("onkeydown");
/**
* The keyup event listener.
*/
protected EventListener keyupListener =
new ScriptingEventListener("onkeyup");
protected EventListener [] listeners = {
svgAbortListener,
svgErrorListener,
svgResizeListener,
svgScrollListener,
svgUnloadListener,
svgZoomListener,
beginListener,
endListener,
repeatListener,
focusinListener,
focusoutListener,
activateListener,
clickListener,
mousedownListener,
mouseupListener,
mouseoverListener,
mouseoutListener,
mousemoveListener,
keypressListener,
keydownListener,
keyupListener
};
Map attrToDOMEvent = new HashMap(SVG_EVENT_ATTRS.length);
Map attrToListener = new HashMap(SVG_EVENT_ATTRS.length);
{
for (int i=0; i listeners
if (elt.hasAttributeNS(null, "onabort")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "SVGAbort",
svgAbortListener, false, null);
}
if (elt.hasAttributeNS(null, "onerror")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "SVGError",
svgErrorListener, false, null);
}
if (elt.hasAttributeNS(null, "onresize")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "SVGResize",
svgResizeListener, false, null);
}
if (elt.hasAttributeNS(null, "onscroll")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "SVGScroll",
svgScrollListener, false, null);
}
if (elt.hasAttributeNS(null, "onunload")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "SVGUnload",
svgUnloadListener, false, null);
}
if (elt.hasAttributeNS(null, "onzoom")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "SVGZoom",
svgZoomListener, false, null);
}
} else {
String name = elt.getLocalName();
if (name.equals(SVGConstants.SVG_SET_TAG) ||
name.startsWith("animate")) {
// animation listeners
if (elt.hasAttributeNS(null, "onbegin")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "beginEvent",
beginListener, false, null);
}
if (elt.hasAttributeNS(null, "onend")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "endEvent",
endListener, false, null);
}
if (elt.hasAttributeNS(null, "onrepeat")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "repeatEvent",
repeatListener, false, null);
}
return;
}
}
}
// UI listeners
if (elt.hasAttributeNS(null, "onfocusin")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMFocusIn",
focusinListener, false, null);
}
if (elt.hasAttributeNS(null, "onfocusout")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMFocusOut",
focusoutListener, false, null);
}
if (elt.hasAttributeNS(null, "onactivate")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMActivate",
activateListener, false, null);
}
if (elt.hasAttributeNS(null, "onclick")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "click",
clickListener, false, null);
}
if (elt.hasAttributeNS(null, "onmousedown")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "mousedown",
mousedownListener, false, null);
}
if (elt.hasAttributeNS(null, "onmouseup")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "mouseup",
mouseupListener, false, null);
}
if (elt.hasAttributeNS(null, "onmouseover")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "mouseover",
mouseoverListener, false, null);
}
if (elt.hasAttributeNS(null, "onmouseout")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "mouseout",
mouseoutListener, false, null);
}
if (elt.hasAttributeNS(null, "onmousemove")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "mousemove",
mousemoveListener, false, null);
}
if (elt.hasAttributeNS(null, "onkeypress")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "keypress",
keypressListener, false, null);
}
if (elt.hasAttributeNS(null, "onkeydown")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "keydown",
keydownListener, false, null);
}
if (elt.hasAttributeNS(null, "onkeyup")) {
target.addEventListenerNS
(XMLConstants.XML_EVENTS_NAMESPACE_URI, "keyup",
keyupListener, false, null);
}
}
/**
* Removes the scripting listeners from the given element and all
* of its descendants.
*/
protected void removeScriptingListeners(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
// Detach the listeners
removeScriptingListenersOn((Element) node);
}
// Removes the listeners from the children
for (Node n = node.getFirstChild();
n != null;
n = n.getNextSibling()) {
removeScriptingListeners(n);
}
}
/**
* Removes the scripting listeners from the given element.
*/
protected void removeScriptingListenersOn(Element elt) {
NodeEventTarget target = (NodeEventTarget)elt;
if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) {
if (SVGConstants.SVG_SVG_TAG.equals(elt.getLocalName())) {
//
© 2015 - 2025 Weber Informatics LLC | Privacy Policy