/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.officeimporter.filter;

import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xwiki.bridge.DocumentAccessBridge;

/**
 * This filter will replace the {@code <img>} tag with corresponding xwiki xhtml syntax. This is required because
 * ordinary xhtml {@code <img>} tags does not render inside wiki pages correctly.
 * 
 * @version $Id: ImageTagFilter.java 13998 2008-11-06 06:29:46Z asiri $
 * @since 1.8M1
 */
public class ImageFilter implements HTMLFilter
{
    private DocumentAccessBridge docBridge;

    private String targetDocument;

    public ImageFilter(DocumentAccessBridge docBridge, String targetDocument)
    {
        this.docBridge = docBridge;
        this.targetDocument = targetDocument;
    }

    /**
     * {@inheritDoc}
     */
    public void filter(Document htmlDocument)
    {
        if (null != targetDocument && null != docBridge) {
            NodeList images = htmlDocument.getElementsByTagName("img");
            for (int i = 0; i < images.getLength(); i++) {
                Node image = images.item(i);
                Node src = null;
                if (image.hasAttributes() && (src = image.getAttributes().getNamedItem("src")) != null) {
                    String fileName = src.getNodeValue();
                    // TODO : We might have to verify that fileName is indeed a file name. (a.k.a not a
                    // url). There might be cases where documents have embedded urls (images).
                    Comment beforeComment = htmlDocument.createComment("startimage:" + fileName);
                    Comment afterComment = htmlDocument.createComment("stopimage");
                    try {
                        src.setNodeValue(docBridge.getAttachmentURL(targetDocument, fileName));
                    } catch (Exception ex) {
                        // Do nothing.
                    }
                    image.getParentNode().insertBefore(beforeComment, image);
                    image.getParentNode().insertBefore(afterComment, image.getNextSibling());
                }
            }
        }
    }
}

@@ -1005,20 +1005,23 @@
 w3c.dom.
-Node
+Element
 ;%0Aimport
@@ -1988,20 +1988,23 @@
     
-Node
+Element
  image =
  ima
@@ -1999,16 +1999,26 @@
  image =
+ (Element)
  images.
@@ -2046,188 +2046,86 @@
     
-Node src = null;%0A                if (image.hasAttributes() && (src = image.getAttributes().getNamedItem(%22src%22)) != null) %7B%0A                    String fileName = src.getNodeValue();
+String src = image.getAttribute(%22src%22);%0A                if (!src.equals(%22%22)) %7B
 %0A   
@@ -2184,26 +2184,14 @@
 hat 
-fileName is indeed
+src is
  a f
@@ -2395,24 +2395,19 @@
 age:%22 + 
-fileName
+src
 );%0A     
@@ -2539,25 +2539,34 @@
     
-src.setNodeValue(
+image.setAttribute(%22src%22, 
 docB
@@ -2608,16 +2608,11 @@
 nt, 
-fileName
+src
 ));%0A
@@ -2885,24 +2885,568 @@
 Sibling());%0A
+                    // The 'align' attribute of images creates a lot of problems. First, OO server has a problem with%0A                    // center aligning images (it aligns them to left). Next, OO server uses %3Cbr clear%22xxx%22%3E for%0A                    // avoiding content wrapping around images which is not valid xhtml. There for, to be consistent and%0A                    // simple we will remove the 'align' attribute of all the images so that they are all left aligned.%0A                    image.removeAttribute(%22align%22);                    %0A
             

Develop the initial feature set for the Office Importer module