Search in sources :

Example 1 with EncodingInfo

use of com.sun.org.apache.xml.internal.serializer.EncodingInfo in project jdk8u_jdk by JetBrains.

the class CheckEncodingPropertiesFile method test.

public static void test(Properties props) throws Exception {
    // First, build a mapping from the properties read from the resource
    // file.
    // We're going to check the consistency of the resource file
    // while building this mapping, and throw errors if the file
    // does not meet our assumptions.
    //
    Map<String, Collection<String>> lines = new HashMap<>();
    final CheckCharsetMapping mapping = new CheckCharsetMapping();
    for (String key : props.stringPropertyNames()) {
        Collection<String> values = getValues(props.getProperty(key));
        lines.put(key, values);
        mapping.addMapping(key, values);
    }
    // Then build maps of EncodingInfos, and print along debugging
    // information that should help understand the content of the
    // resource file and the mapping it defines.
    //
    // Map indexed by java names
    Map<String, EncodingInfo> javaInfos = new HashMap<>();
    // Map indexed by XML names
    Map<String, EncodingInfo> xmlMap = new HashMap<>();
    Map<String, String> preferred = // Java Name -> Preferred Mime Name
    new HashMap<>(mapping.preferredMime);
    // unused...
    List<EncodingInfo> all = new ArrayList<>();
    for (Entry<String, Collection<String>> e : lines.entrySet()) {
        final String charsetName = mapping.getCharsetNameFor(e.getKey());
        if (charsetName == null) {
            System.out.println("!! No charset for: " + e.getKey() + " " + e.getValue());
            continue;
        }
        Charset c = Charset.forName(charsetName);
        EncodingInfo info;
        final String k = e.getKey().toUpperCase();
        final String kc = charsetName.toUpperCase();
        StringBuilder sb = new StringBuilder();
        for (String xml : e.getValue()) {
            final String kx = xml.toUpperCase();
            info = xmlMap.get(kx);
            if (info == null) {
                info = new EncodingInfo(xml, charsetName);
                System.out.println("** XML: " + xml + " -> " + charsetName);
                xmlMap.put(kx, info);
                all.add(info);
            }
            if (!javaInfos.containsKey(k)) {
                javaInfos.put(k, info);
                if (!preferred.containsKey(k)) {
                    preferred.put(k, xml);
                }
                sb.append("** Java: ").append(k).append(" -> ").append(xml).append(" (charset: ").append(charsetName).append(")\n");
            }
            if (!javaInfos.containsKey(kc)) {
                if (!preferred.containsKey(kc)) {
                    preferred.put(kc, xml);
                }
                javaInfos.put(kc, info);
                sb.append("** Java: ").append(kc).append(" -> ").append(xml).append(" (charset: ").append(charsetName).append(")\n");
            }
            if (!javaInfos.containsKey(c.name().toUpperCase())) {
                if (!preferred.containsKey(c.name().toUpperCase())) {
                    preferred.put(c.name().toUpperCase(), xml);
                }
                javaInfos.put(c.name().toUpperCase(), info);
                sb.append("** Java: ").append(c.name().toUpperCase()).append(" -> ").append(xml).append(" (charset: ").append(charsetName).append(")\n");
            }
        }
        if (sb.length() == 0) {
            System.out.println("Nothing new for " + charsetName + ": " + e.getKey() + " -> " + e.getValue());
        } else {
            System.out.print(sb);
        }
    }
    // Now we're going to verify that Encodings.java has done its job
    // correctly. We're going to ask Encodings to convert java names to mime
    // names and mime names to java names - and verify that the returned
    // java names do map to recognized charsets.
    //
    // We're also going to verify that Encodings has recorded the preferred
    // mime name correctly.
    Method m = Encodings.class.getDeclaredMethod("getMimeEncoding", String.class);
    m.setAccessible(true);
    Set<String> xNames = new HashSet<>();
    Set<String> jNames = new HashSet<>();
    for (String name : xmlMap.keySet()) {
        final String javaName = checkConvertMime2Java(name);
        checkPreferredMime(m, javaName, preferred);
        jNames.add(javaName);
        xNames.add(name);
    }
    for (String javaName : lines.keySet()) {
        final String javaCharsetName = mapping.getCharsetNameFor(javaName.toUpperCase());
        if (javaCharsetName == null)
            continue;
        if (!jNames.contains(javaName)) {
            checkPreferredMime(m, javaName, preferred);
            jNames.add(javaName);
        }
        for (String xml : lines.get(javaName)) {
            if (xNames.contains(xml))
                continue;
            final String jName = checkConvertMime2Java(xml);
            xNames.add(xml);
            if (jNames.contains(jName))
                continue;
            checkPreferredMime(m, jName, preferred);
        }
    }
}
Also used : EncodingInfo(com.sun.org.apache.xml.internal.serializer.EncodingInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) Method(java.lang.reflect.Method) Collection(java.util.Collection) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

EncodingInfo (com.sun.org.apache.xml.internal.serializer.EncodingInfo)1 Method (java.lang.reflect.Method)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1