This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-check
executable file
·156 lines (128 loc) · 5.69 KB
/
xml-check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
# Check the input files for well-formedness and schema errors, produce a feedback file if appropriate
WORKSPACE=${WORKSPACE:-/work/space}
INPUTDIR=${1:-input}
SRCDIR=${2:-src}
FILEMASK=${3}*
DATASETS=${4:-iati-datasets}
PREFIX="XML check"
API=${API:-none}
VALLOGL=100
VALLOGS=50
mkdir -p \
$WORKSPACE/tmp/xmltest \
$WORKSPACE/tmp/xmltestlog \
$WORKSPACE/tmp/xmlschemalog \
$WORKSPACE/tmp/iatifeedback \
$WORKSPACE/tmp/feedback \
$WORKSPACE/input \
$WORKSPACE/dest
cd $WORKSPACE/$INPUTDIR
find . -name "${FILEMASK}*" -type f -print | while read FILE; do
NAME=`basename "$FILE"`
MD5="${NAME%.xml}"
cd $WORKSPACE/$INPUTDIR
xmllint --recover --output "$WORKSPACE/tmp/xmltest/$NAME" "$NAME" 2> "$WORKSPACE/tmp/xmltestlog/$NAME"
LL=`cat "$WORKSPACE/tmp/xmltestlog/$NAME" | wc -l`
if [[ $LL -gt 100 ]]; then
echo "$PREFIX $MD5: limiting XML validation log of $LL lines to $VALLOGL lines"
sed -i "2001,$((LL-$VALLOGS))c...skipped $((LL-$VALLOGL)) lines..." "$WORKSPACE/tmp/xmltestlog/$NAME"
fi
if [ -s "$WORKSPACE/tmp/xmltestlog/$NAME" ]; then
mv "$WORKSPACE/tmp/xmltest/$NAME" $WORKSPACE/$SRCDIR
else
cp --preserve "$WORKSPACE/$INPUTDIR/$NAME" $WORKSPACE/$SRCDIR
rm "$WORKSPACE/tmp/xmltest/$NAME"
fi
NSERROR=''
if grep -q "namespace error" "$WORKSPACE/tmp/xmltestlog/$NAME"; then
NSERROR=true
else
NSERROR=false
fi
ISIATI=''
cd $WORKSPACE/$SRCDIR
XMLROOT=`xmllint --xpath "local-name(/*)" "$NAME" 2> /dev/null`
XMLATTR=`xmllint --xpath "string(/*/@version)" "$NAME" 2> /dev/null`
if [[ "$XMLROOT." == "iati-activities." || "$XMLROOT." == "iati-organisations." ]]; then
ISIATI=true
else
ISIATI=false
fi
if [[ $NSERROR == false && $ISIATI == true ]]; then
if [[ $XMLATTR. =~ 1\.0[1-5]\. || $XMLATTR. =~ 2\.0[1-3]\. ]]; then
VERSION=$XMLATTR
elif [[ $XMLATTR. =~ 1\..* ]]; then
VERSION=1.05
else
VERSION=2.03
fi
if xmllint --noout --schema /home/lib/iati-rulesets/lib/schemata/$VERSION/$XMLROOT-schema.xsd "$NAME" 2> "$WORKSPACE/tmp/xmlschemalog/$NAME"; then
rm "$WORKSPACE/tmp/xmlschemalog/$NAME"
if [ -s "$WORKSPACE/tmp/xmltestlog/$NAME" ]; then
echo '<recovered-iati-file/>' > "$WORKSPACE/tmp/iatifeedback/$NAME"
echo "$PREFIX $MD5: recovered-iati-file"
if [[ "$API" != "none" ]]; then
APIDATA="{\"md5\": \"$MD5\", \"file-type\": \"recovered-iati-file\", \"iati-version\": \"$VERSION\"}"
curl -sS -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/$DATASETS/update?where=%7B%22md5%22%3A%22$MD5%22%7D"
fi
else
echo '<iati-file/>' > "$WORKSPACE/tmp/iatifeedback/$NAME"
echo "$PREFIX $MD5: iati-file"
if [[ "$API" != "none" ]]; then
APIDATA="{\"md5\": \"$MD5\", \"file-type\": \"iati-file\", \"iati-version\": \"$VERSION\"}"
curl -sS -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/$DATASETS/update?where=%7B%22md5%22%3A%22$MD5%22%7D"
fi
fi
else
LL=`cat "$WORKSPACE/tmp/xmlschemalog/$NAME" | wc -l`
if [[ $LL -gt 4000 ]]; then
echo "$PREFIX $MD5: limiting schema validation log of $LL lines to $VALLOGL lines"
sed -i "2001,$((LL-$VALLOGS))c...skipped $((LL-$VALLOGL)) lines..." "$WORKSPACE/tmp/xmlschemalog/$NAME"
fi
if [ -s "$WORKSPACE/tmp/xmltestlog/$NAME" ]; then
echo '<recovered-iati-file-with-schema-errors/>' > "$WORKSPACE/tmp/iatifeedback/$NAME"
echo "$PREFIX $MD5: recovered-iati-file-with-schema-errors"
if [[ "$API" != "none" ]]; then
APIDATA="{\"md5\": \"$MD5\", \"file-type\": \"recovered-iati-file-with-schema-errors\", \"iati-version\": \"$VERSION\"}"
curl -sS -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/$DATASETS/update?where=%7B%22md5%22%3A%22$MD5%22%7D"
fi
else
echo '<iati-file-with-schema-errors/>' > "$WORKSPACE/tmp/iatifeedback/$NAME"
echo "$PREFIX $MD5: iati-file-with-schema-errors"
if [[ "$API" != "none" ]]; then
APIDATA="{\"md5\": \"$MD5\", \"file-type\": \"iati-file-with-schema-errors\", \"iati-version\": \"$VERSION\"}"
curl -sS -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/$DATASETS/update?where=%7B%22md5%22%3A%22$MD5%22%7D"
fi
fi
fi
elif [ -s "$WORKSPACE/tmp/xmltestlog/$NAME" ]; then
echo '<not-an-xml-file/>' > "$WORKSPACE/tmp/feedback/$NAME"
echo "$PREFIX $MD5: not-an-xml-file"
rm "$WORKSPACE/src/$NAME"
if [[ "$API" != "none" ]]; then
APIDATA="{\"md5\": \"$MD5\", \"file-type\": \"not-an-xml-file\"}"
curl -sS -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/$DATASETS/update?where=%7B%22md5%22%3A%22$MD5%22%7D"
fi
else
echo '<not-an-iati-file/>' > "$WORKSPACE/tmp/feedback/$NAME"
echo "$PREFIX $MD5: not-an-iati-file"
rm "$WORKSPACE/src/$NAME"
if [[ "$API" != "none" ]]; then
APIDATA="{\"md5\": \"$MD5\", \"file-type\": \"not-an-iati-file\"}"
curl -sS -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/$DATASETS/update?where=%7B%22md5%22%3A%22$MD5%22%7D"
fi
fi
done