/******************************************************************************************************************* * * Traverse the file system with a {@link FilePredicate}. * * @param folder the folder to traverse * @param fileFilter the filter for directory contents * @param predicate the predicate * ******************************************************************************************************************/ private void traverse (final @Nonnull ResourceFile folder, final @Nonnull FileFilter fileFilter, final @Nonnull FilePredicate predicate) { traverse(folder.getPath(), folder, fileFilter, predicate); }
@Nonnull private static String toString (final @CheckForNull Resource resource) { return (resource == null) ? "null" : resource.getFile().getPath().asString(); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Override @Nonnull public List<ResourceFile> getInheritedPropertyFiles (final @Nonnull ResourceFile folder, final @Nonnull Locale locale, final @Nonnull String propertyFileName) { log.trace("getInheritedPropertyFiles({}, {})", folder.getPath(), propertyFileName); final List<String> suffixes = new ArrayList<>(); suffixes.add(""); suffixes.add("_" + locale.getLanguage()); final List<ResourceFile> files = new ArrayList<>(); for (final ResourceFile parentFolder : getHierarchyFolders(folder)) { files.addAll(getFiles("", propertyFileName, parentFolder, suffixes)); } files.addAll(getFiles("Override", propertyFileName, folder, suffixes)); log.trace(">>>> property file candidates: {}", files); return files; }
@Override public String toString() { return "AdminContent(" + getFile().getPath().asString() + ')'; } }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public synchronized ResourceFile createDecoratorFile (final @Nonnull ResourceFile delegateFile) { if (delegateFile == null) { return null; } ResourceFile decorator = delegateLightWeightMap.get(delegateFile); if (decorator == null) { decorator = (delegateFile.isData() ? new DecoratorResourceFile(this, delegateFile) : new DecoratorResourceFolder(this, delegates, delegateFile.getPath(), delegateFile)); delegateLightWeightMap.put(delegateFile, decorator); } return decorator; } }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @SuppressWarnings("unchecked") public <Type> Type resolveProperty (final @Nonnull Id propertyGroupId, final @Nonnull Key<Type> propertyName) throws NotFoundException, IOException { log.trace("resolveProperty({})", propertyName); final ResourceFile propertyFile = findLocalizedFile(propertyName.stringValue()); log.trace(">>>> reading from {}", propertyFile.getPath()); final String mimeType = propertyFile.getMimeType(); final String charset = mimeType.equals("application/xhtml+xml") ? "UTF-8" : Charset.defaultCharset().name(); try { return (Type)filterSetExpander.get().filter(propertyFile.asText(charset), mimeType); } catch (RuntimeException e) // FIXME: introduce a FilterException { throw new IOException(e); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ // FIXME: this should be shared between instances private void initialize() throws IOException, NotFoundException { log.info("Retrieving XSLT templates"); final String template = IOUtils.toString(getClass().getResourceAsStream("/it/tidalwave/northernwind/core/impl/filter/XsltTemplate.xslt")); final StringBuilder xsltBuffer = new StringBuilder(); for (final Resource resource : siteProvider.get().getSite().find(Resource.class).withRelativePath(XSLT_TEMPLATES_PATH).results()) { final ResourceFile file = resource.getFile(); log.info(">>>> {}", file.getPath().asString()); xsltBuffer.append(file.asText("UTF-8")); } final ST t = new ST(template, '%', '%').add("content", xsltBuffer.toString()); xslt = t.render(); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private DefaultLayout loadLayout (final @Nonnull ResourceFile layoutFile) throws IOException { log.trace(">>>> reading layout from {}...", layoutFile.getPath().asString()); final @Cleanup InputStream is = layoutFile.getInputStream(); return modelFactory.createLayout().build().as(Unmarshallable).unmarshal(is); } }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Content build (final @Nonnull Content.Builder builder) { final Content content = mock(Content.class); final String path = builder.getFolder().getPath().asString(); log.trace(">>>> creating Content for {}", path); when(content.toString()).thenReturn(String.format("Content(path=%s)", path)); return content; }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Resource build (final @Nonnull Resource.Builder builder) { final Resource resource = mock(Resource.class); final String path = builder.getFile().getPath().asString(); log.trace(">>>> creating Resource for {}", path); when(resource.toString()).thenReturn(String.format("Resource(path=%s)", path)); return resource; }
@Nonnull public static ResourceFile folder (final @Nonnull ResourceFile parent, final @Nonnull String fileName) { final ResourcePath parentPath = parent.getPath(); return new MockResourceFile(parent, parentPath.appendedWith(fileName), true); }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Media build (final @Nonnull Media.Builder builder) { final Media media = mock(Media.class); final String path = builder.getFile().getPath().asString(); log.trace(">>>> creating Media for {}", path); when(media.toString()).thenReturn(String.format("Media(path=%s)", path)); return media; }
/******************************************************************************************************************* * * Traverse the file system with a {@link FilePredicate}. * * @param file the file to traverse * @param fileFilter the filter for directory contents * @param predicate the predicate * ******************************************************************************************************************/ private void traverse (final @Nonnull ResourcePath rootPath, final @Nonnull ResourceFile file, final @Nonnull FileFilter fileFilter, final @Nonnull FilePredicate predicate) { log.trace("traverse({}, {}, {}, {})", rootPath, file, fileFilter, predicate); final ResourcePath relativePath = file.getPath().urlDecoded().relativeTo(rootPath); if (fileFilter.accept(file)) { predicate.apply(file, relativePath); } for (final ResourceFile child : file.findChildren().results()) { traverse(rootPath, child, fileFilter, predicate); } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @SuppressWarnings("unchecked") public FolderBasedFinderSupport (final @Nonnull Type parentResource) { try { final Site site = siteProvider.get().getSite(); this.typeClass = getInterface(parentResource); this.resourceRootPath = site.find(typeClass).withRelativePath("/").result().getFile().getPath(); this.parentFile = parentResource.getFile(); } catch (NotFoundException e) { throw new RuntimeException(e); // never occurs } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private ResourceFile createMockFolder (final @Nonnull String name) { final ResourceFile folder = mock(ResourceFile.class); when(folder.getName()).thenReturn(name); when(folder.getPath()).thenReturn(new ResourcePath(name)); when(folder.isData()).thenReturn(false); when(folder.isFolder()).thenReturn(true); when(folder.findChildren()).thenReturn(new ListFinder(new ArrayList<ResourceFile>())); when(folder.toString()).thenReturn(name); return folder; }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private ResourceFile createMockFile (final @Nonnull String name) { final ResourceFile folder = mock(ResourceFile.class); when(folder.getName()).thenReturn(name); when(folder.getPath()).thenReturn(new ResourcePath(name)); when(folder.toString()).thenReturn(name); when(folder.isData()).thenReturn(true); when(folder.isFolder()).thenReturn(false); when(folder.findChildren()).thenReturn(new ListFinder(new ArrayList<ResourceFile>())); return folder; }
/******************************************************************************************************************* * * Returns the parent {@code SiteNode}. * * @return the parent node * @throws NotFoundException if the parent doesn't exist * @throws UnsupportedEncodingException * ******************************************************************************************************************/ @Nonnull private SiteNode getParent() throws NotFoundException { final ResourcePath parentRelativePath = getResource().getFile().getParent().getPath().urlDecoded() .relativeTo(site.getNodeFolder().getPath()); return site.find(SiteNode.class).withRelativePath(parentRelativePath.asString()).result(); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull protected ResourceFile createMockFile (final @Nonnull ResourceFileSystem fileSystem, final @Nonnull ResourceFile parentFolder, final @Nonnull String name) { final ResourcePath path = parentFolder.getPath().appendedWith(name); final ResourceFile file = createMockFile(name); when(file.getParent()).thenReturn(parentFolder); when(file.getPath()).thenReturn(path); when(file.toString()).thenReturn(path.asString()); when(fileSystem.findFileByPath(eq(path.asString()))).thenReturn(file); final Collection<ResourceFile> children = new ArrayList<>(parentFolder.findChildren().results()); children.add(file); when(parentFolder.findChildren()).thenReturn(new ListFinder(children)); return file; }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull protected ResourceFile createMockFolder (final @Nonnull ResourceFileSystem fileSystem, final @Nonnull ResourceFile parentFolder, final @Nonnull String name) { final ResourcePath path = parentFolder.getPath().appendedWith(name); final ResourceFile folder = createMockFolder(name); when(folder.getParent()).thenReturn(parentFolder); when(folder.getPath()).thenReturn(path); when(folder.toString()).thenReturn(path.asString()); when(fileSystem.findFileByPath(eq(path.asString()))).thenReturn(folder); final Collection<ResourceFile> children = new ArrayList<>(parentFolder.findChildren().results()); children.add(folder); when(parentFolder.findChildren()).thenReturn(new ListFinder(children)); return folder; }
/******************************************************************************************************************* * * * ******************************************************************************************************************/ private static void dump (final @Nonnull List<String> lines, final @Nonnull ResourceFile fileObject) throws IOException { if (fileObject.isData()) { lines.add(String.format("%s: %s", fileObject.getPath().asString(), fileObject.asText("UTF-8"))); } else { for (final ResourceFile child : fileObject.findChildren().results()) { dump(lines, child); } } }