/**
 * Created by IntelliJ IDEA.
 * User: Owner
 * Date: Sep 4, 2003
 * Time: 9:49:13 PM
 * To change this template use Options | File Templates.
 */
package org.apache.fop.batik;

import org.apache.fop.fo.FObj;
import org.apache.fop.fo.PropertyList;
import org.apache.fop.fo.Status;
import org.apache.fop.fo.DirectPropertyListBuilder;
import org.apache.fop.apps.FOPException;
import org.apache.fop.layout.FontState;
import org.apache.fop.layout.Area;
import org.apache.fop.layout.inline.ForeignObjectArea;
import org.apache.fop.configuration.Configuration;
import org.apache.fop.svg.SVGUserAgent;
import org.apache.batik.dom.svg.*;
import org.apache.batik.dom.util.XMLSupport;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.UnitProcessor;
import org.w3c.dom.Element;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGDocument;
import org.xml.sax.Attributes;

import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import java.net.URL;



/**
 * class representing svg:svg pseudo flow object.
 */
public class BatikElement extends BatikObj {
    public static final String _flowTextTag = "flowText";
    /**
     * inner class for making SVG objects.
     */
    public static class Maker extends FObj.Maker {

        /**
         * make an SVG object.
         *
         * @param parent the parent formatting object
         * @param propertyList the explicit properties of this object
         *
         * @return the SVG object
         */
        public FObj make(FObj parent, PropertyList propertyList,
                         String systemId, int line, int column)
            throws FOPException {
            return new BatikElement(parent, propertyList,
                                  systemId, line, column);
        }
    }

    /**
     * returns the maker for this object.
     *
     * @return the maker for SVG objects
     */
    public static FObj.Maker maker() {
        return new BatikElement.Maker();
    }

    FontState fs;

    /**
     * constructs an SVG object (called by Maker).
     *
     * @param parent the parent formatting object
     * @param propertyList the explicit properties of this object
     */
    public BatikElement(FObj parent, PropertyList propertyList,
                      String systemId, int line, int column) {
        super(parent, propertyList, _flowTextTag, systemId, line, column);
        init();
    }

    private void init() {
        DOMImplementation impl = ExtensibleSVGDOMImplementation.getDOMImplementation();
        String batikNS = BatikElementMapping.NAMESPACE_URI;
        doc = impl.createDocument(batikNS, _flowTextTag, null);

        element = doc.getDocumentElement();

        buildTopLevel(doc, element);
    }

    public void addGraphic(Document docum, Element parent) {
         // do need the parant 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);
     }
}

