github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/old/camwebdav/xml.go (about) 1 // +build THIS_IS_BROKEN 2 3 package main 4 5 import ( 6 "encoding/xml" 7 "io" 8 "log" 9 "net/http" 10 ) 11 12 func parsexml(r io.Reader) *xmlparser { 13 x := &xmlparser{p: xml.NewDecoder(r)} 14 x.next() 15 return x 16 } 17 18 type xmlparser struct { 19 p *xml.Decoder 20 cur xml.Token 21 } 22 23 // next moves to the next token, 24 // skipping anything that is not an element 25 // in the DAV: namespace 26 func (x *xmlparser) next() xml.Token { 27 var err error 28 for { 29 x.cur, err = x.p.Token() 30 if err == io.EOF { 31 return x.cur 32 } else if err != nil { 33 panic(sendHTTPStatus(http.StatusBadRequest)) 34 } 35 switch tok := x.cur.(type) { 36 case xml.StartElement: 37 if tok.Name.Space != "DAV:" { 38 err = x.p.Skip() 39 if err != nil && err != io.EOF { 40 panic(sendHTTPStatus(http.StatusBadRequest)) 41 } 42 } else { 43 return x.cur 44 } 45 case xml.EndElement: 46 return x.cur 47 } 48 } 49 panic("unreachable") 50 } 51 52 func (x *xmlparser) start(name string) bool { 53 el, ok := x.cur.(xml.StartElement) 54 if !ok || el.Name.Local != name { 55 return false 56 } 57 x.next() 58 return true 59 } 60 61 func (x *xmlparser) muststart(name string) { 62 if !x.start(name) { 63 log.Printf("expected start element %q", name) 64 panic(sendHTTPStatus(http.StatusBadRequest)) 65 } 66 } 67 68 func (x *xmlparser) end(name string) bool { 69 if _, ok := x.cur.(xml.EndElement); !ok { 70 return false 71 } 72 x.next() 73 return true 74 } 75 76 func (x *xmlparser) mustend(name string) { 77 if !x.end(name) { 78 log.Printf("expected end element %q", name) 79 panic(sendHTTPStatus(http.StatusBadRequest)) 80 } 81 }