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

software.amazon.smithy.model.traits.ExternalDocumentationTrait Maven / Gradle / Ivy

/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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 software.amazon.smithy.model.traits;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.utils.ToSmithyBuilder;

/**
 * Provides named links to external documentation.
 */
public final class ExternalDocumentationTrait extends AbstractTrait
        implements ToSmithyBuilder {

    public static final ShapeId ID = ShapeId.from("smithy.api#externalDocumentation");

    private final Map urls;

    public ExternalDocumentationTrait(Builder builder) {
        super(ID, builder.sourceLocation);
        this.urls = Collections.unmodifiableMap(builder.urls);

        urls.forEach((name, url) -> validateUrl(name, url, getSourceLocation()));
    }

    private static String validateUrl(String name, String url, SourceLocation location) {
        try {
            new URL(url);
            return url;
        } catch (MalformedURLException e) {
            throw new SourceException(String.format("Each externalDocumentation value must be a valid URL. "
                    + "Found \"%s\" for name \"%s\"", url, name), location);
        }
    }

    /**
     * Gets the external documentation names and links.
     *
     * @return returns the external documentation mapping.
     */
    public Map getUrls() {
        return urls;
    }

    @Override
    protected Node createNode() {
        return ObjectNode.fromStringMap(urls).toBuilder().sourceLocation(getSourceLocation()).build();
    }

    @Override
    public Builder toBuilder() {
        Builder builder = builder().sourceLocation(getSourceLocation());
        urls.forEach(builder::addUrl);
        return builder;
    }

    /**
     * @return Returns an external documentation trait builder.
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Builder used to create the external documentation trait.
     */
    public static final class Builder extends AbstractTraitBuilder {
        private final Map urls = new LinkedHashMap<>();

        public Builder addUrl(String name, String url) {
            urls.put(Objects.requireNonNull(name), Objects.requireNonNull(url));
            return this;
        }

        public Builder removeUrl(String name) {
            urls.remove(name);
            return this;
        }

        public Builder clearUrls() {
            urls.clear();
            return this;
        }

        @Override
        public ExternalDocumentationTrait build() {
            return new ExternalDocumentationTrait(this);
        }
    }

    public static final class Provider implements TraitService {
        @Override
        public ShapeId getShapeId() {
            return ID;
        }

        @Override
        public ExternalDocumentationTrait createTrait(ShapeId target, Node value) {
            Builder builder = builder().sourceLocation(value);
            value.expectObjectNode().getMembers().forEach((k, v) -> {
                builder.addUrl(k.expectStringNode().getValue(), v.expectStringNode().getValue());
            });
            ExternalDocumentationTrait result = builder.build();
            result.setNodeCache(value);
            return result;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy