/** * Creates an empty string constant. */ public static SanitizedContent emptyString(ContentKind kind) throws IOException { return new SanitizedContent("", kind); }
/** * Faithfully assumes the provided value is "safe" and marks it not to be re-escaped. * * When you "ordain" a string as safe content, it means that Soy will NOT re-escape or validate * the contents if printed in the relevant context. You can use this to insert known-safe HTML * into a template via a parameter. * * This doesn't do a lot of strict checking, but makes it easier to differentiate safe constants * in your code. */ public static SanitizedContent ordainAsSafe(String value, ContentKind kind) { return new SanitizedContent(value, kind); } }
/** * Loads assumed-safe content from a Java resource. * * This performs ZERO VALIDATION of the data, and takes you on your word that the input is valid. * We assume that resources should be safe because they are part of the binary, and therefore not * attacker controlled, unless the source code is compromised (in which there's nothing we can * do). * * @param contextClass Class relative to which to load the resource. * @param resourceName The name of the resource, relative to the context class. * @param charset The character set to use, usually Charsets.UTF_8. * @param kind The content kind of the resource. */ public static SanitizedContent fromResource( Class<?> contextClass, String resourceName, Charset charset, ContentKind kind) throws IOException { pretendValidateResource(resourceName, kind); return new SanitizedContent( Resources.toString(Resources.getResource(contextClass, resourceName), charset), kind); }
/** * Creates a SanitizedContent object. * * <p>Package-private. Ideally, if one is available, you should use an existing serializer, * sanitizer, verifier, or extractor that returns SanitizedContent objects. Or, you can use * UnsafeSanitizedContentOrdainer in this package, to make it clear that creating these objects * from arbitrary content is risky unless you absolutely know the input is safe. See the comments * in UnsafeSanitizedContentOrdainer for more recommendations. * * @param content A string of valid content with the given content kind. * @param kind Describes the kind of string that content is. * @param dir The content's direction; null if unknown and thus to be estimated when necessary. */ static SanitizedContent create(String content, ContentKind kind, @Nullable Dir dir) { checkArgument( kind != ContentKind.TEXT, "Use UnsanitizedString for SanitizedContent with a kind of TEXT"); if (Flags.stringIsNotSanitizedContent()) { return new SanitizedContent(content, kind, dir); } return SanitizedCompatString.create(content, kind, dir); }
private File writeHtml(File sourceFile, String syntax, Header header, List<Section> sections, SoyListData index, File baseOutputPath, SoyTofu tofu) throws IOException { String relativeName = getRelativeName(sourceFile); File htmlFile = new File(baseOutputPath, relativeName); FileUtils.forceMkdir(htmlFile.getParentFile()); String relativeBase = getRelativeBase(htmlFile,baseOutputPath); Map<String,String> replaceMap = ImmutableMap.<String,String> builder().put("${basePath}",relativeBase).build(); SanitizedContent headerHtml = new SanitizedContent(header.getDocWithTokenReplacement(replaceMap), SanitizedContent.ContentKind.HTML); SoyMapData soyData = new SoyMapData("title", StringUtils.substringBeforeLast(htmlFile.getName(), "."),"nameWithoutExtension", StringUtils.substringBeforeLast(sourceFile.getName(), "."),"syntax", syntax, "groups", index, "basePath", relativeBase, "myPath", relativeName, "header", headerHtml, "sections", Docco.getSoySectionDataWithTokenReplacement(sections, replaceMap)); String rendered = tofu.newRenderer(".batchPage").setData(soyData).render(); FileUtils.writeStringToFile(htmlFile, rendered); return htmlFile; }
/** Creates a SanitizedContent object with default direction. */ static SanitizedContent create(String content, ContentKind kind) { checkArgument( kind != ContentKind.TEXT, "Use UnsanitizedString for SanitizedContent with a kind of TEXT"); if (Flags.stringIsNotSanitizedContent()) { return new SanitizedContent(content, kind, kind.getDefaultDir()); } return SanitizedCompatString.create(content, kind, kind.getDefaultDir()); }
public void writeHtml(File sourceFile, File outputFile) throws IOException { checkNotNull(sourceFile); checkState(sourceFile.exists(),"Source file doesn't exist!"); checkNotNull(outputFile); File hBase = new File(outputFile.getParentFile(),"horizontal"); FileUtils.forceMkdir(hBase); File hFile = new File(hBase,outputFile.getName()); File vBase = new File(outputFile.getParentFile(),"vertical"); FileUtils.forceMkdir(vBase); File vFile = new File(vBase,outputFile.getName()); String syntax = fileMappings.getSyntaxForFile(sourceFile); DoccoParts parts = getDoccoParts(sourceFile); SanitizedContent header = new SanitizedContent(parts.getHeader().getDoc(), SanitizedContent.ContentKind.HTML); String fileNoExt = StringUtils.substringBeforeLast(sourceFile.getName(),"."); SoyMapData soyData = new SoyMapData("basePath", ".", "title",sourceFile.getName(),"nameWithoutExtension",fileNoExt,"syntax", syntax, "header",header,"sections",Docco.getSoySectionData(parts.getSections())); String renderedH = horizontalTofu.newRenderer(".singlePage").setData(soyData).render(); FileUtils.writeStringToFile(hFile,renderedH); String renderedV = verticalTofu.newRenderer(".singlePage").setData(soyData).render(); FileUtils.writeStringToFile(vFile,renderedV); copyResources(hBase,vBase); }
public static SoyListData getSoySectionData(List<Section> sections) throws IOException { SoyListData soyList = new SoyListData(); for(Section section : sections) { SanitizedContent doc = new SanitizedContent(section.getDoc(), SanitizedContent.ContentKind.HTML); SanitizedContent code = new SanitizedContent(section.getCode(), SanitizedContent.ContentKind.HTML); SoyMapData soyMap = new SoyMapData("doc",doc,"code",section.getCode(),"hidden", section.isHidden(),"title",section.getTitle()); soyList.add(soyMap); } return soyList; }
public static SoyListData getSoySectionDataWithTokenReplacement(List<Section> sections,Map<String,String> replaceMap) throws IOException { SoyListData soyList = new SoyListData(); for(Section section : sections) { SanitizedContent doc = new SanitizedContent(section.getDocWithTokenReplacement(replaceMap), SanitizedContent.ContentKind.HTML); SanitizedContent code = new SanitizedContent(section.getCode(), SanitizedContent.ContentKind.HTML); SoyMapData soyMap = new SoyMapData("doc",doc,"code",section.getCode(),"hidden", section.isHidden(),"title",section.getTitle()); soyList.add(soyMap); } return soyList; }