de.fhg.iais.asc.transformer.jdom.handler.XmlFieldStripper.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.transformer.jdom.handler.XmlFieldStripper.java

Source

package de.fhg.iais.asc.transformer.jdom.handler;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import org.apache.commons.lang.StringUtils;
import org.jdom.Element;

import de.fhg.iais.commons.annotation.UsedBy;

/**
 * Bereinigt einen Feld-Inhalt, wenn Feld und Inhalt angegebenen Mustern entsprechen.
 * 
 * @author kst
 */
@UsedBy("SipMaker - property 'xmlProcessors'")
public class XmlFieldStripper extends AbstractXmlFieldContentTransformer {
    /**
     * @param fieldPattern XPath expression pointing to a field. Matches all if null.
     * @param contentPattern Regex must match field content to trigger changes. Matches all if null.
     */
    public XmlFieldStripper(String fieldPattern, String contentPattern) {
        super(fieldPattern, contentPattern);
    }

    public XmlFieldStripper() {
        this(null, null);
    }

    @Override
    protected String transform(Element element, String text) {
        final String stripped = stripText(text);
        return stripped;
    }

    private static String stripText(final String input) {
        String result = input;
        result = StringUtils.replace(result, "\\s", " ");
        result = StringUtils.replace(result, " {2,}", " ");
        result = StringUtils.replace(result, " .", ".");
        result = StringUtils.replace(result, " ,", ",");
        result = StringUtils.replace(result, " ;", ";");
        result = StringUtils.removeEnd(result, ",");
        result = StringUtils.removeEnd(result, ";");
        result = result.trim();

        return result;
    }
}