func moveNode(xn, parent *xmlx.Node, parentName string) (newParent *xmlx.Node, oldPos int) { oldPos = -1 if root := srcDoc.Root.Children[0]; parent == nil { if parent = subNode(root, parentName); parent == nil { parent = xmlx.NewNode(xn.Type) parent.Name.Local = parentName parent.Parent = root root.Children = append(root.Children, parent) } } if xn.Parent != parent { newParent = parent if xn.Parent != nil { for i, sn := range xn.Parent.Children { if sn == xn { oldPos = i break } } if oldPos >= 0 { xn.Parent.Children[oldPos] = nil } } xn.Parent = parent parent.Children = append(parent.Children, xn) } return }
func replaceNode(xn, nn *xmlx.Node) { pos := -1 for i, sn := range xn.Parent.Children { if sn == xn { pos = i break } } if pos >= 0 { xn.Parent.Children[pos] = nn nn.Parent = xn.Parent xn.Parent = nil } }
func convertImage(xn *xmlx.Node) { hexFormat := attVal(xn, "format") imgHeight := attValU64(xn, "height") imgWidth := attValU64(xn, "width") imgDepth := attValU64(xn, "depth") if imgDepth == 0 { imgDepth = 1 } delAtts(xn, "height", "width", "depth", "format") if len(hexFormat) == 0 { hexFormat = HexFormat } hexData, refUrl, initNode, hexNode := "", "", subNode(xn, "init_from"), subNode(xn, "data") if initNode != nil { refUrl = initNode.Value delNodeForce(initNode, true) initNode = nil } if hexNode != nil { hexData = hexNode.Value delNodeForce(hexNode, true) hexNode = nil } if (len(refUrl) > 0) || (len(hexData) > 0) { initNode = xmlx.NewNode(xn.Type) initNode.Name.Local = "init_from" if len(refUrl) > 0 { ensureChild(initNode, "ref").Value = refUrl } if len(hexData) > 0 { hex := ensureChild(initNode, "hex") setAttr(hex, "format", hexFormat, false) hex.Value = hexData hexData = "" } } if (imgWidth != 0) && (imgHeight != 0) { cn := xmlx.NewNode(xn.Type) switch imgDepth { case 1: cn.Name.Local = "create_2d" case 2: cn.Name.Local = "create_3d" default: cn.Name.Local = "create_cube" } sn := ensureChild(cn, "size_exact") setAttr(sn, "width", fmt.Sprintf("%v", imgWidth), false) setAttr(sn, "height", fmt.Sprintf("%v", imgHeight), false) if initNode != nil { cn.AddChild(initNode) } xn.AddChild(cn) } else if initNode != nil { xn.AddChild(initNode) } if oldParent := xn.Parent; oldParent.Name.Local != "library_images" { logFmt("!!MOVE image!!; this may be BUGGY, please report your use-case at GitHub Issues for this package!!\n") id := attVal(xn, "id") if len(id) == 0 { id = fmt.Sprintf("img_moved_%v", time.Now().UnixNano()) } if _, pos := moveNode(xn, nil, "library_images"); pos >= 0 { xn = xmlx.NewNode(xn.Type) xn.Name.Local = "instance_image" setAttr(xn, "url", "#"+id, false) xn.Parent = oldParent oldParent.Children[pos] = xn } } }