This commit is contained in:
csh 2025-04-16 10:03:34 +08:00
parent 87c3aa75f7
commit f816e8b6e3
20 changed files with 27378 additions and 28116 deletions

View File

@ -1,5 +1,7 @@
# OfficeXml # OfficeXml
[迁移指南](./迁移指南.md)
## 概述 ## 概述
将 docx、pptx、xlsx 等文件中的 xml 转为 tsl 对象 将 docx、pptx、xlsx 等文件中的 xml 转为 tsl 对象
@ -50,7 +52,11 @@ echo p.Rs(1).T.Text; // 输出:(份)
## 基类 ## 基类
`OpenXmlElement.tsf`提供了一些常用的方法,具体可见`tsf`文件 `OpenXmlAttribute`是节点的属性类
`OpenXmlElement.tsf`是节点基类
`OpenXmlSimpleType`是简单类型的元素节点类,继承于`OpenXmlElement`,如`<b />`
`OpenXmlTextElement`是包含文本内容的元素节点类,继承于`OpenXmlElement`,如`<t>文本</t>`
`OpenXmlCompositeElement`是复杂节点的元素节点类,继承于`OpenXmlElement`,会包含一系列的属性或其他元素节点类
## Unit 单元 ## Unit 单元
@ -209,7 +215,7 @@ style := styles_adapter.GetStyleByStyleId("a6");
echo style.Name; // 输出的是"页脚 字符" echo style.Name; // 输出的是"页脚 字符"
``` ```
### 补充 ### 其他设计的补充
```xml ```xml
<m:r> <m:r>
@ -232,3 +238,8 @@ echo style.Name; // 输出的是"页脚 字符"
由于设计取对象时候不包含前缀,那么同时存在多个`rPr`时,如何区分是哪一个? 由于设计取对象时候不包含前缀,那么同时存在多个`rPr`时,如何区分是哪一个?
取`m:rPr`时,可通过`r.RPr`,这是因为它们的前缀都是`m`,所以可直接获取 取`m:rPr`时,可通过`r.RPr`,这是因为它们的前缀都是`m`,所以可直接获取
取`a:rPr`时,需要指定前缀`a`,通过`r.RPr('a')`,进行获取`RPr`对象 取`a:rPr`时,需要指定前缀`a`,通过`r.RPr('a')`,进行获取`RPr`对象
## TODO
- [ ] 命名空间不同,但是名字相同属性的读写访问
- [ ] 属性的删除操作

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

122
autounit/PptxML.tsf Normal file
View File

@ -0,0 +1,122 @@
unit PptxML;
interface
type SldLayoutId = class(OpenXmlCompositeElement)
public
function Create();overload;
function Create(_node: XmlNode);overload;
function Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
function Init();override;
function Copy(_obj: SldLayoutId);override;
public
// attributes property
property Id read ReadXmlAttrId write WriteXmlAttrId;
property Id read ReadXmlAttrId write WriteXmlAttrId;
property Id read ReadXmlAttrId write WriteXmlAttrId;
function ReadXmlAttrId();
function WriteXmlAttrId(_value);
function ReadXmlAttrId();
function WriteXmlAttrId(_value);
function ReadXmlAttrId();
function WriteXmlAttrId(_value);
public
// Attributes
XmlAttrId: OpenXmlAttribute;
XmlAttrId: OpenXmlAttribute;
XmlAttrId: OpenXmlAttribute;
end;
implementation
function SldLayoutId.Create();overload;
begin
{self.}Create(nil, "p", "sldLayoutId");
end;
function SldLayoutId.Create(_node: XmlNode);overload;
begin
class(OpenXmlCompositeElement).Create(_node: XmlNode);
end;
function SldLayoutId.Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
begin
setsysparam(pn_calcctrlword(), getsysparam(pn_calcctrlword()) .| 0x200);
class(OpenXmlCompositeElement).Create(_parent, _prefix, _local_name);
end;
function SldLayoutId.Init();override;
begin
pre := {self.}Prefix ? {self.}Prefix + ":" : "";
attributes_ := array();
attributes_pf_ := array(
pre + "id": makeweakref(thisFunction(WriteXmlAttrId)),
"id": makeweakref(thisFunction(WriteXmlAttrId)),
"r:id": makeweakref(thisFunction(WriteXmlAttrId)),
);
sorted_child_ := array(
);
container_ := new TSOfficeContainer(sorted_child_);
end;
function SldLayoutId.Copy(_obj: SldLayoutId);override;
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
class(OpenXmlCompositeElement).Copy(_obj);
if not ifnil(_obj.Id) then
{self.}Id := _obj.Id;
if not ifnil(_obj.Id) then
{self.}Id := _obj.Id;
if not ifnil(_obj.Id) then
{self.}Id := _obj.Id;
tslassigning := tslassigning_backup;
end;
function SldLayoutId.ReadXmlAttrId();
begin
return {self.}XmlAttrId.Value;
end;
function SldLayoutId.WriteXmlAttrId(_value);
begin
if ifnil({self.}XmlAttrId) then
begin
{self.}XmlAttrId := new OpenXmlAttribute({self.}Prefix, "id", nil);
attributes_[length(attributes_)] := {self.}XmlAttrId;
end
{self.}XmlAttrId.Value := _value;
end;
function SldLayoutId.ReadXmlAttrId();
begin
return {self.}XmlAttrId.Value;
end;
function SldLayoutId.WriteXmlAttrId(_value);
begin
if ifnil({self.}XmlAttrId) then
begin
{self.}XmlAttrId := new OpenXmlAttribute("", "id", nil);
attributes_[length(attributes_)] := {self.}XmlAttrId;
end
{self.}XmlAttrId.Value := _value;
end;
function SldLayoutId.ReadXmlAttrId();
begin
return {self.}XmlAttrId.Value;
end;
function SldLayoutId.WriteXmlAttrId(_value);
begin
if ifnil({self.}XmlAttrId) then
begin
{self.}XmlAttrId := new OpenXmlAttribute("r", "id", nil);
attributes_[length(attributes_)] := {self.}XmlAttrId;
end
{self.}XmlAttrId.Value := _value;
end;
end.

View File

@ -0,0 +1,6 @@
unit PptxMLAdapter;
interface
implementation
end.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -4,545 +4,545 @@ uses VML, TSSafeUnitConverter;
type ShapetypeUnitDecorator = class(Shapetype) type ShapetypeUnitDecorator = class(Shapetype)
public public
function Create(_obj: Shapetype); function Create(_obj: Shapetype);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Shapetype; object_: Shapetype;
end; end;
type FormulasUnitDecorator = class(Formulas) type FormulasUnitDecorator = class(Formulas)
public public
function Create(_obj: Formulas); function Create(_obj: Formulas);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Formulas; object_: Formulas;
end; end;
type LockUnitDecorator = class(Lock) type LockUnitDecorator = class(Lock)
public public
function Create(_obj: Lock); function Create(_obj: Lock);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Lock; object_: Lock;
end; end;
type FUnitDecorator = class(F) type FUnitDecorator = class(F)
public public
function Create(_obj: F); function Create(_obj: F);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: F; object_: F;
end; end;
type StrokeUnitDecorator = class(Stroke) type StrokeUnitDecorator = class(Stroke)
public public
function Create(_obj: Stroke); function Create(_obj: Stroke);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Stroke; object_: Stroke;
end; end;
type PathUnitDecorator = class(Path) type PathUnitDecorator = class(Path)
public public
function Create(_obj: Path); function Create(_obj: Path);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Path; object_: Path;
end; end;
type TextpathUnitDecorator = class(Textpath) type TextpathUnitDecorator = class(Textpath)
public public
function Create(_obj: Textpath); function Create(_obj: Textpath);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Textpath; object_: Textpath;
end; end;
type HandlesUnitDecorator = class(Handles) type HandlesUnitDecorator = class(Handles)
public public
function Create(_obj: Handles); function Create(_obj: Handles);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Handles; object_: Handles;
end; end;
type HUnitDecorator = class(H) type HUnitDecorator = class(H)
public public
function Create(_obj: H); function Create(_obj: H);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: H; object_: H;
end; end;
type ShapeUnitDecorator = class(Shape) type ShapeUnitDecorator = class(Shape)
public public
function Create(_obj: Shape); function Create(_obj: Shape);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Shape; object_: Shape;
end; end;
type WrapUnitDecorator = class(Wrap) type WrapUnitDecorator = class(Wrap)
public public
function Create(_obj: Wrap); function Create(_obj: Wrap);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Wrap; object_: Wrap;
end; end;
type FillUnitDecorator = class(Fill) type FillUnitDecorator = class(Fill)
public public
function Create(_obj: Fill); function Create(_obj: Fill);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Fill; object_: Fill;
end; end;
type ImagedataUnitDecorator = class(Imagedata) type ImagedataUnitDecorator = class(Imagedata)
public public
function Create(_obj: Imagedata); function Create(_obj: Imagedata);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Imagedata; object_: Imagedata;
end; end;
type TextboxUnitDecorator = class(Textbox) type TextboxUnitDecorator = class(Textbox)
public public
function Create(_obj: Textbox); function Create(_obj: Textbox);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: Textbox; object_: Textbox;
end; end;
type OLEObjectUnitDecorator = class(OLEObject) type OLEObjectUnitDecorator = class(OLEObject)
public public
function Create(_obj: OLEObject); function Create(_obj: OLEObject);
function GetObject(); function GetObject();
function Convert(); function Convert();
private private
object_: OLEObject; object_: OLEObject;
end; end;
implementation implementation
function ShapetypeUnitDecorator.Create(_obj: Shapetype); function ShapetypeUnitDecorator.Create(_obj: Shapetype);
begin begin
class(Shapetype).Create(); class(Shapetype).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function ShapetypeUnitDecorator.GetObject(); function ShapetypeUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function ShapetypeUnitDecorator.Convert(); function ShapetypeUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrAnchorId) then if not ifnil(object_.XmlAttrAnchorId) then
{self.}AnchorId := object_.XmlAttrAnchorId.Value; {self.}AnchorId := object_.XmlAttrAnchorId.Value;
if not ifnil(object_.XmlAttrId) then if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value; {self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrCoordsize) then if not ifnil(object_.XmlAttrCoordsize) then
{self.}Coordsize := object_.XmlAttrCoordsize.Value; {self.}Coordsize := object_.XmlAttrCoordsize.Value;
if not ifnil(object_.XmlAttrSpt) then if not ifnil(object_.XmlAttrSpt) then
{self.}Spt := object_.XmlAttrSpt.Value; {self.}Spt := object_.XmlAttrSpt.Value;
if not ifnil(object_.XmlAttrAdj) then if not ifnil(object_.XmlAttrAdj) then
{self.}Adj := object_.XmlAttrAdj.Value; {self.}Adj := object_.XmlAttrAdj.Value;
if not ifnil(object_.XmlAttrPreferrelative) then if not ifnil(object_.XmlAttrPreferrelative) then
{self.}Preferrelative := object_.XmlAttrPreferrelative.Value; {self.}Preferrelative := object_.XmlAttrPreferrelative.Value;
if not ifnil(object_.XmlAttrPath) then if not ifnil(object_.XmlAttrPath) then
{self.}Path := object_.XmlAttrPath.Value; {self.}Path := object_.XmlAttrPath.Value;
if not ifnil(object_.XmlAttrFilled) then if not ifnil(object_.XmlAttrFilled) then
{self.}Filled := object_.XmlAttrFilled.Value; {self.}Filled := object_.XmlAttrFilled.Value;
if not ifnil(object_.XmlAttrStroked) then if not ifnil(object_.XmlAttrStroked) then
{self.}Stroked := object_.XmlAttrStroked.Value; {self.}Stroked := object_.XmlAttrStroked.Value;
if not ifnil(object_.XmlChildStroke) then if not ifnil(object_.XmlChildStroke) then
{self.}XmlChildStroke := new StrokeUnitDecorator(object_.XmlChildStroke); {self.}XmlChildStroke := new StrokeUnitDecorator(object_.XmlChildStroke);
if not ifnil(object_.XmlChildFormulas) then if not ifnil(object_.XmlChildFormulas) then
{self.}XmlChildFormulas := new formulasUnitDecorator(object_.XmlChildFormulas); {self.}XmlChildFormulas := new formulasUnitDecorator(object_.XmlChildFormulas);
if not ifnil(object_.XmlChildPath) then if not ifnil(object_.XmlChildPath) then
{self.}XmlChildPath := new PathUnitDecorator(object_.XmlChildPath); {self.}XmlChildPath := new PathUnitDecorator(object_.XmlChildPath);
if not ifnil(object_.XmlChildTextpath) then if not ifnil(object_.XmlChildTextpath) then
{self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath); {self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath);
if not ifnil(object_.XmlChildHandles) then if not ifnil(object_.XmlChildHandles) then
{self.}XmlChildHandles := new HandlesUnitDecorator(object_.XmlChildHandles); {self.}XmlChildHandles := new HandlesUnitDecorator(object_.XmlChildHandles);
if not ifnil(object_.XmlChildLock) then if not ifnil(object_.XmlChildLock) then
{self.}XmlChildLock := new LockUnitDecorator(object_.XmlChildLock); {self.}XmlChildLock := new LockUnitDecorator(object_.XmlChildLock);
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function FormulasUnitDecorator.Create(_obj: Formulas); function FormulasUnitDecorator.Create(_obj: Formulas);
begin begin
class(Formulas).Create(); class(Formulas).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function FormulasUnitDecorator.GetObject(); function FormulasUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function FormulasUnitDecorator.Convert(); function FormulasUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
elems := object_.Fs(); elems := object_.Fs();
for _,elem in elems do for _,elem in elems do
{self.}AppendChild(new ShapetypeUnitDecorator(elem)); {self.}AppendChild(new ShapetypeUnitDecorator(elem));
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function LockUnitDecorator.Create(_obj: Lock); function LockUnitDecorator.Create(_obj: Lock);
begin begin
class(Lock).Create(); class(Lock).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function LockUnitDecorator.GetObject(); function LockUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function LockUnitDecorator.Convert(); function LockUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrExt) then if not ifnil(object_.XmlAttrExt) then
{self.}Ext := object_.XmlAttrExt.Value; {self.}Ext := object_.XmlAttrExt.Value;
if not ifnil(object_.XmlAttrAspectration) then if not ifnil(object_.XmlAttrAspectration) then
{self.}Aspectration := object_.XmlAttrAspectration.Value; {self.}Aspectration := object_.XmlAttrAspectration.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function FUnitDecorator.Create(_obj: F); function FUnitDecorator.Create(_obj: F);
begin begin
class(F).Create(); class(F).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function FUnitDecorator.GetObject(); function FUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function FUnitDecorator.Convert(); function FUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrEqn) then if not ifnil(object_.XmlAttrEqn) then
{self.}Eqn := object_.XmlAttrEqn.Value; {self.}Eqn := object_.XmlAttrEqn.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function StrokeUnitDecorator.Create(_obj: Stroke); function StrokeUnitDecorator.Create(_obj: Stroke);
begin begin
class(Stroke).Create(); class(Stroke).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function StrokeUnitDecorator.GetObject(); function StrokeUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function StrokeUnitDecorator.Convert(); function StrokeUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrJoinstyle) then if not ifnil(object_.XmlAttrJoinstyle) then
{self.}Joinstyle := object_.XmlAttrJoinstyle.Value; {self.}Joinstyle := object_.XmlAttrJoinstyle.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function PathUnitDecorator.Create(_obj: Path); function PathUnitDecorator.Create(_obj: Path);
begin begin
class(Path).Create(); class(Path).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function PathUnitDecorator.GetObject(); function PathUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function PathUnitDecorator.Convert(); function PathUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrExtrusionok) then if not ifnil(object_.XmlAttrExtrusionok) then
{self.}Extrusionok := object_.XmlAttrExtrusionok.Value; {self.}Extrusionok := object_.XmlAttrExtrusionok.Value;
if not ifnil(object_.XmlAttrTextpathok) then if not ifnil(object_.XmlAttrTextpathok) then
{self.}Textpathok := object_.XmlAttrTextpathok.Value; {self.}Textpathok := object_.XmlAttrTextpathok.Value;
if not ifnil(object_.XmlAttrGradientshapeok) then if not ifnil(object_.XmlAttrGradientshapeok) then
{self.}Gradientshapeok := object_.XmlAttrGradientshapeok.Value; {self.}Gradientshapeok := object_.XmlAttrGradientshapeok.Value;
if not ifnil(object_.XmlAttrConnecttype) then if not ifnil(object_.XmlAttrConnecttype) then
{self.}Connecttype := object_.XmlAttrConnecttype.Value; {self.}Connecttype := object_.XmlAttrConnecttype.Value;
if not ifnil(object_.XmlAttrConnectlocs) then if not ifnil(object_.XmlAttrConnectlocs) then
{self.}Connectlocs := object_.XmlAttrConnectlocs.Value; {self.}Connectlocs := object_.XmlAttrConnectlocs.Value;
if not ifnil(object_.XmlAttrConnectangles) then if not ifnil(object_.XmlAttrConnectangles) then
{self.}Connectangles := object_.XmlAttrConnectangles.Value; {self.}Connectangles := object_.XmlAttrConnectangles.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function TextpathUnitDecorator.Create(_obj: Textpath); function TextpathUnitDecorator.Create(_obj: Textpath);
begin begin
class(Textpath).Create(); class(Textpath).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function TextpathUnitDecorator.GetObject(); function TextpathUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function TextpathUnitDecorator.Convert(); function TextpathUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttr_On) then if not ifnil(object_.XmlAttr_On) then
{self.}_On := object_.XmlAttr_On.Value; {self.}_On := object_.XmlAttr_On.Value;
if not ifnil(object_.XmlAttrFitshape) then if not ifnil(object_.XmlAttrFitshape) then
{self.}Fitshape := object_.XmlAttrFitshape.Value; {self.}Fitshape := object_.XmlAttrFitshape.Value;
if not ifnil(object_.XmlAttrStyle) then if not ifnil(object_.XmlAttrStyle) then
{self.}Style := object_.XmlAttrStyle.Value; {self.}Style := object_.XmlAttrStyle.Value;
if not ifnil(object_.XmlAttrString) then if not ifnil(object_.XmlAttrString) then
{self.}String := object_.XmlAttrString.Value; {self.}String := object_.XmlAttrString.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function HandlesUnitDecorator.Create(_obj: Handles); function HandlesUnitDecorator.Create(_obj: Handles);
begin begin
class(Handles).Create(); class(Handles).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function HandlesUnitDecorator.GetObject(); function HandlesUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function HandlesUnitDecorator.Convert(); function HandlesUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlChildH) then if not ifnil(object_.XmlChildH) then
{self.}XmlChildH := new HUnitDecorator(object_.XmlChildH); {self.}XmlChildH := new HUnitDecorator(object_.XmlChildH);
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function HUnitDecorator.Create(_obj: H); function HUnitDecorator.Create(_obj: H);
begin begin
class(H).Create(); class(H).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function HUnitDecorator.GetObject(); function HUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function HUnitDecorator.Convert(); function HUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrXrange) then if not ifnil(object_.XmlAttrXrange) then
{self.}Xrange := object_.XmlAttrXrange.Value; {self.}Xrange := object_.XmlAttrXrange.Value;
if not ifnil(object_.XmlAttrPosition) then if not ifnil(object_.XmlAttrPosition) then
{self.}Position := object_.XmlAttrPosition.Value; {self.}Position := object_.XmlAttrPosition.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function ShapeUnitDecorator.Create(_obj: Shape); function ShapeUnitDecorator.Create(_obj: Shape);
begin begin
class(Shape).Create(); class(Shape).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function ShapeUnitDecorator.GetObject(); function ShapeUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function ShapeUnitDecorator.Convert(); function ShapeUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrId) then if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value; {self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrStyle) then if not ifnil(object_.XmlAttrStyle) then
{self.}Style := object_.XmlAttrStyle.Value; {self.}Style := object_.XmlAttrStyle.Value;
if not ifnil(object_.XmlAttrSpid) then if not ifnil(object_.XmlAttrSpid) then
{self.}Spid := object_.XmlAttrSpid.Value; {self.}Spid := object_.XmlAttrSpid.Value;
if not ifnil(object_.XmlAttrType) then if not ifnil(object_.XmlAttrType) then
{self.}Type := object_.XmlAttrType.Value; {self.}Type := object_.XmlAttrType.Value;
if not ifnil(object_.XmlAttrGfxdata) then if not ifnil(object_.XmlAttrGfxdata) then
{self.}Gfxdata := object_.XmlAttrGfxdata.Value; {self.}Gfxdata := object_.XmlAttrGfxdata.Value;
if not ifnil(object_.XmlAttrFilled) then if not ifnil(object_.XmlAttrFilled) then
{self.}Filled := object_.XmlAttrFilled.Value; {self.}Filled := object_.XmlAttrFilled.Value;
if not ifnil(object_.XmlAttrAllowincell) then if not ifnil(object_.XmlAttrAllowincell) then
{self.}Allowincell := object_.XmlAttrAllowincell.Value; {self.}Allowincell := object_.XmlAttrAllowincell.Value;
if not ifnil(object_.XmlAttrFillcolor) then if not ifnil(object_.XmlAttrFillcolor) then
{self.}Fillcolor := object_.XmlAttrFillcolor.Value; {self.}Fillcolor := object_.XmlAttrFillcolor.Value;
if not ifnil(object_.XmlAttrStroked) then if not ifnil(object_.XmlAttrStroked) then
{self.}Stroked := object_.XmlAttrStroked.Value; {self.}Stroked := object_.XmlAttrStroked.Value;
if not ifnil(object_.XmlAttrOle) then if not ifnil(object_.XmlAttrOle) then
{self.}Ole := object_.XmlAttrOle.Value; {self.}Ole := object_.XmlAttrOle.Value;
if not ifnil(object_.XmlChildFill) then if not ifnil(object_.XmlChildFill) then
{self.}XmlChildFill := new FillUnitDecorator(object_.XmlChildFill); {self.}XmlChildFill := new FillUnitDecorator(object_.XmlChildFill);
if not ifnil(object_.XmlChildTextbox) then if not ifnil(object_.XmlChildTextbox) then
{self.}XmlChildTextbox := new TextboxUnitDecorator(object_.XmlChildTextbox); {self.}XmlChildTextbox := new TextboxUnitDecorator(object_.XmlChildTextbox);
if not ifnil(object_.XmlChildTextpath) then if not ifnil(object_.XmlChildTextpath) then
{self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath); {self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath);
if not ifnil(object_.XmlChildImagedata) then if not ifnil(object_.XmlChildImagedata) then
{self.}XmlChildImagedata := new ImagedataUnitDecorator(object_.XmlChildImagedata); {self.}XmlChildImagedata := new ImagedataUnitDecorator(object_.XmlChildImagedata);
if not ifnil(object_.XmlChildWrap) then if not ifnil(object_.XmlChildWrap) then
{self.}XmlChildWrap := new WrapUnitDecorator(object_.XmlChildWrap); {self.}XmlChildWrap := new WrapUnitDecorator(object_.XmlChildWrap);
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function WrapUnitDecorator.Create(_obj: Wrap); function WrapUnitDecorator.Create(_obj: Wrap);
begin begin
class(Wrap).Create(); class(Wrap).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function WrapUnitDecorator.GetObject(); function WrapUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function WrapUnitDecorator.Convert(); function WrapUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrAnchorx) then if not ifnil(object_.XmlAttrAnchorx) then
{self.}Anchorx := object_.XmlAttrAnchorx.Value; {self.}Anchorx := object_.XmlAttrAnchorx.Value;
if not ifnil(object_.XmlAttrAnchory) then if not ifnil(object_.XmlAttrAnchory) then
{self.}Anchory := object_.XmlAttrAnchory.Value; {self.}Anchory := object_.XmlAttrAnchory.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function FillUnitDecorator.Create(_obj: Fill); function FillUnitDecorator.Create(_obj: Fill);
begin begin
class(Fill).Create(); class(Fill).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function FillUnitDecorator.GetObject(); function FillUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function FillUnitDecorator.Convert(); function FillUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrOpacity) then if not ifnil(object_.XmlAttrOpacity) then
{self.}Opacity := object_.XmlAttrOpacity.Value; {self.}Opacity := object_.XmlAttrOpacity.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function ImagedataUnitDecorator.Create(_obj: Imagedata); function ImagedataUnitDecorator.Create(_obj: Imagedata);
begin begin
class(Imagedata).Create(); class(Imagedata).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function ImagedataUnitDecorator.GetObject(); function ImagedataUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function ImagedataUnitDecorator.Convert(); function ImagedataUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrId) then if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value; {self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrTitle) then if not ifnil(object_.XmlAttrTitle) then
{self.}Title := object_.XmlAttrTitle.Value; {self.}Title := object_.XmlAttrTitle.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function TextboxUnitDecorator.Create(_obj: Textbox); function TextboxUnitDecorator.Create(_obj: Textbox);
begin begin
class(Textbox).Create(); class(Textbox).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function TextboxUnitDecorator.GetObject(); function TextboxUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function TextboxUnitDecorator.Convert(); function TextboxUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlChildTxbxContent) then if not ifnil(object_.XmlChildTxbxContent) then
{self.}XmlChildTxbxContent := new TxbxContentUnitDecorator(object_.XmlChildTxbxContent); {self.}XmlChildTxbxContent := new TxbxContentUnitDecorator(object_.XmlChildTxbxContent);
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
function OLEObjectUnitDecorator.Create(_obj: OLEObject); function OLEObjectUnitDecorator.Create(_obj: OLEObject);
begin begin
class(OLEObject).Create(); class(OLEObject).Create();
object_ := _obj; object_ := _obj;
{self.}Convert(); {self.}Convert();
end; end;
function OLEObjectUnitDecorator.GetObject(); function OLEObjectUnitDecorator.GetObject();
begin begin
return object_; return object_;
end; end;
function OLEObjectUnitDecorator.Convert(); function OLEObjectUnitDecorator.Convert();
begin begin
tslassigning_backup := tslassigning; tslassigning_backup := tslassigning;
tslassigning := 1; tslassigning := 1;
if not ifnil(object_.XmlAttrType) then if not ifnil(object_.XmlAttrType) then
{self.}Type := object_.XmlAttrType.Value; {self.}Type := object_.XmlAttrType.Value;
if not ifnil(object_.XmlAttrProgID) then if not ifnil(object_.XmlAttrProgID) then
{self.}ProgID := object_.XmlAttrProgID.Value; {self.}ProgID := object_.XmlAttrProgID.Value;
if not ifnil(object_.XmlAttrShapeID) then if not ifnil(object_.XmlAttrShapeID) then
{self.}ShapeID := object_.XmlAttrShapeID.Value; {self.}ShapeID := object_.XmlAttrShapeID.Value;
if not ifnil(object_.XmlAttrDrawAspect) then if not ifnil(object_.XmlAttrDrawAspect) then
{self.}DrawAspect := object_.XmlAttrDrawAspect.Value; {self.}DrawAspect := object_.XmlAttrDrawAspect.Value;
if not ifnil(object_.XmlAttrObjectID) then if not ifnil(object_.XmlAttrObjectID) then
{self.}ObjectID := object_.XmlAttrObjectID.Value; {self.}ObjectID := object_.XmlAttrObjectID.Value;
if not ifnil(object_.XmlAttrId) then if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value; {self.}Id := object_.XmlAttrId.Value;
tslassigning := tslassigning_backup; tslassigning := tslassigning_backup;
end; end;
end. end.

View File

@ -10,7 +10,6 @@ public
Prefix: string; Prefix: string;
ElementName: string; ElementName: string;
Value: any; Value: any;
end; end;
function OpenXmlAttribute.Create(_prefix: string; _local_name: string);overload; function OpenXmlAttribute.Create(_prefix: string; _local_name: string);overload;

View File

@ -0,0 +1,159 @@
type OpenXmlCompositeElement = class(OpenXmlElement)
public
function InitNode(_node: XmlNode);override;
function Deserialize();override;
function Serialize();override;
function Marshal(): tableArray;override;
public
function AppendChild(_element: OpenXmlElement);
function InsertAfter(_element: OpenXmlElement; _pos_element: OpenXmlElement);
function RemoveChild(_element: OpenXmlElement);
function RemoveAttribute(_attr: OpenXmlAttribute);
// function InsertBefore(_pos_obj: tslobj; _obj: tslobj);
// function PrependChild(_obj: tslobj);
// function SetAttribute(_obj: OpenXmlAttribute);
// function RemoveAttribute();
public
function GetOrCreateNode(_obj: tslobj);
end;
function OpenXmlCompositeElement.InitNode(_node: XmlNode);override;
begin
{self.}XmlNode := _node;
if not _node then return;
tslassigning_backup := tslassigning;
tslassigning := 1;
node := _node.FirstChild();
while ifObj(node) do
begin
node_name := node.GetName();
arr := sorted_child_[node_name];
if ifarray(arr) then
begin
pf := arr[1];
obj := pf.Do();
obj.InitNode(node);
end
node := node.NextElement();
end
tslassigning := tslassigning_backup;
end;
function OpenXmlCompositeElement.Deserialize();override;
begin
if not ifObj({self.}XmlNode) then return;
attrs := {self.}XmlNode.Attributes();
for k, v in attrs do
begin
pos := pos("xmlns", k);
if pos then
begin
xmlns_[k] := k = "xmlns" ? new OpenXmlAttribute("", "xmlns", v) : new OpenXmlattribute("xmlns", k[pos+6:], v);
continue;
end
pf := attributes_pf_[k];
if ifnil(pf) then continue;
pf.Do(v);
end
child_elements := container_.GetElements();
for k, v in child_elements do
v.Deserialize();
end;
function OpenXmlCompositeElement.Serialize();override;
begin
if {self.}DeleteSelf() then return;
// xmlns
for k, v in xmlns_ do
{self.}GetNode().SetAttribute(v.ElementName, v.Value);
// Attributes
for k, v in attributes_ do
if not ifnil(v.Value) then
{self.}GetNode().SetAttribute(v.ElementName, v.Value);
else if {self.}XmlNode then
{self.}XmlNode.DeleteAttribute(v.ElementName);
// Children
child_elements := container_.GetElements();
for k, v in child_elements do
v.Serialize();
end;
function OpenXmlCompositeElement.Marshal(): tableArray;override;
begin
child_elements := container_.GetElements(false);
child_arr := array();
for k, v in child_elements do
begin
arr := array("type": "element", "name": v.ElementName, "attributes": array());
if v is Class(OpenXmlTextElement) or v is Class(OpenXmlCompositeElement) then
begin
marshal := v.Marshal();
if length(marshal["children"]) = 0 and length(marshal["attributes"]) = 0 then continue;
arr["children"] := marshal["children"];
arr["attributes"] union= marshal["attributes"];
child_arr[length(child_arr)] := arr;
end
else if v is Class(OpenXmlSimpleType) then
begin
marshal := v.Marshal();
if length(marshal) > 0 then child_arr[length(child_arr)] := arr;
end
else begin
echo "error = marshal.\n";
end
end
tmp_arr := array("type": "element", "name": {self.}ElementName, "children": child_arr, "attributes": array());
for k,v in attributes_ do
begin
if not ifnil(v.Value) then tmp_arr["attributes"][v.ElementName] := v.Value;
end
return tmp_arr;
end;
function OpenXmlCompositeElement.GetOrCreateNode(_obj: tslobj);
begin
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
nearest_node := nil;
child_elements := container_.GetElements(false);
for k, v in child_elements do
begin
if ifObj(v.XmlNode) then nearest_node := v.XmlNode;
if v = _obj then
begin
if ifObj(v.XmlNode) then
return v.XmlNode;
if ifnil(nearest_node) then
return {self.}XmlNode.InsertFirstChild("element", _obj.ElementName);
else
return {self.}XmlNode.InsertAfterChild(nearest_node, "element", _obj.ElementName);
end
end
raise "No child.";
end;
function OpenXmlCompositeElement.AppendChild(_element: OpenXmlElement);
begin
container_.Append(_element);
end;
function OpenXmlCompositeElement.InsertAfter(_element: OpenXmlElement; _pos_element: OpenXmlElement);
begin
container_.InsertAfter(_element, _pos_element);
end;
function OpenXmlCompositeElement.RemoveChild(_element: OpenXmlElement);
begin
_element.Removed := true;
end;
function OpenXmlCompositeElement.RemoveAttribute(_attr: OpenXmlAttribute);
begin
_attr.Value := nil;
end;

View File

@ -5,23 +5,18 @@ public
function Init();virtual; function Init();virtual;
function InitNode(_node: XmlNode);virtual; function InitNode(_node: XmlNode);virtual;
function Copy(_obj: tslobj);virtual; function Copy(_obj: tslobj);virtual;
function Deserialize();virtual;
function Serialize();virtual;
function Marshal(): tableArray;virtual;
function Deserialize(); function Attribute(_attr: string; _ns: string): OpenXmlAttribute;
function Serialize();
function Marshal(): tableArray;
function GetOrCreateNode(_obj: tslobj);
function GetNode(): XmlNode;
function Elements(): array of tslobj;
function Attributes(): array of OpenXmlAttribute; function Attributes(): array of OpenXmlAttribute;
function Elements(): array of tslobj;
function Xmlns(_prefix: string): string; function Xmlns(_prefix: string): string;
public protected
function AppendChild(_obj: tslobj); // OpenXmlElement or OpenXmlPcdata function GetNode(): XmlNode;
function InsertAfter(_obj: tslobj; _pos_obj: tslobj); function DeleteSelf(): boolean;
// function InsertBefore(_pos_obj: tslobj; _obj: tslobj);
// function PrependChild(_obj: tslobj);
// function RemoveChild(_obj: tslobj);
// function SetAttribute(_obj: OpenXmlAttribute);
public public
LocalName: string; // 本地名称 LocalName: string; // 本地名称
@ -29,14 +24,14 @@ public
Prefix: string; // 前缀 Prefix: string; // 前缀
XmlNode: XmlNode; // XMLNode节点 XmlNode: XmlNode; // XMLNode节点
ElementName: string; ElementName: string;
Removed: boolean; // 节点删除标记最后Serialize时候统一删除
protected protected
attributes_: array of OpenXmlAttribute; // 属性 attributes_: array of OpenXmlAttribute; // 属性
attributes_pf_: tableArray; attributes_pf_: tableArray;
// child_elements_: array of tslobj; // 子节点元素 xmlns_: tableArray;
sorted_child_: tableArray; sorted_child_: tableArray;
container_: TSOfficeContainer; container_: TSOfficeContainer;
xmlns_: tableArray;
end; end;
function OpenXmlElement.Create(_node: XmlNode);overload; function OpenXmlElement.Create(_node: XmlNode);overload;
@ -46,7 +41,6 @@ begin
if pos then {self.}Create(nil, node_name[:pos-1], node_name[pos+1:]); if pos then {self.}Create(nil, node_name[:pos-1], node_name[pos+1:]);
else {self.}Create(nil, nil, node_name); else {self.}Create(nil, nil, node_name);
{self.}InitNode(_node); {self.}InitNode(_node);
xmlns_ := array();
end; end;
function OpenXmlElement.Create(_parent: tslobj; _prefix: string; _local_name: string);overload; function OpenXmlElement.Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
@ -56,122 +50,31 @@ begin
{self.}LocalName := _local_name; {self.}LocalName := _local_name;
{self.}XmlNode := nil; {self.}XmlNode := nil;
{self.}ElementName := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%s", {self.}Prefix, {self.}LocalName) : {self.}LocalName; {self.}ElementName := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%s", {self.}Prefix, {self.}LocalName) : {self.}LocalName;
{self.}Removed := false;
{self.}Init(); {self.}Init();
xmlns_ := array(); xmlns_ := array();
end; end;
function OpenXmlElement.InitNode(_node: XmlNode);virtual; function OpenXmlElement.Attribute(_attr: string; _ns: string): OpenXmlAttribute;
begin begin
{self.}XmlNode := _node; if ifnil(_ns) then
if not _node then return; attr_name := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%%s", {self.}Prefix, _attr) : _attr;
tslassigning_backup := tslassigning; else
tslassigning := 1; attr_name := format("%s:%s", _ns, _attr);
node := _node.FirstChild(); return attributes_[attr_name];
while ifObj(node) do
begin
node_name := node.GetName();
arr := sorted_child_[node_name];
if ifarray(arr) then
begin
pf := arr[1];
obj := pf.Do();
obj.InitNode(node);
end
node := node.NextElement();
end
tslassigning := tslassigning_backup;
end; end;
function OpenXmlElement.GetOrCreateNode(_obj: tslobj); function OpenXmlElement.Attributes(): array of OpenXmlAttribute;
begin begin
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self); attrs := array();
nearest_node := nil;
child_elements := container_.Get();
for k, v in child_elements do
begin
if ifObj(v.XmlNode) then nearest_node := v.XmlNode;
if v = _obj then
begin
if ifObj(v.XmlNode) then return v.XmlNode;
if ifnil(nearest_node) then return {self.}XmlNode.InsertFirstChild("element", _obj.ElementName);
else return {self.}XmlNode.InsertAfterChild(nearest_node, "element", _obj.ElementName);
end
end
raise "No child.";
end;
function OpenXmlElement.GetNode(): XmlNode;
begin
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
return {self.}XmlNode;
end;
function OpenXmlElement.Deserialize();
begin
if not ifObj({self.}XmlNode) then return;
attrs := {self.}XmlNode.Attributes();
for k, v in attrs do
begin
pos := pos("xmlns", k);
if pos then
begin
xmlns_[k] := k = "xmlns" ? new OpenXmlAttribute("", "xmlns", v) : new OpenXmlattribute("xmlns", k[pos+6:], v);
continue;
end
pf := attributes_pf_[k];
if ifnil(pf) then continue;
pf.Do(v);
end
child_elements := container_.Get();
for k, v in child_elements do
v.Deserialize();
end;
function OpenXmlElement.Serialize();
begin
// xmlns
for k, v in xmlns_ do
{self.}GetNode().SetAttribute(v.ElementName, v.Value);
// Attributes
for k, v in attributes_ do for k, v in attributes_ do
if not ifnil(v.Value) then {self.}GetNode().SetAttribute(v.ElementName, v.Value); attrs[length(attrs)] := v;
// Children return attrs;
child_elements := container_.Get();
for k, v in child_elements do
v.Serialize();
end; end;
function OpenXmlElement.Marshal(): tableArray; function OpenXmlElement.Elements(): array of tslobj;
begin begin
child_elements := container_.Get(); return container_.GetElements(false);
child_arr := array();
for k, v in child_elements do
begin
arr := array("type": "element", "name": v.ElementName, "attributes": array());
if v is Class(OpenXmlPcdata) or v is Class(OpenXmlElement) then
begin
marshal := v.Marshal();
if length(marshal["children"]) = 0 and length(marshal["attributes"]) = 0 then continue;
arr["children"] := marshal["children"];
arr["attributes"] union= marshal["attributes"];
child_arr[length(child_arr)] := arr;
end
else if v is Class(OpenXmlEmpty) then
begin
marshal := v.Marshal();
if length(marshal) > 0 then child_arr[length(child_arr)] := arr;
end
else begin
echo "error = marshal.\n";
end
end
tmp_arr := array("type": "element", "name": {self.}ElementName, "children": child_arr, "attributes": array());
for k,v in attributes_ do
begin
if not ifnil(v.Value) then tmp_arr["attributes"][v.ElementName] := v.Value;
end
return tmp_arr;
end; end;
function OpenXmlElement.Xmlns(_prefix: string): string; function OpenXmlElement.Xmlns(_prefix: string): string;
@ -185,17 +88,20 @@ begin
return xmlns_[k]; return xmlns_[k];
end; end;
function OpenXmlElement.Elements(): array of tslobj; function OpenXmlElement.GetNode(): XmlNode;
begin begin
return container_.Get(); if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
return {self.}XmlNode;
end; end;
function OpenXmlElement.InsertAfter(_obj: tslobj; _pos_obj: tslobj); function OpenXmlElement.DeleteSelf(): boolean;
begin begin
container_.InsertAfter(_obj, _pos_obj); if {self.}Removed then
begin
if {self.}XmlNode then
{self.}Parent.XmlNode.DeleteChild({self.}XmlNode);
return true;
end
return false;
end; end;
function OpenXmlElement.AppendChild(_obj: tslobj);
begin
container_.Append(_obj);
end;

View File

@ -1,119 +0,0 @@
type OpenXmlEmpty = class
public
function Create(_node: XmlNode);overload;
function Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
function InitNode(_node: XmlNode);
function Copy(_obj: tslobj);virtual;
function GetNode(): XmlNode;
function Marshal(): tableArray;
function BoolValue(): boolean;
function Deserialize();
function Serialize();
public
// attributes property
XmlAttrVal;
property Val read ReadXmlAttrVal write WriteXmlAttrVal;
function ReadXmlAttrVal();
function WriteXmlAttrVal(_value: any);
public
LocalName: string; // 本地名称
[weakref]Parent: tslobj; // 父类
Prefix: string; // 前缀
XmlNode: XmlNode; // XMLNode节点
ElementName: string;
Value: any;
end;
function OpenXmlEmpty.Create(_node: XmlNode);overload;
begin
node_name := _node.GetName();
pos := Pos(":", node_name);
if pos then {self.}Create(nil, node_name[:pos-1], node_name[pos+1:]);
else {self.}Create(nil, nil, node_name);
{self.}InitNode(_node);
end;
function OpenXmlEmpty.Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
begin
{self.}Parent := _parent;
{self.}Prefix := _prefix;
{self.}LocalName := _local_name;
{self.}XmlNode := nil;
{self.}ElementName := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%s", {self.}Prefix, {self.}LocalName) : {self.}LocalName;
end;
function OpenXmlEmpty.InitNode(_node: XmlNode);
begin
{self.}XmlNode := ifObj(_node) ? _node : nil;
end;
function OpenXmlEmpty.Copy(_obj: tslobj);
begin
if not ifnil(_obj.Value) then {self.}Value := _obj.Value;
if not ifnil(_obj.XmlAttrVal.Value) then {self.}Val := _obj.XmlAttrVal.Value;
end;
function OpenXmlEmpty.Marshal(): tableArray;
begin
if not {self.}Value then return array();
attr := array();
if not inil({self.}XmlAttrVal.Value) then attr[{self.}XmlAttrVal.ElementName] := {self.}XmlAttrVal.Value;
arr := array("type": "element", "name": name_, "attributes": attr, "children": array());
return arr;
end;
function OpenXmlEmpty.Deserialize();
begin
if ifObj({self.}XmlNode) then
begin
attrs := {self.}XmlNode.Attributes();
if length(attrs) > 1 then raise "OpenXmlEmpty.Deserialize error.\n";
{self.}Val := nil;
for k,v in attrs do
{self.}Val := v;
{self.}Value := ifnil({self.}Val) ? true : {self.}Val = "0" ? false : true;
end
else begin
{self.}Value := nil;
{self.}XmlAttrVal := nil;
end
end;
function OpenXmlEmpty.GetNode(): XmlNode;
begin
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
return {self.}XmlNode;
end;
function OpenXmlEmpty.Serialize();
begin
if not ifObj({self.}XmlNode) then
begin
if not ifnil({self.}XmlAttrVal.Value) then {self.}GetNode().SetAttribute({self.}XmlAttrVal.ElementName, {self.}XmlAttrVal.Value);
end
else begin
if not ifnil({self.}XmlAttrVal.Value) then {self.}XmlNode.SetAttribute({self.}XmlAttrVal.ElementName, {self.}XmlAttrVal.Value);
else if not {self.}Value and ifObj({self.}Parent.XmlNode) then{self.}Parent.XmlNode.DeleteChild({self.}XmlNode);
end
end;
function OpenXmlEmpty.BoolValue(): boolean;
begin
return ifnil({self.}Val) ? {self.}Value ? true : false : {self.}Val;
end;
function OpenXmlEmpty.ReadXmlAttrVal();
begin
return {self.}XmlAttrVal.Value = "0" ? false : {self.}XmlAttrVal.Value = "1" ? true : {self.}XmlAttrVal.Value;
end;
function OpenXmlEmpty.WriteXmlAttrVal(_value: any);
begin
if ifnil({self.}XmlAttrVal) then
{self.}XmlAttrVal := new OpenXmlAttribute({self.}Prefix, "val", nil);
{self.}XmlAttrVal.Value := _value;
end;

View File

@ -1,87 +0,0 @@
type OpenXmlPcdata = class
public
function Create(_node: XmlNode);overload;
function Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
function Init();virtual;
function InitNode(_node: XmlNode);virtual;
function Copy(_obj: tslobj);virtual;
function GetNode(): XmlNode;
function Marshal(): tableArray;
function Deserialize();
function Serialize();
public
LocalName: string; // 本地名称
[weakref]Parent: tslobj; // 父类
Prefix: string; // 前缀
XmlNode: XmlNode; // XMLNode节点
ElementName: string;
Text: string; // 元素的文本内容
protected
attributes_: array of OpenXmlAttribute; // 属性
attributes_pf_: tableArray;
end;
function OpenXmlPcdata.Create(_node: XmlNode);overload;
begin
node_name := _node.GetName();
pos := Pos(":", node_name);
if pos then {self.}Create(nil, node_name[:pos-1], node_name[pos+1:]);
else {self.}Create(nil, nil, node_name);
{self.}InitNode(_node);
end;
function OpenXmlPcdata.Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
begin
{self.}Parent := _parent;
{self.}Prefix := _prefix;
{self.}LocalName := _local_name;
{self.}XmlNode := nil;
{self.}Text := nil;
{self.}ElementName := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%s", {self.}Prefix, {self.}LocalName) : {self.}LocalName;
{self.}Init();
end;
function OpenXmlPcdata.InitNode(_node: XmlNode);virtual;
begin
{self.}XmlNode := ifObj(_node) ? _node : nil;
end;
function OpenXmlPcdata.Marshal(): tableArray;
begin
arr := array("type": "element", "name": name_, "attributes": array(), "children": array());
for k,v in attributes_ do
if not ifnil(v.Value) then arr["attributes"][v.ElementName] := v.Value;
if not ifnil({self.}Text) then arr["children"] := array(("type": "pcdata", "value": {self.}Text));
return arr;
end;
function OpenXmlPcdata.Deserialize();
begin
if not ifObj({self.}XmlNode) then return;
{self.}Text := {self.}XmlNode.GetText();
attrs := {self.}XmlNode.Attributes();
for k, v in attrs do
begin
pf := attributes_pf_[k];
if ifnil(pf) then continue;
pf.Do(v);
end
end;
function OpenXmlPcdata.GetNode(): XmlNode;
begin
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
return {self.}XmlNode;
end;
function OpenXmlPcdata.Serialize();
begin
if not ifnil({self.}Text) then {self.}GetNode().SetValue({self.}Text);
for k,v in attributes_ do
if not ifnil(v.Value) then {self.}GetNode().SetAttribute(v.ElementName, v.Value);
end;

View File

@ -0,0 +1,104 @@
type OpenXmlSimpleType = class(OpenXmlElement)
public
function Init();override;
function InitNode(_node: XmlNode);override;
function Copy(_obj: tslobj);override;
function Deserialize();override;
function Serialize();override;
function Marshal(): tableArray;override;
public
// attributes property
XmlAttrVal;
property Val read ReadXmlAttrVal write WriteXmlAttrVal;
function ReadXmlAttrVal();
function WriteXmlAttrVal(_value: any);
// 动态获取IsApplied
// IsApplied: boolean; // 是否已经应用和val值有关
property IsApplied read ReadIsApplied;
function ReadIsApplied();
public
Enable: boolean; // 是否启用 <b /> 没有属性需要通过val写属性
end;
function OpenXmlSimpleType.Init();override;
begin
{self.}Enable := false;
end;
function OpenXmlSimpleType.InitNode(_node: XmlNode);override;
begin
{self.}XmlNode := ifObj(_node) ? _node : nil;
end;
function OpenXmlSimpleType.Copy(_obj: tslobj);override;
begin
if not ifnil(_obj.Enable) then {self.}Enable := _obj.Enable;
if not ifnil(_obj.XmlAttrVal.Value) then {self.}Val := _obj.XmlAttrVal.Value;
end;
function OpenXmlSimpleType.Marshal(): tableArray;override;
begin
if not {self.}Enable then return array();
attr := array();
if not inil({self.}XmlAttrVal.Value) then attr[{self.}XmlAttrVal.ElementName] := {self.}XmlAttrVal.Value;
arr := array("type": "element", "name": name_, "attributes": attr, "children": array());
return arr;
end;
function OpenXmlSimpleType.Deserialize();override;
begin
if ifObj({self.}XmlNode) then
begin
attrs := {self.}XmlNode.Attributes();
if length(attrs) > 1 then
begin
echo "OpenXmlSimpleType.Deserialize error.\n";
return;
end
{self.}Val := nil;
for k,v in attrs do
{self.}Val := v;
{self.}Enable := true;
end
else begin
{self.}Enable := false;
{self.}XmlAttrVal := nil;
end
end;
function OpenXmlSimpleType.Serialize();override;
begin
if not ifObj({self.}XmlNode) then
begin
if not ifnil({self.}XmlAttrVal.Value) then {self.}GetNode().SetAttribute({self.}XmlAttrVal.ElementName, {self.}XmlAttrVal.Value);
end
else begin
if not {self.}Enable or {self.}Removed then
{self.}Parent.XmlNode.DeleteChild({self.}XmlNode);
else if not ifnil({self.}XmlAttrVal.Value) then
{self.}XmlNode.SetAttribute({self.}XmlAttrVal.ElementName, {self.}XmlAttrVal.Value);
end
end;
function OpenXmlSimpleType.ReadXmlAttrVal();
begin
return {self.}XmlAttrVal.Value;
end;
function OpenXmlSimpleType.WriteXmlAttrVal(_value: any);
begin
if ifnil({self.}XmlAttrVal) then
{self.}XmlAttrVal := new OpenXmlAttribute({self.}Prefix, "val", nil);
{self.}XmlAttrVal.Value := _value;
{self.}Enable := true;
end;
function OpenXmlSimpleType.ReadIsApplied();
begin
if {self.}Enable then
return {self.}XmlAttrVal.Value = "0" or {self.}XmlAttrVal.Value = "false" ? false : true;
return false;
end;

View File

@ -0,0 +1,59 @@
type OpenXmlTextElement = class(OpenXmlElement)
public
function InitNode(_node: XmlNode);override;
function Copy(_obj: tslobj);virtual;
function Deserialize();override;
function Serialize();override;
function Marshal(): tableArray;override;
public
Text: string; // 元素的文本内容
end;
function OpenXmlTextElement.InitNode(_node: XmlNode);override;
begin
{self.}XmlNode := ifObj(_node) ? _node : nil;
end;
function OpenXmlTextElement.Copy(_obj: tslobj);virtual;
begin
if not ifnil(_obj.Text) then {self.}Text := _obj.Text;
end;
function OpenXmlTextElement.Marshal(): tableArray;override;
begin
arr := array("type": "element", "name": name_, "attributes": array(), "children": array());
for k,v in attributes_ do
if not ifnil(v.Value) then arr["attributes"][v.ElementName] := v.Value;
if not ifnil({self.}Text) then arr["children"] := array(("type": "pcdata", "value": {self.}Text));
return arr;
end;
function OpenXmlTextElement.Deserialize();override;
begin
if not ifObj({self.}XmlNode) then return;
{self.}Text := {self.}XmlNode.GetText();
attrs := {self.}XmlNode.Attributes();
for k, v in attrs do
begin
pos := pos("xmlns", k);
if pos then
begin
xmlns_[k] := k = "xmlns" ? new OpenXmlAttribute("", "xmlns", v) : new OpenXmlattribute("xmlns", k[pos+6:], v);
continue;
end
pf := attributes_pf_[k];
if ifnil(pf) then continue;
pf.Do(v);
end
end;
function OpenXmlTextElement.Serialize();override;
begin
if {self.}DeleteSelf() then return;
if not ifnil({self.}Text) then {self.}GetNode().SetValue({self.}Text);
for k,v in attributes_ do
if not ifnil(v.Value) then {self.}GetNode().SetAttribute(v.ElementName, v.Value);
end;

View File

@ -6,8 +6,8 @@ public
function Append(data: any): boolean; function Append(data: any): boolean;
function Insert(data: any): boolean; function Insert(data: any): boolean;
function InsertAfter(data: any; pos_data: any); function InsertAfter(data: any; pos_data: any);
function Get(name: string; index: integer);overload; function Get(name: string; index: integer);
function Get(): array of tslobj;overload; function GetElements(include_removed: boolean): array of tslobj;
private private
function GetBucketObj(str: string): LinkList; function GetBucketObj(str: string): LinkList;
@ -24,6 +24,7 @@ public
function Create(name: string); function Create(name: string);
function Set(data: any); function Set(data: any);
function Add(data: any); function Add(data: any);
function Delete(data: any): boolean;
function InsertAfter(data: any; pos_data: any): boolean; function InsertAfter(data: any; pos_data: any): boolean;
function Name(): string; function Name(): string;
function Size(): integer; function Size(): integer;
@ -58,7 +59,7 @@ function TSOfficeContainer.Set(data: any): boolean;
begin begin
obj := {self.}GetBucketObj(data.ElementName); obj := {self.}GetBucketObj(data.ElementName);
if ifnil(obj) then return false; if ifnil(obj) then return false;
obj.Set(data); obj.Add(data);
return true; return true;
end; end;
@ -125,7 +126,7 @@ begin
end end
end; end;
function TSOfficeContainer.Get(): array of tslobj;overload; function TSOfficeContainer.GetElements(include_removed: boolean = true): array of tslobj;
begin begin
arr := array(); arr := array();
for i:=0 to current_index_ do for i:=0 to current_index_ do
@ -136,7 +137,8 @@ begin
node := obj.First(); node := obj.First();
while ifObj(node) do while ifObj(node) do
begin begin
arr[length(arr)] := node.data; if include_removed or not node.data.Removed then
arr[length(arr)] := node.data;
node := node.next; node := node.next;
end end
end end
@ -144,7 +146,7 @@ begin
return arr; return arr;
end; end;
function TSOfficeContainer.Get(name: string; index: integer);overload; function TSOfficeContainer.Get(name: string; index: integer);
begin begin
ind := child_[name]; ind := child_[name];
arr := array(); arr := array();
@ -239,6 +241,25 @@ begin
return false; return false;
end; end;
function LinkList.Delete(data: any): boolean;
begin
node := {self.}First();
prev := nil;
while ifObj(node) do
begin
if node.data = data then
begin
if ifnil(prev) then head_.next := node.next;
else if tail_ = node then tail_ := prev;
else prev.next := node.next;
return true;
end
prev := node;
node := node.next;
end
return false;
end;
function LinkList.Name(): string; function LinkList.Name(): string;
begin begin
return name_; return name_;

29
迁移指南.md Normal file
View File

@ -0,0 +1,29 @@
# 迁移指南
## v2.0.0
该版本对基类做了调整
1. `OpenXmlElement` -> `OpenXmlElement`,类名不变,功能变更为基类
2. `OpenXmlPcdata` -> `OpenXmlTextElement`,类名变更,继承基类,使用方式不变
3. `OpenXmlEmpty` -> `OpenXmlSimpleType`,类名变更,继承基类,设计与使用方法需要调整
```pascal
// 弃用
rpr := new RPr();
rpr.B := true; // 启用BB是Empty元素对应xml <b/>
rpr.B.Val := 1; // <b w:val="1" />
// 读
echo rpr.B; // 判断是否启用了B
// 迁移
rpr := new RPr();
rpr.B.Enable := true; // 启用B不设置属性时候对应xml <b />
rpr.B.Val := "0"; // 会自动设置Enable为1对应xml <b w:val="0" />
// 只读
echo rpr.B.IsApplied; // 只读属性判断是否启用了B
```
4. 新增`OpenXmlCompositeElement`复杂类型的节点类,提供各种操作的方法,大部分有子节点的类均继承于该类。如`RPr`会有`B I Sz`等子节点元素,而`B I`这类的元素是`<x />`的`OpenXmlSimpleType`类型的
影响范围:列表暂无;具体可见对应类的`// simple_type property`下的属性