2 # ------------------------------------------------------------------------
3 # Red Hat replacement for org.openoffice.configuration.XMLDefaultGenerator
4 # XSLT processor using xsltproc
5 # ------------------------------------------------------------------------
6 if [ "x$1" = x-Xmx128m ]; then
9 echo First argument not -Xmx128m
12 if [ "x$1" = x-classpath ]; then
15 echo First argument not -classpath
19 if [ "$1" != org.openoffice.configuration.XMLDefaultGenerator ]; then
20 echo Only org.openoffice.configuration.XMLDefaultGenerator emulated
25 xml="`dirname $1`/`basename $1 .xcd`.xml"
26 if echo "$xcd" | grep -qv ^/; then
32 dtd=`grep 'schema:component SYSTEM' $xcd | cut -d\" -f2`
33 if echo "$dtd" | grep -qv ^/; then
34 dtd="`dirname $xcd`/$dtd"
36 if echo "$util" | grep -qv ^/; then
39 # Someone XML aware tell me how XML parser finds out it wants to load
40 # instance{,2}.xsl from the .xcd and .dtd files
41 if echo "$dtd" | grep -q description2; then
42 xsl="$util/instance2.xsl"
44 xsl="$util/instance.xsl"
46 xmli="$misc/instance/$xml"
47 xmlt="$misc/template/$xml"
48 mkdir -p `dirname $xmli`
49 mkdir -p `dirname $xmlt`
50 echo "** Start validating: file:$xcd"
51 tmpd=`mktemp -d /tmp/ooxmlparse.XXXXXX` || exit 1
52 gcc -xc - -o $tmpd/utf8filter <<"EOF"
59 char *buf = NULL, *p, *q, b[8];
62 while (getline (&buf, &bufsize, stdin) >= 0)
65 while ((q = strstr (p, "&#x")) != NULL)
72 w = strtoul (q + 3, &p, 16);
76 b[0] = w, b[1] = '\0';
77 else if (w >= 0x7fffffff)
81 for (i = 2; i < 6; i++)
82 if ((w & (0xffffffff << (5 * i + 1))) == 0)
84 b[0] = 0xffffff00 >> i;
88 b[i] = 0x80 | (w & 0x3f); w >>= 6;
100 cp -a $dtd $tmpd/foo.dtd
101 sed 's~^\(.*<xsl:attribute name="\)xml:lang\(".*\)$~&\
102 \1cfg:xmllang\2~' $xsl > $tmpd/foo.xsl
103 needs="`sed -n -e 's~^.*cfg:component="\([^"]*\)".*$~\1~p' $xcd | sort -u`"
106 n=`echo $np | sed -e 's~\.~/~g'`
108 sed -e '/schema:component SYSTEM/s~^\([^"]*"\)[^"]*\(".*\)$~\1foo.dtd\2~' $n.xcd > $tmpd/$na.xcd
109 ln -sf $na.xcd $tmpd/$na.xml
110 sedcmd="$sedcmd;s@cfg:component=\"$np\"@cfg:component=\"$na\"@g"
112 sed -e '/schema:component SYSTEM/s~^\([^"]*"\)[^"]*\(".*\)$~\1foo.dtd\2~' -e "$sedcmd" $xcd > $tmpd/foo.xcd
113 cat > $tmpd/postprocess.sed <<"EOF"
115 s~xmlns:cfg\(="[^"]*"\)[ ]*\(cfg:package="[^"]*"\)~\2 xmlns:xcfg\1~
116 s~[ ]*xmlns:cfg="[^"]>~~g
117 s~xmlns:cfg="[^"]*"[ ]*~~g
118 s~xmlns:xcfg~xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:cfg~
119 s~^\(<?xml.*\)?>~\1 encoding="utf-8"?>~
120 s~<\([a-zA-Z0-9_:]*\)\([ ][^>]*\)></\1>~<\1\2/>~g
122 s~[ ]name=~ cfg:name=~g
123 s~[ ]type=~ cfg:type=~g
124 s~xmllang=~xml:lang=~g
125 s~xmlns:xsi=""[ ]*~~g
126 s~\(<ooInetSuffix[^>]*\)[ ]cfg:null="true"~\1~g
127 s~cfg:null=~xsi:null=~g
129 pushd $tmpd >/dev/null 2>&1
130 xsltproc foo.xsl foo.xcd 2>/dev/null \
131 | sed -f postprocess.sed | ./utf8filter > instance
132 # For some reason cfg:instance-of from different component doesn't work for template
133 # generation. Work around it.
134 if grep -q 'schema:instance cfg:instance-of="Font" cfg:name="Font" cfg:component="Common"' foo.xcd; then
135 sed -n '/schema:group[ ]*cfg:name="Font"/,/\/schema:group/p;/\/schema:templates/q' Common.xcd > Font.xcd
136 sed '/<schema:templates>/r Font.xcd' foo.xcd > foo.xcd.new
137 sed '/schema:instance cfg:instance-of="Font" cfg:name="Font"/s~[ ]*cfg:component="Common"~~' foo.xcd.new > foo.xcd
139 xsltproc --stringparam templates true foo.xsl foo.xcd 2>/dev/null \
140 | sed -f postprocess.sed | ./utf8filter > template
141 if [ -f foo.xcd.new ]; then
142 # Finish the workaround.
143 awk '/<Font>/ { if (!a) { b=1; next; }
144 sub(/<Font>/,"<Font xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">"); }
145 /<\/Font>/ { if (!a) { a=1; b=0; next; } }
146 { if (b) next; print; }' template > template.new
147 mv -f template.new template
150 cat $tmpd/instance > $xmli
151 cat $tmpd/template > $xmlt
153 echo "** Document is valid!"