<commit_before>/*
 * 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.jackrabbit.webdav;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.jackrabbit.util.Text;

/**
 * <code>AbstractLocatorFactory</code> is an implementation of the DavLocatorFactory
 * interface that defines how a given uri is split to workspace path an
 * resource path and how it's implementation of <code>DavResourceLocator</code>
 * builds the href. In contrast, the conversion from repository path to
 * resource path and vice versa is left to subclasses.
 */
public abstract class AbstractLocatorFactory implements DavLocatorFactory {

    private static Logger log = LoggerFactory.getLogger(AbstractLocatorFactory.class);

    private final String pathPrefix;

    /**
     * Create a new factory
     *
     * @param pathPrefix Prefix, that needs to be removed in order to retrieve
     * the path of the repository item from a given <code>DavResourceLocator</code>.
     */
    public AbstractLocatorFactory(String pathPrefix) {
        this.pathPrefix = pathPrefix;
    }

    //--------------------------------------------------< DavLocatorFactory >---
    /**
     * Create a new <code>DavResourceLocator</code>. Any leading prefix and
     * path-prefix (as defined with the constructor) are removed from the
     * given request handle. The same applies for trailing '/'. The remaining
     * String is called the 'resource handle' and it's first segment is treated
     * as workspace name. If resource handle (and therefore workspace name
     * are missing, both values are set to <code>null</code>.<p/>
     * Examples:
     *
     * <pre>
     * http://www.foo.bar/ (path prefix missing)
     * -> workspace path = null
     * -> resource path  = null
     * -> href           = http://www.foo.bar/pathPrefix/
     *
     * http://www.foo.bar/pathPrefix/
     * -> workspace path = null
     * -> resource path  = null
     * -> href           = http://www.foo.bar/pathPrefix/
     *
     * http://www.foo.bar/pathPrefix/wspName
     * -> workspace path = /wspName
     * -> resource path  = /wspName
     * -> href           = http://www.foo.bar/pathPrefix/wspName
     *
     * http://www.foo.bar/pathPrefix/wspName/anypath
     * -> workspace path = /wspName
     * -> resource path  = /wspName/anypath
     * -> href           = http://www.foo.bar/pathPrefix/wspName/anypath
     * </pre>
     *
     * NOTE: If the given href is an absolute uri it must start with the
     * specified prefix.
     *
     * @param prefix
     * @param href
     * @return a new <code>DavResourceLocator</code>
     * @throws IllegalArgumentException if the given href is <code>null</code>
     */
    public DavResourceLocator createResourceLocator(String prefix, String href) {
        if (href == null) {
            throw new IllegalArgumentException("Request handle must not be null.");
        }

        // build prefix string and remove all prefixes from the given href.
        StringBuffer b = new StringBuffer("");
        if (prefix != null && prefix.length() > 0) {
            b.append(prefix);
            if (href.startsWith(prefix)) {
                href = href.substring(prefix.length());
            }
        }
        if (pathPrefix != null && pathPrefix.length() > 0 && !prefix.endsWith(pathPrefix)) {
            b.append(pathPrefix);
            if (href.startsWith(pathPrefix)) {
                href = href.substring(pathPrefix.length());
            }<commit_msg>- handle null prefix - always remove trailing pathPrefix from href<commit_after>/*
 * 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.jackrabbit.webdav;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.jackrabbit.util.Text;

/**
 * <code>AbstractLocatorFactory</code> is an implementation of the DavLocatorFactory
 * interface that defines how a given uri is split to workspace path an
 * resource path and how it's implementation of <code>DavResourceLocator</code>
 * builds the href. In contrast, the conversion from repository path to
 * resource path and vice versa is left to subclasses.
 */
public abstract class AbstractLocatorFactory implements DavLocatorFactory {

    private static Logger log = LoggerFactory.getLogger(AbstractLocatorFactory.class);

    private final String pathPrefix;

    /**
     * Create a new factory
     *
     * @param pathPrefix Prefix, that needs to be removed in order to retrieve
     * the path of the repository item from a given <code>DavResourceLocator</code>.
     */
    public AbstractLocatorFactory(String pathPrefix) {
        this.pathPrefix = pathPrefix;
    }

    //--------------------------------------------------< DavLocatorFactory >---
    /**
     * Create a new <code>DavResourceLocator</code>. Any leading prefix and
     * path-prefix (as defined with the constructor) are removed from the
     * given request handle. The same applies for trailing '/'. The remaining
     * String is called the 'resource handle' and it's first segment is treated
     * as workspace name. If resource handle (and therefore workspace name
     * are missing, both values are set to <code>null</code>.<p/>
     * Examples:
     *
     * <pre>
     * http://www.foo.bar/ (path prefix missing)
     * -> workspace path = null
     * -> resource path  = null
     * -> href           = http://www.foo.bar/pathPrefix/
     *
     * http://www.foo.bar/pathPrefix/
     * -> workspace path = null
     * -> resource path  = null
     * -> href           = http://www.foo.bar/pathPrefix/
     *
     * http://www.foo.bar/pathPrefix/wspName
     * -> workspace path = /wspName
     * -> resource path  = /wspName
     * -> href           = http://www.foo.bar/pathPrefix/wspName
     *
     * http://www.foo.bar/pathPrefix/wspName/anypath
     * -> workspace path = /wspName
     * -> resource path  = /wspName/anypath
     * -> href           = http://www.foo.bar/pathPrefix/wspName/anypath
     * </pre>
     *
     * NOTE: If the given href is an absolute uri it must start with the
     * specified prefix.
     *
     * @param prefix
     * @param href
     * @return a new <code>DavResourceLocator</code>
     * @throws IllegalArgumentException if the given href is <code>null</code>
     */
    public DavResourceLocator createResourceLocator(String prefix, String href) {
        if (href == null) {
            throw new IllegalArgumentException("Request handle must not be null.");
        }

        // build prefix string and remove all prefixes from the given href.
        StringBuffer b = new StringBuffer("");
        if (prefix != null && prefix.length() > 0) {
            b.append(prefix);
            if (href.startsWith(prefix)) {
                href = href.substring(prefix.length());
            }
        }
        if (pathPrefix != null && pathPrefix.length() > 0) {
            if (!b.toString().endsWith(pathPrefix)) {
                b.append(pathPrefix);
            }
            if (href.startsWith(pathPrefix)) {
                href = href.substring(pathPrefix.length());
            }
{'fraction_non_alphanumeric': 0.10811752409708512, 'fraction_numerical': 0.001858088491464406, 'mean_word_length': 2.469782433521354, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 18, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1922621', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2230, 'n_words': 812}