for (final ResourceFile fileObject : delegateDirectory.findChildren().results())
@Override @Nonnull protected List<? extends ResourceFile> computeResults() { if (name != null) { if (name.contains("/")) { throw new IllegalArgumentException("relativePath: " + name); } final ResourceFile child = getChildrenMap().get(name); return (child != null) ? Collections.singletonList(child) : Collections.<ResourceFile>emptyList(); } // // FIXME: this reproduces the behaviour before the refactoring for NW-192 - but it's (and was) wrong // since it doesn't consider delegates. It should be rewritten by relying on getChildrenMap() and // making it eventually support recursion. // else if (recursive) { return delegate.findChildren().withRecursion(recursive).results(); } else { return new ArrayList<>(getChildrenMap().values()); } } };
for (final ResourceFile fo : zipFolder.findChildren().results())
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private ResourceProperties loadProperties() throws IOException { final ResourceFile file = getFile(); log.debug("loadProperties() for {}", file.getPath().asString()); ResourceProperties properties = modelFactory.createProperties().withPropertyResolver(propertyResolver).build(); try { final ResourceFile propertyFile = file.findChildren().withName("Properties.xml").result(); // FIXME reuse the inheritance helper // log.trace(">>>> reading properties from {} ({})...", propertyFile.getPath().asString(), locale); @Cleanup final InputStream is = propertyFile.getInputStream(); final ResourceProperties tempProperties = // modelFactory.createProperties().build().as(Unmarshallable).unmarshal(is); modelFactory.createProperties().withPropertyResolver(propertyResolver).build().as(Unmarshallable).unmarshal(is); // log.trace(">>>>>>>> read properties: {} ({})", tempProperties, locale); properties = properties.merged(tempProperties); } catch (NotFoundException e) { // ok, no properties } return properties; } }
@BeforeMethod public void setup() throws Exception { final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/CommonsAutoBeans.xml", "META-INF/XsltTemplateTestBeans.xml", "META-INF/CachedUriResolverBeans.xml"); filter = context.getBean(XsltMacroFilter.class); context.getBean(CachedURIResolver.class).setCacheFolderPath("target/CachedUriResolver"); final SiteProvider siteProvider = context.getBean(SiteProvider.class); final Site site = mock(Site.class); when(siteProvider.getSite()).thenReturn(site); final ResourceFileSystemProvider fileSystemProvider = mock(ResourceFileSystemProvider.class); when(site.getFileSystemProvider()).thenReturn(fileSystemProvider); final File root = new File("src/main/resources/content/library/XsltTemplates").getAbsoluteFile(); final ResourceFileSystemProvider localFileSystemProvider = new LocalFileSystemProvider(); final ResourceFile file = localFileSystemProvider.getFileSystem().findFileByPath(root.getAbsolutePath()); final List<Resource> resources = new ArrayList<>(); for (final ResourceFile xsltFile : file.findChildren().results()) { final Resource resource = mock(Resource.class); when(resource.getFile()).thenReturn(xsltFile); resources.add(resource); } when(site.find(eq(Resource.class))).thenReturn(new MockResourceFinder(resources)); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ private void copyFolder (final @Nonnull ResourceFile sourceFolder, final @Nonnull ResourceFile targetFolder) throws IOException { log.trace("copyFolder({}, {}", sourceFolder, targetFolder); for (final ResourceFile sourceChild : sourceFolder.findChildren().results()) { if (!sourceChild.isFolder()) { log.trace(">>>> copying {} into {} ...", sourceChild, targetFolder); sourceChild.copyTo(targetFolder); } } for (final ResourceFile sourceChild : sourceFolder.findChildren().results()) { if (sourceChild.isFolder()) { copyFolder(sourceChild, targetFolder.createFolder(sourceChild.getName())); } } } }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull protected List<? extends Type> computeResults() { final List<Type> result = new ArrayList<>(); for (final ResourceFile childFile : parentFile.findChildren().withRecursion(true).results()) { if (childFile.isFolder()) { try { final String relativeUri = childFile.getPath().relativeTo(resourceRootPath).urlDecoded().asString(); result.add(siteProvider.get().getSite().find(typeClass).withRelativePath(relativeUri).result()); } catch (NotFoundException e) { log.error("", e); } } } return result; }
/******************************************************************************************************************* * ******************************************************************************************************************/ private List<ResourceFile> getFiles (final @Nonnull String prefix, final @Nonnull String propertyFileName, final @Nonnull ResourceFile folder, final @Nonnull List<String> suffixes) { final List<ResourceFile> files = new ArrayList<>(); for (final String localeSuffix : suffixes) { final String fileName = prefix + propertyFileName + localeSuffix + ".xml"; log.trace(">>>> probing {} ...", folder.getPath().asString() + "/" + fileName); try { files.add(folder.findChildren().withName(fileName).result()); } catch (NotFoundException e) { // ok. do nothing } } return files; }
@Override @Nonnull protected List<? extends T> computeResults() { // FIXME: it's not flyweight final List<T> results = new ArrayList<>(); for (final ResourceFile childFile : resourceFile.findChildren().results()) { if (childFile.isFolder()) { try { results.add(productCreator.createProduct(childFile)); } catch (IOException | NotFoundException e) { throw new RuntimeException(e); } } } return results; } }
/******************************************************************************************************************* * * 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); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @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); } } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @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; }
/******************************************************************************************************************* * * ******************************************************************************************************************/ private void emptyFolder (final @Nonnull ResourceFile folder) throws IOException { log.trace("emptyFolder({}, {}", folder); for (final ResourceFile child : folder.findChildren().results()) { child.delete(); } }