//保存所有设备配置 func (p *ConfigProtocol) SaveAllDevConfig(r *http.Request, arg *JsonConfigInputAgr, res *JsonResponse) error { utils.Debug("received ConfigProtocol SaveAllDevConfig request") var ( n dom.Node t dom.Element dt dom.Attribute ) db := dom.NewDOMBuilder(strings.NewReader(arg.Content), dom.NewDOMStore()) d, err2 := db.Build() if err2 != nil { res.Code = 1 res.ErrMsg = fmt.Sprintf("parse xml file error,category :[%+v] ", err2.Error()) utils.Error("parse xml file error,category:[%+v]", err2.Error()) return nil } f := d.ChildNodes() for i := 0; i < len(f); i++ { n = f[i] switch n.Kind() { case dom.ElementKind: t = n.(dom.Element) switch t.Name() { case "Device": // 解析设置 var v []dom.Attribute v = t.Attr() for j := 0; j < len(v); j++ { dt = v[j] if strings.EqualFold(dt.Name(), "Category") { err := SaveDrvConfig(dt.Value(), t.String()) if err != nil { res.Code = 1 res.ErrMsg = fmt.Sprintf("save file error,category :%s content is : [%s]", dt.Value(), t.String()) utils.Error("can't save category [%s] ,content is : [%s]", dt.Value(), t.String()) return nil } } } break default: utils.Error("can't recognize element name: %s", t.Name()) } break } } res.Code = 0 utils.Debug("end ConfigProtocol SaveAllDevConfig request") return nil }
//解析设置 func parseSetting(cur dom.Element) *PrintSetting { var i int var n dom.Node var t dom.Element var ps = PrintSetting{} f := cur.ChildNodes() for i = 0; i < len(f); i++ { n = f[i] switch n.Kind() { case dom.ElementKind: t = n.(dom.Element) if strings.EqualFold("Page", t.Name()) { ps.ps = parsePageSetting(t) } } } return &ps }
func parsePrintFormat(cur dom.Element) *PrintFacade { var i int var ( n dom.Node t dom.Element p PrintFacade ) f := cur.ChildNodes() for i = 0; i < len(f); i++ { n = f[i] switch n.Kind() { case dom.ElementKind: t = n.(dom.Element) switch t.Name() { case "Setting": // 解析设置 p.S = parseSetting(t) break case "Content": //解析内容 root := NewPrintRoot() root.SetName("root") page := NewPage(p.S) root.AddChild(page) page.SetParent(root) parsePrintContent(t, page, p.S.ps) p.V = root printPrintElement(p.V, 0) break default: utils.Error("can't find element name: %s", t.Name()) } break } } return &p }
//解析页面设置 func parsePageSetting(cur dom.Element) *PageSetting { var ( i int tmp string v float64 n dom.Node t dom.Element err error ) var ps = PageSetting{} f := cur.ChildNodes() for i = 0; i < len(f); i++ { n = f[i] switch n.Kind() { case dom.ElementKind: t = n.(dom.Element) tmp, err = findTextNode(t) if err != nil { utils.Error("find element %s not have text value ", t.Name()) continue } switch t.Name() { case "Width": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported width format %s", tmp) return nil } ps.Width = float32(v) break case "Height": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported Height format %s", tmp) return nil } ps.Height = float32(v) break case "Leftmargin": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported Leftmargin format %s", tmp) return nil } ps.Leftmargin = float32(v) break case "Rightmargin": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported Rightmargin format %s", tmp) return nil } ps.Rightmargin = float32(v) break case "Topmargin": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported Topmargin format %s", tmp) return nil } ps.Topmargin = float32(v) break case "Bottommargin": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported Bottommargin format %s", tmp) return nil } ps.Bottommargin = float32(v) break case "LineInterval": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported LineInterval format %s", tmp) return nil } ps.LineInterval = float32(v) break case "ColInterval": v, err = strconv.ParseFloat(tmp, 32) if err != nil { utils.Error("not supported ColInterval format %s", tmp) return nil } ps.ColInterval = float32(v) break } break } } utils.Trace("parse setting result %+v", ps) return &ps }
//解析需要打印的内容 //需要解析子结构,深度遍历->广度遍历 //cur:解析树中的当前节点 //par:父节点 func parsePrintContent(cur dom.Element, par PrintInterface, p *PageSetting) PrintInterface { var i int var ( n dom.Node t dom.Element z PrintInterface prev PrintInterface te dom.Text ) f := cur.ChildNodes() for i = 0; i < len(f); i++ { n = f[i] z = nil switch n.Kind() { case dom.ElementKind: t = n.(dom.Element) utils.Info("find element %s", t.Name()) switch t.Name() { case "Br": z = parseBrElement(t, p) case "Table": z = parseTableElement(t, p) case "Rect": z = parseRectElement(t, p) case "Tr": z = parseTrElement(t, p) case "Td": z = parseTdElement(t, p) case "Bold": z = parseBoldElement(t, p) case "Sup": z = parseSupElement(t, p) case "Sub": z = parseSubElement(t, p) case "Underline": z = parseUnderElement(t, p) case "Doubleheight": z = parseDoubleHeightElement(t, p) case "Doublewidth": z = parseDoubleWidthElement(t, p) default: utils.Error("can't find element name: %s", t.Name()) continue } case dom.TextKind: te = n.(dom.Text) t := strings.TrimSpace(te.Data()) //可能是换行符 //if t == "" || strings.HasPrefix(t, "\r") || strings.HasPrefix(t, "\n") { //utils.Info("detect text line character,text is %s", te.Data()) //continue // changed by jinsl 20151222 if t == "" { utils.Info("detect text line character,text is null") continue } else if strings.HasPrefix(t, "\r") { utils.Info("detect text line character,text is \\r") continue } else if strings.HasPrefix(t, "\n") { utils.Info("detect text line character,text is \\n") continue } else { utils.Info("find text node,content %s", te.Data()) z = parseTextNode(te, p) } default: utils.Debug("find default node %s ,kind %d", n.String(), n.Kind()) } if z == nil { continue } if par != nil { par.AddChild(z) z.SetParent(par) } //加入到列表 if prev == nil { prev = z } } return prev }