Ignore:
Timestamp:
2021-10-26T12:21:07+13:00 (2 years ago)
Author:
cstephen
Message:

Add support for multiple namespaces in interface property keys; i.e. lots of dots :)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java

    r35394 r35707  
    2929import java.util.Enumeration;
    3030import java.util.HashMap;
     31import java.util.HashSet;
    3132import java.util.Locale;
    3233
     
    831832    public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix, String prependToPrefix)
    832833    {
    833           // now we allow looking for files in the interface's resources folder
    834           CustomClassLoader my_loader = new CustomClassLoader(XSLTUtil.class.getClassLoader(), GSFile.interfaceResourceDir(GlobalProperties.getGSDL3Home(), interface_name));
    835       String prefixwithdot = prefix+".";
     834        // now we allow looking for files in the interface's resources folder
     835        CustomClassLoader my_loader = new CustomClassLoader(XSLTUtil.class.getClassLoader(), GSFile.interfaceResourceDir(GlobalProperties.getGSDL3Home(), interface_name));
     836        String prefixwithdot = prefix + ".";
     837
    836838        // 1. Generating Javascript of the form:
    837839        // if(!gs.text) { gs.text = new Array(); }
    838840        // if(!gs.text.dse) { gs.text.dse = new Array(); }
     841
    839842        StringBuffer outputStr = new StringBuffer();
     843        HashSet<String> initialisedDepths = new HashSet<>();
     844
    840845        outputStr.append("if(!" + prependToPrefix + ") { ");
    841846        outputStr.append(prependToPrefix + " = new Array(); ");
    842847        outputStr.append("}\n");
    843         outputStr.append("if(!" + prependToPrefix + "." + prefix + ") { ");
    844         outputStr.append(prependToPrefix + "." + prefix + " = new Array(); ");
    845         outputStr.append("}\n");
     848        initialisedDepths.add(prependToPrefix);
     849
     850        // Handle '.' characters in the prefix. JS identifies these as object accessors, hence requiring a new array for each 'level'.
     851        String currentDepth = prependToPrefix;
     852        String[] prefixComponents = prefix.split("\\.");
     853
     854        for (String element : prefixComponents) {
     855            currentDepth += "." + element;
     856
     857            outputStr.append("if(!" + currentDepth + ") { ");
     858            outputStr.append(currentDepth + " = new Array(); ");
     859            outputStr.append("}\n");
     860
     861            initialisedDepths.add(currentDepth);
     862        }
    846863
    847864        int foundCount = 0;
     
    849866        for (String dictName : new String[] { "interface_" + interface_name, "interface_default", "interface_default2" })
    850867        {
    851           // get all the keys from the english dictionary as this is a complete set
    852                   Dictionary dict = new Dictionary(dictName, "en", my_loader);
     868            // get all the keys from the english dictionary as this is a complete set
     869            Dictionary dict = new Dictionary(dictName, "en", my_loader);
    853870            Enumeration keys = dict.getKeys();
    854871            if (keys == null)
     
    865882                if (key.startsWith(prefixwithdot))
    866883                {
     884                    String[] keyComponents = key.split("\\.");
     885                    currentDepth = prependToPrefix + "." + prefix;
     886
     887                    for (int i = prefixComponents.length; i < keyComponents.length - 1; i++) {
     888                        currentDepth += "." + keyComponents[i];
     889
     890                        if (initialisedDepths.contains(currentDepth)) {
     891                            continue;
     892                        }
     893
     894                        outputStr.append("if(!" + currentDepth + ") { ");
     895                        outputStr.append(currentDepth + " = new Array(); ");
     896                        outputStr.append("}\n");
     897
     898                        initialisedDepths.add(currentDepth);
     899                    }
     900
    867901                  // get the language dependent value for the key. This will return the english if no value found for the given lang
    868902                    String value = getInterfaceText(interface_name, dictName, lang, key, null);
Note: See TracChangeset for help on using the changeset viewer.