quote empty whitespaces for xml encoding

This commit is contained in:
REAndroid 2023-05-07 16:59:38 +02:00
parent b07b8bddfb
commit f49799a02d
4 changed files with 53 additions and 5 deletions

View File

@ -29,7 +29,10 @@ public class XMLDecodeHelper {
return;
}
if(!stringItem.hasStyle()){
writer.text(ValueDecoder.escapeSpecialCharacter(stringItem.get()));
String text = stringItem.get();
text = ValueDecoder.escapeSpecialCharacter(text);
text = ValueDecoder.quoteWhitespace(text);
writer.text(text);
}else {
String xml = stringItem.getXml();
XMLElement element = parseSpanSafe(xml);

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -158,7 +158,9 @@ import java.util.*;
addStyleElement(element);
}else {
String text = ValueDecoder
.unEscapeSpecialCharacter(element.getTextContent());
.unQuoteWhitespace(element.getTextContent());
text = ValueDecoder
.unEscapeSpecialCharacter(text);
addString(text);
}
}

View File

@ -25,7 +25,6 @@ class XMLValuesEncoderString extends XMLValuesEncoder{
super(materials);
}
@Override
void encodeValue(Entry entry, XMLElement element){
if(!element.hasChildElements()){
@ -36,7 +35,9 @@ class XMLValuesEncoderString extends XMLValuesEncoder{
}
@Override
void encodeStringValue(Entry entry, String value){
entry.setValueAsString(ValueDecoder.unEscapeSpecialCharacter(value));
value = ValueDecoder.unQuoteWhitespace(value);
value = ValueDecoder.unEscapeSpecialCharacter(value);
entry.setValueAsString(value);
}
@Override
void encodeNullValue(Entry entry){

View File

@ -50,6 +50,48 @@ public class ValueDecoder {
}
return text.substring(1);
}
public static String quoteWhitespace(String text){
if(!isWhiteSpace(text)){
return text;
}
return "\"" + text + "\"";
}
public static String unQuoteWhitespace(String text){
if(text == null || text.length() < 3){
return text;
}
if(text.charAt(0) != '"' || text.charAt(text.length()-1) != '"'){
return text;
}
String unQuoted = text.substring(1, text.length()-1);
if(!isWhiteSpace(unQuoted)){
return text;
}
return unQuoted;
}
private static boolean isWhiteSpace(String text){
if(text == null || text.length() == 0){
return false;
}
char[] chars = text.toCharArray();
for(int i = 0; i < chars.length; i++){
if(!isWhiteSpace(chars[i])){
return false;
}
}
return true;
}
private static boolean isWhiteSpace(char ch){
switch (ch){
case ' ':
case '\n':
case '\r':
case '\t':
return true;
default:
return false;
}
}
private static boolean isSpecialCharacter(char ch){
switch (ch){
case '@':