]> git.pld-linux.org Git - packages/libreoffice.git/blame - openoffice-xmlparse.sh
- up
[packages/libreoffice.git] / openoffice-xmlparse.sh
CommitLineData
4d52e883
PG
1#!/bin/sh
2# ------------------------------------------------------------------------
3# Red Hat replacement for org.openoffice.configuration.XMLDefaultGenerator
4# XSLT processor using xsltproc
5# ------------------------------------------------------------------------
6if [ "x$1" = x-Xmx128m ]; then
7 shift
8else
9 echo First argument not -Xmx128m
10 exit 1
11fi
12if [ "x$1" = x-classpath ]; then
13 shift
14else
15 echo First argument not -classpath
16 exit 1
17fi
18shift
19if [ "$1" != org.openoffice.configuration.XMLDefaultGenerator ]; then
20 echo Only org.openoffice.configuration.XMLDefaultGenerator emulated
21 exit 1
22fi
23shift
24xcd=$1
25xml="`dirname $1`/`basename $1 .xcd`.xml"
26if echo "$xcd" | grep -qv ^/; then
27 xcd="`pwd`/$xcd"
28fi
29util=$2
30misc=$3
31data=$4
32dtd=`grep 'schema:component SYSTEM' $xcd | cut -d\" -f2`
33if echo "$dtd" | grep -qv ^/; then
34 dtd="`dirname $xcd`/$dtd"
35fi
36if echo "$util" | grep -qv ^/; then
37 util="`pwd`/$util"
38fi
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
41if echo "$dtd" | grep -q description2; then
42 xsl="$util/instance2.xsl"
43else
44 xsl="$util/instance.xsl"
45fi
46xmli="$misc/instance/$xml"
47xmlt="$misc/template/$xml"
48mkdir -p `dirname $xmli`
49mkdir -p `dirname $xmlt`
50echo "** Start validating: file:$xcd"
51tmpd=`mktemp -d /tmp/ooxmlparse.XXXXXX` || exit 1
52gcc -xc - -o $tmpd/utf8filter <<"EOF"
53#define _GNU_SOURCE
54#include <stdio.h>
55#include <string.h>
56
57int 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}
99EOF
100cp -a $dtd $tmpd/foo.dtd
101sed 's~^\(.*<xsl:attribute name="\)xml:lang\(".*\)$~&\
102\1cfg:xmllang\2~' $xsl > $tmpd/foo.xsl
103needs="`sed -n -e 's~^.*cfg:component="\([^"]*\)".*$~\1~p' $xcd | sort -u`"
104sedcmd=""
105for 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"
111done
112sed -e '/schema:component SYSTEM/s~^\([^"]*"\)[^"]*\(".*\)$~\1foo.dtd\2~' -e "$sedcmd" $xcd > $tmpd/foo.xcd
113cat > $tmpd/postprocess.sed <<"EOF"
114s~xmlns:cfg="" ~~g
115s~xmlns:cfg\(="[^"]*"\)[ ]*\(cfg:package="[^"]*"\)~\2 xmlns:xcfg\1~
116s~[ ]*xmlns:cfg="[^"]>~~g
117s~xmlns:cfg="[^"]*"[ ]*~~g
118s~xmlns:xcfg~xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:cfg~
119s~^\(<?xml.*\)?>~\1 encoding="utf-8"?>~
120s~<\([a-zA-Z0-9_:]*\)\([ ][^>]*\)></\1>~<\1\2/>~g
121s~&quot;~"~g
122s~[ ]name=~ cfg:name=~g
123s~[ ]type=~ cfg:type=~g
124s~xmllang=~xml:lang=~g
125s~xmlns:xsi=""[ ]*~~g
126s~\(<ooInetSuffix[^>]*\)[ ]cfg:null="true"~\1~g
127s~cfg:null=~xsi:null=~g
128EOF
129pushd $tmpd >/dev/null 2>&1
130xsltproc 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.
134if 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
138fi
139xsltproc --stringparam templates true foo.xsl foo.xcd 2>/dev/null \
140 | sed -f postprocess.sed | ./utf8filter > template
141if [ -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
148fi
149popd >/dev/null 2>&1
150cat $tmpd/instance > $xmli
151cat $tmpd/template > $xmlt
152rm -rf $tmpd
153echo "** Document is valid!"
154exit 0
This page took 0.078759 seconds and 4 git commands to generate.