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
[迁移指南](./迁移指南.md)
## 概述
将 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 单元
@ -209,7 +215,7 @@ style := styles_adapter.GetStyleByStyleId("a6");
echo style.Name; // 输出的是"页脚 字符"
```
### 补充
### 其他设计的补充
```xml
<m:r>
@ -232,3 +238,8 @@ echo style.Name; // 输出的是"页脚 字符"
由于设计取对象时候不包含前缀,那么同时存在多个`rPr`时,如何区分是哪一个?
取`m:rPr`时,可通过`r.RPr`,这是因为它们的前缀都是`m`,所以可直接获取
取`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)
public
function Create(_obj: Shapetype);
function GetObject();
function Convert();
function Create(_obj: Shapetype);
function GetObject();
function Convert();
private
object_: Shapetype;
object_: Shapetype;
end;
type FormulasUnitDecorator = class(Formulas)
public
function Create(_obj: Formulas);
function GetObject();
function Convert();
function Create(_obj: Formulas);
function GetObject();
function Convert();
private
object_: Formulas;
object_: Formulas;
end;
type LockUnitDecorator = class(Lock)
public
function Create(_obj: Lock);
function GetObject();
function Convert();
function Create(_obj: Lock);
function GetObject();
function Convert();
private
object_: Lock;
object_: Lock;
end;
type FUnitDecorator = class(F)
public
function Create(_obj: F);
function GetObject();
function Convert();
function Create(_obj: F);
function GetObject();
function Convert();
private
object_: F;
object_: F;
end;
type StrokeUnitDecorator = class(Stroke)
public
function Create(_obj: Stroke);
function GetObject();
function Convert();
function Create(_obj: Stroke);
function GetObject();
function Convert();
private
object_: Stroke;
object_: Stroke;
end;
type PathUnitDecorator = class(Path)
public
function Create(_obj: Path);
function GetObject();
function Convert();
function Create(_obj: Path);
function GetObject();
function Convert();
private
object_: Path;
object_: Path;
end;
type TextpathUnitDecorator = class(Textpath)
public
function Create(_obj: Textpath);
function GetObject();
function Convert();
function Create(_obj: Textpath);
function GetObject();
function Convert();
private
object_: Textpath;
object_: Textpath;
end;
type HandlesUnitDecorator = class(Handles)
public
function Create(_obj: Handles);
function GetObject();
function Convert();
function Create(_obj: Handles);
function GetObject();
function Convert();
private
object_: Handles;
object_: Handles;
end;
type HUnitDecorator = class(H)
public
function Create(_obj: H);
function GetObject();
function Convert();
function Create(_obj: H);
function GetObject();
function Convert();
private
object_: H;
object_: H;
end;
type ShapeUnitDecorator = class(Shape)
public
function Create(_obj: Shape);
function GetObject();
function Convert();
function Create(_obj: Shape);
function GetObject();
function Convert();
private
object_: Shape;
object_: Shape;
end;
type WrapUnitDecorator = class(Wrap)
public
function Create(_obj: Wrap);
function GetObject();
function Convert();
function Create(_obj: Wrap);
function GetObject();
function Convert();
private
object_: Wrap;
object_: Wrap;
end;
type FillUnitDecorator = class(Fill)
public
function Create(_obj: Fill);
function GetObject();
function Convert();
function Create(_obj: Fill);
function GetObject();
function Convert();
private
object_: Fill;
object_: Fill;
end;
type ImagedataUnitDecorator = class(Imagedata)
public
function Create(_obj: Imagedata);
function GetObject();
function Convert();
function Create(_obj: Imagedata);
function GetObject();
function Convert();
private
object_: Imagedata;
object_: Imagedata;
end;
type TextboxUnitDecorator = class(Textbox)
public
function Create(_obj: Textbox);
function GetObject();
function Convert();
function Create(_obj: Textbox);
function GetObject();
function Convert();
private
object_: Textbox;
object_: Textbox;
end;
type OLEObjectUnitDecorator = class(OLEObject)
public
function Create(_obj: OLEObject);
function GetObject();
function Convert();
function Create(_obj: OLEObject);
function GetObject();
function Convert();
private
object_: OLEObject;
object_: OLEObject;
end;
implementation
function ShapetypeUnitDecorator.Create(_obj: Shapetype);
begin
class(Shapetype).Create();
object_ := _obj;
{self.}Convert();
class(Shapetype).Create();
object_ := _obj;
{self.}Convert();
end;
function ShapetypeUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function ShapetypeUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrAnchorId) then
{self.}AnchorId := object_.XmlAttrAnchorId.Value;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrCoordsize) then
{self.}Coordsize := object_.XmlAttrCoordsize.Value;
if not ifnil(object_.XmlAttrSpt) then
{self.}Spt := object_.XmlAttrSpt.Value;
if not ifnil(object_.XmlAttrAdj) then
{self.}Adj := object_.XmlAttrAdj.Value;
if not ifnil(object_.XmlAttrPreferrelative) then
{self.}Preferrelative := object_.XmlAttrPreferrelative.Value;
if not ifnil(object_.XmlAttrPath) then
{self.}Path := object_.XmlAttrPath.Value;
if not ifnil(object_.XmlAttrFilled) then
{self.}Filled := object_.XmlAttrFilled.Value;
if not ifnil(object_.XmlAttrStroked) then
{self.}Stroked := object_.XmlAttrStroked.Value;
if not ifnil(object_.XmlChildStroke) then
{self.}XmlChildStroke := new StrokeUnitDecorator(object_.XmlChildStroke);
if not ifnil(object_.XmlChildFormulas) then
{self.}XmlChildFormulas := new formulasUnitDecorator(object_.XmlChildFormulas);
if not ifnil(object_.XmlChildPath) then
{self.}XmlChildPath := new PathUnitDecorator(object_.XmlChildPath);
if not ifnil(object_.XmlChildTextpath) then
{self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath);
if not ifnil(object_.XmlChildHandles) then
{self.}XmlChildHandles := new HandlesUnitDecorator(object_.XmlChildHandles);
if not ifnil(object_.XmlChildLock) then
{self.}XmlChildLock := new LockUnitDecorator(object_.XmlChildLock);
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrAnchorId) then
{self.}AnchorId := object_.XmlAttrAnchorId.Value;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrCoordsize) then
{self.}Coordsize := object_.XmlAttrCoordsize.Value;
if not ifnil(object_.XmlAttrSpt) then
{self.}Spt := object_.XmlAttrSpt.Value;
if not ifnil(object_.XmlAttrAdj) then
{self.}Adj := object_.XmlAttrAdj.Value;
if not ifnil(object_.XmlAttrPreferrelative) then
{self.}Preferrelative := object_.XmlAttrPreferrelative.Value;
if not ifnil(object_.XmlAttrPath) then
{self.}Path := object_.XmlAttrPath.Value;
if not ifnil(object_.XmlAttrFilled) then
{self.}Filled := object_.XmlAttrFilled.Value;
if not ifnil(object_.XmlAttrStroked) then
{self.}Stroked := object_.XmlAttrStroked.Value;
if not ifnil(object_.XmlChildStroke) then
{self.}XmlChildStroke := new StrokeUnitDecorator(object_.XmlChildStroke);
if not ifnil(object_.XmlChildFormulas) then
{self.}XmlChildFormulas := new formulasUnitDecorator(object_.XmlChildFormulas);
if not ifnil(object_.XmlChildPath) then
{self.}XmlChildPath := new PathUnitDecorator(object_.XmlChildPath);
if not ifnil(object_.XmlChildTextpath) then
{self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath);
if not ifnil(object_.XmlChildHandles) then
{self.}XmlChildHandles := new HandlesUnitDecorator(object_.XmlChildHandles);
if not ifnil(object_.XmlChildLock) then
{self.}XmlChildLock := new LockUnitDecorator(object_.XmlChildLock);
tslassigning := tslassigning_backup;
end;
function FormulasUnitDecorator.Create(_obj: Formulas);
begin
class(Formulas).Create();
object_ := _obj;
{self.}Convert();
class(Formulas).Create();
object_ := _obj;
{self.}Convert();
end;
function FormulasUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function FormulasUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
elems := object_.Fs();
for _,elem in elems do
{self.}AppendChild(new ShapetypeUnitDecorator(elem));
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
elems := object_.Fs();
for _,elem in elems do
{self.}AppendChild(new ShapetypeUnitDecorator(elem));
tslassigning := tslassigning_backup;
end;
function LockUnitDecorator.Create(_obj: Lock);
begin
class(Lock).Create();
object_ := _obj;
{self.}Convert();
class(Lock).Create();
object_ := _obj;
{self.}Convert();
end;
function LockUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function LockUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrExt) then
{self.}Ext := object_.XmlAttrExt.Value;
if not ifnil(object_.XmlAttrAspectration) then
{self.}Aspectration := object_.XmlAttrAspectration.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrExt) then
{self.}Ext := object_.XmlAttrExt.Value;
if not ifnil(object_.XmlAttrAspectration) then
{self.}Aspectration := object_.XmlAttrAspectration.Value;
tslassigning := tslassigning_backup;
end;
function FUnitDecorator.Create(_obj: F);
begin
class(F).Create();
object_ := _obj;
{self.}Convert();
class(F).Create();
object_ := _obj;
{self.}Convert();
end;
function FUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function FUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrEqn) then
{self.}Eqn := object_.XmlAttrEqn.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrEqn) then
{self.}Eqn := object_.XmlAttrEqn.Value;
tslassigning := tslassigning_backup;
end;
function StrokeUnitDecorator.Create(_obj: Stroke);
begin
class(Stroke).Create();
object_ := _obj;
{self.}Convert();
class(Stroke).Create();
object_ := _obj;
{self.}Convert();
end;
function StrokeUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function StrokeUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrJoinstyle) then
{self.}Joinstyle := object_.XmlAttrJoinstyle.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrJoinstyle) then
{self.}Joinstyle := object_.XmlAttrJoinstyle.Value;
tslassigning := tslassigning_backup;
end;
function PathUnitDecorator.Create(_obj: Path);
begin
class(Path).Create();
object_ := _obj;
{self.}Convert();
class(Path).Create();
object_ := _obj;
{self.}Convert();
end;
function PathUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function PathUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrExtrusionok) then
{self.}Extrusionok := object_.XmlAttrExtrusionok.Value;
if not ifnil(object_.XmlAttrTextpathok) then
{self.}Textpathok := object_.XmlAttrTextpathok.Value;
if not ifnil(object_.XmlAttrGradientshapeok) then
{self.}Gradientshapeok := object_.XmlAttrGradientshapeok.Value;
if not ifnil(object_.XmlAttrConnecttype) then
{self.}Connecttype := object_.XmlAttrConnecttype.Value;
if not ifnil(object_.XmlAttrConnectlocs) then
{self.}Connectlocs := object_.XmlAttrConnectlocs.Value;
if not ifnil(object_.XmlAttrConnectangles) then
{self.}Connectangles := object_.XmlAttrConnectangles.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrExtrusionok) then
{self.}Extrusionok := object_.XmlAttrExtrusionok.Value;
if not ifnil(object_.XmlAttrTextpathok) then
{self.}Textpathok := object_.XmlAttrTextpathok.Value;
if not ifnil(object_.XmlAttrGradientshapeok) then
{self.}Gradientshapeok := object_.XmlAttrGradientshapeok.Value;
if not ifnil(object_.XmlAttrConnecttype) then
{self.}Connecttype := object_.XmlAttrConnecttype.Value;
if not ifnil(object_.XmlAttrConnectlocs) then
{self.}Connectlocs := object_.XmlAttrConnectlocs.Value;
if not ifnil(object_.XmlAttrConnectangles) then
{self.}Connectangles := object_.XmlAttrConnectangles.Value;
tslassigning := tslassigning_backup;
end;
function TextpathUnitDecorator.Create(_obj: Textpath);
begin
class(Textpath).Create();
object_ := _obj;
{self.}Convert();
class(Textpath).Create();
object_ := _obj;
{self.}Convert();
end;
function TextpathUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function TextpathUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttr_On) then
{self.}_On := object_.XmlAttr_On.Value;
if not ifnil(object_.XmlAttrFitshape) then
{self.}Fitshape := object_.XmlAttrFitshape.Value;
if not ifnil(object_.XmlAttrStyle) then
{self.}Style := object_.XmlAttrStyle.Value;
if not ifnil(object_.XmlAttrString) then
{self.}String := object_.XmlAttrString.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttr_On) then
{self.}_On := object_.XmlAttr_On.Value;
if not ifnil(object_.XmlAttrFitshape) then
{self.}Fitshape := object_.XmlAttrFitshape.Value;
if not ifnil(object_.XmlAttrStyle) then
{self.}Style := object_.XmlAttrStyle.Value;
if not ifnil(object_.XmlAttrString) then
{self.}String := object_.XmlAttrString.Value;
tslassigning := tslassigning_backup;
end;
function HandlesUnitDecorator.Create(_obj: Handles);
begin
class(Handles).Create();
object_ := _obj;
{self.}Convert();
class(Handles).Create();
object_ := _obj;
{self.}Convert();
end;
function HandlesUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function HandlesUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlChildH) then
{self.}XmlChildH := new HUnitDecorator(object_.XmlChildH);
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlChildH) then
{self.}XmlChildH := new HUnitDecorator(object_.XmlChildH);
tslassigning := tslassigning_backup;
end;
function HUnitDecorator.Create(_obj: H);
begin
class(H).Create();
object_ := _obj;
{self.}Convert();
class(H).Create();
object_ := _obj;
{self.}Convert();
end;
function HUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function HUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrXrange) then
{self.}Xrange := object_.XmlAttrXrange.Value;
if not ifnil(object_.XmlAttrPosition) then
{self.}Position := object_.XmlAttrPosition.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrXrange) then
{self.}Xrange := object_.XmlAttrXrange.Value;
if not ifnil(object_.XmlAttrPosition) then
{self.}Position := object_.XmlAttrPosition.Value;
tslassigning := tslassigning_backup;
end;
function ShapeUnitDecorator.Create(_obj: Shape);
begin
class(Shape).Create();
object_ := _obj;
{self.}Convert();
class(Shape).Create();
object_ := _obj;
{self.}Convert();
end;
function ShapeUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function ShapeUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrStyle) then
{self.}Style := object_.XmlAttrStyle.Value;
if not ifnil(object_.XmlAttrSpid) then
{self.}Spid := object_.XmlAttrSpid.Value;
if not ifnil(object_.XmlAttrType) then
{self.}Type := object_.XmlAttrType.Value;
if not ifnil(object_.XmlAttrGfxdata) then
{self.}Gfxdata := object_.XmlAttrGfxdata.Value;
if not ifnil(object_.XmlAttrFilled) then
{self.}Filled := object_.XmlAttrFilled.Value;
if not ifnil(object_.XmlAttrAllowincell) then
{self.}Allowincell := object_.XmlAttrAllowincell.Value;
if not ifnil(object_.XmlAttrFillcolor) then
{self.}Fillcolor := object_.XmlAttrFillcolor.Value;
if not ifnil(object_.XmlAttrStroked) then
{self.}Stroked := object_.XmlAttrStroked.Value;
if not ifnil(object_.XmlAttrOle) then
{self.}Ole := object_.XmlAttrOle.Value;
if not ifnil(object_.XmlChildFill) then
{self.}XmlChildFill := new FillUnitDecorator(object_.XmlChildFill);
if not ifnil(object_.XmlChildTextbox) then
{self.}XmlChildTextbox := new TextboxUnitDecorator(object_.XmlChildTextbox);
if not ifnil(object_.XmlChildTextpath) then
{self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath);
if not ifnil(object_.XmlChildImagedata) then
{self.}XmlChildImagedata := new ImagedataUnitDecorator(object_.XmlChildImagedata);
if not ifnil(object_.XmlChildWrap) then
{self.}XmlChildWrap := new WrapUnitDecorator(object_.XmlChildWrap);
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrStyle) then
{self.}Style := object_.XmlAttrStyle.Value;
if not ifnil(object_.XmlAttrSpid) then
{self.}Spid := object_.XmlAttrSpid.Value;
if not ifnil(object_.XmlAttrType) then
{self.}Type := object_.XmlAttrType.Value;
if not ifnil(object_.XmlAttrGfxdata) then
{self.}Gfxdata := object_.XmlAttrGfxdata.Value;
if not ifnil(object_.XmlAttrFilled) then
{self.}Filled := object_.XmlAttrFilled.Value;
if not ifnil(object_.XmlAttrAllowincell) then
{self.}Allowincell := object_.XmlAttrAllowincell.Value;
if not ifnil(object_.XmlAttrFillcolor) then
{self.}Fillcolor := object_.XmlAttrFillcolor.Value;
if not ifnil(object_.XmlAttrStroked) then
{self.}Stroked := object_.XmlAttrStroked.Value;
if not ifnil(object_.XmlAttrOle) then
{self.}Ole := object_.XmlAttrOle.Value;
if not ifnil(object_.XmlChildFill) then
{self.}XmlChildFill := new FillUnitDecorator(object_.XmlChildFill);
if not ifnil(object_.XmlChildTextbox) then
{self.}XmlChildTextbox := new TextboxUnitDecorator(object_.XmlChildTextbox);
if not ifnil(object_.XmlChildTextpath) then
{self.}XmlChildTextpath := new TextpathUnitDecorator(object_.XmlChildTextpath);
if not ifnil(object_.XmlChildImagedata) then
{self.}XmlChildImagedata := new ImagedataUnitDecorator(object_.XmlChildImagedata);
if not ifnil(object_.XmlChildWrap) then
{self.}XmlChildWrap := new WrapUnitDecorator(object_.XmlChildWrap);
tslassigning := tslassigning_backup;
end;
function WrapUnitDecorator.Create(_obj: Wrap);
begin
class(Wrap).Create();
object_ := _obj;
{self.}Convert();
class(Wrap).Create();
object_ := _obj;
{self.}Convert();
end;
function WrapUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function WrapUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrAnchorx) then
{self.}Anchorx := object_.XmlAttrAnchorx.Value;
if not ifnil(object_.XmlAttrAnchory) then
{self.}Anchory := object_.XmlAttrAnchory.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrAnchorx) then
{self.}Anchorx := object_.XmlAttrAnchorx.Value;
if not ifnil(object_.XmlAttrAnchory) then
{self.}Anchory := object_.XmlAttrAnchory.Value;
tslassigning := tslassigning_backup;
end;
function FillUnitDecorator.Create(_obj: Fill);
begin
class(Fill).Create();
object_ := _obj;
{self.}Convert();
class(Fill).Create();
object_ := _obj;
{self.}Convert();
end;
function FillUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function FillUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrOpacity) then
{self.}Opacity := object_.XmlAttrOpacity.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrOpacity) then
{self.}Opacity := object_.XmlAttrOpacity.Value;
tslassigning := tslassigning_backup;
end;
function ImagedataUnitDecorator.Create(_obj: Imagedata);
begin
class(Imagedata).Create();
object_ := _obj;
{self.}Convert();
class(Imagedata).Create();
object_ := _obj;
{self.}Convert();
end;
function ImagedataUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function ImagedataUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrTitle) then
{self.}Title := object_.XmlAttrTitle.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
if not ifnil(object_.XmlAttrTitle) then
{self.}Title := object_.XmlAttrTitle.Value;
tslassigning := tslassigning_backup;
end;
function TextboxUnitDecorator.Create(_obj: Textbox);
begin
class(Textbox).Create();
object_ := _obj;
{self.}Convert();
class(Textbox).Create();
object_ := _obj;
{self.}Convert();
end;
function TextboxUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function TextboxUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlChildTxbxContent) then
{self.}XmlChildTxbxContent := new TxbxContentUnitDecorator(object_.XmlChildTxbxContent);
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlChildTxbxContent) then
{self.}XmlChildTxbxContent := new TxbxContentUnitDecorator(object_.XmlChildTxbxContent);
tslassigning := tslassigning_backup;
end;
function OLEObjectUnitDecorator.Create(_obj: OLEObject);
begin
class(OLEObject).Create();
object_ := _obj;
{self.}Convert();
class(OLEObject).Create();
object_ := _obj;
{self.}Convert();
end;
function OLEObjectUnitDecorator.GetObject();
begin
return object_;
return object_;
end;
function OLEObjectUnitDecorator.Convert();
begin
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrType) then
{self.}Type := object_.XmlAttrType.Value;
if not ifnil(object_.XmlAttrProgID) then
{self.}ProgID := object_.XmlAttrProgID.Value;
if not ifnil(object_.XmlAttrShapeID) then
{self.}ShapeID := object_.XmlAttrShapeID.Value;
if not ifnil(object_.XmlAttrDrawAspect) then
{self.}DrawAspect := object_.XmlAttrDrawAspect.Value;
if not ifnil(object_.XmlAttrObjectID) then
{self.}ObjectID := object_.XmlAttrObjectID.Value;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
tslassigning := tslassigning_backup;
tslassigning_backup := tslassigning;
tslassigning := 1;
if not ifnil(object_.XmlAttrType) then
{self.}Type := object_.XmlAttrType.Value;
if not ifnil(object_.XmlAttrProgID) then
{self.}ProgID := object_.XmlAttrProgID.Value;
if not ifnil(object_.XmlAttrShapeID) then
{self.}ShapeID := object_.XmlAttrShapeID.Value;
if not ifnil(object_.XmlAttrDrawAspect) then
{self.}DrawAspect := object_.XmlAttrDrawAspect.Value;
if not ifnil(object_.XmlAttrObjectID) then
{self.}ObjectID := object_.XmlAttrObjectID.Value;
if not ifnil(object_.XmlAttrId) then
{self.}Id := object_.XmlAttrId.Value;
tslassigning := tslassigning_backup;
end;
end.

View File

@ -10,7 +10,6 @@ public
Prefix: string;
ElementName: string;
Value: any;
end;
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 InitNode(_node: XmlNode);virtual;
function Copy(_obj: tslobj);virtual;
function Deserialize();virtual;
function Serialize();virtual;
function Marshal(): tableArray;virtual;
function Deserialize();
function Serialize();
function Marshal(): tableArray;
function GetOrCreateNode(_obj: tslobj);
function GetNode(): XmlNode;
function Elements(): array of tslobj;
function Attribute(_attr: string; _ns: string): OpenXmlAttribute;
function Attributes(): array of OpenXmlAttribute;
function Elements(): array of tslobj;
function Xmlns(_prefix: string): string;
public
function AppendChild(_obj: tslobj); // OpenXmlElement or OpenXmlPcdata
function InsertAfter(_obj: tslobj; _pos_obj: tslobj);
// function InsertBefore(_pos_obj: tslobj; _obj: tslobj);
// function PrependChild(_obj: tslobj);
// function RemoveChild(_obj: tslobj);
// function SetAttribute(_obj: OpenXmlAttribute);
protected
function GetNode(): XmlNode;
function DeleteSelf(): boolean;
public
LocalName: string; // 本地名称
@ -29,14 +24,14 @@ public
Prefix: string; // 前缀
XmlNode: XmlNode; // XMLNode节点
ElementName: string;
Removed: boolean; // 节点删除标记最后Serialize时候统一删除
protected
attributes_: array of OpenXmlAttribute; // 属性
attributes_pf_: tableArray;
// child_elements_: array of tslobj; // 子节点元素
xmlns_: tableArray;
sorted_child_: tableArray;
container_: TSOfficeContainer;
xmlns_: tableArray;
end;
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:]);
else {self.}Create(nil, nil, node_name);
{self.}InitNode(_node);
xmlns_ := array();
end;
function OpenXmlElement.Create(_parent: tslobj; _prefix: string; _local_name: string);overload;
@ -56,122 +50,31 @@ begin
{self.}LocalName := _local_name;
{self.}XmlNode := nil;
{self.}ElementName := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%s", {self.}Prefix, {self.}LocalName) : {self.}LocalName;
{self.}Removed := false;
{self.}Init();
xmlns_ := array();
end;
function OpenXmlElement.InitNode(_node: XmlNode);virtual;
function OpenXmlElement.Attribute(_attr: string; _ns: string): OpenXmlAttribute;
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;
if ifnil(_ns) then
attr_name := ifString({self.}Prefix) and {self.}Prefix <> "" ? format("%s:%%s", {self.}Prefix, _attr) : _attr;
else
attr_name := format("%s:%s", _ns, _attr);
return attributes_[attr_name];
end;
function OpenXmlElement.GetOrCreateNode(_obj: tslobj);
function OpenXmlElement.Attributes(): array of OpenXmlAttribute;
begin
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
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
attrs := array();
for k, v in attributes_ do
if not ifnil(v.Value) then {self.}GetNode().SetAttribute(v.ElementName, v.Value);
// Children
child_elements := container_.Get();
for k, v in child_elements do
v.Serialize();
attrs[length(attrs)] := v;
return attrs;
end;
function OpenXmlElement.Marshal(): tableArray;
function OpenXmlElement.Elements(): array of tslobj;
begin
child_elements := container_.Get();
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;
return container_.GetElements(false);
end;
function OpenXmlElement.Xmlns(_prefix: string): string;
@ -185,17 +88,20 @@ begin
return xmlns_[k];
end;
function OpenXmlElement.Elements(): array of tslobj;
function OpenXmlElement.GetNode(): XmlNode;
begin
return container_.Get();
if not ifObj({self.}XmlNode) then {self.}XmlNode := {self.}Parent.GetOrCreateNode(self);
return {self.}XmlNode;
end;
function OpenXmlElement.InsertAfter(_obj: tslobj; _pos_obj: tslobj);
function OpenXmlElement.DeleteSelf(): boolean;
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;
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 Insert(data: any): boolean;
function InsertAfter(data: any; pos_data: any);
function Get(name: string; index: integer);overload;
function Get(): array of tslobj;overload;
function Get(name: string; index: integer);
function GetElements(include_removed: boolean): array of tslobj;
private
function GetBucketObj(str: string): LinkList;
@ -24,6 +24,7 @@ public
function Create(name: string);
function Set(data: any);
function Add(data: any);
function Delete(data: any): boolean;
function InsertAfter(data: any; pos_data: any): boolean;
function Name(): string;
function Size(): integer;
@ -58,7 +59,7 @@ function TSOfficeContainer.Set(data: any): boolean;
begin
obj := {self.}GetBucketObj(data.ElementName);
if ifnil(obj) then return false;
obj.Set(data);
obj.Add(data);
return true;
end;
@ -125,7 +126,7 @@ begin
end
end;
function TSOfficeContainer.Get(): array of tslobj;overload;
function TSOfficeContainer.GetElements(include_removed: boolean = true): array of tslobj;
begin
arr := array();
for i:=0 to current_index_ do
@ -136,7 +137,8 @@ begin
node := obj.First();
while ifObj(node) do
begin
arr[length(arr)] := node.data;
if include_removed or not node.data.Removed then
arr[length(arr)] := node.data;
node := node.next;
end
end
@ -144,7 +146,7 @@ begin
return arr;
end;
function TSOfficeContainer.Get(name: string; index: integer);overload;
function TSOfficeContainer.Get(name: string; index: integer);
begin
ind := child_[name];
arr := array();
@ -239,6 +241,25 @@ begin
return false;
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;
begin
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`下的属性