From 808783b8b502fb6b965efad61046e7e025ff4616 Mon Sep 17 00:00:00 2001 From: loy Date: Tue, 3 Aug 2021 10:01:50 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-8675=20=E6=96=B0=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=88=B011.0=E7=9A=84=E5=86=85=E7=BD=AE=E6=96=B0?= =?UTF-8?q?=E5=BC=95=E6=93=8E=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cptx/io/DesignReadWritableProvider.java | 37 ++------- .../designer/toolbar/TemplateTransformer.java | 7 +- .../designer/utils/DesignerCptxFileUtils.java | 79 ------------------- .../java/com/fr/nx/app/designer/CptxApp.java | 6 +- .../com/fr/nx/app/designer/JStreamBook.java | 4 +- .../com/fr/nx/app/designer/CptxAppTest.java | 16 ++-- 6 files changed, 24 insertions(+), 125 deletions(-) delete mode 100644 designer-base/src/main/java/com/fr/nx/app/designer/utils/DesignerCptxFileUtils.java diff --git a/designer-base/src/main/java/com/fr/nx/app/designer/cptx/io/DesignReadWritableProvider.java b/designer-base/src/main/java/com/fr/nx/app/designer/cptx/io/DesignReadWritableProvider.java index 0896e66e96..cfb7c44955 100644 --- a/designer-base/src/main/java/com/fr/nx/app/designer/cptx/io/DesignReadWritableProvider.java +++ b/designer-base/src/main/java/com/fr/nx/app/designer/cptx/io/DesignReadWritableProvider.java @@ -2,14 +2,9 @@ package com.fr.nx.app.designer.cptx.io; import com.fr.common.annotations.Negative; import com.fr.file.FILE; -import com.fr.nx.app.designer.utils.DesignerCptxFileUtils; -import com.fr.nx.cptx.io.handle.impl.AbstractCptxIOProvider; -import com.fr.nx.cptx.pack.util.CompiledReportInputStream; +import com.fr.nx.cptx.io.handle.impl.CompileToPathCptxIOProvider; import com.fr.nx.cptx.pack.util.CompiledReportOutputStream; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import com.fr.nx.cptx.utils.CptxFileUtils; /** * 用于设计器保存编译后的结果并缓存可web预览模版 @@ -20,41 +15,21 @@ import java.io.OutputStream; * @Description: 用于cptx写操作的Provider */ @Negative(until = "2020-05-10") -public class DesignReadWritableProvider extends AbstractCptxIOProvider { +public class DesignReadWritableProvider extends CompileToPathCptxIOProvider { /** * 原文件 */ private final FILE file; - /** - * 编译文件夹 - */ - private final String compileDir; - - private CompiledReportOutputStream outputStream; public DesignReadWritableProvider(FILE file) { + super(CptxFileUtils.getFormatPath(file.getPath())); this.file = file; - this.compileDir = DesignerCptxFileUtils.generateCompileDir(file); } - @Override - public InputStream open() throws Exception { - if (outputStream == null) { - throw new IOException("can not read cptx template before compile"); - } - return new CompiledReportInputStream(outputStream.toByteArray()); - } - /** - * 对于写操作,用到的只有这个方法,以流形式来操作 - * - * @return 保存的目标输出流 - * @throws Exception e - */ @Override - public OutputStream createTemp() throws Exception { - outputStream = new CompiledReportOutputStream(file.asOutputStream(), compileDir); - return outputStream; + public CompiledReportOutputStream outputOpen() throws Exception { + return new CompiledReportOutputStream(file.asOutputStream(), getCompilePath()); } } diff --git a/designer-base/src/main/java/com/fr/nx/app/designer/toolbar/TemplateTransformer.java b/designer-base/src/main/java/com/fr/nx/app/designer/toolbar/TemplateTransformer.java index 6294fa4d88..42490fe7d5 100644 --- a/designer-base/src/main/java/com/fr/nx/app/designer/toolbar/TemplateTransformer.java +++ b/designer-base/src/main/java/com/fr/nx/app/designer/toolbar/TemplateTransformer.java @@ -136,12 +136,12 @@ public enum TemplateTransformer { @NotNull public static TransformResultInfo compileCPTX(WorkBook workbook, FILE file) { //对于非工作目录的cptx,修改无需执行编译 - if(!(file instanceof FileNodeFILE)){ + if (!(file instanceof FileNodeFILE)) { try { OutputStream outputStream = file.asOutputStream(); workbook.export(outputStream); return TransformResultInfo.generateResult(TransformResult.SUCCESS).saved(true); - }catch (Exception e){ + } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); return TransformResultInfo.generateResult(TransformResult.FAILED).saved(false); } @@ -206,9 +206,8 @@ public enum TemplateTransformer { // 读取可 web 预览模版并缓存 try { // todo 是否考虑异步 - String timestampedPath = CptxFileUtils.getTimestampedPath(file.getPath()); CptxTemplate previewCptxTemplate = CptxIOManager.open(cptx).getTemplate(); - CptxTemplatePool.getInstance().addCptxTemplate(timestampedPath, previewCptxTemplate); + CptxTemplatePool.getInstance().addCptxTemplate(CptxFileUtils.getFormatPath(file.getPath()), previewCptxTemplate); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } diff --git a/designer-base/src/main/java/com/fr/nx/app/designer/utils/DesignerCptxFileUtils.java b/designer-base/src/main/java/com/fr/nx/app/designer/utils/DesignerCptxFileUtils.java deleted file mode 100644 index a51e047688..0000000000 --- a/designer-base/src/main/java/com/fr/nx/app/designer/utils/DesignerCptxFileUtils.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fr.nx.app.designer.utils; - -import com.fr.file.FILE; -import com.fr.general.CommonIOUtils; -import com.fr.log.FineLoggerFactory; -import com.fr.main.impl.WorkBook; -import com.fr.nx.marshal.impl.xml.DefaultXMLObjectUnmarshaler; -import com.fr.nx.marshal.util.MarshalUtil; -import com.fr.nx.cptx.entry.metadata.CptxMetadata; -import com.fr.nx.cptx.io.handle.impl.OriginCptProvider; -import com.fr.nx.cptx.marshal.util.CptxMarshalUtil; -import com.fr.nx.cptx.utils.CptxFileUtils; -import com.fr.stable.EncodeConstants; -import com.fr.stable.StableUtils; -import com.fr.stable.xml.XMLableReader; -import com.fr.workspace.WorkContext; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.io.InputStreamReader; - -/** - * Created by loy on 2021/1/18. - * - *

用于设计器的cptx的一些工具类 - *

- */ -public class DesignerCptxFileUtils { - - /** - * 根据file来获取cptx中的cpt - * @param file - * @return - */ - public static WorkBook getWorkBook(final FILE file){ - try (InputStream input = file.asInputStream(); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { - CommonIOUtils.copyBinaryTo(input, outputStream); - OriginCptProvider provider = new OriginCptProvider(outputStream.toByteArray()); - return CptxFileUtils.getWorkBook(provider); - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - return null; - } - - /** - * 获取模板的元数据 - * @param file - * @return - */ - public static CptxMetadata getMetadata(FILE file){ - try { - String metadataPath = StableUtils.pathJoin(generateCompileDir(file), CptxMarshalUtil.NAME_METADATA); - if(!WorkContext.getWorkResource().exist(metadataPath)){ - return null; - } - InputStream metadataInput = WorkContext.getWorkResource().openStream(metadataPath); - XMLableReader reader = XMLableReader.createXMLableReader( - new InputStreamReader(metadataInput, EncodeConstants.ENCODING_UTF_8)); - CptxMetadata metadata = (CptxMetadata) new DefaultXMLObjectUnmarshaler().unmarshal( - MarshalUtil.createMarshalableExtra(CptxMetadata.class), reader); - return metadata; - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - return null; - } - - /** - * 根据文件获取编译后的文件夹绝对路径,比如原文件位于reportlets/test/merge.cptx,转化后为evn://assets/engine/test/merge文件夹 - * @param file - * @return - */ - public static String generateCompileDir(FILE file){ - String filePath = file.getPath(); - return CptxFileUtils.generateCompileDir(filePath); - } -} diff --git a/designer-realize/src/main/java/com/fr/nx/app/designer/CptxApp.java b/designer-realize/src/main/java/com/fr/nx/app/designer/CptxApp.java index 9186a67346..f76be4d89f 100644 --- a/designer-realize/src/main/java/com/fr/nx/app/designer/CptxApp.java +++ b/designer-realize/src/main/java/com/fr/nx/app/designer/CptxApp.java @@ -5,7 +5,7 @@ import com.fr.design.mainframe.AbstractAppProvider; import com.fr.design.mainframe.JTemplate; import com.fr.file.FILE; import com.fr.main.impl.WorkBook; -import com.fr.nx.app.designer.utils.DesignerCptxFileUtils; +import com.fr.nx.cptx.utils.CptxFileUtils; /** * 支持设计器打开cptx文件 @@ -35,7 +35,7 @@ public class CptxApp extends AbstractAppProvider { */ @Override public JTemplate openTemplate(FILE tplFile) { - WorkBook workBook = DesignerCptxFileUtils.getWorkBook(tplFile); + WorkBook workBook = CptxFileUtils.getWorkBook(CptxFileUtils.getFormatPath(tplFile.getPath())); if (workBook == null) { workBook = new WorkBook(); } @@ -44,7 +44,7 @@ public class CptxApp extends AbstractAppProvider { @Override public WorkBook asIOFile(FILE file) { - WorkBook workBook = DesignerCptxFileUtils.getWorkBook(file); + WorkBook workBook = CptxFileUtils.getWorkBook(CptxFileUtils.getFormatPath(file.getPath())); if (workBook == null) { workBook = new WorkBook(); } diff --git a/designer-realize/src/main/java/com/fr/nx/app/designer/JStreamBook.java b/designer-realize/src/main/java/com/fr/nx/app/designer/JStreamBook.java index 2fa748f954..48585b7fda 100644 --- a/designer-realize/src/main/java/com/fr/nx/app/designer/JStreamBook.java +++ b/designer-realize/src/main/java/com/fr/nx/app/designer/JStreamBook.java @@ -14,12 +14,12 @@ import com.fr.general.ComparatorUtils; import com.fr.locale.InterProviderFactory; import com.fr.log.FineLoggerFactory; import com.fr.main.impl.WorkBook; -import com.fr.nx.app.designer.utils.DesignerCptxFileUtils; import com.fr.nx.cptx.entry.metadata.CptxMetadata; import com.fr.nx.app.designer.menu.CalculateAttrAction; import com.fr.nx.app.designer.toolbar.TemplateTransformer; import com.fr.nx.app.designer.toolbar.TransformResult; import com.fr.nx.app.designer.toolbar.TransformResultInfo; +import com.fr.nx.cptx.utils.CptxFileUtils; import com.fr.stable.StringUtils; import com.fr.stable.project.ProjectConstants; import com.fr.third.jodd.util.ArraysUtil; @@ -103,7 +103,7 @@ public class JStreamBook extends JWorkBook { } private String getSuffix() { - CptxMetadata metadata = DesignerCptxFileUtils.getMetadata(this.getEditingFILE()); + CptxMetadata metadata = CptxFileUtils.getMetadata(this.getEditingFILE().getPath()); if (metadata != null && metadata.isForceCpt()) { return InterProviderFactory.getProvider().getLocText("Fine-Plugin_Engine_Compatibility_Mode"); } diff --git a/designer-realize/src/test/java/com/fr/nx/app/designer/CptxAppTest.java b/designer-realize/src/test/java/com/fr/nx/app/designer/CptxAppTest.java index 26ca454c5d..52d3ca42dd 100644 --- a/designer-realize/src/test/java/com/fr/nx/app/designer/CptxAppTest.java +++ b/designer-realize/src/test/java/com/fr/nx/app/designer/CptxAppTest.java @@ -3,7 +3,7 @@ package com.fr.nx.app.designer; import com.fr.file.AbstractFILE; import com.fr.file.FILE; import com.fr.main.impl.WorkBook; -import com.fr.nx.app.designer.utils.DesignerCptxFileUtils; +import com.fr.nx.cptx.utils.CptxFileUtils; import org.easymock.EasyMock; import org.junit.Assert; import org.junit.FixMethodOrder; @@ -21,7 +21,7 @@ import org.powermock.modules.junit4.PowerMockRunnerDelegate; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(JUnit4.class) @PrepareForTest({ - DesignerCptxFileUtils.class + CptxFileUtils.class }) public class CptxAppTest { @@ -34,20 +34,24 @@ public class CptxAppTest { @Test public void testAsIOFile() { - PowerMock.mockStatic(DesignerCptxFileUtils.class); + PowerMock.mockStatic(CptxFileUtils.class); FILE file = new AbstractFILE() { + @Override + public String getPath() { + return "path/to/file.cpt"; + } }; WorkBook workBook = new WorkBook(); - EasyMock.expect(DesignerCptxFileUtils.getWorkBook(file)) + EasyMock.expect(CptxFileUtils.getWorkBook(file.getPath())) .andReturn(workBook).once() .andReturn(null).once(); - PowerMock.replay(DesignerCptxFileUtils.class); + PowerMock.replay(CptxFileUtils.class); Assert.assertSame(workBook, new CptxApp().asIOFile(file)); Assert.assertNotNull(new CptxApp().asIOFile(file)); - PowerMock.verify(DesignerCptxFileUtils.class); + PowerMock.verify(CptxFileUtils.class); }