-
Notifications
You must be signed in to change notification settings - Fork 2
/
devisio.rex
216 lines (189 loc) · 7.84 KB
/
devisio.rex
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*REXX 2.0.0
Copyright (c) 2009-2020, Andrew J. Armstrong
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*REXX*****************************************************************
** **
** NAME - DEVISIO **
** **
** FUNCTION - Removes non-SVG markup from SVG documents created by **
** Microsoft Visio. The Microsoft-specific extensions are **
** identified by the 'v:' name space. **
** **
** USAGE - You can run this Rexx on an IBM mainframe, or on a PC **
** by using Regina Rexx from: **
** **
** http://regina-rexx.sourceforge.net **
** **
** **
** SYNTAX - DEVISIO infile outfile [(options...] **
** **
** Where, **
** infile = Microsoft Visio SVG file. **
** outfile = SVG file. **
** options = Options **
** **
** NOTES - 1. You will have to either append the PARSEXML and **
** PRETTY source files manually to this file, or run **
** this file through the REXX rexx pre-processor. **
** **
** To use the pre-processor, run: **
** **
** tso rexxpp your.rexx.lib(devisio) **
** **
** **
** AUTHOR - Andrew J. Armstrong <[email protected]> **
** **
** HISTORY - Date By Reason (most recent at the top please) **
** -------- --- ----------------------------------------- **
** 20090822 AJA Changed from GPL to BSD license. **
** 20051026 AJA Initial version. **
** **
**********************************************************************/
parse arg sFileIn sFileOut' ('sOptions')'
numeric digits 16
parse value sourceline(1) with . sVersion
say 'VIS000I Microsoft Visio SVG extensions remover' sVersion
if sFileIn = ''
then do
say 'Syntax:'
say ' devisio infile outfile (options'
say
say 'Where:'
say ' infile = Microsoft Visio SVG file'
say ' outfile = SVG file (without Microsoft extensions)'
exit
end
sOptions = 'NOBLANKS' translate(sOptions)
call initParser sOptions /* DO THIS FIRST! Sets g. vars to '' */
parse source g.0ENV .
if g.0ENV = 'TSO'
then do
address ISPEXEC
'CONTROL ERRORS RETURN'
g.0LINES = 0
end
call Prolog
/* Open the specified file and parse it */
nParseRC = parseFile(sFileIn)
doc = getDocumentElement()
g.0PREFIXES = getVisioNamespacePrefixes(doc)
say 'VIS002I Removing elements and attributes with prefixes:',
g.0PREFIXES
call removeVisioTags doc
call findAllReferences doc
call removeUnusedReferences doc
/* fix ups to make the svg render properly...*/
call setAttributes doc,,
'xml:space','default',,
'xmlns:xlink','http://www.w3.org/1999/xlink'
call prettyPrinter sFileOut
call Epilog
exit
getVisioNamespacePrefixes: procedure expose g.
parse arg doc
sAttrNames = getAttributeNames(doc)
sPrefixes = ''
do i = 1 to words(sAttrNames)
sAttrName = word(sAttrNames,i)
if left(sAttrName,6) = 'xmlns:'
then do
sNameSpace = getAttribute(doc,sAttrName)
if pos('schemas.microsoft.com',sNameSpace) > 0
then do
sPrefix = substr(sAttrName,7)
sPrefixes = sPrefixes sPrefix
say 'VIS001I Removing' sAttrName'='getAttribute(doc,sAttrName)
call removeAttribute doc,sAttrName
end
end
end
return strip(sPrefixes)
removeVisioTags: procedure expose g.
parse arg node
sTagName = getNodeName(node)
if isVisioExtension(sTagName)
then call removeChild node
else do
if sTagName = 'marker' /* HACK: fixes Visio 2003 bug */
then call setAttribute node,'overflow','visible'
sAttrNames = getAttributeNames(node)
do i = 1 to words(sAttrNames)
sAttrName = word(sAttrNames,i)
if isVisioExtension(sAttrName)
then call removeAttribute node,sAttrName
end
children = getChildNodes(node)
do i = 1 to words(children)
child = word(children,i)
call removeVisioTags child
end
end
return
isVisioExtension: procedure expose g.
parse arg sTagName .
if wordpos(sTagName,'title desc') > 0 then return 1
if pos(':',sTagName) = 0 then return 0
parse arg sPrefix':'
if wordpos(sPrefix,g.0PREFIXES) > 0 then return 1
return 0
findAllReferences: procedure expose g.
parse arg node
sText = getNodeValue(node)
do while pos('url(#',sText) > 0
parse var sText 'url(#'sId')'sText
sRef = '#'sId
g.0ID.sRef = 1
end
if hasAttribute(node,'xlink:href')
then do
sId = getAttribute(node,'xlink:href')
if left(sId,1) = '#' /* is it a local reference? */
then g.0ID.sId = 1 /* For example: g.0ID.#mrkr13-14 = 1 */
end
children = getChildNodes(node)
do i = 1 to words(children)
child = word(children,i)
call findAllReferences child
end
return
removeUnusedReferences: procedure expose g.
parse arg node
if hasAttribute(node,'id')
then do
sId = getAttribute(node,'id')
sRef = '#'sId
if g.0ID.sRef <> 1
then call removeAttribute node,'id'
end
children = getChildNodes(node)
do i = 1 to words(children)
child = word(children,i)
call removeUnusedReferences child
end
return
Prolog: procedure expose g.
return
Epilog: procedure expose g.
return
/*INCLUDE pretty.rex */