MCPcopy Create free account
hub / github.com/XmirrorSecurity/OpenSCA-cli / EncodeToken

Method EncodeToken

opensca/sca/java/xml/marshal.go:210–263  ·  view source on GitHub ↗

EncodeToken writes the given XML token to the stream. It returns an error if StartElement and EndElement tokens are not properly matched. EncodeToken does not call Flush, because usually it is part of a larger operation such as Encode or EncodeElement (or a custom Marshaler's MarshalXML invoked dur

(t Token)

Source from the content-addressed store, hash-verified

208// EncodeToken allows writing a ProcInst with Target set to "xml" only as the first token
209// in the stream.
210func (enc *Encoder) EncodeToken(t Token) error {
211
212 p := &enc.p
213 switch t := t.(type) {
214 case StartElement:
215 if err := p.writeStart(&t); err != nil {
216 return err
217 }
218 case EndElement:
219 if err := p.writeEnd(t.Name); err != nil {
220 return err
221 }
222 case CharData:
223 escapeText(p, t, false)
224 case Comment:
225 if bytes.Contains(t, endComment) {
226 return fmt.Errorf("xml: EncodeToken of Comment containing --> marker")
227 }
228 p.WriteString("<!--")
229 p.Write(t)
230 p.WriteString("-->")
231 return p.cachedWriteError()
232 case ProcInst:
233 // First token to be encoded which is also a ProcInst with target of xml
234 // is the xml declaration. The only ProcInst where target of xml is allowed.
235 if t.Target == "xml" && p.w.Buffered() != 0 {
236 return fmt.Errorf("xml: EncodeToken of ProcInst xml target only valid for xml declaration, first token encoded")
237 }
238 if !isNameString(t.Target) {
239 return fmt.Errorf("xml: EncodeToken of ProcInst with invalid Target")
240 }
241 if bytes.Contains(t.Inst, endProcInst) {
242 return fmt.Errorf("xml: EncodeToken of ProcInst containing ?> marker")
243 }
244 p.WriteString("<?")
245 p.WriteString(t.Target)
246 if len(t.Inst) > 0 {
247 p.WriteByte(' ')
248 p.Write(t.Inst)
249 }
250 p.WriteString("?>")
251 case Directive:
252 if !isValidDirective(t) {
253 return fmt.Errorf("xml: EncodeToken of Directive containing wrong < or > markers")
254 }
255 p.WriteString("<!")
256 p.Write(t)
257 p.WriteString(">")
258 default:
259 return fmt.Errorf("xml: EncodeToken of invalid token type")
260
261 }
262 return p.cachedWriteError()
263}
264
265// isValidDirective reports whether dir is a valid directive text,
266// meaning angle brackets are matched, ignoring comments and strings.

Callers

nothing calls this directly

Calls 9

escapeTextFunction · 0.85
isNameStringFunction · 0.85
isValidDirectiveFunction · 0.85
writeStartMethod · 0.80
writeEndMethod · 0.80
WriteStringMethod · 0.80
WriteMethod · 0.80
cachedWriteErrorMethod · 0.80
WriteByteMethod · 0.80

Tested by

no test coverage detected