package folioxml.export.html;


import folioxml.config.ExportLocations;
import folioxml.config.InfobaseConfigBase;
import folioxml.core.InvalidMarkupException;
import folioxml.export.ExportingNodeListProcessor;
import folioxml.export.FileNode;
import folioxml.export.LogStreamProvider;
import folioxml.export.NodeListProcessor;
import folioxml.xml.Node;
import folioxml.xml.NodeFilter;
import folioxml.xml.NodeList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LinkMapper implements NodeListProcessor, ExportingNodeListProcessor {

    public InfobaseConfigBase c;

    private HashMap<String, Object> infobaseToUriMap;

    public LinkMapper(InfobaseConfigBase c){
        this.c = c;

        Map<String,Object> mapperConfig = c.getObject("link_mapper") != null ? (Map<String,Object>)c.getObject("link_mapper") : new HashMap<String,Object>();

        if (mapperConfig.get("infobases") != null) {
            infobaseToUriMap = (HashMap<String,Object>) mapperConfig.get("infobases");
        }
    }

    @Override
    public NodeList process(NodeList nodes) throws InvalidMarkupException, IOException {

        NodeList links = nodes.filterByTagName("a|link", true);
        for (Node n:links.list()) {
            String infobase = n.get("infobase");
            if (infobase == null) continue;
            if (infobaseToUriMap.containsKey(infobase)) {
                String old = n.toXmlString(true);
                n.setTagName("a");
                n.set("href", (String) infobaseToUriMap.get(infobase));
                n.removeAttr("infobase");
                provider.getNamedStream("mapped_links").append(" in record ").append(n.rootNode().get("folioId")).append("\n").append(old).append(" ... to:\n").append(n.toXmlString((true))).append("\n");

            }

        }
        return nodes;
    }

    @Override
    public void setFileNode(FileNode fn) {

    }

    LogStreamProvider provider;

    @Override
    public void setLogProvider(LogStreamProvider provider) {
        this.provider = provider;
    }

    @Override
    public void setExportLocations(ExportLocations el) {

    }

}

@@ -712,16 +712,67 @@
 riMap;%0A%0A
+%0A    private HashMap%3CString, Object%3E uriToUriMap;%0A%0A
     publ
@@ -1134,24 +1134,159 @@
 ;%0A        %7D%0A
+        if (mapperConfig.get(%22urls%22) != null) %7B%0A            uriToUriMap = (HashMap%3CString,Object%3E) mapperConfig.get(%22urls%22);%0A        %7D%0A
     %7D%0A%0A    @
@@ -1480,24 +1480,59 @@
 s.list()) %7B%0A
+            String newUrl = null;%0A%0A
             
@@ -1597,83 +1597,403 @@
 ase 
-== null) 
+!= null && infobaseToUriMap != null && infobaseToUriMap.
 cont
+a
 in
-ue;%0A            if (infobaseToUriMap.containsKey(infobase)) 
+sKey(infobase)) %7B%0A                newUrl =  (String) infobaseToUriMap.get(infobase);%0A            %7D%0A            String href = n.get(%22href%22);%0A            if (href != null && uriToUriMap != null && uriToUriMap.containsKey(href)) %7B%0A                newUrl =  (String) uriToUriMap.get(href);%0A            %7D%0A%0A            if (newUrl != null)
 %7B%0A  
@@ -2097,58 +2097,28 @@
   n.
-set(%22href%22, (String) infobaseToUriMap.get(
+removeAttr(%22
 infobase
 ));%0A
@@ -2113,17 +2113,17 @@
 infobase
-)
+%22
 );%0A     
@@ -2135,37 +2135,34 @@
       n.
-removeAttr(%22infobase%22
+set(%22href%22, newUrl
 );%0A     
@@ -2360,17 +2360,16 @@
 (%22%5Cn%22);%0A
-%0A
         

Add uri->uri link remapping