Last active
August 30, 2023 15:30
-
-
Save gdulus/0ed8b30bb45222bf608bac4ad0375984 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract sealed class Action permits WriteOdif, CopyOcif, CopyOip, CopyResource, CopyJsps { | |
| } | |
| public final class WriteOdif extends Action { ... } | |
| public final class CopyOcif extends Action { ... } | |
| public final class CopyOip extends Action { ... } | |
| public final class CopyResource extends Action { ... } | |
| public final class CopyJsps extends Action { ... } | |
| public static final class ActionsBuilder { | |
| private static final List<Action> _actions = new ArrayList<>(); | |
| private ActionsBuilder(Action action) { | |
| _actions.add(action); | |
| } | |
| public ActionsBuilder add(Action action) { | |
| _actions.add(action); | |
| return this; | |
| } | |
| public List<Action> getActions() { | |
| return _actions; | |
| } | |
| public static ActionsBuilder build(Action action) { | |
| return new ActionsBuilder(action); | |
| } | |
| } | |
| public abstract sealed class Writer permits DirWriter, ZipWriter { | |
| public void wirte(ActionsBuilder actionsBuilder) { | |
| actionsBuilder.getActions().stream().forEach(action -> { | |
| switch (action) { | |
| case WriteOdif a -> writeOdif(a); | |
| case CopyOcif a -> copyOcif(a); | |
| ... | |
| } | |
| } | |
| ); | |
| } | |
| protected abstract void writeOdif(WriteOdif writeOdif); | |
| protected abstract void copyOcif(CopyOcif copyOcif); | |
| ... | |
| } | |
| public final class ZipWriter extends Writer { | |
| private final OutputStream _out; | |
| public ZipWriter(OutputStream out) { | |
| _out = out; | |
| } | |
| ... | |
| } | |
| public final class DirWriter extends Writer { | |
| private final File _out; | |
| public DirWriter(File out) { | |
| _out = out; | |
| } | |
| ... | |
| } | |
| -------------------------- | |
| private File createTempOipFile(OpheliaCmsContext cmsContext, String templateName) throws IOException { | |
| File tmpFile = null; | |
| File templateRoot = getTemplateRoot(cmsContext, templateName); | |
| InputStream odifStream = getOdifStream(cmsContext, templateName); | |
| if (odifStream != null) { | |
| tmpFile = IOHelper.createTempFile(getClass().getSimpleName(), "oip"); | |
| tmpFile.getParentFile().mkdirs(); | |
| OutputStream outputStream = new FileOutputStream(tmpFile); | |
| ActionsBuilder actions = ActionsBuilder.build(new WriteOdif(odifStream)); | |
| File ocifDir = new File(templateRoot, OCIF_DIR_NAME); | |
| if (ocifDir.isDirectory()) { | |
| actions.add(new CopyOcif(ocifDir)); | |
| } else { | |
| File ocifOip = new File(templateRoot, OCIF_OIP_FILE_NAME); | |
| if (ocifOip.isFile()) { | |
| try (FileInputStream fileInputStream = new FileInputStream(ocifOip)) { | |
| actions.add(new CopyOip(fileInputStream)); | |
| } | |
| } | |
| } | |
| File resources = new File(templateRoot, OipConstants.RESOURCES_PATH_PREFIX); | |
| if (resources.exists() && resources.isDirectory()) { | |
| actions.add(new CopyResource(resources)); | |
| } | |
| File jsp = new File(templateRoot, OipConstants.JSPS_PATH_PREFIX); | |
| if (jsp.exists() && jsp.isDirectory()) { | |
| actions.add(new CopyJsps(jsp)); | |
| } | |
| new ZipWriter(outputStream).wirte(actions); | |
| } | |
| return tmpFile; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment