<commit_before>package nl.mpi.arbil;

import nl.mpi.arbil.data.ImdiTreeObject;
import nl.mpi.arbil.data.ImdiSchema;

/**
 * Document   : ImdiField
 * Created on : Wed Dec 03 13:29:30 CET 2008
 * @author <EMAIL>
 */
public class ImdiField {

    public ImdiTreeObject parentImdi;
    public String xmlPath;
    private String translatedPath = null;
    public String fieldValue = "";
    public String originalFieldValue = fieldValue;
    @Deprecated
    public String fieldID;
    private ImdiVocabularies.Vocabulary fieldVocabulary = null;
    private boolean hasVocabularyType = false;
    public boolean vocabularyIsOpen;
    public boolean vocabularyIsList;
    private String keyName = null;
    private String originalKeyName = null;
    private String languageId = null;
    private String originalLanguageId = null;
    private int isRequiredField = -1;
    private int canValidateField = -1;
    private int siblingCount;

    public ImdiField(ImdiTreeObject localParentImdi, String tempPath, String tempValue, int tempSiblingCount) {
        parentImdi = localParentImdi;
        fieldValue = tempValue;
        originalFieldValue = fieldValue;
        xmlPath = tempPath;
        siblingCount = tempSiblingCount;
    }

//private String originalValue = null;
//    public boolean fieldNeedsSaveToDisk() {
//        if (originalValue == null) {
//            return false;
//        } else {
//            return fieldValue.compareTo(originalValue) != 0;
//        }
//    }
    public boolean isRequiredField() {
        if (isRequiredField < 0) {
            isRequiredField = 0;
            String fullXmlPath = getGenericFullXmlPath();
            for (String currentRequiredField : parentImdi.getNodeTemplate().requiredFields) {
                if (fullXmlPath.matches(currentRequiredField)) {
                    isRequiredField = 1;
                    break;
                }
            }
        }
        return isRequiredField == 1;
    }

    public boolean fieldValueValidates() {
        return fieldValueValidatesToTemplate() && fieldValueValidatesToVocabulary();
    }

    private boolean fieldValueValidatesToVocabulary() {
//      if this has a closed vocabulary then check that the current value matches one of the values in the vocabulary
        if (hasVocabulary() && !vocabularyIsOpen) {
            return null != getVocabulary().findVocabularyItem(fieldValue);
        } else {
            return true;
        }
    }

    private boolean fieldValueValidatesToTemplate() {
        boolean isValidValue = true;
        if (canValidateField != 0) { // only do this the first time or once a field constraint has been found
            canValidateField = 0;
            String fullXmlPath = getGenericFullXmlPath();
            for (String[] currentRequiredField : parentImdi.getNodeTemplate().fieldConstraints) {
                if (fullXmlPath.matches(currentRequiredField[0])) {
                    canValidateField = 1;
                    isValidValue = (fieldValue.matches(currentRequiredField[1]));
                    break;
                }
            }
        }
        return isValidValue;
    }

    private boolean valuesDiffer(String leftString, String rightString) {
        if (leftString == null) {
            return rightString != null;
        }
        return (!leftString.equals(rightString));
    }

    public boolean fieldNeedsSaveToDisk() {
        if (valuesDiffer(originalFieldValue, fieldValue)) {
            return true;
        }
        if (valuesDiffer(originalLanguageId, languageId)) {
            return true;
        }
        if (valuesDiffer(originalKeyName, keyName)) {
            return true;
        }
        return false;
    }

    public String getFieldValue() {
        return fieldValue;
    }

    // returns the full xml path with the path indexes replaced by x's
    public String getGenericFullXmlPath() {
        return getFullXmlPath().replaceAll("\\(\\d*?\\)", "(x)").replaceFirst("\\(x\\)$", "");
    }

    public String getFullXmlPath() {
        String returnValue;
        String[] pathStringArray = this.parentImdi.getUrlString().split("#");
        if (pathStringArray.length > 1) {
            returnValue = pathStringArray[1] + this.xmlPath;
        } else {
            returnValue = this.xmlPath;
        }
        if (siblingCount > 0) {
            returnValue = returnValue + "(" + (siblingCount + 1) + ")";
        }
        return returnValue;
    }

    public void setFieldValue(String fieldValueToBe, boolean updateUI, boolean excludeFromUndoHistory) {
        fieldValueToBe = fieldValueToBe.trim();
        if (!this.fieldValue.equals(fieldValueToBe)) {
            if (!excludeFromUndoHistory) {
                LinorgJournal.getSingleInstance().recordFieldChange(this, this.fieldValue, fieldValueToBe, LinorgJournal.UndoType.Value);
            }
            LinorgJournal.getSingleInstance().saveJournalEntry(this.parentImdi.getUrlString(), getFullXmlPath(), this.fieldValue, fieldValueToBe, "edit");
            this.fieldValue = fieldValueToBe;
            new FieldChangeTriggers().actOnChange(this);
            // this now scans all fields in the imdiparent and its child nodes to set the "needs save to disk" flag in the imdi nodes
            parentImdi.setImdiNeedsSaveToDisk(this, updateUI);
        }
    }

    public boolean hasVocabulary() {
        return (fieldVocabulary != null);
    }

    public String getLanguageId() {
        return languageId;
    }

    public void setLanguageId(String languageIdLocal, boolean updateUI, boolean excludeFromUndoHistory) {
        String oldLanguageId = getLanguageId();
        boolean valueChanged = false;
        // this is expanded for readability
        if (oldLanguageId == null && languageIdLocal == null) {
            valueChanged = false;
        } else if (languageIdLocal == null && oldLanguageId != null) {
            valueChanged = true;
        } else if (!languageIdLocal.equals(oldLanguageId)) {
            valueChanged = true;
        }
        if (valueChanged) {// if the value has changed then record it in the undo list and the journal
            if (!excludeFromUndoHistory) {
                LinorgJournal.getSingleInstance().recordFieldChange(this, this.getLanguageId(), languageIdLocal, LinorgJournal.UndoType.LanguageId);
            }
            LinorgJournal.getSingleInstance().saveJournalEntry(this.parentImdi.getUrlString(), getFullXmlPath() + ":LanguageId", oldLanguageId, languageIdLocal, "edit");
            //addFieldAttribute("LanguageId", languageIdLocal);
            languageId = languageIdLocal;
//            fieldLanguageId = languageId;
            parentImdi.setImdiNeedsSaveToDisk(this, updateUI);
        }

    }

    public ImdiVocabularies.VocabularyItem[] getLanguageList() {
        // TODO: move this url to somewhere appropriate (preferably in the imdi file)
        return ImdiVocabularies.getSingleInstance().getVocabulary(this, "http://www.mpi.nl/IMDI/Schema/ISO639-2Languages.xml").getVocabularyItems();
    }

    public ImdiVocabularies.Vocabulary getVocabulary() {
        return fieldVocabulary;
    }

    public ImdiField[] getSiblingField(String pathString) {
//        System.out.println("getSiblingField: " + pathString);
        for (ImdiField[] tempField : parentImdi.getFields().values().toArray(new ImdiField[][]{})) {
//            System.out.println("tempField[0].getFullXmlPath(): " + tempField[0].getFullXmlPath());
            if (tempField[0].getFullXmlPath().equals(pathString)) {
                return tempField;
            }
        }
        return null;
    }

    public boolean isDisplayable() {
        return (fieldValue != null && /*fieldValue.trim().length() > 0 && */ !xmlPath.contains("CorpusLink") && !xmlPath.endsWith(".Keys") && !xmlPath.endsWith(".History"));
    }

    public void finishLoading() {
    }

    public void revertChanges() {
        setFieldValue(originalFieldValue, false, false);
        setLanguageId(originalLanguageId, false, false);
        setKeyName(originalKeyName, false, false);
        boolean updateUI = true;
        parentImdi.setImdiNeedsSaveToDisk(this, updateUI);
    }

    public void setFieldAttribute(String fieldIDLocal, String cvType, String cvUrlString, String languageIdLocal, String keyNameLocal) {
        fieldID = fieldIDLocal;
        languageId = languageIdLocal;
        originalLanguageId = languageId;
        keyName = keyNameLocal;
        originalKeyName = keyName;
        // set for the vocabulary type
        hasVocabularyType = false;
        if (cvType != null) {
            if (cvType.equals("OpenVocabularyList")) {
                vocabularyIsList = true;
                vocabularyIsOpen = true;
                hasVocabularyType = true;
            } else if (cvType.equals("OpenVocabulary")) {
                vocabularyIsList = false;
                vocabularyIsOpen = true;
                hasVocabularyType = true;
            } else if (cvType.equals("ClosedVocabularyList")) {
                vocabularyIsList = true;
                vocabularyIsOpen = false;
                hasVocabularyType = true;
            } else if (cvType.equals("ClosedVocabulary")) {
                vocabularyIsList = false;
                vocabularyIsOpen = false;
                hasVocabularyType = true;
            }
        }
        if (hasVocabularyType) {
            if (cvUrlString != null && cvUrlString.length() > 0) {
                fieldVocabulary = ImdiVocabularies.getSingleInstance().getVocabulary(this, cvUrlString);
            }
        }
    }

    @Override
    public String toString() {
//            System.out.println("ImdiField: " + fieldValue);
//            if (!isDisplayable()) {
//                return "check attributes";// fieldAttributes.keys().toString();
//            }
        return fieldValue;
    }

    public int getFieldID() {
        if (fieldID != null) {
            return Integer.parseInt(fieldID.substring(1));
        } else {
            return -1;
        }
    }

    public String getKeyName() {
        return keyName;
    }
<commit_msg>Updated the application title text in the windows installer. Modified the schema validation method (Check Format) to first use the name space url defined in the xml and then fall back to the imdi schema and then the schema provided in the jar file. Added clarin XSD reader. Loaded Clarin templates from the schema location of cmdi files. Addded component insertion into clarin files (via add menu). Added auto recovery from history when a file is missing. Updated the add menu contents to handle clarin sub nodes. Corrected nodes left after a delete from the tree. Updated the sub child node detection when reading in the metadata xml to handle cmdi templates with full paths. Added clarin link reader. Inserted Clarin linked nodes into the Arbil tree. Stored all clarin profile schemas on disk when they are first seen. Added Handle resolution and general URL resolution in Clarin links. Added test fields to the wizard html. Added CMDI function to tell when a resource can be added or not to a specific node. Set the profile name in clarin templates. Added test for drag and drop onto clarin nodes. Added resource drop action to add resource files to CMDI nodes. Set the drag cursor to indicate allow drops to CMDI nodes based on the values in the schema. Made more robust against broken cmdi resource links. Added resource proxy nodes for CMDI files when adding resources. Truncated clarin pathnames. Provided a default node name based on the profile name of a cmdi node. Started getting the filed constraints from the xsd. Corrected the adding of sub nodes such as actors. When a sub node is added the correct node path is returned for display in a table. Added check for updates menu item. Added template and profile selection dialogue. Added thread and progress bar for clarin profile loading due to 12mb file sizes. Populated the add menu from the chosen templates and profiles. Enabled adding the selected builtin templates and clarin profiles from the add menu. Modified the template system to work with multiple template/profile selections rather than a single selection. Removed need for "id" attributes that clash with the clarin resource attributes. Added icons to the add menu to make it clear which nodes are IMDI or Clarin before adding them. Prevented error with the language dropdown during editing in the table. Sorted the entries in the templates/profiles dialogue. Removed unrequired jar dependancies. Made the import dialogue process ready to start importing cmdi nodes. Added ability for users to add custom profile urls and files in the templates/profiles dialogue so that users can preview their work while it is still being designed in the clarin servers workspaces. Updated the schema validator to not use the 'id' fields. Modified add and remove corpus links not to use the 'id' fields. Added schema check when saving clarin nodes. Modified the table default display order so that it does not use the 'id' fields to sort rows and columns. Removed the use of the 'id' attribute from determining if a field is editable or not.<commit_after>package nl.mpi.arbil;

import nl.mpi.arbil.data.ImdiTreeObject;
import nl.mpi.arbil.data.ImdiSchema;

/**
 * Document   : ImdiField
 * Created on : Wed Dec 03 13:29:30 CET 2008
 * @author <EMAIL>
 */
public class ImdiField {

    public ImdiTreeObject parentImdi;
    public String xmlPath;
    private String translatedPath = null;
    public String fieldValue = "";
    public String originalFieldValue = fieldValue;
    private int fieldOrder = -1;
    private ImdiVocabularies.Vocabulary fieldVocabulary = null;
    private boolean hasVocabularyType = false;
    public boolean vocabularyIsOpen;
    public boolean vocabularyIsList;
    private String keyName = null;
    private String originalKeyName = null;
    private String languageId = null;
    private String originalLanguageId = null;
    private int isRequiredField = -1;
    private int canValidateField = -1;
    private int siblingCount;

    public ImdiField(int fieldOrderLocal, ImdiTreeObject localParentImdi, String tempPath, String tempValue, int tempSiblingCount) {
        fieldOrder = fieldOrderLocal;
        parentImdi = localParentImdi;
        fieldValue = tempValue;
        originalFieldValue = fieldValue;
        xmlPath = tempPath;
        siblingCount = tempSiblingCount;
    }

//private String originalValue = null;
//    public boolean fieldNeedsSaveToDisk() {
//        if (originalValue == null) {
//            return false;
//        } else {
//            return fieldValue.compareTo(originalValue) != 0;
//        }
//    }
    public boolean isRequiredField() {
        if (isRequiredField < 0) {
            isRequiredField = 0;
            String fullXmlPath = getGenericFullXmlPath();
            for (String currentRequiredField : parentImdi.getNodeTemplate().requiredFields) {
                if (fullXmlPath.matches(currentRequiredField)) {
                    isRequiredField = 1;
                    break;
                }
            }
        }
        return isRequiredField == 1;
    }

    public boolean fieldValueValidates() {
        return fieldValueValidatesToTemplate() && fieldValueValidatesToVocabulary();
    }

    private boolean fieldValueValidatesToVocabulary() {
//      if this has a closed vocabulary then check that the current value matches one of the values in the vocabulary
        if (hasVocabulary() && !vocabularyIsOpen) {
            return null != getVocabulary().findVocabularyItem(fieldValue);
        } else {
            return true;
        }
    }

    private boolean fieldValueValidatesToTemplate() {
        boolean isValidValue = true;
        if (canValidateField != 0) { // only do this the first time or once a field constraint has been found
            canValidateField = 0;
            String fullXmlPath = getGenericFullXmlPath();
            for (String[] currentRequiredField : parentImdi.getNodeTemplate().fieldConstraints) {
                if (fullXmlPath.matches(currentRequiredField[0])) {
                    canValidateField = 1;
                    isValidValue = (fieldValue.matches(currentRequiredField[1]));
                    break;
                }
            }
        }
        return isValidValue;
    }

    private boolean valuesDiffer(String leftString, String rightString) {
        if (leftString == null) {
            return rightString != null;
        }
        return (!leftString.equals(rightString));
    }

    public boolean fieldNeedsSaveToDisk() {
        if (valuesDiffer(originalFieldValue, fieldValue)) {
            return true;
        }
        if (valuesDiffer(originalLanguageId, languageId)) {
            return true;
        }
        if (valuesDiffer(originalKeyName, keyName)) {
            return true;
        }
        return false;
    }

    public String getFieldValue() {
        return fieldValue;
    }

    // returns the full xml path with the path indexes replaced by x's
    public String getGenericFullXmlPath() {
        return getFullXmlPath().replaceAll("\\(\\d*?\\)", "(x)").replaceFirst("\\(x\\)$", "");
    }

    public String getFullXmlPath() {
        String returnValue;
        String[] pathStringArray = this.parentImdi.getUrlString().split("#");
        if (pathStringArray.length > 1) {
            returnValue = pathStringArray[1] + this.xmlPath;
        } else {
            returnValue = this.xmlPath;
        }
        if (siblingCount > 0) {
            returnValue = returnValue + "(" + (siblingCount + 1) + ")";
        }
        return returnValue;
    }

    public void setFieldValue(String fieldValueToBe, boolean updateUI, boolean excludeFromUndoHistory) {
        fieldValueToBe = fieldValueToBe.trim();
        if (!this.fieldValue.equals(fieldValueToBe)) {
            if (!excludeFromUndoHistory) {
                LinorgJournal.getSingleInstance().recordFieldChange(this, this.fieldValue, fieldValueToBe, LinorgJournal.UndoType.Value);
            }
            LinorgJournal.getSingleInstance().saveJournalEntry(this.parentImdi.getUrlString(), getFullXmlPath(), this.fieldValue, fieldValueToBe, "edit");
            this.fieldValue = fieldValueToBe;
            new FieldChangeTriggers().actOnChange(this);
            // this now scans all fields in the imdiparent and its child nodes to set the "needs save to disk" flag in the imdi nodes
            parentImdi.setImdiNeedsSaveToDisk(this, updateUI);
        }
    }

    public boolean hasVocabulary() {
        return (fieldVocabulary != null);
    }

    public String getLanguageId() {
        return languageId;
    }

    public void setLanguageId(String languageIdLocal, boolean updateUI, boolean excludeFromUndoHistory) {
        String oldLanguageId = getLanguageId();
        boolean valueChanged = false;
        // this is expanded for readability
        if (oldLanguageId == null && languageIdLocal == null) {
            valueChanged = false;
        } else if (languageIdLocal == null && oldLanguageId != null) {
            valueChanged = true;
        } else if (!languageIdLocal.equals(oldLanguageId)) {
            valueChanged = true;
        }
        if (valueChanged) {// if the value has changed then record it in the undo list and the journal
            if (!excludeFromUndoHistory) {
                LinorgJournal.getSingleInstance().recordFieldChange(this, this.getLanguageId(), languageIdLocal, LinorgJournal.UndoType.LanguageId);
            }
            LinorgJournal.getSingleInstance().saveJournalEntry(this.parentImdi.getUrlString(), getFullXmlPath() + ":LanguageId", oldLanguageId, languageIdLocal, "edit");
            //addFieldAttribute("LanguageId", languageIdLocal);
            languageId = languageIdLocal;
//            fieldLanguageId = languageId;
            parentImdi.setImdiNeedsSaveToDisk(this, updateUI);
        }

    }

    public ImdiVocabularies.VocabularyItem[] getLanguageList() {
        // TODO: move this url to somewhere appropriate (preferably in the imdi file)
        return ImdiVocabularies.getSingleInstance().getVocabulary(this, "http://www.mpi.nl/IMDI/Schema/ISO639-2Languages.xml").getVocabularyItems();
    }

    public ImdiVocabularies.Vocabulary getVocabulary() {
        return fieldVocabulary;
    }

    public ImdiField[] getSiblingField(String pathString) {
//        System.out.println("getSiblingField: " + pathString);
        for (ImdiField[] tempField : parentImdi.getFields().values().toArray(new ImdiField[][]{})) {
//            System.out.println("tempField[0].getFullXmlPath(): " + tempField[0].getFullXmlPath());
            if (tempField[0].getFullXmlPath().equals(pathString)) {
                return tempField;
            }
        }
        return null;
    }

    public boolean isDisplayable() {
        return (fieldValue != null && /*fieldValue.trim().length() > 0 && */ !xmlPath.contains("CorpusLink") && !xmlPath.endsWith(".Keys") && !xmlPath.endsWith(".History"));
    }

    public void finishLoading() {
    }

    public void revertChanges() {
        setFieldValue(originalFieldValue, false, false);
        setLanguageId(originalLanguageId, false, false);
        setKeyName(originalKeyName, false, false);
        boolean updateUI = true;
        parentImdi.setImdiNeedsSaveToDisk(this, updateUI);
    }

    public void setFieldAttribute(String cvType, String cvUrlString, String languageIdLocal, String keyNameLocal) {
        languageId = languageIdLocal;
        originalLanguageId = languageId;
        keyName = keyNameLocal;
        originalKeyName = keyName;
        // set for the vocabulary type
        hasVocabularyType = false;
        if (cvType != null) {
            if (cvType.equals("OpenVocabularyList")) {
                vocabularyIsList = true;
                vocabularyIsOpen = true;
                hasVocabularyType = true;
            } else if (cvType.equals("OpenVocabulary")) {
                vocabularyIsList = false;
                vocabularyIsOpen = true;
                hasVocabularyType = true;
            } else if (cvType.equals("ClosedVocabularyList")) {
                vocabularyIsList = true;
                vocabularyIsOpen = false;
                hasVocabularyType = true;
            } else if (cvType.equals("ClosedVocabulary")) {
                vocabularyIsList = false;
                vocabularyIsOpen = false;
                hasVocabularyType = true;
            }
        }
        if (hasVocabularyType) {
            if (cvUrlString != null && cvUrlString.length() > 0) {
                fieldVocabulary = ImdiVocabularies.getSingleInstance().getVocabulary(this, cvUrlString);
            }
        }
    }

    @Override
    public String toString() {
//            System.out.println("ImdiField: " + fieldValue);
//            if (!isDisplayable()) {
//                return "check attributes";// fieldAttributes.keys().toString();
//            }
        return fieldValue;
    }

    public int getFieldOrder() {
        return fieldOrder;
    }

    public String getKeyName() {
        return keyName;
    }

{'fraction_non_alphanumeric': 0.08678024584860901, 'fraction_numerical': 0.0034073754582704336, 'mean_word_length': 2.5003019323671496, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904745', 'n_tokens_mistral': 6178, 'n_tokens_neox': 5979, 'n_words': 1752}