本文整理汇总了Java中com.google.common.html.types.SafeUrls类的典型用法代码示例。如果您正苦于以下问题:Java SafeUrls类的具体用法?Java SafeUrls怎么用?Java SafeUrls使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SafeUrls类属于com.google.common.html.types包,在下文中一共展示了SafeUrls类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCommonSafeUrlTypeConversions
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
@Test
public void testCommonSafeUrlTypeConversions() {
final String testUrl = "http://blahblah";
final SanitizedContent sanitizedConstantUri = SanitizedContents.constantUri(testUrl);
final SafeUrl safeUrl = SafeUrls.fromConstant(testUrl);
final SanitizedContent sanitizedUrl = SanitizedContents.fromSafeUrl(safeUrl);
assertThat(sanitizedUrl.getContentKind()).isEqualTo(ContentKind.URI);
assertThat(sanitizedUrl.getContent()).isEqualTo(testUrl);
assertThat(sanitizedUrl).isEqualTo(sanitizedConstantUri);
// Proto conversions.
final SafeUrlProto safeUrlProto = SafeUrls.toProto(safeUrl);
final SanitizedContent sanitizedUrlProto = SanitizedContents.fromSafeUrlProto(safeUrlProto);
assertThat(sanitizedUrlProto.getContent()).isEqualTo(testUrl);
assertThat(sanitizedUrlProto).isEqualTo(sanitizedConstantUri);
}
示例2: renderScripts
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private SoyValue renderScripts(List<SafeUrlProto> urls) {
String template = "{namespace dossier.soy.dynamic}{template .scripts kind=\"html\"}";
for (SafeUrlProto proto : urls) {
String url = SafeUrls.fromProto(proto).getSafeUrlString();
template += "<script src=\"" + url + "\" defer></script>";
}
template += "{/template}";
return filesetBuilderProvider
.get()
.add(template, "<dynamic>")
.build()
.compileToTofu()
.newRenderer("dossier.soy.dynamic.scripts")
.setContentKind(SanitizedContent.ContentKind.HTML)
.renderStrict();
}
示例3: buildPropertyLink
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private NamedType buildPropertyLink(
TypeExpressionParser parser, JSType type, String propertyName) {
NamedType namedType = parseNamedType(parser, type);
if (!urlString(namedType.getLink().getHref()).isEmpty()
// Extern links are always fully qualified.
&& !urlString(namedType.getLink().getHref()).startsWith("http")) {
SafeUrl url = SafeUrls.fromProto(namedType.getLink().getHref());
url = SafeUrls.sanitize(url.getSafeUrlString() + "#" + propertyName);
namedType =
namedType
.toBuilder()
.setLink(namedType.getLink().toBuilder().setHref(SafeUrls.toProto(url)))
.build();
}
return namedType;
}
示例4: caseNamedType
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
@Override
public Void caseNamedType(NamedType type) {
String name = type.getReferenceName();
com.github.jsdossier.proto.NamedType.Builder namedType = createNamedType(name);
if (SafeUrls.fromProto(namedType.getLink().getHref()).getSafeUrlString().isEmpty()) {
// If there is no href, we were not able to resolve the type, so assume it is
// nullable by default.
currentExpression()
.getUnionTypeBuilder()
.addType(TypeExpression.newBuilder().setNamedType(namedType))
.addType(NULL_TYPE);
} else {
currentExpression().setNamedType(namedType);
}
return null;
}
示例5: makeLink
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
public static SafeHtmlProto makeLink(String href) {
SafeUrlProto safeHref = Unapproved.makeSafeUrlProto(href);
// Use the builder to make the link.
return SafeHtmls.toProto(
new SafeHtmlBuilder("a")
.setHref(SafeUrls.fromProto(safeHref))
.build());
}
示例6: makeLink
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
public static SafeHtml makeLink(String href) {
SafeUrl safeHref;
safeHref = SafeUrls.fromConstant(href); // href is not a constant!!!
// If href is "javascript:doEvil()" we could be in trouble.
// Instead, sanitize the URL.
// safeHref = SafeUrls.sanitize(href);
// Use the builder to make the link.
return new SafeHtmlBuilder("a")
.setHref(safeHref)
.build();
}
示例7: toSafeUrlProto
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
/**
* Converts a Soy {@link SanitizedContent} of kind URI into a {@link SafeUrlProto}.
*
* @throws IllegalStateException if this SanitizedContent's content kind is not {@link
* ContentKind#URI}.
*/
public SafeUrlProto toSafeUrlProto() {
Preconditions.checkState(
getContentKind() == ContentKind.URI,
"toSafeUrlProto() only valid for SanitizedContent of kind URI, is: %s",
getContentKind());
return SafeUrls.toProto(
UncheckedConversions.safeUrlFromStringKnownToSatisfyTypeContract(getContent()));
}
示例8: createSourceLink
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
/** Creates a link to a specific line in a rendered source file. */
public SourceLink createSourceLink(Path path, Position position) {
if (urlTemplate.isPresent()) {
path = dfs.getSourceRelativePath(path);
} else {
path = dfs.getPath(path);
if (pathContext.isPresent()) {
path = dfs.getRelativePath(pathContext.get(), path);
}
}
String pathStr = getUriPath(path);
SourceLink.Builder link =
SourceLink.newBuilder()
.setPath(SafeUrls.toProto(sanitize(pathStr)))
.setLine(position.getLine());
if (urlTemplate.isPresent()) {
String url =
urlTemplate
.get()
.replaceAll("%path%", pathStr)
.replaceAll("%line%", String.valueOf(position.getLine()));
link.setUrl(SafeUrls.toProto(sanitize(url)));
}
return link.build();
}
示例9: compareNamedTypes
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private static Comparator<NamedType> compareNamedTypes() {
return (o1, o2) -> {
int diff = o1.getQualifiedName().compareTo(o2.getQualifiedName());
if (diff == 0) {
// Might not have a qualified name.
diff = o1.getName().compareTo(o2.getName());
if (diff == 0) {
String href1 = SafeUrls.fromProto(o1.getLink().getHref()).getSafeUrlString();
String href2 = SafeUrls.fromProto(o2.getLink().getHref()).getSafeUrlString();
diff = href1.compareTo(href2);
}
}
return diff;
};
}
示例10: checkLink
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private static void checkLink(SourceLink link, String url, String text, int line) {
SourceLink expected =
SourceLink.newBuilder()
.setPath(HtmlConversions.newSafeUrlProtoForTest(text))
.setLine(line)
.setUrl(SafeUrls.toProto(SafeUrls.sanitize(url)))
.build();
assertThat(link).isEqualTo(expected);
}
示例11: testNewSafeUrlProtoForTest
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
public void testNewSafeUrlProtoForTest() {
String url = "https://www.google.com";
SafeUrlProto proto = HtmlConversions.newSafeUrlProtoForTest(url);
assertEquals(SafeUrls.sanitize(url), SafeUrls.fromProto(proto));
}
示例12: fromSafeUrlProto
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
/** Converts a {@link SafeUrlProto} into a Soy {@link SanitizedContent} of kind URI. */
public static SanitizedContent fromSafeUrlProto(SafeUrlProto url) {
return SanitizedContent.create(
SafeUrls.fromProto(url).getSafeUrlString(), ContentKind.URI, Dir.LTR);
}
示例13: appendFragment
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private static TypeLink.Builder appendFragment(TypeLink original, String fragment) {
SafeUrl url = SafeUrls.fromProto(original.getHref());
url = sanitize(url.getSafeUrlString() + "#" + fragment);
return original.toBuilder().setHref(SafeUrls.toProto(url));
}
示例14: urlString
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private static String urlString(SafeUrlProto url) {
return SafeUrls.fromProto(url).getSafeUrlString();
}
示例15: parseMarkdown
import com.google.common.html.types.SafeUrls; //导入依赖的package包/类
private String parseMarkdown(String text, LinkFactory linkFactory) {
if (isNullOrEmpty(text)) {
return "";
}
StringBuilder builder = new StringBuilder(text.length());
int start = 0;
while (true) {
int tagletStart = findInlineTagStart(text, start);
if (tagletStart == -1) {
if (start < text.length()) {
builder.append(text.substring(start));
}
break;
} else if (tagletStart > start) {
builder.append(text.substring(start, tagletStart));
}
int tagletEnd = findInlineTagEnd(text, tagletStart + 1);
if (tagletEnd == -1) {
builder.append(text.substring(start));
break;
}
String tagletName = getTagletName(text, tagletStart);
String tagletPrefix = "{@" + tagletName + " ";
String tagletText = text.substring(tagletStart + tagletPrefix.length(), tagletEnd);
switch (tagletName) {
case "code":
builder.append("<code>").append(HTML_ESCAPER.escape(tagletText)).append("</code>");
break;
case "link":
case "linkplain":
LinkInfo info = LinkInfo.fromText(tagletText);
@Nullable NamedType type = linkFactory.resolveTypeReference(info.type);
String linkText = info.text;
boolean codeLink = "link".equals(tagletName);
if (type == null
|| SafeUrls.fromProto(type.getLink().getHref()).getSafeUrlString().isEmpty()) {
if (codeLink) {
linkText = "<code>" + linkText + "</code>";
}
builder.append(linkText);
} else {
try {
soyRenderer.render(builder, linkText, type.getLink(), codeLink);
} catch (IOException e) {
throw new AssertionError("should never happen", e);
}
}
break;
case "literal":
default:
builder.append(HTML_ESCAPER.escape(tagletText));
}
start = tagletEnd + 1;
}
String markdown = builder.toString();
Node root = parser.parse(markdown);
return sanitize(renderer.render(root));
}