001/*
002 * Copyright © 2012, 2013, 2014 Royal Botanic Gardens, Kew.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
005 *
006 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
009 */
010package org.kew.rmf.transformers;
011
012/**
013 * A generic transformer that searches for all occurrences of a pattern (regEx)
014 * `a` in a string and replaces each with a string `b`;
015 *
016 * It takes two optional parameters, `removeMultipleWhitespaces` (default true) and
017 * `trimIt` (default true)
018 */
019public class A2BTransformer extends RegexDefCollection implements Transformer {
020
021    protected String a = "";
022    protected String b = "";
023    private boolean removeMultipleWhitespaces = true;
024    private boolean trimIt = true;
025
026    @Override
027    public String transform(String s) {
028        s = s.replaceAll(this.getA(), this.getB());
029        if (this.removeMultipleWhitespaces) s = s.replaceAll("\\s+", " ");
030        if (this.trimIt) s = s.trim();
031        return s;
032    }
033
034    public String getA() {
035        return a;
036    }
037    public void setA(String a) {
038        this.a = a;
039    }
040    public String getB() {
041        return b;
042    }
043    public void setB(String b) {
044        this.b = b;
045    }
046
047    public boolean isRemoveMultipleWhitespaces() {
048        return removeMultipleWhitespaces;
049    }
050
051    public void setRemoveMultipleWhitespaces(boolean removeMultipleWhitespaces) {
052        this.removeMultipleWhitespaces = removeMultipleWhitespaces;
053    }
054
055    public boolean isTrimIt() {
056        return trimIt;
057    }
058
059    public void setTrimIt(boolean trimIt) {
060        this.trimIt = trimIt;
061    }
062}