]> git.pld-linux.org Git - packages/libreoffice.git/blob - openoffice-xmlparse.sh
- up
[packages/libreoffice.git] / openoffice-xmlparse.sh
1 #!/bin/sh
2 # ------------------------------------------------------------------------
3 # Red Hat replacement for org.openoffice.configuration.XMLDefaultGenerator
4 # XSLT processor using xsltproc
5 # ------------------------------------------------------------------------
6 if [ "x$1" = x-Xmx128m ]; then
7   shift
8 else
9   echo First argument not -Xmx128m
10   exit 1
11 fi
12 if [ "x$1" = x-classpath ]; then
13   shift
14 else
15   echo First argument not -classpath
16   exit 1
17 fi
18 shift
19 if [ "$1" != org.openoffice.configuration.XMLDefaultGenerator ]; then
20   echo Only org.openoffice.configuration.XMLDefaultGenerator emulated
21   exit 1
22 fi
23 shift
24 xcd=$1
25 xml="`dirname $1`/`basename $1 .xcd`.xml"
26 if echo "$xcd" | grep -qv ^/; then
27   xcd="`pwd`/$xcd"
28 fi
29 util=$2
30 misc=$3
31 data=$4
32 dtd=`grep 'schema:component SYSTEM' $xcd | cut -d\" -f2`
33 if echo "$dtd" | grep -qv ^/; then
34   dtd="`dirname $xcd`/$dtd"
35 fi
36 if echo "$util" | grep -qv ^/; then
37   util="`pwd`/$util"
38 fi
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"
43 else
44   xsl="$util/instance.xsl"
45 fi
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"
53 #define _GNU_SOURCE
54 #include <stdio.h>
55 #include <string.h>
56
57 int main (void)
58 {
59   char *buf = NULL, *p, *q, b[8];
60   size_t bufsize = 0;
61
62   while (getline (&buf, &bufsize, stdin) >= 0)
63     {
64       p = buf;
65       while ((q = strstr (p, "&#x")) != NULL)
66         {
67           wchar_t w;
68           int i;
69
70           *q = '\0';
71           fputs (p, stdout);
72           w = strtoul (q + 3, &p, 16);
73           if (*p++ != ';')
74             abort ();
75           if (w < 0x80)
76             b[0] = w, b[1] = '\0';
77           else if (w >= 0x7fffffff)
78             abort ();
79           else
80             {
81               for (i = 2; i < 6; i++)
82                 if ((w & (0xffffffff << (5 * i + 1))) == 0)
83                   break;
84               b[0] = 0xffffff00 >> i;
85               b[i--] = '\0';
86               do
87                 {
88                   b[i] = 0x80 | (w & 0x3f); w >>= 6;
89                 }
90               while (--i > 0);
91               b[0] |= w;
92             }
93           fputs (b, stdout);
94         }
95       fputs (p, stdout);
96     }
97   exit (0);
98 }
99 EOF
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`"
104 sedcmd=""
105 for np in $needs; do
106   n=`echo $np | sed -e 's~\.~/~g'`
107   na=`basename $n`
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"
111 done
112 sed -e '/schema:component SYSTEM/s~^\([^"]*"\)[^"]*\(".*\)$~\1foo.dtd\2~' -e "$sedcmd" $xcd > $tmpd/foo.xcd
113 cat > $tmpd/postprocess.sed <<"EOF"
114 s~xmlns:cfg="" ~~g
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
121 s~&quot;~"~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
128 EOF
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
138 fi
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
148 fi
149 popd >/dev/null 2>&1
150 cat $tmpd/instance > $xmli
151 cat $tmpd/template > $xmlt
152 rm -rf $tmpd
153 echo "** Document is valid!"
154 exit 0
This page took 0.035773 seconds and 3 git commands to generate.