-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
963a1c4
commit d749691
Showing
16 changed files
with
3,217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/beevik/etree" | ||
) | ||
|
||
type XMLDoc struct { | ||
VMMetaObj | ||
doc *etree.Document | ||
} | ||
|
||
func (f *XMLDoc) VMTypeString() string { | ||
return "ЗаписьXML" | ||
} | ||
|
||
func (f *XMLDoc) VMRegister() { | ||
f.VMRegisterConstructor(func(args VMSlice) error { | ||
return nil | ||
}) | ||
|
||
f.doc = etree.NewDocument() | ||
|
||
f.VMRegisterMethod("ЗаписатьОбъявленияXML", VMFuncOneParam(f.ЗаписатьОбъявленияXML)) | ||
f.VMRegisterMethod("ЗаписатьКомментарийXML", VMFuncOneParam(f.ЗаписатьКомментарийXML)) | ||
f.VMRegisterMethod("ФорматироватьXML", VMFuncOneParam(f.ФорматироватьXML)) | ||
f.VMRegisterMethod("ЗаписатьСтрокуXML", VMFuncZeroParams(f.ЗаписатьСтрокуXML)) | ||
f.VMRegisterMethod("ЗаписатьФайлXML", VMFuncOneParam(f.ЗаписатьФайлXML)) | ||
} | ||
|
||
func (f *XMLDoc) ЗаписатьОбъявленияXML(params VMStringMap, rets *VMSlice) error { | ||
for key, value := range params { | ||
f.doc.CreateProcInst(key, string(value.(VMString))) | ||
} | ||
return nil | ||
} | ||
|
||
func (f *XMLDoc) ЗаписатьКомментарийXML(params VMString, rets *VMSlice) error { | ||
f.doc.CreateComment(string(params)) | ||
return nil | ||
} | ||
|
||
func (f *XMLDoc) ФорматироватьXML(params VMInt, rets *VMSlice) error { | ||
f.doc.Indent(int(params)) | ||
return nil | ||
} | ||
|
||
func (f *XMLDoc) ЗаписатьСтрокуXML(rets *VMSlice) error { | ||
str, err := f.doc.WriteToString() | ||
if err != nil { | ||
return VMErrorSaveXML | ||
} | ||
rets.Append(VMString(str)) | ||
return nil | ||
} | ||
|
||
func (f *XMLDoc) ЗаписатьФайлXML(filePath VMString, rets *VMSlice) error { | ||
err := f.doc.WriteToFile(string(filePath)) | ||
if err != nil { | ||
return VMErrorSaveXML | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/beevik/etree" | ||
) | ||
|
||
type XMLElem struct { | ||
VMMetaObj | ||
elem *etree.Element | ||
} | ||
|
||
func (f *XMLElem) VMTypeString() string { | ||
return "ЭлементXML" | ||
} | ||
|
||
func (f *XMLElem) VMRegister() { | ||
f.VMRegisterConstructor(func(args VMSlice) error { | ||
if len(args) != 2 { | ||
return VMErrorNeedArgs(2) | ||
} | ||
|
||
name, ok := args[0].(VMString) | ||
if !ok { | ||
return VMErrorNeedString | ||
} | ||
|
||
//TODO: inheritance? | ||
var parent *etree.Element | ||
xmlElem, ok := args[1].(*XMLElem) | ||
if ok { | ||
parent = xmlElem.elem | ||
} else { | ||
xmlDoc, ok := args[1].(*XMLDoc) | ||
parent = &xmlDoc.doc.Element | ||
if !ok { | ||
return VMErrorIncorrectFieldType | ||
} | ||
} | ||
|
||
f.elem = parent.CreateElement(string(name)) | ||
return nil | ||
}) | ||
|
||
f.VMRegisterMethod("ЗаписатьТекстXML", VMFuncOneParam(f.ЗаписатьТекстXML)) | ||
f.VMRegisterMethod("ЗаписатьАтрибутыXML", VMFuncOneParam(f.ЗаписатьАтрибутыXML)) | ||
} | ||
|
||
func (f *XMLElem) ЗаписатьТекстXML(params VMString, rets *VMSlice) error { | ||
f.elem.SetText(string(params)) | ||
return nil | ||
} | ||
|
||
func (f *XMLElem) ЗаписатьАтрибутыXML(params VMStringMap, rets *VMSlice) error { | ||
for key, value := range params { | ||
f.elem.CreateAttr(key, string(value.(VMString))) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Запись XML с помощью библиотеки | ||
// https://github.com/beevik/etree | ||
|
||
// Конструктор объекта | ||
XML = Новый ЗаписьXML() | ||
|
||
// Объявления XML заданные структурой Гонца | ||
ОбъявленияXML = {"xml": 'version="1.0" encoding="UTF-8"', | ||
"xml-stylesheet": 'type="text/xsl" href="style.xsl"', | ||
} | ||
|
||
// Объявления XML добавляются методом ЗаписатьОбъявленияXML(СтруктураОбъявленияXML) | ||
// (см. CreateProcInst) | ||
XML.ЗаписатьОбъявленияXML(ОбъявленияXML) | ||
|
||
// Комментарии XML добавляются методом ЗаписатьКомментарийXML(СтрокаКомментария) | ||
// (см. CreateComment) | ||
XML.ЗаписатьКомментарийXML("Это комментарий XML") | ||
|
||
// Элементы древовидно добавляються методом ЗаписатьЭлементXML(СтрокаИмяЭлемента) | ||
// (см. CreateElement) | ||
Персоны = Новый ЭлементXML("persons",XML) | ||
Персона = Новый ЭлементXML("persons",Персоны) | ||
|
||
// Текст элемента XML добавляется методом ЗаписатьТекстXML(СтрокаТекста) | ||
// (см. SetText) | ||
Персона.ЗаписатьТекстXML("Мигачев А.И.") | ||
|
||
// Атрибуты элемента заданные структурой Гонца | ||
АтрибутыXML = {"name": "Алексей", | ||
"phone": "+79107910520", | ||
"email": "[email protected]", | ||
} | ||
|
||
// Атрибуты элемента XML добавляются методом ЗаписатьАтрибутыXML(СтруктураАттрибутыXML) | ||
// (см. CreateAttr) | ||
Персона.ЗаписатьАтрибутыXML(АтрибутыXML) | ||
|
||
// Форматирование XML пробелами выполняет метод ФорматироватьXML(ЧислоЦелоеПробелов) | ||
// (см. Indent) | ||
XML.ФорматироватьXML(10) | ||
|
||
// Запись результата с строку или файл выполняют методы ЗаписатьСтрокуXML() и ЗаписатьСтрокуXML(СтрокаИмяФайла) | ||
// (см. WriteTo) | ||
СтрокаXML = XML.ЗаписатьСтрокуXML() | ||
XML.ЗаписатьФайлXML("C:/Users/Vladimir/Desktop/test.xml") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.