org.robotstxt4j.model.RobotsTxt.java Source code

Java tutorial

Introduction

Here is the source code for org.robotstxt4j.model.RobotsTxt.java

Source

/*
 * Copyright (C) 2012 Nicolas A. Brard-Nault
 *
 * 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.
 */

package org.robotstxt4j.model;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.robotstxt4j.matcher.RelativePathMatcher;
import org.robotstxt4j.matcher.UserAgentMatcher;
import org.robotstxt4j.misc.AbstractValueObject;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

@SuppressWarnings("serial")
public class RobotsTxt extends AbstractValueObject implements Serializable {
    final private Set<DisallowDirective> disallowDirectives;
    final private Set<SitemapDirective> sitemapDirectives;

    public RobotsTxt(final Builder builder) {
        checkNotNull(builder.disallowDirectives);
        checkNotNull(builder.sitemapDirectives);
        this.disallowDirectives = new HashSet<>(builder.disallowDirectives);
        this.sitemapDirectives = new HashSet<>(builder.sitemapDirectives);
    }

    public Iterable<SitemapDirective> getSiteMapDirectives() {
        return Collections.unmodifiableCollection(sitemapDirectives);
    }

    public Iterable<DisallowDirective> getDisallowDirectives() {
        return Collections.unmodifiableCollection(disallowDirectives);
    }

    public boolean isUserAgentAllowed(final UserAgent userAgent, final RelativePath path) {
        final Optional<DisallowDirective> found = Iterables.tryFind(disallowDirectives,
                new Predicate<DisallowDirective>() {
                    @Override
                    public boolean apply(final DisallowDirective input) {
                        return input.disallows(userAgent, path);
                    }
                });

        return !found.isPresent();
    }

    public static class Builder {
        final private Set<SitemapDirective> sitemapDirectives;
        final private Set<DisallowDirective> disallowDirectives;

        public Builder() {
            sitemapDirectives = new HashSet<SitemapDirective>();
            disallowDirectives = new HashSet<DisallowDirective>();
        }

        public Builder withDisallowDirective(final UserAgentMatcher userAgentMatcher,
                final RelativePathMatcher pathMatcher) {
            disallowDirectives.add(DisallowDirective.create(userAgentMatcher, pathMatcher));
            return this;
        }

        public Builder withSitemapDirective(final URI siteMapUri) {
            sitemapDirectives.add(SitemapDirective.create(siteMapUri));
            return this;
        }

        public RobotsTxt build() {
            return new RobotsTxt(this);
        }

        public static Builder get() {
            return new Builder();
        }
    }

    public static Builder create() {
        return Builder.get();
    }
}