Debian Codegraph @

eutils/valid.go

raw
1 // ===========================================================================
2 //
3 // PUBLIC DOMAIN NOTICE
4 // National Center for Biotechnology Information (NCBI)
5 //
6 // This software/database is a "United States Government Work" under the
7 // terms of the United States Copyright Act. It was written as part of
8 // the author's official duties as a United States Government employee and
9 // thus cannot be copyrighted. This software/database is freely available
10 // to the public for use. The National Library of Medicine and the U.S.
11 // Government do not place any restriction on its use or reproduction.
12 // We would, however, appreciate having the NCBI and the author cited in
13 // any work or product based on this material.
14 //
15 // Although all reasonable efforts have been taken to ensure the accuracy
16 // and reliability of the software and data, the NLM and the U.S.
17 // Government do not and cannot warrant the performance or results that
18 // may be obtained by using this software or data. The NLM and the U.S.
19 // Government disclaim all warranties, express or implied, including
20 // warranties of performance, merchantability or fitness for any particular
21 // purpose.
22 //
23 // ===========================================================================
24 //
25 // File Name: valid.go
26 //
27 // Author: Jonathan Kans
28 //
29 // ==========================================================================
30
31 package eutils
32
33 import (
34 "fmt"
35 "os"
36 "strings"
37 )
38
39 // ValidateXML checks for well-formed XML
40 func ValidateXML(rdr <-chan XMLBlock, fnd string, html bool, max int) int {
41
42 if rdr == nil {
43 return 0
44 }
45
46 countLines = true
47
48 tknq := CreateTokenizer(rdr)
49
50 if tknq == nil {
51 DisplayError("Unable to create validator tokenizer")
52 os.Exit(1)
53 }
54
55 find := ParseIndex(fnd)
56
57 // report unexpectedly large maximum nesting depth (undocumented)
58 maxDepth := 0
59 depthLine := 0
60 depthID := ""
61 maxLine := 0
62
63 // warn if HTML tags are not well-formed
64 unbalancedHTML := func(text string) bool {
65
66 var arry []string
67
68 idx := 0
69 txtlen := len(text)
70
71 inTag := false
72 start := 0
73 stop := -1
74 var last byte
75
76 for idx < txtlen {
77 ch := text[idx]
78 if ch == '<' {
79 if inTag {
80 return true
81 }
82 inTag = true
83 start = idx
84 stop = -1
85 } else if ch == ' ' || ch == '\n' {
86 // space before attributes marks end of tag
87 if stop < 0 {
88 stop = idx
89 }
90 } else if ch == '>' {
91 if !inTag {
92 return true
93 }
94 inTag = false
95 if stop < 0 {
96 stop = idx
97 }
98 curr := text[start+1 : stop]
99 if strings.HasPrefix(curr, "/") {
100 curr = curr[1:]
101 if len(arry) < 1 {
102 return true
103 }
104 prev := arry[len(arry)-1]
105 if curr != prev {
106 return true
107 }
108 arry = arry[:len(arry)-1]
109 } else if last == '/' {
110 // ignore self-closing tag
111 } else {
112 arry = append(arry, curr)
113 }
114 }
115 last = ch
116 idx++
117 }
118
119 if inTag {
120 return true
121 }
122
123 if len(arry) > 0 {
124 return true
125 }
126
127 return false
128 }
129
130 // warn if HTML tags are encoded
131 encodedHTML := func(str string) bool {
132
133 lookAhead := func(txt string, to int) string {
134
135 mx := len(txt)
136 if to > mx {
137 to = mx
138 }
139 pos := strings.Index(txt[:to], "gt;")
140 if pos > 0 {
141 to = pos + 3
142 }
143 return txt[:to]
144 }
145
146 for i, ch := range str {
147 if ch == '<' {
148 continue
149 } else if ch != '&' {
150 continue
151 } else if strings.HasPrefix(str[i:], "&lt;") {
152 sub := lookAhead(str[i:], 14)
153 _, ok := HTMLRepair(sub)
154 if ok {
155 return true
156 }
157 } else if strings.HasPrefix(str[i:], "&amp;lt;") {
158 sub := lookAhead(str[i:], 22)
159 _, ok := HTMLRepair(sub)
160 if ok {
161 return true
162 }
163 } else if strings.HasPrefix(str[i:], "&amp;amp;") {
164 return true
165 }
166 }
167
168 return false
169 }
170
171 currID := ""
172
173 // verifyLevel recursive definition
174 var verifyLevel func(string, string, int)
175
176 // verify integrity of XML object nesting (well-formed)
177 verifyLevel = func(parent, prev string, level int) {
178
179 status := START
180 for tkn := range tknq {
181
182 tag := tkn.Tag
183 name := tkn.Name
184 line := tkn.Line
185 maxLine = line
186
187 if level > maxDepth {
188 maxDepth = level
189 depthLine = line
190 depthID = currID
191 }
192
193 switch tag {
194 case STARTTAG:
195 if status == CHAR && !doMixed {
196 fmt.Fprintf(os.Stdout, "%s%8d\t<%s> not expected after contents\n", currID, line, name)
197 }
198 verifyLevel(name, parent, level+1)
199 // returns here after recursion
200 status = STOP
201 case SELFTAG:
202 status = OTHER
203 case STOPTAG:
204 if parent != name && parent != "" {
205 fmt.Fprintf(os.Stdout, "%s%8d\tExpected </%s>, found </%s>\n", currID, line, parent, name)
206 }
207 if level < 1 {
208 fmt.Fprintf(os.Stdout, "%s%8d\tUnexpected </%s> at end of XML\n", currID, line, name)
209 }
210 // break recursion
211 return
212 case CONTENTTAG:
213 // check for content index match
214 if find != nil && find.Index != "" {
215 if parent == find.Match || find.Match == "" {
216 if find.Parent == "" || prev == find.Parent {
217 currID = name + "\t"
218 }
219 }
220 }
221 if status != START && !doMixed {
222 fmt.Fprintf(os.Stdout, "%s%8d\tContents not expected before </%s>\n", currID, line, parent)
223 }
224 if allowEmbed {
225 if unbalancedHTML(name) {
226 fmt.Fprintf(os.Stdout, "%s%8d\tUnbalanced mixed-content tags in <%s>\n", currID, line, parent)
227 }
228 if html && encodedHTML(name) {
229 fmt.Fprintf(os.Stdout, "%s%8d\tEncoded mixed-content markup in <%s>\n", currID, line, parent)
230 }
231 }
232 status = CHAR
233 case CDATATAG, COMMENTTAG:
234 status = OTHER
235 case DOCTYPETAG:
236 case PROCESSTAG:
237 case NOTAG:
238 case ISCLOSED:
239 if level > 0 {
240 fmt.Fprintf(os.Stdout, "%s%8d\tUnexpected end of data\n", currID, line)
241 }
242 return
243 default:
244 status = OTHER
245 }
246 }
247 }
248
249 verifyLevel("", "", 0)
250
251 // raised maxDepth test to 100 because of PMC nesting depth of 97 levels
252 // in oa_comm_xml.PMC009xxxxxx.baseline.2022-12-18.tar.gz release file,
253 // from complex math formulae markup in PMC9439944
254 if max <= 0 || max > 10000 {
255 max = 100
256 }
257 if maxDepth > max {
258 fmt.Fprintf(os.Stdout, "%s%8d\tMaximum nesting, %d levels\n", depthID, depthLine, maxDepth)
259 }
260
261 return maxLine
262 }