How to Implement Resource Prefix for Nested Resources

This article shows how to add resource prefix for nested resources using the Aspose.Tasks for Java API.

Resource Prefix for Nested Resources Aspose.Tasks for Java provides ResourceSavingArgs.NestedUri property which allows saving nested resources (e.g. PNG inside SVG files) in the separate folder.

  1public class ResourcePrefixForNestedResources implements IFontSavingCallback, ICssSavingCallback, IImageSavingCallback {
  2
  3    public static String dataDir = Utils.getDataDir(ResourcePrefixForNestedResources.class);
  4
  5    public static void main(String[] args) throws IOException {
  6        FileInputStream fs = new FileInputStream(dataDir + "Project1.mpp");
  7        Project project = new Project(fs);
  8        HtmlSaveOptions options = GetSaveOptions(1);
  9        FileOutputStream stream = new FileOutputStream(dataDir + "document.html");
 10        project.save(stream, options);
 11    }
 12
 13    public void cssSaving(CssSavingArgs args) {
 14        FileOutputStream stream;
 15        try {
 16            stream = new FileOutputStream(dataDir + "css/" + args.getFileName());
 17            args.setStream(stream);
 18            args.setKeepStreamOpen(false);
 19            args.setUri(dataDir + "css/" + args.getFileName());
 20        } catch (FileNotFoundException e) {
 21            e.printStackTrace();
 22        }
 23    }
 24
 25    public void fontSaving(FontSavingArgs args) {
 26        FileOutputStream stream;
 27        try {
 28            stream = new FileOutputStream(dataDir + "fonts/" + args.getFileName());
 29            args.setStream(stream);
 30            args.setKeepStreamOpen(false);
 31            args.setUri(dataDir + "fonts/" + args.getFileName());
 32        } catch (FileNotFoundException e) {
 33            e.printStackTrace();
 34        }
 35    }
 36
 37    public void imageSaving(ImageSavingArgs args) {
 38        if (args.getFileName().endsWith("png")) {
 39            FileOutputStream stream1;
 40            try {
 41                stream1 = new FileOutputStream(dataDir + "resources/nestedResources/" + args.getFileName());
 42                args.setStream(stream1);
 43                args.setKeepStreamOpen(false);
 44                args.setUri(dataDir + "resources/" + args.getFileName());
 45                args.setNestedUri(dataDir + "nestedResources/" + args.getFileName());
 46            } catch (FileNotFoundException e) {
 47                e.printStackTrace();
 48            }
 49        } else {
 50            FileOutputStream stream2;
 51            try {
 52                stream2 = new FileOutputStream(dataDir + "resources/" + args.getFileName());
 53                args.setStream(stream2);
 54                args.setKeepStreamOpen(false);
 55                args.setUri(dataDir + "resources/" + args.getFileName());
 56            } catch (FileNotFoundException e) {
 57                e.printStackTrace();
 58            }
 59        }
 60    }
 61
 62    private static HtmlSaveOptions GetSaveOptions(int pageNumber) {
 63        HtmlSaveOptions saveOptions = new HtmlSaveOptions();
 64        saveOptions.setIncludeProjectNameInPageHeader(false);
 65        saveOptions.setIncludeProjectNameInTitle(false);
 66        saveOptions.setPageSize(PageSize.A3);
 67        saveOptions.setTimescale(Timescale.ThirdsOfMonths);
 68        saveOptions.setReduceFooterGap(true);
 69        saveOptions.setFontFaceTypes(FontFaceType.Ttf);
 70        saveOptions.setExportCss(ResourceExportType.AsFile);
 71        saveOptions.setExportFonts(ResourceExportType.AsFile);
 72        saveOptions.setExportImages(ResourceExportType.AsFile);
 73
 74        ResourcePrefixForNestedResources program = new ResourcePrefixForNestedResources();
 75        saveOptions.setFontSavingCallback(program);
 76        saveOptions.setCssSavingCallback(program);
 77        saveOptions.setImageSavingCallback(program);
 78
 79        saveOptions.getPages().clear();
 80        saveOptions.getPages().add(pageNumber);
 81
 82        File dirFonts = new File(dataDir + "fonts");
 83        if (!dirFonts.exists()) {
 84            dirFonts.mkdir();
 85        }
 86
 87        File dirResources = new File(dataDir + "resources");
 88        if (!dirResources.exists()) {
 89            dirResources.mkdir();
 90        }
 91
 92        File dirNResources = new File(dataDir + "nestedResources");
 93        if (!dirNResources.exists()) {
 94            dirNResources.mkdir();
 95        }
 96
 97        File dirCSS = new File(dataDir + "css");
 98        if (!dirCSS.exists()) {
 99            dirCSS.mkdir();
100        }
101
102        return saveOptions;
103    }
104
105}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.