Revision: 13058
http://sourceforge.net/p/foray/code/13058
Author: victormote
Date: 2023-01-12 19:22:37 +0000 (Thu, 12 Jan 2023)
Log Message:
-----------
Allow foreign namespace to do some post-parse processing.
Modified Paths:
--------------
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeParser4a.java
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/NamespaceForeign.java
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/ForeignNamespaceHandler.java
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceMath.java
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceSvg.java
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeParser4a.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeParser4a.java 2023-01-12 18:14:40 UTC (rev 13057)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeParser4a.java 2023-01-12 19:22:37 UTC (rev 13058)
@@ -387,7 +387,9 @@
throw new IllegalStateException("Unexpected document type:" + document.getClass().getName());
}
- parent.setForeignXml(document);
+ this.proxyHandler.finalizeParsing();
+ /* The document instance can change during finalizeParsing, so get it again. */
+ parent.setForeignXml(this.proxyHandler.getDocument());
this.proxyHandler = null;
}
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/NamespaceForeign.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/NamespaceForeign.java 2023-01-12 18:14:40 UTC (rev 13057)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/NamespaceForeign.java 2023-01-12 19:22:37 UTC (rev 13058)
@@ -82,4 +82,13 @@
*/
public abstract ForeignNamespaceHandler getHandler();
+ /**
+ * Gives subclasses an opportunity to do any post-parsing finalization work.
+ * CAVEAT: The subclass may replace the object returned by {@link ForeignNamespaceHandler#getDocument()} as part of
+ * this process.
+ * Always use the value returned after running this method for any downstream processing.
+ * @param handler The handler containing the parsed document and content handler.
+ */
+ public abstract void finalizeHandler(ForeignNamespaceHandler handler);
+
}
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/ForeignNamespaceHandler.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/ForeignNamespaceHandler.java 2023-01-12 18:14:40 UTC (rev 13057)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/ForeignNamespaceHandler.java 2023-01-12 19:22:37 UTC (rev 13058)
@@ -28,6 +28,8 @@
package org.foray.fotree.foreign;
+import org.foray.fotree.NamespaceForeign;
+
import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
@@ -44,7 +46,18 @@
/** The content handler to be used to parse the foreign XML. */
private ContentHandler contentHandler;
+ /** The foreign namespace that created this handler. */
+ private NamespaceForeign namespace;
+
/**
+ * Constructor.
+ * @param namespace The foreign namespace that created this handler.
+ */
+ public ForeignNamespaceHandler(final NamespaceForeign namespace) {
+ this.namespace = namespace;
+ }
+
+ /**
* Returns the document.
* @return The document.
*/
@@ -62,7 +75,7 @@
/**
* Returns the content handler to be used to parse the foreign XML.
- * @return the content handler to be used to parse the foreign XML..
+ * @return the content handler to be used to parse the foreign XML.
*/
public ContentHandler getContentHandler() {
return contentHandler;
@@ -76,5 +89,11 @@
this.contentHandler = contentHandler;
}
+ /**
+ * Allow the namespace to do any specific post-parse processing that is needed.
+ */
+ public void finalizeParsing() {
+ this.namespace.finalizeHandler(this);
+ }
}
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceMath.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceMath.java 2023-01-12 18:14:40 UTC (rev 13057)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceMath.java 2023-01-12 19:22:37 UTC (rev 13058)
@@ -72,7 +72,7 @@
final ContentHandler contentHandler = new Sax2DomParser(document);
- final ForeignNamespaceHandler handler = new ForeignNamespaceHandler();
+ final ForeignNamespaceHandler handler = new ForeignNamespaceHandler(this);
handler.setDocument(document);
handler.setContentHandler(contentHandler);
@@ -79,4 +79,9 @@
return handler;
}
+ @Override
+ public void finalizeHandler(final ForeignNamespaceHandler handler) {
+ /* Nothing to do here. */
+ }
+
}
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceSvg.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceSvg.java 2023-01-12 18:14:40 UTC (rev 13057)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/foreign/NamespaceSvg.java 2023-01-12 19:22:37 UTC (rev 13058)
@@ -29,15 +29,25 @@
package org.foray.fotree.foreign;
import org.foray.fotree.NamespaceForeign;
+import org.foray.primitive.StringUtils;
+import org.foray.xml.DomUtil;
import org.foray.xml.Sax2DomParser;
import org.axsl.constants.XmlConstants;
+import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.anim.dom.SVGDOMImplementation;
+import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.ls.LSOutput;
+import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.svg.SVGDocument;
import org.xml.sax.ContentHandler;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
/**
* This class manages the namespace that is used for SVG.
*/
@@ -77,7 +87,7 @@
// return new SAXSVGDocumentFactory(SaxParser.getParserClassName());
final ContentHandler contentHandler = new Sax2DomParser(document);
- final ForeignNamespaceHandler handler = new ForeignNamespaceHandler();
+ final ForeignNamespaceHandler handler = new ForeignNamespaceHandler(this);
handler.setDocument(document);
handler.setContentHandler(contentHandler);
@@ -84,4 +94,26 @@
return handler;
}
+ @Override
+ public void finalizeHandler(final ForeignNamespaceHandler handler) {
+ /* Reparse the SVG document using the standard Batik approach. */
+ final LSSerializer lss = DomUtil.createLSSerializer();
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final LSOutput lso = DomUtil.createLSOutput(baos, StringUtils.EMPTY_STRING);
+ lso.setByteStream(baos);
+ lss.write(handler.getDocument(), lso);
+ final byte[] bytes = baos.toByteArray();
+ final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+
+ final String parser = XMLResourceDescriptor.getXMLParserClassName();
+ final SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
+ try {
+ final SVGDocument document = factory.createSVGDocument("", bais);
+ handler.setDocument(document);
+ } catch (final IOException e) {
+ /* This shouldn't happen. We aren't actually doing IO. */
+ throw new IllegalStateException(e);
+ }
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|