@@ -69,11 +69,26 @@ rename_or_merge() {
6969# Validate XML file is well-formed before processing
7070validate_xml () {
7171 local file=" $1 "
72- # Basic validation: check for opening and closing resources tags
72+ if command -v python3 & > /dev/null; then
73+ python3 - " $file " << 'PY '
74+ import sys
75+ import xml.etree.ElementTree as ET
76+
77+ try:
78+ ET.parse(sys.argv[1])
79+ except Exception as e:
80+ print(e)
81+ raise SystemExit(1)
82+ PY
83+ return $?
84+ fi
85+
86+ # Fallback validation: check for opening and closing resources tags
7387 if ! grep -q ' <resources' " $file " 2> /dev/null || ! grep -q ' </resources>' " $file " 2> /dev/null; then
74- echo " Warning: $file appears to be malformed XML, skipping normalization "
88+ echo " missing resources root "
7589 return 1
7690 fi
91+
7792 return 0
7893}
7994
@@ -164,10 +179,33 @@ echo ""
164179echo " Normalizing XML formatting..."
165180
166181# Normalize XML
182+ MARKUP_FIXED_COUNT=0
167183NORMALIZED_COUNT=0
184+ XML_ERROR_COUNT=0
168185while IFS= read -r file; do
186+ if sed \
187+ -e ' s#<accent>#\<accent\>#g' \
188+ -e ' s#</accent>#\</accent\>#g' \
189+ -e ' s#<bold>#\<bold\>#g' \
190+ -e ' s#</bold>#\</bold\>#g' \
191+ " $file " > " $file .markup.tmp" ; then
192+ if cmp -s " $file " " $file .markup.tmp" ; then
193+ rm -f " $file .markup.tmp"
194+ elif mv " $file .markup.tmp" " $file " ; then
195+ MARKUP_FIXED_COUNT=$(( MARKUP_FIXED_COUNT + 1 ))
196+ else
197+ echo " Warning: Failed to fix markup in $file "
198+ rm -f " $file .markup.tmp"
199+ fi
200+ else
201+ echo " Warning: Failed to scan markup in $file "
202+ rm -f " $file .markup.tmp"
203+ fi
204+
169205 # Validate XML before processing
170- if ! validate_xml " $file " ; then
206+ if ! validation_error=$( validate_xml " $file " 2>&1 ) ; then
207+ echo " Error: $file is malformed XML: $validation_error "
208+ XML_ERROR_COUNT=$(( XML_ERROR_COUNT + 1 ))
171209 continue
172210 fi
173211
@@ -196,8 +234,14 @@ while IFS= read -r file; do
196234 fi
197235done < <( find " $RES_DIR " -type f -path " */values-*/strings.xml" 2> /dev/null)
198236
237+ echo " Fixed markup in $MARKUP_FIXED_COUNT files"
199238echo " Normalized $NORMALIZED_COUNT files"
200239
240+ if [ " $XML_ERROR_COUNT " -ne 0 ]; then
241+ echo " Error: Found $XML_ERROR_COUNT malformed XML file(s) after pull"
242+ exit 1
243+ fi
244+
201245echo " "
202246echo " Cleaning up empty files and directories..."
203247
@@ -239,3 +283,4 @@ echo "Complete!"
239283echo " Renamed: $RENAMED_COUNT , Removed: $REMOVED_COUNT "
240284echo " Normalized: $NORMALIZED_COUNT "
241285echo " Deleted files: $EMPTY_COUNT , Deleted dirs: $DELETED_DIRS "
286+ echo " Pull complete!"
0 commit comments