func getReferencedXML(reference *etree.Element, inputDoc *etree.Document) (outputDoc *etree.Document, err error) { uri := reference.SelectAttrValue("URI", "") uri = strings.Replace(uri, "#", "", 1) // populate doc with the referenced xml from the Reference URI if uri == "" { outputDoc = inputDoc } else { path := fmt.Sprintf(".//[@ID='%s']", uri) e := inputDoc.FindElement(path) if e != nil { outputDoc = etree.NewDocument() outputDoc.SetRoot(e.Copy()) } else { // SAML v1.1 Assertions use AssertionID path := fmt.Sprintf(".//[@AssertionID='%s']", uri) e := inputDoc.FindElement(path) if e != nil { outputDoc = etree.NewDocument() outputDoc.SetRoot(e.Copy()) } } } if outputDoc == nil { return nil, errors.New("signedxml: unable to find refereced xml") } return outputDoc, nil }
func TestLibirtPluginNet(t *testing.T) { Convey("List interfaces", t, func() { buf, err := ioutil.ReadFile("./test_domain.xml") if err != nil { panic(err) } domXMLStr := string(buf) domXML := etree.NewDocument() domXML.ReadFromString(domXMLStr) data := listInterfaces(domXML) So(data, ShouldResemble, []string{"tap88709cbd-90"}) }) Convey("List interfaces with no net interface", t, func() { buf, err := ioutil.ReadFile("./test_domain_2.xml") if err != nil { panic(err) } domXMLStr := string(buf) domXML := etree.NewDocument() domXML.ReadFromString(domXMLStr) data := listInterfaces(domXML) So(data, ShouldResemble, []string{}) }) }
func TestLibirtPluginDisk(t *testing.T) { Convey("List disks", t, func() { buf, err := ioutil.ReadFile("./test_domain.xml") if err != nil { panic(err) } domXMLStr := string(buf) domXML := etree.NewDocument() domXML.ReadFromString(domXMLStr) data := listDisks(domXML) So(data, ShouldResemble, []string{"vda"}) }) Convey("List disks with no disk device", t, func() { buf, err := ioutil.ReadFile("./test_domain_2.xml") if err != nil { panic(err) } domXMLStr := string(buf) domXML := etree.NewDocument() domXML.ReadFromString(domXMLStr) data := listDisks(domXML) So(data, ShouldResemble, []string{}) }) }
func ExamplePath() { doc := etree.NewDocument() doc.ReadFromString(xml) for _, e := range doc.FindElements(".//book[author='Charles Dickens']") { book := etree.NewDocument() book.SetRoot(e.Copy()) book.Indent(2) book.WriteTo(os.Stdout) } // Output: // <book> // <title>Great Expectations</title> // <author>Charles Dickens</author> // </book> }
// Create an etree Document, add XML entities to it, and serialize it // to stdout. func ExampleDocument_creating() { doc := etree.NewDocument() doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`) people := doc.CreateElement("People") people.CreateComment("These are all known people") jon := people.CreateElement("Person") jon.CreateAttr("name", "Jon O'Reilly") sally := people.CreateElement("Person") sally.CreateAttr("name", "Sally") doc.Indent(2) doc.WriteTo(os.Stdout) // Output: // <?xml version="1.0" encoding="UTF-8"?> // <?xml-stylesheet type="text/xsl" href="style.xsl"?> // <People> // <!--These are all known people--> // <Person name="Jon O'Reilly"/> // <Person name="Sally"/> // </People> }
func (v *Validator) validateSignature() error { doc := etree.NewDocument() doc.SetRoot(v.signedInfo.Copy()) signedInfo, err := doc.WriteToString() if err != nil { return err } canonSignedInfo, err := v.canonAlgorithm.Process(signedInfo, "") if err != nil { return err } b64, err := base64.StdEncoding.DecodeString(v.sigValue) if err != nil { return err } sig := []byte(b64) v.signingCert = x509.Certificate{} for _, cert := range v.Certificates { err := cert.CheckSignature(v.sigAlgorithm, []byte(canonSignedInfo), sig) if err == nil { v.signingCert = cert return nil } } return errors.New("signedxml: Calculated signature does not match the " + "SignatureValue provided") }
// XML is building xml. func (su *sitemapURL) XML() []byte { doc := etree.NewDocument() url := doc.CreateElement("url") SetBuilderElementValue(url, su.data.URLJoinBy("loc", "host", "loc"), "loc") SetBuilderElementValue(url, su.data, "expires") SetBuilderElementValue(url, su.data, "mobile") if !SetBuilderElementValue(url, su.data, "changefreq") { changefreq := url.CreateElement("changefreq") changefreq.SetText("weekly") } if !SetBuilderElementValue(url, su.data, "priority") { priority := url.CreateElement("priority") priority.SetText("0.5") } if !SetBuilderElementValue(url, su.data, "lastmod") { lastmod := url.CreateElement("lastmod") lastmod.SetText(time.Now().Format(time.RFC3339)) } SetBuilderElementValue(url, su.data, "news") SetBuilderElementValue(url, su.data, "image") SetBuilderElementValue(url, su.data, "video") SetBuilderElementValue(url, su.data, "geo") buf := &bytes.Buffer{} // doc.Indent(2) doc.WriteTo(buf) return buf.Bytes() }
func ExampleAddElement() { docR := readXml(bookstore) root := docR.SelectElement("bookstore") docA := etree.NewDocument() docA.ReadFromString(xml) docA.Indent(2) docA.WriteTo(os.Stdout) // To directly add docA under root //root.AddElement(docA.Root()) // To add all book nodes under docA to root for _, e := range docA.FindElements(".//book") { // add (e) under root root.AddElement(e) } fmt.Println() docR.Indent(2) docR.WriteTo(os.Stdout) fmt.Println() docA.Indent(2) docA.WriteTo(os.Stdout) }
func NodeSearch(filename string) { doc := etree.NewDocument() if err := doc.ReadFromFile(filename); err != nil { panic(err) } aut := Person{Fname: "ПЕТР", Mname: "ПЕТРОВИЧ", Lname: "ПЕТУХОВ"} root := doc.FindElement("./FictionBook/description/title-info") fmt.Println("ROOT element:", root.Tag) aut.CreateXml(root) // doc := xmlx.New() fo, err := os.Create("output.fb2") if err != nil { panic(err) } doc.IndentTabs() doc.WriteTo(fo) // if err := doc.LoadFile(filename, nil); err != nil { // fmt.Printf("LoadFile(): %s", err) // return // } // node := doc.SelectNode("http://www.gribuser.ru/xml/fictionbook/2.0", "middle-name") // if node == nil { // fmt.Printf("SelectNode(): No node found.") // return // } // // addTo := node.Parent // fmt.Printf("%v\n", node.Type) }
func TestMobileSitemaps(t *testing.T) { doc := etree.NewDocument() root := doc.CreateElement("root") data := URL{"loc": "/mobile", "mobile": true} expect := []byte(` <root> <loc>/mobile</loc> <mobile:mobile/> </root>`) SetBuilderElementValue(root, data.URLJoinBy("loc", "host", "loc"), "loc") SetBuilderElementValue(root, data, "mobile") buf := &bytes.Buffer{} doc.WriteTo(buf) mdata, _ := mxj.NewMapXml(buf.Bytes()) mexpect, _ := mxj.NewMapXml(expect) if !reflect.DeepEqual(mdata, mexpect) { t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) } }
func TestImageSitemaps(t *testing.T) { doc := etree.NewDocument() root := doc.CreateElement("root") data := URL{"loc": "/images", "image": []URL{ {"loc": "http://www.example.com/image.png", "title": "Image"}, {"loc": "http://www.example.com/image1.png", "title": "Image1"}, }} expect := []byte(` <root> <image:image> <image:loc>http://www.example.com/image.png</image:loc> <image:title>Image</image:title> </image:image> <image:image> <image:loc>http://www.example.com/image1.png</image:loc> <image:title>Image1</image:title> </image:image> </root>`) SetBuilderElementValue(root, data, "image") buf := &bytes.Buffer{} doc.WriteTo(buf) mdata, _ := mxj.NewMapXml(buf.Bytes()) mexpect, _ := mxj.NewMapXml(expect) if !reflect.DeepEqual(mdata, mexpect) { t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) } }
func init() { log.SetLevel(log.DebugLevel) // do a deep copy for etree of job config.xml JobConfig = etree.NewDocument() if err := JobConfig.ReadFromFile(BaseCfg); err != nil { log.Errorf(err.Error()) return } /* // connect mongodb MgoDB = getMongoDB() if MgoDB == nil { return } */ // session uses memory GlobalSessions, _ = session.NewManager( "memory", `{"cookieName":"sessionId","enableSetCookie":true,"gclifetime":30,"ProviderConfig":"{\"cookieName\":\"sessionId\",\"securityKey\":\"beegocookiehashkey\"}"}`) go GlobalSessions.GC() JenkinsClient = make(map[string]*gojenkins.Jenkins) /* for { JenkinsClient = getJenkinsClient() if JenkinsClient == nil { time.Sleep(10) continue } else { break } } */ }
func main() { doc := etree.NewDocument() if err := doc.ReadFromFile("et_dump.xml"); err != nil { panic(err) } doc.Indent(2) doc.WriteTo(os.Stdout) }
// NewValidator returns a *Validator for the XML provided func NewValidator(xml string) (*Validator, error) { doc := etree.NewDocument() err := doc.ReadFromString(xml) if err != nil { return nil, err } v := &Validator{signatureData: signatureData{xml: doc}} return v, nil }
// NewSigner returns a *Signer for the XML provided func NewSigner(xml string) (*Signer, error) { doc := etree.NewDocument() err := doc.ReadFromString(xml) if err != nil { return nil, err } s := &Signer{signatureData: signatureData{xml: doc}} return s, nil }
func (c *Config) getIDPEndpointFromMetadata(body string) string { doc := etree.NewDocument() err := doc.ReadFromString(body) if err != nil { panic(err) } endpointElement := doc.FindElement("//PassiveRequestorEndpoint/EndpointReference/Address[1]") if endpointElement == nil { panic(errors.New("go-wsfed: unable to find Passive Requestor Endpoint in metadata")) } return endpointElement.Text() }
func processTransform(transform *etree.Element, docIn *etree.Document) (docOut *etree.Document, err error) { transformAlgoURI := transform.SelectAttrValue("Algorithm", "") if transformAlgoURI == "" { return nil, errors.New("signedxml: unable to find Algorithm in Transform") } transformAlgo, ok := CanonicalizationAlgorithms[transformAlgoURI] if !ok { return nil, fmt.Errorf("signedxml: unable to find matching transform"+ "algorithm for %s in CanonicalizationAlgorithms", transformAlgoURI) } var transformContent string if transform.ChildElements() != nil { tDoc := etree.NewDocument() tDoc.SetRoot(transform.Copy()) transformContent, err = tDoc.WriteToString() if err != nil { return nil, err } } docString, err := docIn.WriteToString() if err != nil { return nil, err } docString, err = transformAlgo.Process(docString, transformContent) if err != nil { return nil, err } docOut = etree.NewDocument() docOut.ReadFromString(docString) return docOut, nil }
func (e *ExclusiveCanonicalization) loadPrefixList(transformXML string) { if transformXML != "" { tDoc := etree.NewDocument() tDoc.ReadFromString(transformXML) inclNSNode := tDoc.Root().SelectElement("InclusiveNamespaces") if inclNSNode != nil { prefixList := inclNSNode.SelectAttrValue("PrefixList", "") if prefixList != "" { e.inclusiveNamespacePrefixList = strings.Split(prefixList, " ") } } } }
func (s *Signer) setSignature() error { doc := etree.NewDocument() doc.SetRoot(s.signedInfo.Copy()) signedInfo, err := doc.WriteToString() if err != nil { return err } canonSignedInfo, err := s.canonAlgorithm.Process(signedInfo, "") if err != nil { return err } var hashed, signature []byte //var h1, h2 *big.Int signingAlgorithm, ok := signingAlgorithms[s.sigAlgorithm] if !ok { return errors.New("signedxml: unsupported algorithm") } hasher := signingAlgorithm.hash.New() hasher.Write([]byte(canonSignedInfo)) hashed = hasher.Sum(nil) switch signingAlgorithm.algorithm { case "rsa": signature, err = rsa.SignPKCS1v15(rand.Reader, s.privateKey.(*rsa.PrivateKey), signingAlgorithm.hash, hashed) /* case "dsa": h1, h2, err = dsa.Sign(rand.Reader, s.privateKey.(*dsa.PrivateKey), hashed) case "ecdsa": h1, h2, err = ecdsa.Sign(rand.Reader, s.privateKey.(*ecdsa.PrivateKey), hashed) */ } if err != nil { return err } // DSA and ECDSA has not been validated /* if signature == nil && h1 != nil && h2 != nil { signature = append(h1.Bytes(), h2.Bytes()...) } */ b64 := base64.StdEncoding.EncodeToString(signature) sigValueElement := s.signature.SelectElement("SignatureValue") sigValueElement.SetText(b64) return nil }
func TestJustSetLocElementAndThenItNeedsCompleteValues(t *testing.T) { smu, err := NewSitemapURL(URL{"loc": "path", "host": "http://example.com"}) if err != nil { t.Fatalf(`Fatal to validate! This is a critical error: %s`, err) } doc := etree.NewDocument() doc.ReadFromBytes(smu.XML()) var elm *etree.Element url := doc.SelectElement("url") elm = url.SelectElement("loc") if elm == nil { t.Errorf(`Failed to generate xml that loc element is blank: %s`, elm) } if elm != nil && elm.Text() != "http://example.com/path" { t.Errorf(`Failed to generate xml thats deferrent value in loc element: %s`, elm.Text()) } elm = url.SelectElement("priority") if elm == nil { t.Errorf(`Failed to generate xml that priority element is nil: %s`, elm) } if elm != nil && elm.Text() != "0.5" { t.Errorf(`Failed to generate xml thats deferrent value in priority element: %s`, elm.Text()) } elm = url.SelectElement("changefreq") if elm == nil { t.Errorf(`Failed to generate xml that changefreq element is nil: %s`, elm) } if elm != nil && elm.Text() != "weekly" { t.Errorf(`Failed to generate xml thats deferrent value in changefreq element: %s`, elm.Text()) } elm = url.SelectElement("lastmod") if elm == nil { t.Errorf(`Failed to generate xml that lastmod element is nil: %s`, elm) } if elm != nil { if _, err := time.Parse(time.RFC3339, elm.Text()); err != nil { t.Errorf(`Failed to generate xml thats failed to parse datetime in lastmod element: %s`, err) } } }
// XML and sitemapIndexURL.XML are almost the same behavior. func (su *sitemapIndexURL) XML() []byte { doc := etree.NewDocument() sitemap := doc.CreateElement("sitemap") SetBuilderElementValue(sitemap, su.data, "loc") if !SetBuilderElementValue(sitemap, su.data, "lastmod") { lastmod := sitemap.CreateElement("lastmod") lastmod.SetText(time.Now().Format(time.RFC3339)) } buf := &bytes.Buffer{} // doc.Indent(2) doc.WriteTo(buf) return buf.Bytes() }
func ExamplePath() { xml := `<bookstore><book><title>Great Expectations</title> <author>Charles Dickens</author></book><book><title>Ulysses</title> <author>James Joyce</author></book></bookstore>` doc := etree.NewDocument() doc.ReadFromString(xml) for _, e := range doc.FindElements(".//book[author='Charles Dickens']") { book := etree.CreateDocument(e) book.Indent(2) book.WriteTo(os.Stdout) } // Output: // <book> // <title>Great Expectations</title> // <author>Charles Dickens</author> // </book> }
func TestAttrWithoutTypedef(t *testing.T) { doc := etree.NewDocument() root := doc.CreateElement("root") data := URL{"loc": "/videos", "video": URL{ "thumbnail_loc": "http://www.example.com/video1_thumbnail.png", "title": "Title", "description": "Description", "content_loc": "http://www.example.com/cool_video.mpg", "category": "Category", "tag": []string{"one", "two", "three"}, "player_loc": Attrs{"https://f.vimeocdn.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}}, }} expect := []byte(` <root> <video:video> <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> <video:title>Title</video:title> <video:description>Description</video:description> <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> <video:tag>one</video:tag> <video:tag>two</video:tag> <video:tag>three</video:tag> <video:category>Category</video:category> <video:player_loc allow_embed="Yes" autoplay="autoplay=1">https://f.vimeocdn.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26</video:player_loc> </video:video> </root>`) SetBuilderElementValue(root, data, "video") buf := &bytes.Buffer{} // doc.Indent(2) doc.WriteTo(buf) mdata, _ := mxj.NewMapXml(buf.Bytes()) mexpect, _ := mxj.NewMapXml(expect) // print(string(buf.Bytes())) if !reflect.DeepEqual(mdata, mexpect) { t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) } }
func TestNewsSitemaps(t *testing.T) { doc := etree.NewDocument() root := doc.CreateElement("root") data := URL{"loc": "/news", "news": URL{ "publication": URL{ "name": "Example", "language": "en", }, "title": "My Article", "keywords": "my article, articles about myself", "stock_tickers": "SAO:PETR3", "publication_date": "2011-08-22", "access": "Subscription", "genres": "PressRelease", }} expect := []byte(` <root> <news:news> <news:keywords>my article, articles about myself</news:keywords> <news:stock_tickers>SAO:PETR3</news:stock_tickers> <news:publication_date>2011-08-22</news:publication_date> <news:access>Subscription</news:access> <news:genres>PressRelease</news:genres> <news:publication> <news:name>Example</news:name> <news:language>en</news:language> </news:publication> <news:title>My Article</news:title> </news:news> </root>`) SetBuilderElementValue(root, data, "news") buf := &bytes.Buffer{} doc.WriteTo(buf) mdata, _ := mxj.NewMapXml(buf.Bytes()) mexpect, _ := mxj.NewMapXml(expect) if !reflect.DeepEqual(mdata, mexpect) { t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) } }
func TestJustSetLocElement(t *testing.T) { smu, err := NewSitemapURL(URL{"loc": "path", "host": "http://example.com"}) if err != nil { t.Fatalf(`Fatal to validate! This is a critical error: %s`, err) } doc := etree.NewDocument() doc.ReadFromBytes(smu.XML()) var elm *etree.Element url := doc.SelectElement("url") elm = url.SelectElement("loc") if elm == nil { t.Errorf(`Failed to generate xml that loc element is blank: %s`, elm) } if elm != nil && elm.Text() != "http://example.com/path" { t.Errorf(`Failed to generate xml thats deferrent value in loc element: %s`, elm.Text()) } }
func DemoRemoveElement() { // doc := readXml(xml) // for _, e := range doc.FindElements(".//book") { doc := readXml(bookstore) for _, e := range doc.FindElements(".//book[@category='WEB']") { // FindElement returns the first element matched by the XPath string a := e.FindElement(".//author") // RemoveElement removes the given child element e.RemoveElement(a) an := e.CreateElement("author") an.CreateComment("removed") book := etree.NewDocument() book.SetRoot(e.Copy()) book.Indent(2) book.WriteTo(os.Stdout) } }
// XML and sitemapIndexURL.XML are almost the same behavior. func (su *sitemapIndexURL) XML() []byte { doc := etree.NewDocument() sitemap := doc.CreateElement("sitemap") SetBuilderElementValue(sitemap, su.data, "loc") if _, ok := SetBuilderElementValue(sitemap, su.data, "lastmod"); !ok { lastmod := sitemap.CreateElement("lastmod") lastmod.SetText(time.Now().Format(time.RFC3339)) } if su.opts.pretty { doc.Indent(2) } buf := poolBuffer.Get() doc.WriteTo(buf) bytes := buf.Bytes() poolBuffer.Put(buf) return bytes }
// Process is called to transfrom the XML using the ExclusiveCanonicalization // algorithm func (e ExclusiveCanonicalization) Process(inputXML string, transformXML string) (outputXML string, err error) { e.namespaces = make(map[string]string) doc := etree.NewDocument() doc.WriteSettings.CanonicalEndTags = true doc.WriteSettings.CanonicalText = true doc.WriteSettings.CanonicalAttrVal = true err = doc.ReadFromString(inputXML) if err != nil { return "", err } e.loadPrefixList(transformXML) e.processDocLevelNodes(doc) e.processRecursive(doc.Root(), nil, "") outputXML, err = doc.WriteToString() return outputXML, err }
func TestVideoSitemaps(t *testing.T) { doc := etree.NewDocument() root := doc.CreateElement("root") data := URL{"loc": "/videos", "video": URL{ "thumbnail_loc": "http://www.example.com/video1_thumbnail.png", "title": "Title", "description": "Description", "content_loc": "http://www.example.com/cool_video.mpg", "category": "Category", "tag": []string{"one", "two", "three"}, }} expect := []byte(` <root> <video:video> <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> <video:title>Title</video:title> <video:description>Description</video:description> <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> <video:tag>one</video:tag> <video:tag>two</video:tag> <video:tag>three</video:tag> <video:category>Category</video:category> </video:video> </root>`) SetBuilderElementValue(root, data, "video") buf := &bytes.Buffer{} doc.WriteTo(buf) mdata, _ := mxj.NewMapXml(buf.Bytes()) mexpect, _ := mxj.NewMapXml(expect) if !reflect.DeepEqual(mdata, mexpect) { t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) } }
// Process is called to transfrom the XML using the EnvelopedSignature // algorithm func (e EnvelopedSignature) Process(inputXML string, transformXML string) (outputXML string, err error) { doc := etree.NewDocument() doc.ReadFromString(inputXML) sig := doc.FindElement(".//Signature") if sig == nil { return "", errors.New("signedxml: unable to find Signature node") } sigParent := sig.Parent() elem := sigParent.RemoveChild(sig) if elem == nil { return "", errors.New("signedxml: unable to remove Signature element") } docString, err := doc.WriteToString() if err != nil { return "", err } //logger.Println(docString) return docString, nil }