Search in sources :

Example 1 with Filters

use of gen.core.filters.Filters in project omegat by omegat-org.

the class FilterMaster method loadConfig.

/**
 * Loads information about the filters from an XML file. If there's an error loading a file, it calls
 * <code>setupDefaultFilters</code>.
 *
 * @throws IOException
 */
public static Filters loadConfig(File configFile) throws IOException {
    if (!configFile.exists()) {
        return null;
    }
    Filters result;
    try {
        Unmarshaller unm = CONFIG_CTX.createUnmarshaller();
        result = (Filters) unm.unmarshal(configFile);
    } catch (Exception e) {
        Log.logErrorRB("FILTERMASTER_ERROR_LOADING_FILTERS_CONFIG");
        Log.log(e);
        result = new Filters();
    }
    if (addNewFiltersToConfig(result)) {
        saveConfig(result, configFile);
    }
    return result;
}
Also used : Filters(gen.core.filters.Filters) Unmarshaller(javax.xml.bind.Unmarshaller) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 2 with Filters

use of gen.core.filters.Filters in project omegat by omegat-org.

the class Preferences method initFilters.

public static synchronized void initFilters() {
    if (didInitFilters) {
        return;
    }
    didInitFilters = true;
    File filtersFile = new File(StaticUtils.getConfigDir(), FilterMaster.FILE_FILTERS);
    Filters f = null;
    try {
        f = FilterMaster.loadConfig(filtersFile);
    } catch (Exception ex) {
        Log.log(ex);
    }
    if (f == null) {
        f = FilterMaster.createDefaultFiltersConfig();
    }
    filters = f;
}
Also used : Filters(gen.core.filters.Filters) File(java.io.File) IOException(java.io.IOException)

Example 3 with Filters

use of gen.core.filters.Filters in project omegat by omegat-org.

the class Preferences method setFilters.

public static void setFilters(Filters newFilters) {
    Filters oldValue = filters;
    filters = newFilters;
    File filtersFile = new File(StaticUtils.getConfigDir(), FilterMaster.FILE_FILTERS);
    try {
        FilterMaster.saveConfig(filters, filtersFile);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    // Must manually check for equality (see FiltersUtil.filtersEqual() Javadoc)
    if (!FiltersUtil.filtersEqual(oldValue, newFilters)) {
        PROP_CHANGE_SUPPORT.firePropertyChange(Preferences.PROPERTY_FILTERS, oldValue, newFilters);
    }
}
Also used : Filters(gen.core.filters.Filters) IOException(java.io.IOException) File(java.io.File)

Example 4 with Filters

use of gen.core.filters.Filters in project omegat by omegat-org.

the class FiltersTest method testFiltersComparison.

@Test
public void testFiltersComparison() {
    Filters orig = FilterMaster.createDefaultFiltersConfig();
    Filters clone = FilterMaster.createDefaultFiltersConfig();
    assertNotSame(orig, clone);
    // Filters does not itself implement equals() with the semantics we want
    assertNotEquals(orig, clone);
    // Use external implementation instead
    assertTrue(FiltersUtil.filtersEqual(orig, clone));
    // Shallow change
    clone.setIgnoreFileContext(!clone.isIgnoreFileContext());
    assertFalse(FiltersUtil.filtersEqual(orig, clone));
    // Deep change
    clone = FilterMaster.createDefaultFiltersConfig();
    Files file = clone.getFilters().get(0).getFiles().get(0);
    file.setTargetEncoding(file.getTargetEncoding() + "foo");
    assertFalse(FiltersUtil.filtersEqual(orig, clone));
}
Also used : Filters(gen.core.filters.Filters) Files(gen.core.filters.Files) Test(org.junit.Test)

Example 5 with Filters

use of gen.core.filters.Filters in project omegat by omegat-org.

the class Convert20to21 method convertFiltersConfig.

/**
 * Convert filters config ('filters.conf') into new format.
 *
 * @param fromFile
 *            old config file
 * @param toFile
 *            new config file
 * @throws Exception
 */
public static void convertFiltersConfig(final File fromFile, final File toFile) throws Exception {
    if (!fromFile.exists()) {
        return;
    }
    String c = read(fromFile);
    org.omegat.convert.v20to21.data.Filters filters;
    XMLDecoder xmldec = new XMLDecoder(new ByteArrayInputStream(c.getBytes("UTF-8")));
    try {
        filters = (org.omegat.convert.v20to21.data.Filters) xmldec.readObject();
    } finally {
        xmldec.close();
    }
    Filters res = new Filters();
    for (org.omegat.convert.v20to21.data.OneFilter f : filters.getFilter()) {
        Filter fo = new Filter();
        res.getFilters().add(fo);
        fo.setClassName(f.getClassName());
        fo.setEnabled(f.isOn());
        for (org.omegat.convert.v20to21.data.Instance i : f.getInstance()) {
            Files io = new Files();
            fo.getFiles().add(io);
            io.setSourceFilenameMask(i.getSourceFilenameMask());
            io.setTargetFilenamePattern(i.getTargetFilenamePattern());
            io.setSourceEncoding(i.getSourceEncoding());
            io.setTargetEncoding(i.getTargetEncoding());
        }
        Serializable opts = f.getOptions();
        if (opts != null) {
            BeanInfo bi = Introspector.getBeanInfo(opts.getClass());
            for (PropertyDescriptor prop : bi.getPropertyDescriptors()) {
                if ("class".equals(prop.getName())) {
                    continue;
                }
                Object value = prop.getReadMethod().invoke(opts);
                Filter.Option op = new Filter.Option();
                op.setName(prop.getName());
                op.setValue(value.toString());
                fo.getOption().add(op);
            }
        }
    }
    convertTextFilter(res);
    convertHTMLFilter2(res);
    JAXBContext ctx = JAXBContext.newInstance(Filters.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(res, toFile);
}
Also used : Serializable(java.io.Serializable) Marshaller(javax.xml.bind.Marshaller) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) JAXBContext(javax.xml.bind.JAXBContext) Filters(gen.core.filters.Filters) ByteArrayInputStream(java.io.ByteArrayInputStream) Filter(gen.core.filters.Filter) XMLDecoder(java.beans.XMLDecoder) Files(gen.core.filters.Files)

Aggregations

Filters (gen.core.filters.Filters)8 IOException (java.io.IOException)3 Files (gen.core.filters.Files)2 Filter (gen.core.filters.Filter)2 File (java.io.File)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 XMLDecoder (java.beans.XMLDecoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 Test (org.junit.Test)1 AbstractFilter (org.omegat.filters2.AbstractFilter)1 IFilter (org.omegat.filters2.IFilter)1 TranslationException (org.omegat.filters2.TranslationException)1 FilterMaster (org.omegat.filters2.master.FilterMaster)1