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 string
014 * {@link #a} and replaces each with a string {@link #b}.
015 * <br/>
016 * It takes two optional parameters, {@link #removeMultipleWhitespaces} (default true) and
017 * {@link #trimIt} (default true)
018 */
019public class A2BTransformer implements Transformer {
020
021        private String a = "";
022        private String b = "";
023        private boolean removeMultipleWhitespaces = true;
024        private boolean trimIt = true;
025
026        @Override
027        public String transform(String s) {
028                if (s == null) return null;
029
030                s = s.replace(getA(), getB());
031
032                if (this.removeMultipleWhitespaces) {
033                        s = SqueezeWhitespaceTransformer.MULTIPLE_WHITESPACE.matcher(s).replaceAll(" ");
034                }
035
036                if (this.trimIt) {
037                        s = s.trim();
038                }
039
040                return s;
041        }
042
043        public String getA() {
044                return a;
045        }
046        public void setA(String a) {
047                this.a = a;
048        }
049
050        public String getB() {
051                return b;
052        }
053        public void setB(String b) {
054                this.b = b;
055        }
056
057        public boolean isRemoveMultipleWhitespaces() {
058                return removeMultipleWhitespaces;
059        }
060        public void setRemoveMultipleWhitespaces(boolean removeMultipleWhitespaces) {
061                this.removeMultipleWhitespaces = removeMultipleWhitespaces;
062        }
063
064        public boolean isTrimIt() {
065                return trimIt;
066        }
067        public void setTrimIt(boolean trimIt) {
068                this.trimIt = trimIt;
069        }
070}