Beispiel #1
0
func (s *stream) String() string {
	var buf bytes.Buffer
	buf.WriteString(`<stream:stream xmlns="`)
	buf.WriteString(NsClient)
	buf.WriteString(`" xmlns:stream="`)
	buf.WriteString(NsStream)
	buf.WriteString(`"`)
	if s.To != "" {
		buf.WriteString(` to="`)
		xml.Escape(&buf, []byte(s.To))
		buf.WriteString(`"`)
	}
	if s.From != "" {
		buf.WriteString(` from="`)
		xml.Escape(&buf, []byte(s.From))
		buf.WriteString(`"`)
	}
	if s.Id != "" {
		buf.WriteString(` id="`)
		xml.Escape(&buf, []byte(s.Id))
		buf.WriteString(`"`)
	}
	if s.Lang != "" {
		buf.WriteString(` xml:lang="`)
		xml.Escape(&buf, []byte(s.Lang))
		buf.WriteString(`"`)
	}
	if s.Version != "" {
		buf.WriteString(` version="`)
		xml.Escape(&buf, []byte(s.Version))
		buf.WriteString(`"`)
	}
	buf.WriteString(">")
	return buf.String()
}
Beispiel #2
0
/*
映射端口

POST /ipc HTTP/1.1
HOST: 192.168.1.253:1900
Content-Type: text/xml; charset="utf-8"
Content-Length: 615
SOAPACTION: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>5678</NewExternalPort>
<NewProtocol>TCP</NewProtocol>
<NewInternalPort>3389</NewInternalPort>
<NewInternalClient>192.168.1.100</NewInternalClient>
<NewEnabled>1</NewEnabled>
<NewPortMappingDescription>xxxxxxxx</NewPortMappingDescription>
<NewLeaseDuration>0</NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>
*/
func (this *UPnPService) Link(v *AddPortMapping) (err error) {
	server_url := "http://" + this.ServerHost + "/ipc"
	util.DEBUG.Logf("linking %s : %+v", server_url, v)
	buf := bytes.NewBufferString("<?xml version=\"1.0\"?>\r\n")
	buf.WriteString("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n")
	buf.WriteString("<s:Body>\r\n")
	buf.WriteString("<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">\r\n")
	buf.WriteString("<NewRemoteHost>")
	xml.Escape(buf, []byte(v.NewRemoteHost))
	buf.WriteString("</NewRemoteHost>\r\n")
	buf.WriteString("<NewExternalPort>" + util.ToString(v.NewExternalPort) + "</NewExternalPort>\r\n")
	buf.WriteString("<NewProtocol>")
	xml.Escape(buf, []byte(v.NewProtocol))
	buf.WriteString("</NewProtocol>\r\n")
	buf.WriteString("<NewInternalPort>" + util.ToString(v.NewInternalPort) + "</NewInternalPort>\r\n")
	buf.WriteString("<NewInternalClient>")
	xml.Escape(buf, []byte(v.NewInternalClient))
	buf.WriteString("</NewInternalClient>\r\n")
	buf.WriteString("<NewEnabled>" + util.ToString(v.NewEnabled) + "</NewEnabled>\r\n")
	buf.WriteString("<NewPortMappingDescription>")
	xml.Escape(buf, []byte(v.NewPortMappingDescription))
	buf.WriteString("</NewPortMappingDescription>\r\n")
	buf.WriteString("<NewLeaseDuration>" + util.ToString(v.NewLeaseDuration) + "</NewLeaseDuration>\r\n")
	buf.WriteString("</u:AddPortMapping>\r\n</s:Body>\r\n</s:Envelope>\r\n\r\n")
	res, err := http.Post(server_url, "text/xml", buf)
	if err != nil {
		return
	}
	defer res.Body.Close()
	b, err := ioutil.ReadAll(res.Body)
	util.DEBUG.Log("\n[response]\n", string(b))
	return
}
Beispiel #3
0
func (this *Element) Export(w io.Writer) {
	io.WriteString(w, "<")
	io.WriteString(w, this.Tag)
	keys := make([]string, 0, len(this.attributes))
	for k, _ := range this.attributes {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
		v := this.attributes[k]
		io.WriteString(w, " ")
		xml.Escape(w, []byte(k))
		io.WriteString(w, "=\"")
		xml.Escape(w, []byte(v))
		io.WriteString(w, "\"")
	}
	if this.IsSelfClosing() {
		io.WriteString(w, " />")
	} else {
		io.WriteString(w, ">")
		for _, c := range this.children {
			c.Export(w)
		}
		io.WriteString(w, "</")
		io.WriteString(w, this.Tag)
		io.WriteString(w, ">")
	}
}
Beispiel #4
0
func (b *blog) atomServer() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/atom+xml")

		fullArticles := make([]fullArticle, len(b.articles))
		for i, article := range b.articles {
			content, err := b.renderContent(article.Url)
			if err != nil {
				http.NotFound(w, r)
				return
			}
			var buf bytes.Buffer
			xml.Escape(&buf, []byte(content))

			fullArticles[i] = fullArticle{
				Article: article,
				Content: template.HTML(buf.String()),
			}
		}

		atom := &atomData{
			Articles: fullArticles,
		}
		sort.Sort(atom)
		atom.Updated = atom.Articles[0].Article.Date

		err := b.tmpl.ExecuteTemplate(w, "atom", atom)
		if err != nil {
			http.Error(w, err.Error(), 500)
		}
	}
}
Beispiel #5
0
// encodeMap encodes a map to an XML plist dict.
func (e *Encoder) encodeMap(dict map[string]interface{}) error {
	err := e.writeString("<dict>\n")
	if err != nil {
		return err
	}

	e.indentLevel++

	for k, v := range dict {
		_, err = e.bw.WriteString(e.indent() + "<key>")
		if err != nil {
			return err
		}
		xml.Escape(e.bw, []byte(k))
		_, err = e.bw.WriteString("</key>\n")
		if err != nil {
			return err
		}

		err = e.encodeAny(v)
		if err != nil {
			return err
		}
	}

	e.indentLevel--

	err = e.writeString("</dict>\n")
	if err != nil {
		return err
	}

	return nil
}
Beispiel #6
0
func escapeXML(input []byte) []byte {
	var buf *bytes.Buffer = bytes.NewBuffer(make([]byte, 0, len(input)+10))

	xml.Escape(buf, input)

	return buf.Bytes()
}
Beispiel #7
0
func encodeValue(val reflect.Value) ([]byte, error) {
	var b []byte
	var err error

	if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
		if val.IsNil() {
			return []byte("<value/>"), nil
		}

		val = val.Elem()
	}

	switch val.Kind() {
	case reflect.Struct:
		switch val.Interface().(type) {
		case time.Time:
			t := val.Interface().(time.Time)
			b = []byte(fmt.Sprintf("<dateTime.iso8601>%s</dateTime.iso8601>", t.Format(iso8601)))
		default:
			b, err = encodeStruct(val)
		}
	case reflect.Map:
		b, err = encodeMap(val)
	case reflect.Slice:
		b, err = encodeSlice(val)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		b = []byte(fmt.Sprintf("<int>%s</int>", strconv.FormatInt(val.Int(), 10)))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		b = []byte(fmt.Sprintf("<i4>%s</i4>", strconv.FormatUint(val.Uint(), 10)))
	case reflect.Float32, reflect.Float64:
		b = []byte(fmt.Sprintf("<double>%s</double>",
			strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits())))
	case reflect.Bool:
		if val.Bool() {
			b = []byte("<boolean>1</boolean>")
		} else {
			b = []byte("<boolean>0</boolean>")
		}
	case reflect.String:
		var buf bytes.Buffer

		xml.Escape(&buf, []byte(val.String()))

		if _, ok := val.Interface().(Base64); ok {
			b = []byte(fmt.Sprintf("<base64>%s</base64>", buf.String()))
		} else {
			b = []byte(fmt.Sprintf("<value><string>%s</string></value>", buf.String()))
		}
	default:
		return nil, fmt.Errorf("xmlrpc encode error: unsupported type")
	}

	if err != nil {
		return nil, err
	}

	return []byte(fmt.Sprintf("%s", string(b))), nil
}
Beispiel #8
0
func (SitemapPlugin) Exec(topCtx mustache.Context) {
	base_path := FromCtx(topCtx, "urls.base_path").(string)
	f, err := os.OpenFile("compiled"+base_path+"sitemap.xml", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
	if err != nil {
		log.Println("Error when create sitemap", err)
		return
	}
	defer f.Close()

	//自行拼接XML比官方的xml包还靠谱

	f.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)
	f.WriteString("\n")
	f.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`)
	f.WriteString("\n")

	production_url := FromCtx(topCtx, "site.config.production_url").(string)
	production_url_base := FromCtx(topCtx, "site.config.production_url_base").(string)

	f.WriteString("\t<url>\n")
	f.WriteString("\t\t<loc>")
	xml.Escape(f, []byte(production_url+"/")) //够弱智不? 竟然要传入一个io.Reader
	f.WriteString("</loc>\n")
	f.WriteString("\t</url>\n")

	post_ids := FromCtx(topCtx, "db.posts.chronological").([]string)
	posts := FromCtx(topCtx, "db.posts.dictionary").(map[string]Mapper)
	for _, id := range post_ids {
		f.WriteString("\t<url>\n")
		post := posts[id]
		f.WriteString("\t\t<loc>")
		xml.Escape(f, []byte(production_url_base))
		xml.Escape(f, []byte(post.Url()))
		f.WriteString("</loc>\n")
		f.WriteString(fmt.Sprintf("\t\t<lastmod>%s</lastmod>\n", post["date"])) // 是否应该抹除呢? 考虑中
		f.WriteString("\t\t<changefreq>weekly</changefreq>\n")
		f.WriteString("\t</url>\n")
	}

	f.WriteString(`</urlset>`)
	f.Sync()
	// ~_~ 大功告成!
}
Beispiel #9
0
func (d *Decoder) escapeString(Value string) string {
	var b *bytes.Buffer = bytes.NewBuffer([]byte{})
	var writer io.Writer = b
	var result string

	xml.Escape(writer, []byte(Value))
	result, _ = b.ReadString(0x00)

	return result
}
Beispiel #10
0
func (SitemapPlugin) Exec(public string, topCtx mustache.Context) {
	f, err := os.OpenFile(filepath.Join(public, "/sitemap.xml"), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, DEFAULT_FILE_MODE)
	if err != nil {
		Log(ERROR, "When create sitemap %s", err)
		return
	}
	defer f.Close()

	//自行拼接XML比官方的xml包还靠谱

	f.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)
	f.WriteString("\n")
	f.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`)
	f.WriteString("\n")

	production_url := FromCtx(topCtx, "site.config.production_url").(string)

	f.WriteString("\t<url>\n")
	f.WriteString("\t\t<loc>")
	xml.Escape(f, []byte(production_url+"/")) //够弱智不? 竟然要传入一个io.Reader
	f.WriteString("</loc>\n")
	f.WriteString("\t</url>\n")

	post_ids := FromCtx(topCtx, "db.posts.chronological").([]string)
	posts := FromCtx(topCtx, "db.posts.dictionary").(map[string]Mapper)
	for _, id := range post_ids {
		f.WriteString("\t<url>\n")
		post := posts[id]
		f.WriteString("\t\t<loc>")
		xml.Escape(f, []byte(production_url))
		xml.Escape(f, []byte(post.Url()))
		f.WriteString("</loc>\n")
		f.WriteString(fmt.Sprintf("\t\t<lastmod>%s</lastmod>\n", post["date"])) // 是否应该抹除呢? 考虑中
		f.WriteString("\t\t<changefreq>weekly</changefreq>\n")
		f.WriteString("\t</url>\n")
	}

	f.WriteString(`</urlset>`)
	f.Sync()
	// ~_~ 大功告成!
}
// Write a xml.Attr.
func writeXMLAttr(w io.Writer, attr xml.Attr) error {
	if err := writeXMLName(w, attr.Name); err != nil {
		return err
	}
	if _, err := w.Write([]byte{'=', '\''}); err != nil {
		return err
	}
	xml.Escape(w, []byte(attr.Value))
	if _, err := w.Write([]byte{'\''}); err != nil {
		return err
	}
	return nil
}
Beispiel #12
0
func postIssueComment(ctxt appengine.Context, id, comment string) error {
	cfg, err := oauthConfig(ctxt)
	if err != nil {
		return fmt.Errorf("oauthconfig: %v", err)
	}

	var tok oauth.Token
	if err := app.ReadMeta(ctxt, "codelogin.token", &tok); err != nil {
		return fmt.Errorf("reading token: %v", err)
	}

	tr := &oauth.Transport{
		Config:    cfg,
		Token:     &tok,
		Transport: urlfetch.Client(ctxt).Transport,
	}
	client := tr.Client()

	var buf bytes.Buffer
	buf.WriteString(`<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009'>
  <content type='html'>`)
	xml.Escape(&buf, []byte(comment))
	buf.WriteString(`</content>
  <author>
    <name>ignored</name>
  </author>
  <issues:updates>
  </issues:updates>
</entry>
`)
	u := "https://code.google.com/feeds/issues/p/go/issues/" + id + "/comments/full"
	req, err := http.NewRequest("POST", u, &buf)
	if err != nil {
		return fmt.Errorf("write: %v", err)
	}
	req.Header.Set("Content-Type", "application/atom+xml")
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("write: %v", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != 201 {
		buf.Reset()
		io.Copy(&buf, resp.Body)
		return fmt.Errorf("write: %v\n%s", resp.Status, buf.String())
	}
	return nil
}
Beispiel #13
0
// encodeString encodes a string to the XML plist format.
func (e *Encoder) encodeString(str string) error {
	_, err := e.bw.WriteString(e.indent() + "<string>")
	if err != nil {
		return err
	}

	xml.Escape(e.bw, []byte(str))

	_, err = e.bw.WriteString("</string>\n")
	if err != nil {
		return err
	}

	return nil
}
Beispiel #14
0
// encodeStruct encodes a struct to an XML plist dict.
func (e *Encoder) encodeStruct(rv reflect.Value) error {
	err := e.writeString("<dict>\n")
	if err != nil {
		return err
	}

	e.indentLevel++

	for i := 0; i < rv.NumField(); i++ {
		rt := rv.Type()
		f := rt.Field(i)
		name := f.Tag.Get("plist")
		if name == "-" {
			continue
		}
		if name == "" {
			name = f.Name
		}

		_, err = e.bw.WriteString(e.indent() + "<key>")
		if err != nil {
			return err
		}
		xml.Escape(e.bw, []byte(name))
		_, err = e.bw.WriteString("</key>\n")
		if err != nil {
			return err
		}

		err = e.encodeAny(rv.Field(i).Interface())
		if err != nil {
			return err
		}
	}

	e.indentLevel--

	err = e.writeString("</dict>\n")
	if err != nil {
		return err
	}

	return nil
}
Beispiel #15
0
// TODO: base64
func (e *encoder) encodeValue(val reflect.Value) {
	if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
		val = val.Elem()
	}

	fmt.Fprint(e, "<value>")

	switch val.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		fmt.Fprintf(e, "<int>%d</int>", val.Int())
	case reflect.Bool:
		var n int
		if val.Bool() {
			n = 1
		} else {
			n = 0
		}
		fmt.Fprintf(e, "<boolean>%d</boolean>", n)
	case reflect.String:
		fmt.Fprint(e, "<string>")
		xml.Escape(e, []byte(val.String()))
		fmt.Fprint(e, "</string>")
	case reflect.Float32, reflect.Float64:
		fmt.Fprintf(e, "<double>%f</double>", val.Float())
	case reflect.Struct:
		if t, isTime := val.Interface().(time.Time); isTime {
			fmt.Fprintf(e, "<dateTime.iso8601>%s</dateTime.iso8601>", t.Format(iso8601))
		} else {
			e.encodeStruct(val)
		}
	case reflect.Slice:
		e.encodeSlice(val)
	}

	fmt.Fprint(e, "</value>")
}
Beispiel #16
0
// Link begins a link named "name", with the specified title.
// Standard Reference: http://www.w3.org/TR/SVG11/linking.html#Links
func (svg *SVG) Link(href string, title string) {
	svg.printf("<a xlink:href=\"%s\" xlink:title=\"", href)
	xml.Escape(svg.Writer, []byte(title))
	svg.println("\">")
}
Beispiel #17
0
func write(id int, ch *Change) error {
	var buf bytes.Buffer
	buf.WriteString(`<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009'>
  <content type='html'>`)
	xml.Escape(&buf, []byte(ch.Comment))
	buf.WriteString(`</content>
  <author>
    <name>ignored</name>
  </author>
  <issues:updates>
`)
	tag := func(t, data string) {
		buf.WriteString(`    ` + t)
		xml.Escape(&buf, []byte(data))
		buf.WriteString(`</` + t[1:])
	}

	if ch.Summary != "" {
		tag("<issues:summary>", ch.Summary)
	}
	if ch.Status != "" {
		status := ch.Status
		merge := ""
		if strings.HasPrefix(status, "Duplicate ") {
			merge = strings.TrimPrefix(status, "Duplicate ")
			status = "Duplicate"
		}
		tag("<issues:status>", status)
		if merge != "" {
			tag("<issues:mergedIntoUpdate>", merge)
		}
	}
	if ch.Owner != "" {
		tag("<issues:ownerUpdate>", ch.Owner)
	}
	for _, l := range ch.Label {
		tag("<issues:label>", l)
	}
	for _, cc := range ch.CC {
		tag("<issues:ccUpdate>", cc)
	}
	buf.WriteString(`
  </issues:updates>
</entry>
`)

	// Done with XML!

	u := "https://code.google.com/feeds/issues/p/" + *project + "/issues/" + fmt.Sprint(id) + "/comments/full"
	req, err := http.NewRequest("POST", u, &buf)
	if err != nil {
		return fmt.Errorf("write: %v", err)
	}
	req.Header.Set("Content-Type", "application/atom+xml")
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("write: %v", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != 201 {
		buf.Reset()
		io.Copy(&buf, resp.Body)
		return fmt.Errorf("write: %v\n%s", resp.Status, buf.String())
	}
	return nil
}
Beispiel #18
0
func (p *Post) HtmlXML() string {
	var buf bytes.Buffer
	xml.Escape(&buf, []byte(p.Html))
	return buf.String()
}
Beispiel #19
0
func escape(w io.Writer, str string) {
	xml.Escape(w, []uint8(str))
}
Beispiel #20
0
func (this *writer) Escape(s string) []byte {
	this.buf.Reset()
	xml.Escape(this.buf, []byte(s))
	return this.buf.Bytes()
}
Beispiel #21
0
func xmlEscape(s string) string {
	var buffer bytes.Buffer
	xml.Escape(&buffer, []byte(s))
	return buffer.String()
}
Beispiel #22
0
// Textpath places text optionally styled text along a previously defined path
// Standard Reference: http://www.w3.org/TR/SVG11/text.html#TextPathElement
func (svg *SVG) Textpath(t string, pathid string, s ...string) {
	svg.printf("<text %s<textPath xlink:href=\"%s\">", endstyle(s, ">"), pathid)
	xml.Escape(svg.Writer, []byte(t))
	svg.println(`</textPath></text>`)
}
Beispiel #23
0
// tt creates a xml element, tag containing s
func (svg *SVG) tt(tag string, s string) {
	svg.print("<" + tag + ">")
	xml.Escape(svg.Writer, []byte(s))
	svg.println("</" + tag + ">")
}
Beispiel #24
0
// Filter text according to options.
func Filter(text string, options *Options) (filtered string, err error) {
	// This function filters incoming text from clients according to the three options:
	//
	// StripHTML:
	//    If true, all HTML shall be stripped.
	//    When stripping br tags, append a newline to the output stream.
	//    When stripping p tags, append a newline after the end tag.
	//
	// MaxTextsageLength:
	//    Text length for "plain" messages (messages without images)
	//
	// MaxImageMessageLength:
	//    Text length for messages with images.

	if options == nil {
		options = &defaultOptions
	}

	max := options.MaxTextMessageLength
	maximg := options.MaxImageMessageLength

	if options.StripHTML {
		// Does the message include HTML? If not, take the fast path.
		if strings.Index(text, "<") == -1 {
			filtered = strings.TrimSpace(text)
		} else {
			// Strip away all HTML
			out := bytes.NewBuffer(nil)
			buf := bytes.NewBufferString(text)
			parser := xml.NewDecoder(buf)
			parser.Strict = false
			parser.AutoClose = xml.HTMLAutoClose
			parser.Entity = xml.HTMLEntity
			for {
				tok, err := parser.Token()
				if err == io.EOF {
					break
				} else if err != nil {
					return "", err
				}

				switch t := tok.(type) {
				case xml.CharData:
					out.Write(t)
				case xml.EndElement:
					if t.Name.Local == "p" || t.Name.Local == "br" {
						out.WriteString("\n")
					}
				}
			}
			filtered = strings.TrimSpace(out.String())
		}
		if max != 0 && len(filtered) > max {
			return "", ErrExceedsTextMessageLength
		}
	} else {
		// No limits
		if max == 0 && maximg == 0 {
			return text, nil
		}

		// Too big for images?
		if maximg != 0 && len(text) > maximg {
			return "", ErrExceedsImageMessageLength
		}

		// Under max plain length?
		if max == 0 || len(text) <= max {
			return text, nil
		}

		// Over max length, under image limit. If text doesn't include
		// any HTML, this is a no-go. If there is HTML, we can attempt to
		// strip away data URIs to see if we can get the message to fit
		// into the plain message limit.
		if strings.Index(text, "<") == -1 {
			return "", ErrExceedsTextMessageLength
		}

		// Simplify the received HTML data by stripping away data URIs
		out := bytes.NewBuffer(nil)
		buf := bytes.NewBufferString(text)
		parser := xml.NewDecoder(buf)
		parser.Strict = false
		parser.AutoClose = xml.HTMLAutoClose
		parser.Entity = xml.HTMLEntity
		for {
			tok, err := parser.Token()
			if err == io.EOF {
				break
			} else if err != nil {
				return "", err
			}

			switch t := tok.(type) {
			case xml.CharData:
				out.Write(t)
			case xml.StartElement:
				out.WriteString("<")
				xml.Escape(out, []byte(t.Name.Local))
				for _, attr := range t.Attr {
					if t.Name.Local == "img" && attr.Name.Local == "src" {
						continue
					}
					out.WriteString(" ")
					xml.Escape(out, []byte(attr.Name.Local))
					out.WriteString(`="`)
					out.WriteString(attr.Value)
					out.WriteString(`"`)
				}
				out.WriteString(">")
			case xml.EndElement:
				out.WriteString("</")
				xml.Escape(out, []byte(t.Name.Local))
				out.WriteString(">")
			}
		}

		filtered = strings.TrimSpace(out.String())
		if len(filtered) > max {
			return "", ErrExceedsTextMessageLength
		}
	}

	return
}
Beispiel #25
0
// Text places the specified text, t at x,y according to the style specified in s
// Standard Reference: http://www.w3.org/TR/SVG11/text.html#TextElement
func (svg *SVG) Text(x float64, y float64, t string, s ...string) {
	svg.printf(`<text %s %s`, loc(x, y), endstyle(s, ">"))
	xml.Escape(svg.Writer, []byte(t))
	svg.println(`</text>`)
}
Beispiel #26
0
// Gid begins a group, with the specified id
func (svg *SVG) Gid(s string) {
	svg.print(`<g id="`)
	xml.Escape(svg.Writer, []byte(s))
	svg.println(`">`)
}
Beispiel #27
0
func escapeString(s string) string {
	buffer := bytes.NewBuffer([]byte{})
	xml.Escape(buffer, []byte(s))

	return fmt.Sprintf("%v", buffer)
}
Beispiel #28
0
func postMovedNote(ctxt appengine.Context, kind, id string) error {
	var old Issue
	if err := app.ReadData(ctxt, "Issue", id, &old); err != nil {
		return err
	}
	updateIssue(&old)
	if !old.NeedGithubNote {
		err := app.Transaction(ctxt, func(ctxt appengine.Context) error {
			var old Issue
			if err := app.ReadData(ctxt, "Issue", id, &old); err != nil {
				return err
			}
			old.NeedGithubNote = false
			return app.WriteData(ctxt, "Issue", id, &old)
		})
		return err
	}

	cfg, err := oauthConfig(ctxt)
	if err != nil {
		return fmt.Errorf("oauthconfig: %v", err)
	}

	var tok oauth.Token
	if err := app.ReadMeta(ctxt, "codelogin.token", &tok); err != nil {
		return fmt.Errorf("reading token: %v", err)
	}

	tr := &oauth.Transport{
		Config:    cfg,
		Token:     &tok,
		Transport: &urlfetch.Transport{Context: ctxt, Deadline: 45 * time.Second},
	}
	client := tr.Client()

	status := ""
	if old.State != "closed" {
		status = "<issues:status>Moved</issues:status>"
	}

	var buf bytes.Buffer
	buf.WriteString(`<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009'>
  <content type='html'>`)
	xml.Escape(&buf, []byte(fmt.Sprintf("This issue has moved to https://golang.org/issue/%s\n", id)))
	buf.WriteString(`</content>
  <author>
    <name>ignored</name>
  </author>
  <issues:sendEmail>False</issues:sendEmail>
  <issues:updates>
    <issues:label>IssueMoved</issues:label>
    <issues:label>Restrict-AddIssueComment-Commit</issues:label>
    ` + status + `
  </issues:updates>
</entry>
`)
	u := "https://code.google.com/feeds/issues/p/go/issues/" + id + "/comments/full"
	req, err := http.NewRequest("POST", u, &buf)
	if err != nil {
		return fmt.Errorf("write: %v", err)
	}
	req.Header.Set("Content-Type", "application/atom+xml")
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("write: %v", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != 201 {
		buf.Reset()
		io.Copy(&buf, resp.Body)
		return fmt.Errorf("write: %v\n%s", resp.Status, buf.String())
	}

	err = app.Transaction(ctxt, func(ctxt appengine.Context) error {
		var old Issue
		if err := app.ReadData(ctxt, "Issue", id, &old); err != nil {
			return err
		}
		old.NeedGithubNote = false
		old.Label = append(old.Label, "IssueMoved", "Restrict-AddIssueComment-Commit")
		return app.WriteData(ctxt, "Issue", id, &old)
	})
	return err
}
Beispiel #29
0
func (d DisplayNamePropertyValue) XML(b *bytes.Buffer) {
	b.WriteString("<displayname>")
	xml.Escape(b, []byte(d))
	b.WriteString("</displayname>")
}
Beispiel #30
0
// xmlEncode wraps xml.Escape() so that it can be simply used with strings
func xmlEncode(s string) string {
	w := bytes.NewBuffer([]byte{})
	xml.Escape(w, []byte(s))
	return w.String()
}