Search in sources :

Example 6 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class PrepareTranslation method extract.

private static String extract(String documentName, File f, String target) throws Exception {
    String xml = IOUtils.readStringAndClose(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8), -1);
    // the template contains ${} instead of text
    StringBuilder template = new StringBuilder(xml.length());
    int id = 0;
    SortedProperties prop = new SortedProperties();
    XMLParser parser = new XMLParser(xml);
    StringBuilder buff = new StringBuilder();
    Stack<String> stack = new Stack<>();
    String tag = "";
    boolean ignoreEnd = false;
    String nextKey = "";
    // for debugging
    boolean templateIsCopy = false;
    while (true) {
        int event = parser.next();
        if (event == XMLParser.END_DOCUMENT) {
            break;
        } else if (event == XMLParser.CHARACTERS) {
            String s = parser.getText();
            if (s.trim().length() == 0) {
                if (buff.length() > 0) {
                    buff.append(s);
                } else {
                    template.append(s);
                }
            } else if ("p".equals(tag) || "li".equals(tag) || "a".equals(tag) || "td".equals(tag) || "th".equals(tag) || "h1".equals(tag) || "h2".equals(tag) || "h3".equals(tag) || "h4".equals(tag) || "body".equals(tag) || "b".equals(tag) || "code".equals(tag) || "form".equals(tag) || "span".equals(tag) || "em".equals(tag) || "div".equals(tag) || "strong".equals(tag) || "label".equals(tag)) {
                if (buff.length() == 0) {
                    nextKey = documentName + "_" + (1000 + id++) + "_" + tag;
                    template.append(getSpace(s, true));
                } else if (templateIsCopy) {
                    buff.append(getSpace(s, true));
                }
                buff.append(s);
            } else if ("pre".equals(tag) || "title".equals(tag) || "script".equals(tag) || "style".equals(tag)) {
                // ignore, don't translate
                template.append(s);
            } else {
                System.out.println(f.getName() + " invalid wrapper tag for text: " + tag + " text: " + s);
                System.out.println(parser.getRemaining());
                throw new Exception();
            }
        } else if (event == XMLParser.START_ELEMENT) {
            stack.add(tag);
            String name = parser.getName();
            if ("code".equals(name) || "a".equals(name) || "b".equals(name) || "span".equals(name)) {
                // keep tags if wrapped, but not if this is the wrapper
                if (buff.length() > 0) {
                    buff.append(parser.getToken());
                    ignoreEnd = false;
                } else {
                    ignoreEnd = true;
                    template.append(parser.getToken());
                }
            } else if ("p".equals(tag) || "li".equals(tag) || "td".equals(tag) || "th".equals(tag) || "h1".equals(tag) || "h2".equals(tag) || "h3".equals(tag) || "h4".equals(tag) || "body".equals(tag) || "form".equals(tag)) {
                if (buff.length() > 0) {
                    if (templateIsCopy) {
                        template.append(buff.toString());
                    } else {
                        template.append("${" + nextKey + "}");
                    }
                    add(prop, nextKey, buff);
                }
                template.append(parser.getToken());
            } else {
                template.append(parser.getToken());
            }
            tag = name;
        } else if (event == XMLParser.END_ELEMENT) {
            String name = parser.getName();
            if ("code".equals(name) || "a".equals(name) || "b".equals(name) || "span".equals(name) || "em".equals(name) || "strong".equals(name)) {
                if (ignoreEnd) {
                    if (buff.length() > 0) {
                        if (templateIsCopy) {
                            template.append(buff.toString());
                        } else {
                            template.append("${" + nextKey + "}");
                        }
                        add(prop, nextKey, buff);
                    }
                    template.append(parser.getToken());
                } else {
                    if (buff.length() > 0) {
                        buff.append(parser.getToken());
                    }
                }
            } else {
                if (buff.length() > 0) {
                    if (templateIsCopy) {
                        template.append(buff.toString());
                    } else {
                        template.append("${" + nextKey + "}");
                    }
                    add(prop, nextKey, buff);
                }
                template.append(parser.getToken());
            }
            tag = stack.pop();
        } else if (event == XMLParser.DTD) {
            template.append(parser.getToken());
        } else if (event == XMLParser.COMMENT) {
            template.append(parser.getToken());
        } else {
            int eventType = parser.getEventType();
            throw new Exception("Unexpected event " + eventType + " at " + parser.getRemaining());
        }
    // if(!xml.startsWith(template.toString())) {
    // System.out.println(nextKey);
    // System.out.println(template.substring(template.length()-60)
    // +";");
    // System.out.println(xml.substring(template.length()-60,
    // template.length()));
    // System.out.println(template.substring(template.length()-55)
    // +";");
    // System.out.println(xml.substring(template.length()-55,
    // template.length()));
    // break;
    // }
    }
    new File(target).mkdirs();
    String propFileName = target + "/_docs_" + MAIN_LANGUAGE + ".properties";
    Properties old = load(propFileName, false);
    prop.putAll(old);
    store(prop, propFileName, false);
    String t = template.toString();
    if (templateIsCopy && !t.equals(xml)) {
        for (int i = 0; i < Math.min(t.length(), xml.length()); i++) {
            if (t.charAt(i) != xml.charAt(i)) {
                int start = Math.max(0, i - 30), end = Math.min(i + 30, xml.length());
                t = t.substring(start, end);
                xml = xml.substring(start, end);
            }
        }
        System.out.println("xml--------------------------------------------------: ");
        System.out.println(xml);
        System.out.println("t---------------------------------------------------: ");
        System.out.println(t);
        System.exit(1);
    }
    return t;
}
Also used : InputStreamReader(java.io.InputStreamReader) SortedProperties(org.h2.util.SortedProperties) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) Stack(java.util.Stack) XMLParser(org.h2.build.doc.XMLParser) File(java.io.File)

Example 7 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class PrepareTranslation method prepare.

private static void prepare(String baseDir, String path, boolean utf8) throws IOException {
    String suffix = utf8 ? ".prop" : ".properties";
    File dir = new File(path);
    File main = null;
    ArrayList<File> translations = new ArrayList<>();
    for (File f : dir.listFiles()) {
        if (f.getName().endsWith(suffix) && f.getName().indexOf('_') >= 0) {
            if (f.getName().endsWith("_" + MAIN_LANGUAGE + suffix)) {
                main = f;
            } else {
                translations.add(f);
            }
        }
    }
    SortedProperties p = load(main.getAbsolutePath(), utf8);
    Properties base = load(baseDir + "/" + main.getName(), utf8);
    store(p, main.getAbsolutePath(), utf8);
    for (File trans : translations) {
        String language = trans.getName();
        language = language.substring(language.lastIndexOf('_') + 1, language.lastIndexOf('.'));
        prepare(p, base, trans, utf8);
    }
    store(p, baseDir + "/" + main.getName(), utf8);
}
Also used : ArrayList(java.util.ArrayList) SortedProperties(org.h2.util.SortedProperties) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) File(java.io.File)

Example 8 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class PrepareTranslation method prepare.

private static void prepare(Properties main, Properties base, File trans, boolean utf8) throws IOException {
    SortedProperties p = load(trans.getAbsolutePath(), utf8);
    Properties oldTranslations = new Properties();
    for (Object k : base.keySet()) {
        String key = (String) k;
        String m = base.getProperty(key);
        String t = p.getProperty(key);
        if (t != null && !t.startsWith("#")) {
            oldTranslations.setProperty(m, t);
        }
    }
    HashSet<String> toTranslate = new HashSet<>();
    // add missing keys, using # and the value from the main file
    for (Object k : main.keySet()) {
        String key = (String) k;
        String now = main.getProperty(key);
        if (!p.containsKey(key)) {
            String t = oldTranslations.getProperty(now);
            if (t == null) {
                // System.out.println(trans.getName() +
                // ": key " + key + " not found in " +
                // "translation file; added # 'translation'");
                t = "#" + now;
                p.put(key, t);
            } else {
                p.put(key, t);
            }
        } else {
            String t = p.getProperty(key);
            String last = base.getProperty(key);
            if (t.startsWith("#") && !t.startsWith("##")) {
                // not translated before
                t = oldTranslations.getProperty(now);
                if (t == null) {
                    t = "#" + now;
                }
                p.put(key, t);
            } else if (last != null && !last.equals(now)) {
                t = oldTranslations.getProperty(now);
                if (t == null) {
                    // main data changed since the last run: review
                    // translation
                    System.out.println(trans.getName() + ": key " + key + " changed, please review; last=" + last + " now=" + now);
                    String old = p.getProperty(key);
                    t = "#" + now + " #" + old;
                    p.put(key, t);
                } else {
                    p.put(key, t);
                }
            }
        }
    }
    for (String key : toTranslate) {
        String now = main.getProperty(key);
        String t;
        System.out.println(trans.getName() + ": key " + key + " not found in translation file; added dummy # 'translation'");
        t = "#" + now;
        p.put(key, t);
    }
    // (deleted or typo in the key)
    for (Object k : new ArrayList<>(p.keySet())) {
        String key = (String) k;
        if (!main.containsKey(key)) {
            p.remove(key);
        }
    }
    store(p, trans.getAbsolutePath(), utf8);
}
Also used : ArrayList(java.util.ArrayList) SortedProperties(org.h2.util.SortedProperties) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) HashSet(java.util.HashSet)

Example 9 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class PropertiesToUTF8 method textUTF8ToProperties.

/**
 * Convert a translation file (in UTF-8) to a properties file (without
 * special characters).
 *
 * @param source the source file name
 * @param target the target file name
 */
static void textUTF8ToProperties(String source, String target) throws Exception {
    if (!new File(source).exists()) {
        return;
    }
    try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(source), StandardCharsets.UTF_8))) {
        SortedProperties prop = new SortedProperties();
        StringBuilder buff = new StringBuilder();
        String key = null;
        boolean found = false;
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            line = line.trim();
            if (line.length() == 0) {
                continue;
            }
            if (line.startsWith("@")) {
                if (key != null) {
                    prop.setProperty(key, buff.toString());
                    buff.setLength(0);
                }
                found = true;
                key = line.substring(1);
            } else {
                if (buff.length() > 0) {
                    buff.append(System.getProperty("line.separator"));
                }
                buff.append(line);
            }
        }
        if (found) {
            prop.setProperty(key, buff.toString());
        }
        prop.store(target);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SortedProperties(org.h2.util.SortedProperties) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader)

Example 10 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class FileContentHash method hash.

private Info hash(String path) throws IOException {
    if (FileUtils.isDirectory(path)) {
        long totalSize = 0;
        SortedProperties propOld;
        SortedProperties propNew = new SortedProperties();
        String hashFileName = path + "/" + HASH_INDEX;
        if (FileUtils.exists(hashFileName)) {
            propOld = SortedProperties.loadProperties(hashFileName);
        } else {
            propOld = new SortedProperties();
        }
        List<String> list = FileUtils.newDirectoryStream(path);
        Collections.sort(list);
        MessageDigest mdDir = createMessageDigest();
        for (String f : list) {
            String name = FileUtils.getName(f);
            if (name.equals(HASH_INDEX)) {
                continue;
            }
            long length = FileUtils.size(f);
            String entry = "name_" + name + "-mod_" + FileUtils.lastModified(f) + "-size_" + length;
            String hash = propOld.getProperty(entry);
            if (hash == null || FileUtils.isDirectory(f)) {
                Info info = hash(f);
                byte[] b = info.hash;
                hash = StringUtils.convertBytesToHex(b);
                totalSize += info.size;
                entry = "name_" + name + "-mod_" + FileUtils.lastModified(f) + "-size_" + info.size;
            } else {
                totalSize += length;
                checkCollision(f, length, StringUtils.convertHexToBytes(hash));
            }
            propNew.put(entry, hash);
            mdDir.update(entry.getBytes(StandardCharsets.UTF_8));
            mdDir.update(hash.getBytes(StandardCharsets.UTF_8));
        }
        String oldFile = propOld.toString();
        String newFile = propNew.toString();
        if (!oldFile.equals(newFile)) {
            if (WRITE_HASH_INDEX) {
                propNew.store(path + "/" + HASH_INDEX);
            }
        }
        Info info = new Info();
        info.hash = mdDir.digest();
        info.size = totalSize;
        return info;
    }
    MessageDigest md = createMessageDigest();
    InputStream in = FileUtils.newInputStream(path);
    long length = FileUtils.size(path);
    byte[] buff = new byte[1024 * 1024];
    while (true) {
        int len = in.read(buff);
        if (len < 0) {
            break;
        }
        md.update(buff, 0, len);
        long t = System.nanoTime();
        if (nextLog == 0 || t > nextLog) {
            System.out.println("Checking " + path);
            nextLog = t + 5000 * 1000000L;
        }
    }
    in.close();
    byte[] b = md.digest();
    checkCollision(path, length, b);
    Info info = new Info();
    info.hash = b;
    info.size = length;
    return info;
}
Also used : InputStream(java.io.InputStream) SortedProperties(org.h2.util.SortedProperties) MessageDigest(java.security.MessageDigest)

Aggregations

SortedProperties (org.h2.util.SortedProperties)13 Properties (java.util.Properties)6 IOException (java.io.IOException)4 DbException (org.h2.message.DbException)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 SysProperties (org.h2.engine.SysProperties)2 InputStream (java.io.InputStream)1 LineNumberReader (java.io.LineNumberReader)1 RandomAccessFile (java.io.RandomAccessFile)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BindException (java.net.BindException)1 ConnectException (java.net.ConnectException)1 InetAddress (java.net.InetAddress)1