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;
}
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;
}
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);
}
}
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));
}
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);
}
Aggregations