deweese 2002/07/23 17:02:28 Modified: . build.bat sources/org/apache/batik/bridge ScriptingEnvironment.java UpdateManager.java sources/org/apache/batik/dom/util SAXDocumentFactory.java sources/org/apache/batik/swing/svg JSVGComponent.java Log: 1) getURL now reports when security settings prohibit fetching data. 2) UpdateManager no longer triggers a repaint after _every_ java script function. It only does a repaint if a certain amount of time has passed or if no more java script functions are pending. 3) SAXDocumentFactory no longer requires you to provide the expected type of the document element prior to parsing. 4) JSVGComponent now triggers redraws in response to scripts run from key events. Revision Changes Path 1.9 +1 -1 xml-batik/build.bat Index: build.bat =================================================================== RCS file: /home/cvs/xml-batik/build.bat,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- build.bat 18 Oct 2001 07:40:11 -0000 1.8 +++ build.bat 24 Jul 2002 00:02:27 -0000 1.9 @@ -24,7 +24,7 @@ :: ----- Execute The Requested Build ------------------------------------------ -%JAVA_HOME%\bin\java.exe %ANT_OPTS% -classpath %CP% org.apache.tools.ant.Main -Dant.home=%ANT_HOME% %1 -Dargs="%2 %3 %4 %5 %6 %7 %8 %9" +%JAVA_HOME%\bin\java.exe %ANT_OPTS% -classpath %CP% org.apache.tools.ant.Main -emacs -Dant.home=%ANT_HOME% %1 -Dargs="%2 %3 %4 %5 %6 %7 %8 %9" :: ----- Cleanup the environment ---------------------------------------------- 1.33 +23 -6 xml-batik/sources/org/apache/batik/bridge/ScriptingEnvironment.java Index: ScriptingEnvironment.java =================================================================== RCS file: /home/cvs/xml-batik/sources/org/apache/batik/bridge/ScriptingEnvironment.java,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- ScriptingEnvironment.java 14 Jun 2002 13:12:24 -0000 1.32 +++ ScriptingEnvironment.java 24 Jul 2002 00:02:27 -0000 1.33 @@ -19,6 +19,7 @@ import java.util.Timer; import java.util.TimerTask; +import org.apache.batik.dom.util.SAXDocumentFactory; import org.apache.batik.dom.svg.SAXSVGDocumentFactory; import org.apache.batik.dom.svg.SVGOMDocument; @@ -682,6 +683,7 @@ * org.apache.batik.script.Window#parseXML(String,Document)}. */ public DocumentFragment parseXML(String text, Document doc) { + // System.out.println("Text: " + text); SAXSVGDocumentFactory df = new SAXSVGDocumentFactory (XMLResourceDescriptor.getXMLParserClassName()); String uri = ((SVGOMDocument)bridgeContext.getDocument()). @@ -699,10 +701,10 @@ sb.append(FRAGMENT_PREFIX); sb.append(text); sb.append("</svg>"); - text = sb.toString(); + String newText = sb.toString(); try { - Document d = df.createDocument(uri, - new StringReader(text)); + Document d = df.createDocument + (uri, new StringReader(newText)); for (Node n = d.getDocumentElement().getFirstChild(); n != null; n = n.getNextSibling()) { @@ -714,7 +716,19 @@ } } } catch (Exception exc) { - // !!! TODO: warning + SAXDocumentFactory sdf = new SAXDocumentFactory + (doc.getImplementation(), + XMLResourceDescriptor.getXMLParserClassName()); + try { + Document d = sdf.createDocument + ("http://example.org/Foo", "foo", + uri, new StringReader(text)); + result = doc.createDocumentFragment(); + result.appendChild(doc.importNode(d.getDocumentElement(), true)); + } catch (Exception ext) { + if (userAgent != null) + userAgent.displayError(ext); + } } } return result; @@ -742,7 +756,7 @@ burl = ((SVGOMDocument)document).getURLObject(); final ParsedURL purl = new ParsedURL(burl, uri); String e = EncodingUtilities.javaEncoding(enc); - e = (e == null) ? enc : e; + e = ((e == null) ? enc : e); Reader r = new InputStreamReader(purl.openStream(), e); r = new BufferedReader(r); @@ -767,6 +781,9 @@ } }); } catch (Exception e) { + if (e instanceof SecurityException) { + userAgent.displayError(e); + } updateRunnableQueue.invokeLater(new Runnable() { public void run() { try { 1.17 +30 -1 xml-batik/sources/org/apache/batik/bridge/UpdateManager.java Index: UpdateManager.java =================================================================== RCS file: /home/cvs/xml-batik/sources/org/apache/batik/bridge/UpdateManager.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- UpdateManager.java 19 Mar 2002 09:25:40 -0000 1.16 +++ UpdateManager.java 24 Jul 2002 00:02:28 -0000 1.17 @@ -45,6 +45,19 @@ */ public class UpdateManager implements RunnableQueue.RunHandler { + static final long MIN_REPAINT_TIME; + static { + String s = System.getProperty + ("org.apache.batik.min_repaint_time", "20"); + long value = 20; + try { + value = Long.parseLong(s); + } catch (NumberFormatException nfe) { + } finally { + MIN_REPAINT_TIME = value; + } + } + /** * Tells whether the given SVG document is dynamic. */ @@ -322,16 +335,32 @@ } } + long lastRepaint=0; + /** * Repaints the dirty areas, if needed. */ public void repaint() { + long ctime = System.currentTimeMillis(); if (updateTracker.hasChanged()) { + if (ctime-lastRepaint < MIN_REPAINT_TIME) { + // We very recently did a repaint check if other + // repaint runnables are pending. + Iterator i = updateRunnableQueue.iterator(); + while (i.hasNext()) + if (!(i.next() instanceof NoRepaintRunnable)) + // have a pending repaint runnable so we + // will skip this repaint and we will let + // the next one pick it up. + return; + } + List dirtyAreas = updateTracker.getDirtyAreas(); if (dirtyAreas != null) { updateRendering(dirtyAreas); } updateTracker.clear(); + lastRepaint = System.currentTimeMillis(); } } 1.12 +146 -57 xml-batik/sources/org/apache/batik/dom/util/SAXDocumentFactory.java Index: SAXDocumentFactory.java =================================================================== RCS file: /home/cvs/xml-batik/sources/org/apache/batik/dom/util/SAXDocumentFactory.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- SAXDocumentFactory.java 12 Jun 2002 08:20:34 -0000 1.11 +++ SAXDocumentFactory.java 24 Jul 2002 00:02:28 -0000 1.12 @@ -8,6 +8,10 @@ package org.apache.batik.dom.util; +import java.util.List; +import java.util.LinkedList; +import java.util.Iterator; + import java.io.InputStream; import java.io.InterruptedIOException; import java.io.IOException; @@ -58,7 +62,7 @@ * The created document. */ protected Document document; - + /** * The created document descriptor. */ @@ -95,11 +99,6 @@ protected boolean isValidating; /** - * Whether the document element has been parsed. - */ - protected boolean documentElementParsed; - - /** * The stack used to store the namespace URIs. */ protected HashTableStack namespaces; @@ -109,6 +108,57 @@ */ protected ErrorHandler errorHandler; + interface PreInfo { + public Node createNode(Document doc); + } + + static class ProcessingInstructionInfo implements PreInfo { + public String target, data; + public ProcessingInstructionInfo(String target, String data) { + this.target = target; + this.data = data; + } + public Node createNode(Document doc) { + return doc.createProcessingInstruction(target, data); + } + }; + + static class CommentInfo implements PreInfo { + public String comment; + public CommentInfo(String comment) { + this.comment = comment; + } + public Node createNode(Document doc) { + return doc.createComment(comment); + } + }; + + static class CDataInfo implements PreInfo { + public String cdata; + public CDataInfo(String cdata) { + this.cdata = cdata; + } + public Node createNode(Document doc) { + return doc.createCDATASection(cdata); + } + }; + + static class TextInfo implements PreInfo { + public String text; + public TextInfo(String text) { + this.text = text; + } + public Node createNode(Document doc) { + return doc.createTextNode(text); + } + }; + + /** + * Various elements encountered prior to real document root element. + * List of PreInfo objects. + */ + protected List preInfo; + /** * Creates a new SAXDocumentFactory object. * No document descriptor will be created while generating a document. @@ -178,7 +228,7 @@ } /** - * Creates a GenericDocument. + * Creates a Document. * @param ns The namespace URI of the root element. * @param root The name of the root element. * @param uri The document URI. @@ -188,7 +238,42 @@ protected Document createDocument(String ns, String root, String uri, InputSource is) throws IOException { - document = implementation.createDocument(ns, root, null); + Document ret = createDocument(is); + Element docElem = ret.getDocumentElement(); + + String lname = root; + String nsURI = ns; + if (ns == null) { + int idx = lname.indexOf(':'); + String nsp = (idx == -1 || idx == lname.length()-1) + ? "" + : lname.substring(0, idx); + nsURI = namespaces.get(nsp); + if (idx != -1 && idx != lname.length()-1) { + lname = lname.substring(idx+1); + } + } + + + if ((!docElem.getNamespaceURI().equals(nsURI)) || + (!docElem.getLocalName().equals(lname))) { + throw new IOException("Root element does not match requested Document."); + } + + return ret; + } + + + /** + * Creates a Document. + * @param ns The namespace URI of the root element. + * @param root The name of the root element. + * @param uri The document URI. + * @param is The document input source. + * @exception IOException if an error occured while reading the document. + */ + protected Document createDocument(InputSource is) + throws IOException { try { XMLReader parser = XMLReaderFactory.createXMLReader(parserClassName); @@ -216,7 +301,10 @@ throw new IOException(e.getMessage()); } - return document; + currentNode = null; + Document ret = document; + document = null; + return ret; } /** @@ -267,15 +355,15 @@ * org.xml.sax.ContentHandler#startDocument()}. */ public void startDocument() throws SAXException { + preInfo = new LinkedList(); namespaces = new HashTableStack(); namespaces.put("xml", XMLSupport.XML_NAMESPACE_URI); namespaces.put("xmlns", XMLSupport.XMLNS_NAMESPACE_URI); namespaces.put("", null); - documentElementParsed = false; inCDATA = false; inDTD = false; - currentNode = document; + currentNode = null; if (createDocumentDescriptor) { documentDescriptor = new DocumentDescriptor(); @@ -327,34 +415,21 @@ ? "" : rawName.substring(0, idx); String nsURI = namespaces.get(nsp); - - if (currentNode == document) { - e = document.getDocumentElement(); - String lname = rawName; - if (idx != -1 && idx != rawName.length()-1) { - lname = rawName.substring(idx+1); - } - if (e.getNamespaceURI() != null && nsp.length() != 0) { - if (!e.getLocalName().equals(lname)) { - throw new SAXException("Bad root element"); - } - e.setPrefix(nsp); - } - String xmlns; - Attr attr = (nsp.equals("")) - ? e.getAttributeNodeNS(XMLSupport.XMLNS_NAMESPACE_URI, - xmlns = "xmlns") - : e.getAttributeNodeNS(XMLSupport.XMLNS_NAMESPACE_URI, - xmlns = "xmlns:" + nsp); - if (attr != null) { - namespaces.put(nsp, attr.getValue()); - } - documentElementParsed = true; - } else { - e = document.createElementNS(nsURI, rawName); - currentNode.appendChild(e); - } - currentNode = e; + if (currentNode == null) { + document = implementation.createDocument(nsURI, rawName, null); + Iterator i = preInfo.iterator(); + currentNode = e = document.getDocumentElement(); + while (i.hasNext()) { + PreInfo pi = (PreInfo)i.next(); + Node n = pi.createNode(document); + document.insertBefore(n, e); + } + preInfo = null; + } else { + e = document.createElementNS(nsURI, rawName); + currentNode.appendChild(e); + currentNode = e; + } // Storage of the line number. if (createDocumentDescriptor && locator != null) { @@ -380,6 +455,14 @@ /** * <b>SAX</b>: Implements {@link + * org.xml.sax.ErrorHandler#fatalError(SAXParseException)}. + */ + public void fatalError(SAXParseException ex) throws SAXException { + throw ex; + } + + /** + * <b>SAX</b>: Implements {@link * org.xml.sax.ErrorHandler#error(SAXParseException)}. */ public void error(SAXParseException ex) throws SAXException { @@ -399,7 +482,8 @@ */ public void endElement(String uri, String localName, String rawName) throws SAXException { - currentNode = currentNode.getParentNode(); + if (currentNode != null) + currentNode = currentNode.getParentNode(); namespaces.pop(); } @@ -410,10 +494,15 @@ public void characters(char ch[], int start, int length) throws SAXException { String data = new String(ch, start, length); - Node n = (inCDATA) - ? document.createCDATASection(data) - : document.createTextNode(data); - currentNode.appendChild(n); + if (currentNode == null) { + if (inCDATA) preInfo.add(new CDataInfo(data)); + else preInfo.add(new TextInfo(data)); + } else { + Node n = (inCDATA) + ? document.createCDATASection(data) + : document.createTextNode(data); + currentNode.appendChild(n); + } } /** @@ -423,12 +512,11 @@ public void processingInstruction(String target, String data) throws SAXException { if (!inDTD) { - Node n = document.createProcessingInstruction(target, data); - if (currentNode == document && !documentElementParsed) { - currentNode.insertBefore(n, document.getDocumentElement()); - } else { - currentNode.appendChild(n); - } + if (currentNode == null) + preInfo.add(new ProcessingInstructionInfo(target, data)); + else + currentNode.appendChild + (document.createProcessingInstruction(target, data)); } } @@ -486,12 +574,13 @@ */ public void comment(char ch[], int start, int length) throws SAXException { if (!inDTD) { - Node n = document.createComment(new String(ch, start, length)); - if (currentNode == document && !documentElementParsed) { - currentNode.insertBefore(n, document.getDocumentElement()); - } else { - currentNode.appendChild(n); - } + String str = new String(ch, start, length); + if (currentNode == null) { + preInfo.add(new CommentInfo(str)); + } else { + currentNode.appendChild + (document.createComment(str)); + } } } } 1.56 +59 -1 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.55 retrieving revision 1.56 diff -u -r1.55 -r1.56 --- JSVGComponent.java 14 Jun 2002 13:12:25 -0000 1.55 +++ JSVGComponent.java 24 Jul 2002 00:02:28 -0000 1.56 @@ -1308,6 +1308,64 @@ /** * Dispatches the event to the GVT tree. */ + protected void dispatchKeyTyped(final KeyEvent e) { + if (!isDynamicDocument) { + super.dispatchKeyTyped(e); + return; + } + + if (updateManager != null && updateManager.isRunning()) { + updateManager.getUpdateRunnableQueue().invokeLater + (new Runnable() { + public void run() { + eventDispatcher.keyTyped(e); + } + }); + } + + } + + /** + * Dispatches the event to the GVT tree. + */ + protected void dispatchKeyPressed(final KeyEvent e) { + if (!isDynamicDocument) { + super.dispatchKeyPressed(e); + return; + } + + if (updateManager != null && updateManager.isRunning()) { + updateManager.getUpdateRunnableQueue().invokeLater + (new Runnable() { + public void run() { + eventDispatcher.keyPressed(e); + } + }); + } + } + + /** + * Dispatches the event to the GVT tree. + */ + protected void dispatchKeyReleased(final KeyEvent e) { + if (!isDynamicDocument) { + super.dispatchKeyReleased(e); + return; + } + + if (updateManager != null && updateManager.isRunning()) { + updateManager.getUpdateRunnableQueue().invokeLater + (new Runnable() { + public void run() { + eventDispatcher.keyReleased(e); + } + }); + } + } + + /** + * Dispatches the event to the GVT tree. + */ protected void dispatchMouseClicked(final MouseEvent e) { if (!isDynamicDocument) { super.dispatchMouseClicked(e);
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]