|
| 1 | +/* |
| 2 | +Copyright 2019 Google Inc. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package jsonnet |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/xml" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "strings" |
| 25 | +) |
| 26 | + |
| 27 | +type stack struct { |
| 28 | + elements []interface{} |
| 29 | +} |
| 30 | + |
| 31 | +func (s *stack) Push(e interface{}) { |
| 32 | + s.elements = append(s.elements, e) |
| 33 | +} |
| 34 | + |
| 35 | +func (s *stack) Pop() (interface{}, error) { |
| 36 | + if len(s.elements) == 0 { |
| 37 | + return nil, errors.New("cannot pop from empty stack") |
| 38 | + } |
| 39 | + l := len(s.elements) |
| 40 | + e := s.elements[l-1] |
| 41 | + s.elements = s.elements[:l-1] |
| 42 | + return e, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (s *stack) Size() int { |
| 46 | + return len(s.elements) |
| 47 | +} |
| 48 | + |
| 49 | +type jsonMLBuilder struct { |
| 50 | + stack *stack |
| 51 | + currDepth int |
| 52 | +} |
| 53 | + |
| 54 | +// BuildJsonmlFromString returns a jsomML form of given xml string. |
| 55 | +func BuildJsonmlFromString(s string) ([]interface{}, error) { |
| 56 | + b := newBuilder() |
| 57 | + d := xml.NewDecoder(strings.NewReader(s)) |
| 58 | + |
| 59 | + for { |
| 60 | + token, err := d.Token() |
| 61 | + if err == io.EOF { |
| 62 | + break |
| 63 | + } |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + if err := b.addToken(token); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if b.stack.Size() == 0 { |
| 75 | + // No nodes has been identified |
| 76 | + return nil, fmt.Errorf("%s is not a valid XML", s) |
| 77 | + } |
| 78 | + |
| 79 | + return b.build(), nil |
| 80 | +} |
| 81 | + |
| 82 | +func newBuilder() *jsonMLBuilder { |
| 83 | + return &jsonMLBuilder{ |
| 84 | + stack: &stack{}, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (b *jsonMLBuilder) addToken(token xml.Token) error { |
| 89 | + switch token.(type) { |
| 90 | + case xml.StartElement: |
| 91 | + // check for multiple roots |
| 92 | + if b.currDepth == 0 && b.stack.Size() > 0 { |
| 93 | + // There are multiple root elements |
| 94 | + return errors.New("XML cannot have multiple root elements") |
| 95 | + } |
| 96 | + |
| 97 | + t := token.(xml.StartElement) |
| 98 | + node := []interface{}{t.Name.Local} |
| 99 | + // Add Attributes |
| 100 | + if len(t.Attr) > 0 { |
| 101 | + attr := make(map[string]interface{}) |
| 102 | + for _, a := range t.Attr { |
| 103 | + attr[a.Name.Local] = a.Value |
| 104 | + } |
| 105 | + node = append(node, attr) |
| 106 | + } |
| 107 | + b.stack.Push(node) |
| 108 | + b.currDepth++ |
| 109 | + case xml.CharData: |
| 110 | + t := token.(xml.CharData) |
| 111 | + s := strings.TrimSpace(string(t)) |
| 112 | + if len(s) > 0 { |
| 113 | + // Skip whitespace only string |
| 114 | + b.appendToLastNode(string(t)) |
| 115 | + } |
| 116 | + case xml.EndElement: |
| 117 | + b.squashLastNode() |
| 118 | + b.currDepth-- |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (b *jsonMLBuilder) build() []interface{} { |
| 125 | + root, _ := b.stack.Pop() |
| 126 | + return root.([]interface{}) |
| 127 | +} |
| 128 | + |
| 129 | +func (b *jsonMLBuilder) appendToLastNode(e interface{}) { |
| 130 | + if b.stack.Size() == 0 { |
| 131 | + return |
| 132 | + } |
| 133 | + node, _ := b.stack.Pop() |
| 134 | + n := node.([]interface{}) |
| 135 | + n = append(n, e) |
| 136 | + b.stack.Push(n) |
| 137 | +} |
| 138 | + |
| 139 | +func (b *jsonMLBuilder) squashLastNode() { |
| 140 | + if b.stack.Size() < 2 { |
| 141 | + return |
| 142 | + } |
| 143 | + n, _ := b.stack.Pop() |
| 144 | + b.appendToLastNode(n) |
| 145 | +} |
0 commit comments