Ignore:
Timestamp:
2021-11-24T13:59:16+13:00 (2 years ago)
Author:
cstephen
Message:

Modified the Javascript object graph that is generated in XSLTUtil.java#getInterfaceStringsAsJavascript(....) to use empty objects for the nodes, rather than arrays

File:
1 edited

Legend:

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

    r35707 r35745  
    828828    }
    829829
    830     // generates javascript: 2 arrays are declared and populated with strings that declare variables and assign their values
    831     // to be strings loaded from the interface_name.properties file for the language.   
     830    /**
     831     * Generates a Javascript object graph to store language strings. Leaf nodes contain the string and the previous node, the key.
     832     * Further preceding nodes denote the prefix of the string (i.e. the gs3 language strings object, which interface they belong to etc.).
     833     * Accessing a language string on a well-formed JS object graph will be similar to this: const myString = gs.text.atea.asr.Title;
     834     * @param interface_name The name of the interface to retrieve language strings for.
     835     * @param lang The language to retrieve.
     836     * @param prefix The prefix to place the language strings under (e.g. atea.asr).
     837     * @param prependToPrefix
     838     * @return Javascript code that will generate the language string object graph.
     839     */
    832840    public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix, String prependToPrefix)
    833841    {
    834842        // 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));
     843        CustomClassLoader my_loader = new CustomClassLoader(XSLTUtil.class.getClassLoader(),
     844                GSFile.interfaceResourceDir(GlobalProperties.getGSDL3Home(), interface_name));
    836845        String prefixwithdot = prefix + ".";
    837 
    838         // 1. Generating Javascript of the form:
    839         // if(!gs.text) { gs.text = new Array(); }
    840         // if(!gs.text.dse) { gs.text.dse = new Array(); }
    841846
    842847        StringBuffer outputStr = new StringBuffer();
    843848        HashSet<String> initialisedDepths = new HashSet<>();
    844849
     850        // Initialise the root prefix object
    845851        outputStr.append("if(!" + prependToPrefix + ") { ");
    846         outputStr.append(prependToPrefix + " = new Array(); ");
     852        outputStr.append(prependToPrefix + " = {}; ");
    847853        outputStr.append("}\n");
    848854        initialisedDepths.add(prependToPrefix);
    849855
    850         // Handle '.' characters in the prefix. JS identifies these as object accessors, hence requiring a new array for each 'level'.
     856        // Initialise the prefix object structure, handling '.' characters.
     857        // JS identifies these as property accessors, hence requiring a new object for each 'level'.
    851858        String currentDepth = prependToPrefix;
    852859        String[] prefixComponents = prefix.split("\\.");
     
    856863
    857864            outputStr.append("if(!" + currentDepth + ") { ");
    858             outputStr.append(currentDepth + " = new Array(); ");
     865            outputStr.append(currentDepth + " = {}; ");
    859866            outputStr.append("}\n");
    860867
     
    862869        }
    863870
    864         int foundCount = 0;
    865 
    866         for (String dictName : new String[] { "interface_" + interface_name, "interface_default", "interface_default2" })
    867         {
    868             // get all the keys from the english dictionary as this is a complete set
     871        for (String dictName : new String[] { "interface_" + interface_name, "interface_default",
     872                "interface_default2" }) {
     873            // get all the keys from the english dictionary as this is a complete set
    869874            Dictionary dict = new Dictionary(dictName, "en", my_loader);
    870875            Enumeration keys = dict.getKeys();
    871             if (keys == null)
    872             {
     876            if (keys == null) {
    873877                continue;
    874878            }
     
    876880            // Get all properties in the language-specific dictionary with the given key prefix
    877881            // Create Javascript strings of the form:
    878             // prependToPrefix.key= "value";\n
    879             while (keys.hasMoreElements())
    880             {
     882            // prefixPath.key= "value";\n
     883            while (keys.hasMoreElements()) {
    881884                String key = (String) keys.nextElement();
    882                 if (key.startsWith(prefixwithdot))
    883                 {
     885                if (key.startsWith(prefixwithdot)) {
    884886                    String[] keyComponents = key.split("\\.");
    885887                    currentDepth = prependToPrefix + "." + prefix;
     
    893895
    894896                        outputStr.append("if(!" + currentDepth + ") { ");
    895                         outputStr.append(currentDepth + " = new Array(); ");
     897                        outputStr.append(currentDepth + " = {}; ");
    896898                        outputStr.append("}\n");
    897899
     
    899901                    }
    900902
    901                   // get the language dependent value for the key. This will return the english if no value found for the given lang
     903                    // get the language dependent value for the key. This will return the english if no value found for the given lang
    902904                    String value = getInterfaceText(interface_name, dictName, lang, key, null);
    903905
     
    910912                }
    911913            }
    912 
    913             if (foundCount > 0)
    914             {
    915                 break;
    916             }
    917914        }
    918915
Note: See TracChangeset for help on using the changeset viewer.