All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cocoon.sax.component.RegexpLinkRewriterTransformer Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.cocoon.sax.component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class RegexpLinkRewriterTransformer
        extends AbstractLinkRewriterTransformer {

    private static final String REGEXP_PREFIX = "regexp";

    private final transient Map matchingMap =
            new HashMap();

    public RegexpLinkRewriterTransformer() {
        super();
    }

    public RegexpLinkRewriterTransformer(
            final Map regexpMatch) {

        super();

        init(regexpMatch);
    }

    @Override
    public void setup(final Map parameters) {
        if (parameters == null || parameters.isEmpty()) {
            return;
        }

        super.setup(parameters);

        final Map regexpMatch = new HashMap();

        String[] split;
        for (Map.Entry parameter : parameters.entrySet()) {
            if (parameter.getKey().startsWith(REGEXP_PREFIX)) {
                split = ((String) parameter.getValue()).split(" ");
                if (split.length == 2) {
                    regexpMatch.put(split[0], split[1]);
                } else {
                    LOG.error("Invalid regexp as parameter, ignoring: "
                            + parameter.getValue());
                }
            }
        }

        this.init(regexpMatch);
    }

    private void init(final Map regexpMatch) {
        if (regexpMatch != null && !regexpMatch.isEmpty()) {
            for (Map.Entry entry : regexpMatch.entrySet()) {
                try {
                    this.matchingMap.put(Pattern.compile(entry.getKey()),
                            entry.getValue());
                } catch (PatternSyntaxException e) {
                    LOG.error("Could not compile regular expression '"
                            + entry.getKey() + "'", e);
                }
            }
        }
    }

    @Override
    protected String rewrite(final String elementNS,
            final String elementName,
            final String attributeNS,
            final String attributeName,
            final String link)
            throws LinkRewriterException {

        String result = link;

        Map.Entry entry;
        Matcher matcher;
        for (Iterator> itor =
                matchingMap.entrySet().iterator();
                itor.hasNext() && result.equals(link);) {

            entry = itor.next();
            matcher = entry.getKey().matcher(link);
            result = matcher.replaceAll(entry.getValue());
        }
        if (LOG.isDebugEnabled() && link.equals(result)) {
            LOG.debug("No match found for '" + link + "'");
        }

        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy