<commit_before>package nl.mpi.arbil.MetadataFile;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.transform.TransformerException;
import mpi.imdi.api.IMDIDom;
import mpi.imdi.api.IMDILink;
import mpi.imdi.api.WSNodeType;
import mpi.util.OurURL;
import nl.mpi.arbil.GuiHelper;
import nl.mpi.arbil.LinorgWindowManager;
import org.w3c.dom.Document;

/**
 *  Document   : ImdiUtils
 *  Created on : May 21, 2010, 9:30:03 PM
 *  Author     : <NAME>
 */
public class ImdiUtils implements MetadataUtils {

    public static IMDIDom api = new IMDIDom();

    private boolean isCatalogue(URI sourceURI) {
        try {
            mpi.util.OurURL inUrlLocal = new mpi.util.OurURL(sourceURI.toURL());
            org.w3c.dom.Document nodDom = api.loadIMDIDocument(inUrlLocal, false);
            return null != org.apache.xpath.XPathAPI.selectSingleNode(nodDom, "/:METATRANSCRIPT/:Catalogue");
        } catch (MalformedURLException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        } catch (TransformerException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        }
        return false;
    }

    private boolean isSession(URI sourceURI) {
        try {
            mpi.util.OurURL inUrlLocal = new mpi.util.OurURL(sourceURI.toURL());
            org.w3c.dom.Document nodDom = api.loadIMDIDocument(inUrlLocal, false);
            return null != org.apache.xpath.XPathAPI.selectSingleNode(nodDom, "/:METATRANSCRIPT/:Session");
        } catch (MalformedURLException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        } catch (TransformerException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        }
        return false;
    }

    public boolean addCorpusLink(URI nodeURI, URI linkURI[]) {
        try {
            Document nodDom;
            OurURL inUrlLocal = new OurURL(nodeURI.toURL());
            nodDom = api.loadIMDIDocument(inUrlLocal, false);
            if (nodDom == null) {
                GuiHelper.linorgBugCatcher.logError(new Exception(api.getMessage()));
                LinorgWindowManager.getSingleInstance().addMessageDialogToQueue("Error reading via the IMDI API", "Add Link");
                return false;
            } else {
                int nodeType = WSNodeType.CORPUS;
                for (URI currentLinkUri : linkURI) {
                    if (isCatalogue(currentLinkUri)) {
                        nodeType = WSNodeType.CATALOGUE;
                    }
                    if (isSession(currentLinkUri)) {
                        nodeType = WSNodeType.SESSION;
                    }
                    api.createIMDILink(nodDom, inUrlLocal, currentLinkUri.toString(), "", nodeType, "");
                }
                api.writeDOM(nodDom, new File(nodeURI), true);
                return true;
            }
        } catch (MalformedURLException ex) {
            GuiHelper.linorgBugCatcher.logError(ex);
        }
        return true;
    }

    public boolean copyMetadataFile(URI sourceURI, File destinationFile, URI[] linksNotToUpdate, boolean updateLinks) {
        try {
            mpi.util.OurURL inUrlLocal = new mpi.util.OurURL(sourceURI.toURL());
            mpi.util.OurURL destinationUrl = new mpi.util.OurURL(destinationFile.toURL());

            org.w3c.dom.Document nodDom = api.loadIMDIDocument(inUrlLocal, false);
            if (nodDom == null) {
                GuiHelper.linorgBugCatcher.logError(new Exception(api.getMessage()));
                LinorgWindowManager.getSingleInstance().addMessageDialogToQueue("Error reading via the IMDI API", "Copy IMDI File");
                return false;
            } else {
                mpi.imdi.api.IMDILink[] links = api.getIMDILinks(nodDom, inUrlLocal, mpi.imdi.api.WSNodeType.UNKNOWN);
                if (links != null && updateLinks) {
                    for (mpi.imdi.api.IMDILink currentLink : links) {
                        boolean linkNotToUpdateFound = false;
                        if (linksNotToUpdate != null) {
                            for (URI updatableLink : linksNotToUpdate) {
                                try {
                                    if (currentLink.getRawURL().toURL().toURI().equals(updatableLink)) {
                                        linkNotToUpdateFound = true;
                                        break;
                                    }
                                } catch (URISyntaxException exception) {
                                    GuiHelper.linorgBugCatcher.logError(exception);
                                }
                            }
                        }
                        System.out.println("currentLink: " + linkNotToUpdateFound + " : " + currentLink.getRawURL().toString());
                        if (linkNotToUpdateFound) {
                            // todo: this is not going to always work because the changeIMDILink is too limited, when a link points to a different domain for example
                            // todo: cont... or when a remote imdi is imported without its files then exported while copying its files, the files will be copied but the links not updated by the api
                            // todo: cont... this must instead take oldurl newurl and the new imdi file location
                            api.changeIMDILink(nodDom, destinationUrl, currentLink);
                        }<commit_msg>Corrected message text. Made sure that the default templates are always added on first run. Modified the export to optionally rename the exported metadata files to match local corpus tree names. Modified the import export to rename and copy (or not) the resource files.<commit_after>package nl.mpi.arbil.MetadataFile;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.transform.TransformerException;
import mpi.imdi.api.IMDIDom;
import mpi.imdi.api.IMDILink;
import mpi.imdi.api.WSNodeType;
import mpi.util.OurURL;
import nl.mpi.arbil.GuiHelper;
import nl.mpi.arbil.LinorgWindowManager;
import org.w3c.dom.Document;

/**
 *  Document   : ImdiUtils
 *  Created on : May 21, 2010, 9:30:03 PM
 *  Author     : <NAME>
 */
public class ImdiUtils implements MetadataUtils {

    public static IMDIDom api = new IMDIDom();

    private boolean isCatalogue(URI sourceURI) {
        try {
            mpi.util.OurURL inUrlLocal = new mpi.util.OurURL(sourceURI.toURL());
            org.w3c.dom.Document nodDom = api.loadIMDIDocument(inUrlLocal, false);
            return null != org.apache.xpath.XPathAPI.selectSingleNode(nodDom, "/:METATRANSCRIPT/:Catalogue");
        } catch (MalformedURLException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        } catch (TransformerException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        }
        return false;
    }

    private boolean isSession(URI sourceURI) {
        try {
            mpi.util.OurURL inUrlLocal = new mpi.util.OurURL(sourceURI.toURL());
            org.w3c.dom.Document nodDom = api.loadIMDIDocument(inUrlLocal, false);
            return null != org.apache.xpath.XPathAPI.selectSingleNode(nodDom, "/:METATRANSCRIPT/:Session");
        } catch (MalformedURLException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        } catch (TransformerException exception) {
            GuiHelper.linorgBugCatcher.logError(exception);
        }
        return false;
    }

    public boolean addCorpusLink(URI nodeURI, URI linkURI[]) {
        try {
            Document nodDom;
            OurURL inUrlLocal = new OurURL(nodeURI.toURL());
            nodDom = api.loadIMDIDocument(inUrlLocal, false);
            if (nodDom == null) {
                GuiHelper.linorgBugCatcher.logError(new Exception(api.getMessage()));
                LinorgWindowManager.getSingleInstance().addMessageDialogToQueue("Error reading via the IMDI API", "Add Link");
                return false;
            } else {
                int nodeType = WSNodeType.CORPUS;
                for (URI currentLinkUri : linkURI) {
                    if (isCatalogue(currentLinkUri)) {
                        nodeType = WSNodeType.CATALOGUE;
                    }
                    if (isSession(currentLinkUri)) {
                        nodeType = WSNodeType.SESSION;
                    }
                    api.createIMDILink(nodDom, inUrlLocal, currentLinkUri.toString(), "", nodeType, "");
                }
                api.writeDOM(nodDom, new File(nodeURI), true);
                return true;
            }
        } catch (MalformedURLException ex) {
            GuiHelper.linorgBugCatcher.logError(ex);
        }
        return true;
    }

    public boolean copyMetadataFile(URI sourceURI, File destinationFile, URI[][] linksToUpdate, boolean updateLinks) {
        try {
            mpi.util.OurURL inUrlLocal = new mpi.util.OurURL(sourceURI.toURL());
            mpi.util.OurURL destinationUrl = new mpi.util.OurURL(destinationFile.toURI().toURL());

            org.w3c.dom.Document nodDom = api.loadIMDIDocument(inUrlLocal, false);
            if (nodDom == null) {
                GuiHelper.linorgBugCatcher.logError(new Exception(api.getMessage()));
                LinorgWindowManager.getSingleInstance().addMessageDialogToQueue("Error reading via the IMDI API", "Copy IMDI File");
                return false;
            } else {
                mpi.imdi.api.IMDILink[] links = api.getIMDILinks(nodDom, inUrlLocal, mpi.imdi.api.WSNodeType.UNKNOWN);
                if (links != null && updateLinks) {
                    for (mpi.imdi.api.IMDILink currentLink : links) {
                        URI linkUriToUpdate = null;
                        if (linksToUpdate != null) {
                            for (URI[] updatableLink : linksToUpdate) {
                                try {
                                    if (currentLink.getRawURL().toURL().toURI().equals(updatableLink[0])) {
                                        linkUriToUpdate = updatableLink[1];
                                        break;
                                    }
                                } catch (URISyntaxException exception) {
                                    GuiHelper.linorgBugCatcher.logError(exception);
                                }
                            }
                        }
                        System.out.println("currentLink: " + linkUriToUpdate + " : " + currentLink.getRawURL().toString());
                        if (linkUriToUpdate != null) {
                            // todo: this is not going to always work because the changeIMDILink is too limited, when a link points to a different domain for example
                            // todo: cont... or when a remote imdi is imported without its files then exported while copying its files, the files will be copied but the links not updated by the api
                            // todo: cont... this must instead take oldurl newurl and the new imdi file location
//                            api.changeIMDILink(nodDom, destinationUrl, currentLink);
                            // todo: check how removeIMDILink and createIMDILink handles info links compared to changeIMDILink
                            api.removeIMDILink(nodDom, currentLink);
                            api.createIMDILink(nodDom, destinationUrl, linkUriToUpdate.toASCIIString(), currentLink.getLinkName(), currentLink.getNodeType(), currentLink.getSpec());
                        }
{'fraction_non_alphanumeric': 0.08957253335599558, 'fraction_numerical': 0.0027194697034078354, 'mean_word_length': 1.8702439024390245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1551780', 'n_tokens_mistral': 3242, 'n_tokens_neox': 3066, 'n_words': 698}