/** * Creates a new {@code LinksSnippet} that will extract links using the given * {@code linkExtractor} and document them using the given {@code descriptors}. The * given {@code attributes} will be included in the model during template rendering. * If {@code ignoreUndocumentedLinks} is {@code true}, undocumented links will be * ignored and will not trigger a failure. * @param linkExtractor the link extractor * @param descriptors the link descriptors * @param attributes the additional attributes * @param ignoreUndocumentedLinks whether undocumented links should be ignored */ protected LinksSnippet(LinkExtractor linkExtractor, List<LinkDescriptor> descriptors, Map<String, Object> attributes, boolean ignoreUndocumentedLinks) { super("links", attributes); this.linkExtractor = linkExtractor; for (LinkDescriptor descriptor : descriptors) { Assert.notNull(descriptor.getRel(), "Link descriptors must have a rel"); this.descriptorsByRel.put(descriptor.getRel(), descriptor); } this.ignoreUndocumentedLinks = ignoreUndocumentedLinks; }
/** * Returns a model for the given {@code descriptor}. * @param descriptor the descriptor * @return the model */ protected Map<String, Object> createModelForDescriptor(LinkDescriptor descriptor) { Map<String, Object> model = new HashMap<>(); model.put("rel", descriptor.getRel()); model.put("description", descriptor.getDescription()); model.put("optional", descriptor.isOptional()); model.putAll(descriptor.getAttributes()); return model; }
private List<Map<String, Object>> createLinksModel(Map<String, List<Link>> links) { List<Map<String, Object>> model = new ArrayList<>(); for (Entry<String, LinkDescriptor> entry : this.descriptorsByRel.entrySet()) { LinkDescriptor descriptor = entry.getValue(); if (!descriptor.isIgnored()) { if (descriptor.getDescription() == null) { descriptor = createDescriptor( getDescriptionFromLinkTitle(links, descriptor.getRel()), descriptor); } model.add(createModelForDescriptor(descriptor)); } } return model; }
private LinkDescriptor createDescriptor(String description, LinkDescriptor source) { LinkDescriptor newDescriptor = new LinkDescriptor(source.getRel()) .description(description); if (source.isOptional()) { newDescriptor.optional(); } if (source.isIgnored()) { newDescriptor.ignored(); } return newDescriptor; }