src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

element.go (2174B)


      1 // Copyright 2009 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // Copied and modified from Go 1.14 stdlib's encoding/xml
      6 
      7 package xml
      8 
      9 // A Name represents an XML name (Local) annotated
     10 // with a name space identifier (Space).
     11 // In tokens returned by Decoder.Token, the Space identifier
     12 // is given as a canonical URL, not the short prefix used
     13 // in the document being parsed.
     14 type Name struct {
     15 	Space, Local string
     16 }
     17 
     18 // An Attr represents an attribute in an XML element (Name=Value).
     19 type Attr struct {
     20 	Name  Name
     21 	Value string
     22 }
     23 
     24 /*
     25 NewAttribute returns a pointer to an attribute.
     26 It takes in a local name aka attribute name, and value
     27 representing the attribute value.
     28 */
     29 func NewAttribute(local, value string) Attr {
     30 	return Attr{
     31 		Name: Name{
     32 			Local: local,
     33 		},
     34 		Value: value,
     35 	}
     36 }
     37 
     38 /*
     39 NewNamespaceAttribute returns a pointer to an attribute.
     40 It takes in a local name aka attribute name, and value
     41 representing the attribute value.
     42 
     43 NewNamespaceAttribute appends `xmlns:` in front of namespace
     44 prefix.
     45 
     46 For creating a name space attribute representing
     47 `xmlns:prefix="http://example.com`, the breakdown would be:
     48 local = "prefix"
     49 value = "http://example.com"
     50 */
     51 func NewNamespaceAttribute(local, value string) Attr {
     52 	attr := NewAttribute(local, value)
     53 
     54 	// default name space identifier
     55 	attr.Name.Space = "xmlns"
     56 	return attr
     57 }
     58 
     59 // A StartElement represents an XML start element.
     60 type StartElement struct {
     61 	Name Name
     62 	Attr []Attr
     63 }
     64 
     65 // Copy creates a new copy of StartElement.
     66 func (e StartElement) Copy() StartElement {
     67 	attrs := make([]Attr, len(e.Attr))
     68 	copy(attrs, e.Attr)
     69 	e.Attr = attrs
     70 	return e
     71 }
     72 
     73 // End returns the corresponding XML end element.
     74 func (e StartElement) End() EndElement {
     75 	return EndElement{e.Name}
     76 }
     77 
     78 // returns true if start element local name is empty
     79 func (e StartElement) isZero() bool {
     80 	return len(e.Name.Local) == 0
     81 }
     82 
     83 // An EndElement represents an XML end element.
     84 type EndElement struct {
     85 	Name Name
     86 }
     87 
     88 // returns true if end element local name is empty
     89 func (e EndElement) isZero() bool {
     90 	return len(e.Name.Local) == 0
     91 }