Skip to content

Commit

Permalink
fix(plc4go): adjusted xml writing
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jan 24, 2022
1 parent e9d1dcf commit 5a05613
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ func ReadProprietaryEventType(readBuffer utils.ReadBuffer, value BACnetEventType
return readBuffer.ReadUint32("proprietaryEventType", bitsToRead)
}
func ReadObjectType(readBuffer utils.ReadBuffer) (BACnetObjectType, error) {
readValue, err := readBuffer.ReadUint16("ObjectType", 10)
readValue, err := readBuffer.ReadUint16("objectType", 10)
if err != nil {
return 0, err
}
return BACnetObjectType(readValue), nil
}

func WriteObjectType(writeBuffer utils.WriteBuffer, value BACnetObjectType) error {
return writeBuffer.WriteUint16("ObjectType", 10, uint16(value), utils.WithAdditionalStringRepresentation(value.name()))
return writeBuffer.WriteUint16("objectType", 10, uint16(value), utils.WithAdditionalStringRepresentation(value.name()))
}

func WriteProprietaryObjectType(writeBuffer utils.WriteBuffer, baCnetObjectType BACnetObjectType, value uint16) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
s7Model "github.com/apache/plc4x/plc4go/internal/plc4go/s7/readwrite"
"github.com/apache/plc4x/plc4go/internal/plc4go/spi/utils"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/subchen/go-xmldom"
"os"
"strconv"
Expand Down Expand Up @@ -115,7 +116,7 @@ func RunParserSerializerTestsuite(t *testing.T, testPath string, skippedTestCase
}
referenceXml := child.FindOneByName("xml")
normalizeXml(referenceXml)
referenceSerialized := referenceXml.FirstChild().XML()
referenceSerialized := referenceXml.FirstChild().XMLPretty()

// Get the raw input by decoding the hex-encoded binary input
rawInput, err := hex.DecodeString(rawInputText)
Expand Down Expand Up @@ -166,13 +167,11 @@ func RunParserSerializerTestsuite(t *testing.T, testPath string, skippedTestCase

{
// First try to use the native xml writer
var err error
serializable := msg.(utils.Serializable)
buffer := utils.NewXmlWriteBuffer()
if err = serializable.Serialize(buffer); err == nil {
if err := serializable.Serialize(buffer); err == nil {
actualXml := buffer.GetXmlString()
err = CompareResults([]byte(actualXml), []byte(referenceSerialized))
if err != nil {
if err := CompareResults([]byte(actualXml), []byte(referenceSerialized)); err != nil {
border := strings.Repeat("=", 100)
fmt.Printf(
"\n"+
Expand All @@ -198,6 +197,7 @@ func RunParserSerializerTestsuite(t *testing.T, testPath string, skippedTestCase
actualXml,
err,
testCaseName)
assert.Equal(t, referenceSerialized, actualXml)
t.Error("Error comparing the results: " + err.Error())
return
}
Expand Down
32 changes: 30 additions & 2 deletions plc4go/internal/plc4go/spi/utils/WriteBufferXmlBased.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"math/big"
"strings"
"unicode"
)

type WriteBufferXmlBased interface {
Expand Down Expand Up @@ -152,13 +153,40 @@ func (x *xmlWriteBuffer) WriteBigFloat(logicalName string, bitLength uint8, valu
return x.encodeElement(logicalName, value, x.generateAttr(rwFloatKey, uint(bitLength), writerArgs...), writerArgs...)
}

//[^\u0009\r\n\u0020-\uD7FF\uE000-\uFFFD\ud800\udc00-\udbff\udfff]
var printableRange = &unicode.RangeTable{
R16: []unicode.Range16{
{0x0009, 0x0009, 1},
{0x000D, 0x000D, 1},
{0x000A, 0x000A, 1},
{0x0020, 0xD7FF, 1},
{0xE000, 0xFFFD, 1},
{0xD800, 0xD800, 1},
{0xDC00, 0xDBFF, 1},
{0xDFFF, 0xDFFF, 1},
},
}

func (x *xmlWriteBuffer) WriteString(logicalName string, bitLength uint32, encoding string, value string, writerArgs ...WithWriterArgs) error {
attr := x.generateAttr(rwStringKey, uint(bitLength), writerArgs...)
attr = append(attr, xml.Attr{Name: xml.Name{Local: rwEncodingKey}, Value: encoding})
return x.encodeElement(logicalName, value, attr, writerArgs...)
var valueToBeWritten interface{}
cleanedUpString := strings.TrimFunc(value, func(r rune) bool {
return !unicode.In(r, printableRange)
})
cleanedUpString = value
valueToBeWritten = cleanedUpString
if strings.Contains(cleanedUpString, "\n") {
valueToBeWritten = struct {
S string `xml:",innerxml"`
}{
S: "<![CDATA[" + cleanedUpString + "]]>",
}
}
return x.encodeElement(logicalName, valueToBeWritten, attr, writerArgs...)
}

func (x *xmlWriteBuffer) WriteVirtual(logicalName string, value interface{}, writerArgs ...WithWriterArgs) error {
func (x *xmlWriteBuffer) WriteVirtual(_ string, _ interface{}, _ ...WithWriterArgs) error {
// NO-OP
return nil
}
Expand Down

0 comments on commit 5a05613

Please sign in to comment.