Skip to content

Commit

Permalink
修复了PublicRes和 DocumentRes可能存在0到n个的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
Trisia committed Oct 8, 2021
1 parent 75f55b3 commit 7222624
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
9 changes: 2 additions & 7 deletions ofdrw-core/src/main/java/org/ofdrw/core/OFDElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,14 @@ public OFDElement addOFDEntity(String name, Serializable value) {
* 设置OFD参数
* <p>
* 如果参数已经存在则修改参数
* <p>
* 如果属性值value为null,表示删除该类元素
*
* 如果需要删除元素,请使用 {@link #removeOFDElemByNames}
*
* @param name 元素名称
* @param value 元素文本
* @return this
*/
public OFDElement setOFDEntity(String name, Serializable value) {
if (value == null) {
this.removeOFDElemByNames(name);
return this;
}

Element e = this.getOFDElement(name);
if (e == null) {
return addOFDEntity(name, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.ofdrw.core.basicStructure.doc;

import org.dom4j.Element;
import org.ofdrw.core.DefaultElementProxy;
import org.ofdrw.core.OFDElement;
import org.ofdrw.core.basicStructure.pageObj.CT_TemplatePage;
import org.ofdrw.core.basicType.ST_ID;
import org.ofdrw.core.basicType.ST_Loc;
import org.ofdrw.core.basicType.ST_RefID;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -43,6 +43,18 @@ public CT_CommonData setMaxUnitID(ST_ID maxUnitID) {
this.setOFDEntity("MaxUnitID", maxUnitID);
return this;
}

/**
* 【必选】
* 设置 当前文档中所有对象使用标识的最大值。
* 初始值为 0。MaxUnitID主要用于文档编辑,
* 在向文档增加一个新对象时,需要分配一个
* 新的标识符,新标识符取值宜为 MaxUnitID + 1,
* 同时需要修改此 MaxUnitID值。
*
* @param maxUnitID 对象标识符最大值
* @return this
*/
public CT_CommonData setMaxUnitID(long maxUnitID) {
return setMaxUnitID(new ST_ID(maxUnitID));
}
Expand Down Expand Up @@ -82,7 +94,7 @@ public CT_PageArea getPageArea() {

/**
* 【可选】
* 设置 公共资源序列 路径
* 设置 公共资源序列 路径 (如果已经存在PublicRes那么替换)
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,字形和颜色空间等宜在公共资源文件中描述
Expand All @@ -97,7 +109,7 @@ public CT_CommonData setPublicRes(ST_Loc publicRes) {

/**
* 【可选】
* 获取 公共资源序列
* 获取 公共资源序列(列表中的第一个PublicRes)
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,字形和颜色空间等宜在公共资源文件中描述
Expand All @@ -110,7 +122,44 @@ public ST_Loc getPublicRes() {

/**
* 【可选】
* 设置 文件资源序列 路径
* 添加 公共资源序列 路径
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,字形和颜色空间等宜在公共资源文件中描述
*
* @param publicRes 公共资源序列
* @return this
*/
public CT_CommonData addPublicRes(ST_Loc publicRes) {
if (publicRes == null || publicRes.getLoc() == null) {
return this;
}
this.addOFDEntity("PublicRes", publicRes);
return this;
}


/**
* 【可选】
* 获取 公共资源 序列
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,字形和颜色空间等宜在公共资源文件中描述
* <p>
* PublicRes 数量为 0~n
*
* @return 公共资源序列路径
*/
public List<ST_Loc> getPublicResList() {
return this.getOFDElements("PublicRes", OFDElement::new)
.stream()
.map(DefaultElementProxy::getText)
.map(ST_Loc::getInstance).collect(Collectors.toList());
}

/**
* 【可选】
* 设置 文件资源序列 路径(DocumentRes已经存在那么替换)
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,
Expand All @@ -124,6 +173,25 @@ public CT_CommonData setDocumentRes(ST_Loc documentRes) {
return this;
}

/**
* 【可选】
* 添加 文件资源序列 路径
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,
* 绘制参数、多媒体和矢量图像等宜在文件资源文件中描述
*
* @param documentRes 公共资源序列
* @return this
*/
public CT_CommonData addDocumentRes(ST_Loc documentRes) {
if (documentRes == null || documentRes.getLoc() == null) {
return this;
}
this.addOFDEntity("DocumentRes", documentRes);
return this;
}

/**
* 【可选】
* 获取 文件资源序列 路径
Expand All @@ -138,6 +206,25 @@ public ST_Loc getDocumentRes() {
return ST_Loc.getInstance(this.getOFDElementText("DocumentRes"));
}

/**
* 【可选】
* 获取 文件资源序列
* <p>
* 公共资源序列,每个节点指向OFD包内的一个资源描述文件,
* 源部分的描述键见 7.9,
* 绘制参数、多媒体和矢量图像等宜在文件资源文件中描述
* <p>
* DocumentRes 数量为 0~n
*
* @return 文件资源序列 路径
*/
public List<ST_Loc> getDocumentResList() {
return this.getOFDElements("DocumentRes", OFDElement::new)
.stream()
.map(DefaultElementProxy::getText)
.map(ST_Loc::getInstance).collect(Collectors.toList());
}

/**
* 【可选】
* 增加 模板页序列
Expand All @@ -161,7 +248,7 @@ public CT_CommonData addTemplatePage(CT_TemplatePage templatePage) {
* @return 模板页序列 (可能为空容器)
*/
public List<CT_TemplatePage> getTemplatePages() {
return this.getOFDElements("TemplatePage",CT_TemplatePage::new);
return this.getOFDElements("TemplatePage", CT_TemplatePage::new);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package org.ofdrw.core.basicStructure.doc;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.ofdrw.TestTool;
import org.ofdrw.core.basicStructure.pageTree.CT_TemplatePageTest;
import org.ofdrw.core.basicType.ST_ID;
import org.ofdrw.core.basicType.ST_Loc;
import org.ofdrw.core.basicType.ST_RefID;

import java.util.List;

public class CT_CommonDataTest {


public static CT_CommonData commonDataCase(){
public static CT_CommonData commonDataCase() {
return new CT_CommonData()
.setMaxUnitID(ST_ID.getInstance("777"))
.setPageArea(CT_PageAreaTest.pageAreaCase())
Expand All @@ -22,7 +25,28 @@ public static CT_CommonData commonDataCase(){
}

@Test
public void gen(){
public void gen() {
TestTool.genXml("CommonData", commonDataCase());
}

@Test
void addDocumentRes() {
final CT_CommonData commonData = new CT_CommonData().addPublicRes(ST_Loc.getInstance("PublicRes.xml"))
.addPublicRes(ST_Loc.getInstance("PublicRes_1.xml"))
.addPublicRes(ST_Loc.getInstance(" "));
TestTool.genXml("CommonData", commonData);
final List<ST_Loc> list = commonData.getPublicResList();
Assertions.assertEquals(2, list.size());
}

@Test
void testAddDocumentRes() {
final CT_CommonData commonData = new CT_CommonData()
.addDocumentRes(ST_Loc.getInstance("DocumentRes.xml"))
.addDocumentRes(ST_Loc.getInstance("DocumentRes_1.xml"))
.addDocumentRes(ST_Loc.getInstance(" "));
TestTool.genXml("CommonData", commonData);
final List<ST_Loc> list = commonData.getDocumentResList();
Assertions.assertEquals(2, list.size());
}
}

0 comments on commit 7222624

Please sign in to comment.