]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
handles foreign namespace elements and unknown elements better
authorKeiron Liddle <keiron@apache.org>
Thu, 13 Sep 2001 07:49:32 +0000 (07:49 +0000)
committerKeiron Liddle <keiron@apache.org>
Thu, 13 Sep 2001 07:49:32 +0000 (07:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194462 13f79535-47bb-0310-9956-ffa450edef68

src/org/apache/fop/fo/DirectPropertyListBuilder.java
src/org/apache/fop/fo/FOTreeBuilder.java
src/org/apache/fop/fo/UnknownXMLObj.java [new file with mode: 0644]
src/org/apache/fop/fo/XMLElement.java [new file with mode: 0644]
src/org/apache/fop/fo/XMLObj.java [new file with mode: 0644]
src/org/apache/fop/svg/SVGElement.java
src/org/apache/fop/svg/XMLObj.java [deleted file]

index e039b3731848cf75de6f1dac570f432f9d28ce19..7c6b6f6e481ca8bd258651fac46eba3f46862f85 100644 (file)
@@ -35,7 +35,7 @@ public class DirectPropertyListBuilder extends PropertyListBuilder {
         return ret;
     }
 
-    public class AttrPropertyList extends PropertyList {
+    public static class AttrPropertyList extends PropertyList {
         Attributes attributes;
         AttrPropertyList(Attributes attr) {
             super(null, null, null);
index 359ca6fbc5cd662cc3082122c2a7b1a944b8879c..ae67d2d9124b632c7b5bec40f769eda08a8407ac 100644 (file)
@@ -27,6 +27,7 @@ import org.xml.sax.Attributes;
 // Java
 import java.util.Hashtable;
 import java.util.Stack;
+import java.util.Vector;
 import java.io.IOException;
 
 /**
@@ -48,6 +49,8 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
      */
     protected Hashtable fobjTable = new Hashtable();
 
+    protected Vector namespaces = new Vector();
+
     /**
      * class that builds a property list for each formatting object
      */
@@ -99,6 +102,7 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
     public void addMapping(String namespaceURI, String localName,
                            FObj.Maker maker) {
         this.fobjTable.put(namespaceURI + "^" + localName, maker);
+        this.namespaces.addElement(namespaceURI.intern());
     }
 
     /**
@@ -215,13 +219,20 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
         PropertyListBuilder currentListBuilder =
             (PropertyListBuilder)this.propertylistTable.get(uri);
 
+        boolean foreignXML = false;
         if (fobjMaker == null) {
             if (!this.unknownFOs.containsKey(fullName)) {
                 this.unknownFOs.put(fullName, "");
                 log.error("Unknown formatting object "
                                        + fullName);
             }
-            fobjMaker = new Unknown.Maker();    // fall back
+            if(namespaces.contains(uri.intern())) {
+                // fall back
+                fobjMaker = new Unknown.Maker();
+            } else {
+                fobjMaker = new UnknownXMLObj.Maker(uri, localName);
+                foreignXML = true;
+            }
         }
 
         try {
@@ -231,6 +242,8 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
                     currentListBuilder.makeList(fullName, attlist,
                                                 (currentFObj == null) ? null
                                                 : currentFObj.properties, currentFObj);
+            } else if(foreignXML) {
+                list = new DirectPropertyListBuilder.AttrPropertyList(attlist);
             } else {
                 if(currentFObj == null) {
                     throw new FOPException("Invalid XML or missing namespace");
diff --git a/src/org/apache/fop/fo/UnknownXMLObj.java b/src/org/apache/fop/fo/UnknownXMLObj.java
new file mode 100644 (file)
index 0000000..5c734aa
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.fo;
+
+import org.apache.fop.fo.*;
+import org.apache.fop.layout.Area;
+import org.apache.fop.layout.FontState;
+import org.apache.fop.layout.inline.*;
+import org.apache.fop.apps.FOPException;
+
+import org.w3c.dom.Element;
+
+public class UnknownXMLObj extends XMLObj {
+    String namespace;
+
+    /**
+     * inner class for making unknown xml objects.
+     */
+    public static class Maker extends FObj.Maker {
+        String space;
+        String tag;
+
+        Maker(String sp, String t) {
+            space = sp;
+            tag = t;
+        }
+
+        /**
+         * make an unknown xml object.
+         *
+         * @param parent the parent formatting object
+         * @param propertyList the explicit properties of this object
+         *
+         * @return the unknown xml object
+         */
+        public FObj make(FObj parent,
+                         PropertyList propertyList) throws FOPException {
+            return new UnknownXMLObj(parent, propertyList, space, tag);
+        }
+    }
+
+    /**
+     * returns the maker for this object.
+     *
+     * @return the maker for an unknown xml object
+     */
+    public static FObj.Maker maker(String space, String tag) {
+        return new UnknownXMLObj.Maker(space, tag);
+    }
+
+    /**
+     * constructs an unknown xml object (called by Maker).
+     *
+     * @param parent the parent formatting object
+     * @param propertyList the explicit properties of this object
+     */
+    protected UnknownXMLObj(FObj parent, PropertyList propertyList, String space, String tag) {
+        super(parent, propertyList, tag);
+        this.namespace = space;
+        this.name = this.namespace + ":" + tag;
+    }
+
+    public String getNameSpace() {
+        return this.namespace;
+    }
+
+    protected void addChild(FONode child) {
+        if(doc == null) {
+            createBasicDocument();
+        }
+        super.addChild(child);
+    }
+
+    protected void addCharacters(char data[], int start, int length) {
+        if(doc == null) {
+            createBasicDocument();
+        }
+        super.addCharacters(data, start, length);
+    }
+
+    public Status layout(Area area) throws FOPException {
+        //if (!(area instanceof ForeignObjectArea)) {
+            // this is an error
+            //throw new FOPException("Foreign XML not in fo:instream-foreign-object");
+        //}
+        log.error("no handler defined for " + this.name + " foreign xml");
+
+        /* return status */
+        return new Status(Status.OK);
+    }
+}
+
diff --git a/src/org/apache/fop/fo/XMLElement.java b/src/org/apache/fop/fo/XMLElement.java
new file mode 100644 (file)
index 0000000..dc85864
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.fo;
+
+// FOP
+import org.apache.fop.fo.properties.*;
+import org.apache.fop.layout.Area;
+import org.apache.fop.layout.inline.*;
+import org.apache.fop.apps.FOPException;
+
+/**
+ * class representing svg:svg pseudo flow object.
+ */
+public class XMLElement extends XMLObj {
+    String namespace = "";
+
+    /**
+     * inner class for making XML objects.
+     */
+    public static class Maker extends FObj.Maker {
+        String tag;
+
+        Maker(String t) {
+            tag = t;
+        }
+
+        /**
+         * make an XML object.
+         *
+         * @param parent the parent formatting object
+         * @param propertyList the explicit properties of this object
+         *
+         * @return the XML object
+         */
+        public FObj make(FObj parent,
+                         PropertyList propertyList) throws FOPException {
+            return new XMLElement(parent, propertyList, tag);
+        }
+    }
+
+    /**
+     * returns the maker for this object.
+     *
+     * @return the maker for XML objects
+     */
+    public static FObj.Maker maker(String tag) {
+        return new XMLElement.Maker(tag);
+    }
+
+    /**
+     * constructs an XML object (called by Maker).
+     *
+     * @param parent the parent formatting object
+     * @param propertyList the explicit properties of this object
+     */
+    public XMLElement(FObj parent, PropertyList propertyList, String tag) {
+        super(parent, propertyList, tag);
+        init();
+    }
+
+    /**
+     * layout this formatting object.
+     *
+     * @param area the area to layout the object into
+     *
+     * @return the status of the layout
+     */
+    public Status layout(final Area area) throws FOPException {
+
+        if (!(area instanceof ForeignObjectArea)) {
+            // this is an error
+            throw new FOPException("XML not in fo:instream-foreign-object");
+        }
+
+        /* return status */
+        return new Status(Status.OK);
+    }
+
+    private void init() {
+        createBasicDocument();
+    }
+
+    public String getNameSpace() {
+        return namespace;
+    }
+}
diff --git a/src/org/apache/fop/fo/XMLObj.java b/src/org/apache/fop/fo/XMLObj.java
new file mode 100644 (file)
index 0000000..1b17bf2
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.fo;
+
+// FOP
+import org.apache.fop.fo.*;
+import org.apache.fop.layout.Area;
+import org.apache.fop.layout.FontState;
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.layout.LinkSet;
+import org.apache.fop.datatypes.IDReferences;
+
+import org.w3c.dom.*;
+import org.xml.sax.Attributes;
+
+import java.util.*;
+
+/**
+ * Since SVG objects are not layed out then this class checks
+ * that this element is not being layed out inside some incorrect
+ * element.
+ */
+public abstract class XMLObj extends FObj {
+
+    protected String tagName = "";
+
+    protected Element element;
+    protected Document doc;
+
+    /**
+     *
+     * @param parent the parent formatting object
+     * @param propertyList the explicit properties of this object
+     */
+    public XMLObj(FObj parent, PropertyList propertyList, String tag) {
+        super(parent, propertyList);
+        tagName = tag;
+    }
+
+    public abstract String getNameSpace();
+
+    protected static Hashtable ns = new Hashtable();
+
+    public void addGraphic(Document doc, Element parent) {
+        this.doc = doc;
+        element = doc.createElementNS(getNameSpace(), tagName);
+
+        if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
+            Attributes attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
+            for (int count = 0; count < attr.getLength(); count++) {
+                String rf = attr.getValue(count);
+                String qname = attr.getQName(count);
+                if (qname.indexOf(":") == -1) {
+                    element.setAttribute(qname, rf);
+                } else {
+                    String pref =
+                        qname.substring(0, qname.indexOf(":"));
+                    if (pref.equals("xmlns")) {
+                        ns.put(qname.substring(qname.indexOf(":")
+                                                      + 1), rf);
+                    }
+                    ns.put("xlink", "http://www.w3.org/1999/xlink");
+                    element.setAttributeNS((String)ns.get(pref),
+                                           qname, rf);
+                }
+            }
+        } else {
+        }
+
+        parent.appendChild(element);
+    }
+
+    public void buildTopLevel(Document doc, Element svgRoot) {
+        // build up the info for the top level element
+        if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
+            Attributes attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
+            for (int count = 0; count < attr.getLength(); count++) {
+                String rf = attr.getValue(count);
+                String qname = attr.getQName(count);
+                if (qname.indexOf(":") == -1) {
+                    element.setAttribute(qname, rf);
+                } else {
+                    String pref =
+                       qname.substring(0, qname.indexOf(":"));
+                    if (pref.equals("xmlns")) {
+                        ns.put(qname.substring(qname.indexOf(":")
+                                                      + 1), rf);
+                    }
+                    ns.put("xlink", "http://www.w3.org/1999/xlink");
+                    element.setAttributeNS((String)ns.get(pref),
+                                           qname, rf);
+                }
+            }
+        } else {
+        }
+    }
+
+    public Document createBasicDocument() {
+        doc = null;
+
+        element = null;
+        try {
+            // DOMImplementation impl = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
+            // String ns = GraphElementMapping.URI;
+            // doc = impl.createDocument(ns, "graph", null);
+            doc =
+                javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+            Element el = doc.createElement("graph");
+            doc.appendChild(el);
+
+            element = doc.getDocumentElement();
+            buildTopLevel(doc, element);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return doc;
+    }
+
+    protected void addChild(FONode child) {
+        if (child instanceof XMLObj) {
+            ((XMLObj)child).addGraphic(doc, element);
+        }
+    }
+
+    protected void addCharacters(char data[], int start, int length) {
+        String str = new String(data, start, length - start);
+        org.w3c.dom.Text text = doc.createTextNode(str);
+        element.appendChild(text);
+    }
+
+    /**
+     * layout this formatting object.
+     *
+     * @param area the area to layout the object into
+     * @return the status of the layout
+     */
+    public Status layout(Area area) throws FOPException {
+        /* generate a warning */
+        log.error("" + this.name + " outside foreign xml");
+
+        /* return status */
+        return new Status(Status.OK);
+    }
+
+    public void removeID(IDReferences idReferences) {}
+
+    /**
+     * These method overrides prevent problems with the different types.
+     */
+    public void setIsInTableCell() {}
+
+    public void forceStartOffset(int offset) {}
+
+    public void forceWidth(int width) {}
+
+    public void resetMarker() {}
+
+    public void setLinkSet(LinkSet linkSet) {}
+
+    public Vector getMarkerSnapshot(Vector snapshot) {
+        return snapshot;
+    }
+
+    public void rollback(Vector snapshot) {}
+
+}
+
index 21117880330086ec055ab54dc30e6de0edbcf02e..e500f6d2f630367f8fe0397aa8360ead93a1a1f2 100644 (file)
@@ -96,6 +96,7 @@ public class SVGElement extends SVGObj {
             this.marker = 0;
         }
 
+        final Element svgRoot = element;
         /* create an SVG area */
         /* if width and height are zero, get the bounds of the content. */
         DefaultSVGContext dc = new DefaultSVGContext() {
@@ -105,7 +106,7 @@ public class SVGElement extends SVGObj {
             }
 
             public float getViewportWidth(Element e) throws IllegalStateException {
-                if(e == element) {
+                if(e == svgRoot) {
                     ForeignObjectArea foa = (ForeignObjectArea)area;
                     if(!foa.isContentWidthAuto()) {
                         return foa.getContentWidth();
@@ -115,7 +116,7 @@ public class SVGElement extends SVGObj {
             }
 
             public float getViewportHeight(Element e) throws IllegalStateException {
-                if(e == element) {
+                if(e == svgRoot) {
                     ForeignObjectArea foa = (ForeignObjectArea)area;
                     if(!foa.isContentHeightAuto()) {
                         return foa.getContentHeight();
diff --git a/src/org/apache/fop/svg/XMLObj.java b/src/org/apache/fop/svg/XMLObj.java
deleted file mode 100644 (file)
index 383005b..0000000
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
- * For details on use and redistribution please refer to the
- * LICENSE file included with these sources.
- */
-
-package org.apache.fop.svg;
-
-// FOP
-import org.apache.fop.fo.*;
-import org.apache.fop.layout.Area;
-import org.apache.fop.layout.FontState;
-import org.apache.fop.apps.FOPException;
-import org.apache.fop.layout.LinkSet;
-
-import org.w3c.dom.*;
-import org.xml.sax.Attributes;
-
-import java.util.*;
-
-/**
- * Since SVG objects are not layed out then this class checks
- * that this element is not being layed out inside some incorrect
- * element.
- */
-public abstract class XMLObj extends FObj {
-
-    protected String tagName = "";
-
-    protected Element element;
-    protected Document doc;
-
-    /**
-     *
-     * @param parent the parent formatting object
-     * @param propertyList the explicit properties of this object
-     */
-    public XMLObj(FObj parent, PropertyList propertyList, String tag) {
-        super(parent, propertyList);
-        tagName = tag;
-    }
-
-    public abstract String getNameSpace();
-
-    protected static Hashtable ns = new Hashtable();
-
-    public void addGraphic(Document doc, Element parent) {
-        this.doc = doc;
-        element = doc.createElementNS(getNameSpace(), tagName);
-
-        if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
-            Attributes attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
-            for (int count = 0; count < attr.getLength(); count++) {
-                String rf = attr.getValue(count);
-                String qname = attr.getQName(count);
-                if (qname.indexOf(":") == -1) {
-                    element.setAttribute(qname, rf);
-                } else {
-                    String pref =
-                        qname.substring(0, qname.indexOf(":"));
-                    if (pref.equals("xmlns")) {
-                        ns.put(qname.substring(qname.indexOf(":")
-                                                      + 1), rf);
-                    }
-                    ns.put("xlink", "http://www.w3.org/1999/xlink");
-                    element.setAttributeNS((String)ns.get(pref),
-                                           qname, rf);
-                }
-            }
-        } else {
-        }
-
-        parent.appendChild(element);
-    }
-
-    public void buildTopLevel(Document doc, Element svgRoot) {
-        // build up the info for the top level element
-        if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
-            Attributes attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
-            for (int count = 0; count < attr.getLength(); count++) {
-                String rf = attr.getValue(count);
-                String qname = attr.getQName(count);
-                if (qname.indexOf(":") == -1) {
-                    element.setAttribute(qname, rf);
-                } else {
-                    String pref =
-                       qname.substring(0, qname.indexOf(":"));
-                    if (pref.equals("xmlns")) {
-                        ns.put(qname.substring(qname.indexOf(":")
-                                                      + 1), rf);
-                    }
-                    ns.put("xlink", "http://www.w3.org/1999/xlink");
-                    element.setAttributeNS((String)ns.get(pref),
-                                           qname, rf);
-                }
-            }
-        } else {
-        }
-    }
-
-    public Document createBasicDocument() {
-        doc = null;
-
-        element = null;
-        try {
-            // DOMImplementation impl = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
-            // String ns = GraphElementMapping.URI;
-            // doc = impl.createDocument(ns, "graph", null);
-            doc =
-                javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
-            Element el = doc.createElement("graph");
-            doc.appendChild(el);
-
-            element = doc.getDocumentElement();
-            buildTopLevel(doc, element);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return doc;
-    }
-
-    protected void addChild(FONode child) {
-        if (child instanceof XMLObj) {
-            ((XMLObj)child).addGraphic(doc, element);
-        }
-    }
-
-    protected void addCharacters(char data[], int start, int length) {
-        String str = new String(data, start, length - start);
-        org.w3c.dom.Text text = doc.createTextNode(str);
-        element.appendChild(text);
-    }
-
-    /**
-     * layout this formatting object.
-     *
-     * @param area the area to layout the object into
-     * @return the status of the layout
-     */
-    public Status layout(Area area) throws FOPException {
-        /* generate a warning */
-        log.error("" + this.name + " outside foreign xml");
-
-        /* return status */
-        return new Status(Status.OK);
-    }
-
-    /**
-     * These method overrides prevent problems with the different types.
-     */
-    public void setIsInTableCell() {}
-
-    public void forceStartOffset(int offset) {}
-
-    public void forceWidth(int width) {}
-
-    public void resetMarker() {}
-
-    public void setLinkSet(LinkSet linkSet) {}
-
-    public Vector getMarkerSnapshot(Vector snapshot) {
-        return snapshot;
-    }
-
-    public void rollback(Vector snapshot) {}
-
-}
-