Debian Codegraph @

test/dtdgport.awk

raw
1 # dtdgport.awk
2 # Reads an XML document from standard input and
3 # prints a DTD for this document to standard output.
4 # http://saxon.sourceforge.net/dtdgen.html
5 # JK 2004-10-09
6 # JK 2006-03-05
7 # JK 2007-08-05 Converted for portability from the original dtd_generator.awk
8
9 # The program makes an internal list of all the elements
10 # and attributes that appear in your document, noting how
11 # they are nested, and noting which elements contain
12 # character data.
13
14 BEGIN {
15 while (getXMLEVENT(ARGV[1])) {
16 # Remember each element.
17 if ( XMLEVENT == "STARTELEM" ) {
18 # Remember the parent names of each child node.
19 name[XMLDEPTH] = XMLSTARTELEM
20 if (XMLDEPTH>1)
21 child[name[XMLDEPTH-1], XMLSTARTELEM] ++
22 # Count how often the element occurs.
23 elem[XMLSTARTELEM] ++
24 # Remember all the attributes with the element.
25 for (a in XMLATTR)
26 attr[XMLSTARTELEM,a] ++
27 }
28 }
29 }
30
31 END { print_elem(1, name[1]) } # name[1] is the root
32
33 # Print one element (including sub-elements) but only once.
34 function print_elem(depth, element, c, atn, chl, n, i, myChildren) {
35 if (already_printed[element]++)
36 return
37 indent=sprintf("%*s", 2*depth-2, "")
38 myChildren=""
39 for (c in child) {
40 split(c, chl, SUBSEP)
41 if (element == chl[1]) {
42 if (myChildren=="")
43 myChildren = chl[2]
44 else
45 myChildren = myChildren " | " chl[2]
46 }
47 }
48 # If an element has no child nodes, declare it as such.
49 if (myChildren=="")
50 print indent "<!ELEMENT", element , "( #PCDATA ) >"
51 else
52 print indent "<!ELEMENT", element , "(", myChildren, ")* >"
53 # After the element name itself, list its attributes.
54 for (a in attr) {
55 split(a, atn, SUBSEP)
56 # Treat only those attributes that belong to the current element.
57 if (element == atn[1]) {
58 # If an attribute occured each time with its element, notice this.
59 if (attr[element, atn[2]] == elem[element])
60 print indent "<!ATTLIST", element, atn[2], "CDATA #REQUIRED>"
61 else
62 print indent "<!ATTLIST", element, atn[2], "CDATA #IMPLIED>"
63 }
64 }
65 # Now go through the child nodes of this elements and print them.
66 gsub(/[\|]/, " ", myChildren)
67 n=split(myChildren, chl)
68 for(i=1; i<=n; i++) {
69 print_elem(depth+1, chl[i])
70 split(myChildren, chl)
71 }
72 }
73
74 ##
75 # getXMLEVENT( file ): # read next xml-data into XMLEVENT,XMLNAME,XMLATTR
76 # # referenced entities are not resolved
77 # Parameters:
78 # file -- path to xml file
79 # External variables:
80 # XMLEVENT -- type of item read, e.g. "STARTELEM"(tag), "ENDELEM"(end tag),
81 # "COMMENT"(comment), "CHARDATA"(data)
82 # XMLNAME -- value of item, e.g. tagname if type is "STARTELEM" or "ENDELEM"
83 # XMLATTR -- Map of attributes, only set if XMLEVENT=="STARTELEM"
84 # XMLPATH -- Path to current tag, e.g. /TopLevelTag/SubTag1/SubTag2
85 # XMLROW -- current line number in input file
86 # XMLERROR -- error text, set on parse error
87 # Returns:
88 # 1 on successful read: XMLEVENT, XMLNAME, XMLATTR are set accordingly
89 # "" at end of file or parse error, XMLERROR is set on error
90 # Private Data:
91 # _XMLIO -- buffer, XMLROW, XMLPATH for open files
92 ##
93
94 function getXMLEVENT( file ,end,p,q,tag,att,accu,mline,mode,S0,ex,dtd) {
95 XMLEVENT=XMLNAME=XMLERROR=XMLSTARTELEM=XMLENDELEM = ""
96 split("", XMLATTR)
97 S0 = _XMLIO[file,"S0"]
98 XMLROW = _XMLIO[file,"line"];
99 XMLPATH = _XMLIO[file,"path"];
100 XMLDEPTH=_XMLIO[file,"depth"]+0;
101 dtd = _XMLIO[file,"dtd"];
102 while (!XMLEVENT) {
103 if (S0 == "") {
104 if (1 != (getline S0 < file))
105 break;
106 XMLROW ++;
107 S0 = S0 RS;
108 }
109 if (mode == "") {
110 mline = XMLROW
111 accu=""
112 p = substr(S0,1,1)
113 if (p != "<" && !(dtd && p=="]"))
114 mode="CHARDATA"
115 else if (p == "]") {
116 S0 = substr(S0,2)
117 mode="ENDDOCT"
118 end=">"
119 dtd=0
120 } else if ( substr(S0,1,4) == "<!--" ) {
121 S0=substr(S0,5)
122 mode="COMMENT"
123 end="-->"
124 } else if ( substr(S0,1,9) == "<!DOCTYPE" ) {
125 S0 = substr(S0,10)
126 mode = "STARTDOCT"
127 end = ">"
128 } else if (substr(S0,1,9) == "<![CDATA[" ) {
129 S0 = substr(S0,10)
130 mode = "CDA"
131 end = "]]>"
132 } else if ( substr(S0,1,2) == "<!" ) {
133 S0 = substr(S0,3)
134 mode = "DEC"
135 end = ">"
136 } else if (substr(S0,1,2) == "<?") {
137 S0 = substr(S0,3)
138 mode = "PROCINST"
139 end = "?>"
140 } else if ( substr(S0,1,2)=="</" ) {
141 S0 = substr(S0,3)
142 mode = "ENDELEM"
143 end = ">";
144 tag = S0
145 sub(/[ \n\r\t>].*$/,"",tag)
146 S0 = substr(S0,length(tag)+1)
147 ex = XMLPATH
148 sub(/\/[^\/]*$/,"",XMLPATH)
149 ex = substr(ex, length(XMLPATH)+2)
150 if (tag != ex) {
151 XMLERROR = "unexpected close tag <" ex ">..</" tag ">"
152 break
153 }
154 } else {
155 S0 = substr(S0,2)
156 mode = "STARTELEM"
157 tag = S0
158 sub(/[ \n\r\t\/>].*$/,"",tag)
159 S0 = substr(S0, length(tag)+1)
160 if (tag !~ /^[A-Za-z:_][0-9A-Za-z:_.-]*$/ ) { # /^[[:alpha:]:_][[:alnum:]:_.-]*$/
161 XMLERROR = "invalid tag name '" tag "'"
162 break
163 }
164 XMLPATH = XMLPATH "/" tag;
165 }
166 } else if (mode == "CHARDATA") { # terminated by "<" or EOF
167 p = index(S0, "<")
168 if (dtd && (q=index(S0,"]")) && (!p || q<p) )
169 p = q
170 if (p) {
171 XMLEVENT = "CHARDATA"
172 XMLNAME = accu unescapeXML(substr(S0, 1, p-1))
173 S0 = substr(S0, p)
174 mode = ""
175 } else {
176 accu = accu unescapeXML(S0)
177 S0 = ""
178 }
179 } else if ( mode == "STARTELEM" ) {
180 sub(/^[ \n\r\t]*/,"",S0)
181 if (S0 == "")
182 continue
183 if (substr(S0, 1, 2) == "/>" ) {
184 S0 = substr(S0, 3)
185 mode = ""
186 XMLEVENT = "STARTELEM"
187 XMLNAME = XMLSTARTELEM = tag
188 XMLDEPTH ++
189 S0 = "</" tag ">" S0
190 } else if (substr(S0, 1, 1) == ">" ) {
191 S0 = substr(S0, 2)
192 mode = ""
193 XMLEVENT = "STARTELEM"
194 XMLNAME = XMLSTARTELEM = tag
195 XMLDEPTH ++
196 } else {
197 att = S0
198 sub(/[= \n\r\t\/>].*$/,"",att)
199 S0 = substr(S0, length(att) + 1)
200 mode = "ATTR"
201 if (att !~ /^[A-Za-z:_][0-9A-Za-z:_.-]*$/ ) { # /^[[:alpha:]:_][[:alnum:]:_.-]*$/
202 XMLERROR = "invalid attribute name '" att "'"
203 break
204 }
205 }
206 } else if (mode == "ATTR") {
207 sub(/^[ \n\r\t]*/, "", S0)
208 if (S0 == "")
209 continue
210 if (substr(S0,1,1) == "=" ) {
211 S0 = substr(S0,2)
212 mode = "EQ"
213 } else {
214 XMLATTR[att] = att
215 mode = "STARTELEM"
216 }
217 } else if (mode == "EQ") {
218 sub(/^[ \n\r\t]*/,"",S0)
219 if (S0 == "")
220 continue
221 end = substr(S0,1,1)
222 if (end == "\"" || end == "'") {
223 S0 = substr(S0,2)
224 accu = ""
225 mode = "VALUE"
226 } else {
227 accu = S0
228 sub(/[ \n\r\t\/>].*$/,"", accu)
229 S0 = substr(S0, length(accu)+1)
230 XMLATTR[att] = unescapeXML(accu)
231 mode = "STARTELEM"
232 }
233 } else if (mode == "VALUE") { # terminated by end
234 if (p = index(S0, end)) {
235 XMLATTR[att] = accu unescapeXML(substr(S0,1,p-1))
236 S0 = substr(S0, p+length(end))
237 mode = "STARTELEM"
238 } else {
239 accu = accu unescapeXML(S0)
240 S0=""
241 }
242 } else if (mode == "STARTDOCT") { # terminated by "[" or ">"
243 if ((q = index(S0, "[")) && (!(p = index(S0,end)) || q<p )) {
244 XMLEVENT = mode
245 XMLNAME = accu substr(S0, 1, q-1)
246 S0 = substr(S0, q+1)
247 mode = ""
248 dtd = 1
249 } else if (p = index(S0,end)) {
250 XMLEVENT = mode
251 XMLNAME = accu substr(S0, 1, p-1)
252 S0 = "]" substr(S0, p)
253 mode = ""
254 dtd = 1
255 } else {
256 accu = accu S0
257 S0 = ""
258 }
259 } else if (p = index(S0,end)) { # terminated by end
260 XMLEVENT = mode
261 XMLNAME = XMLENDELEM = ( mode=="ENDELEM" ? tag : accu substr(S0,1,p-1))
262 if (mode=="ENDELEM") XMLDEPTH --
263 S0 = substr(S0, p+length(end))
264 mode = ""
265 } else {
266 accu = accu S0
267 S0 = ""
268 }
269 }
270 _XMLIO[file, "S0"] = S0;
271 _XMLIO[file, "line"] = XMLROW;
272 _XMLIO[file, "path"] = XMLPATH;
273 _XMLIO[file, "depth"] = XMLDEPTH;
274 _XMLIO[file, "dtd"] = dtd;
275 if (mode == "CHARDATA") {
276 mode = ""
277 if (accu != "")
278 XMLEVENT = "CHARDATA"
279 XMLNAME = ""
280 $0 = accu
281 }
282 if (XMLEVENT) {
283 if (XMLEVENT == "STARTELEM") {
284 # Copy attributes into $0.
285 NF=0
286 for (ex in XMLATTR) {
287 NF ++
288 $NF = ex
289 }
290 }
291 return 1
292 }
293 close(file);
294 delete _XMLIO[file, "S0"];
295 delete _XMLIO[file, "line"];
296 delete _XMLIO[file, "path"];
297 delete _XMLIO[file, "depth"];
298 delete _XMLIO[file, "dtd"];
299 if (XMLERROR)
300 XMLERROR = file ":" XMLROW": " XMLERROR
301 else if (mode) XMLERROR=file ":" mline ": " "unterminated " mode
302 else if (XMLPATH) XMLERROR=file ":" XMLROW": " "unclosed tag(s) " XMLPATH
303 } # func. getXMLEVENT
304
305 # unescape data and attribute values, used by getXMLEVENT
306 function unescapeXML(text) {
307 gsub( "&apos;", "'", text )
308 gsub( "&quot;", "\"", text )
309 gsub( "&gt;", ">", text )
310 gsub( "&lt;", "<", text )
311 gsub( "&amp;", "\\&", text)
312 return text
313 }
314
315 # close xml file
316 function closeXMLEVENT(file) {
317 close(file);
318 delete _XMLIO[file,"S0"]
319 delete _XMLIO[file,"line"]
320 delete _XMLIO[file,"path"];
321 delete _XMLIO[file,"depth"];
322 delete _XMLIO[file,"dtd"]
323 delete _XMLIO[file,"open"]
324 delete _XMLIO[file,"IND"]
325 }