001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.test;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028import java.util.Properties;
029
030/**
031 * Differ for translation files.
032 *
033 * @since 7.3
034 */
035public class TranslationMessagesDiffer {
036
037    protected final Properties src;
038
039    protected final Properties dst;
040
041    public TranslationMessagesDiffer(Properties src, Properties dst) {
042        super();
043        this.src = src;
044        this.dst = dst;
045    }
046
047    public List<String> getMissingDestKeys() {
048        return getMissingKeys(src, dst);
049    }
050
051    public List<String> getAdditionalDestKeys() {
052        return getMissingKeys(dst, src);
053    }
054
055    protected List<String> getMissingKeys(Properties src, Properties dst) {
056        List<String> missing = new ArrayList<>();
057        for (Object item : src.keySet()) {
058            if (!dst.containsKey(item)) {
059                missing.add((String) item);
060            }
061        }
062        Collections.sort(missing);
063        return missing;
064    }
065
066    public static TranslationProperties extractProps(File file) throws IOException {
067        InputStream in = new FileInputStream(file);
068        TranslationProperties props = new TranslationProperties();
069        props.load(in);
070        return props;
071    }
072
073}