fixes
This commit is contained in:
1 parent
c347d2418f
commit
1ad180a052
49 files changed
+6339
-89
No files matched your search
@@ -31,12 +31,18 @@ type feedRow map[string]string
|
||||
|
||||
func detectFeedFormat(feedType, contentType, urlHint string, sample []byte) string {
|
||||
ft := strings.ToLower(strings.TrimSpace(feedType))
|
||||
// Binary workbooks win over the declared type: local uploads default to text/csv.
|
||||
if ft == "excel" || isExcelSample(sample) {
|
||||
return "excel"
|
||||
}
|
||||
if ft == "csv" || ft == "xml" || ft == "json" {
|
||||
return ft
|
||||
}
|
||||
ct := strings.ToLower(contentType)
|
||||
u := strings.ToLower(urlHint)
|
||||
switch {
|
||||
case isExcelContentType(ct) || isExcelName(u):
|
||||
return "excel"
|
||||
case strings.Contains(ct, "json") || strings.Contains(u, ".json") || strings.Contains(u, "format=json"):
|
||||
return "json"
|
||||
case strings.Contains(ct, "csv") || strings.HasSuffix(u, ".csv"):
|
||||
@@ -183,13 +189,75 @@ func guessXMLItemLocal(data []byte) string {
|
||||
return cand
|
||||
}
|
||||
}
|
||||
if p := guessRepeatedXMLItemPath([]byte(sample)); p != "" {
|
||||
return leafName(p)
|
||||
}
|
||||
return "item"
|
||||
}
|
||||
|
||||
// guessRepeatedXMLItemPath finds the record element of feeds with non-English tags
|
||||
// (<izdelki><izdelek>…): the most repeated element that has child elements, the
|
||||
// shallowest on ties. Tolerates a truncated sample.
|
||||
func guessRepeatedXMLItemPath(data []byte) string {
|
||||
dec := xml.NewDecoder(bytes.NewReader(data))
|
||||
dec.Strict = false
|
||||
var stack []string
|
||||
counts := map[string]int{}
|
||||
hasChildren := map[string]bool{}
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
switch t := tok.(type) {
|
||||
case xml.StartElement:
|
||||
if len(stack) > 0 {
|
||||
hasChildren[strings.Join(stack, "/")] = true
|
||||
}
|
||||
stack = append(stack, t.Name.Local)
|
||||
counts[strings.Join(stack, "/")]++
|
||||
case xml.EndElement:
|
||||
if len(stack) > 0 {
|
||||
stack = stack[:len(stack)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
best, bestN := "", 1
|
||||
for p, n := range counts {
|
||||
if !hasChildren[p] {
|
||||
continue
|
||||
}
|
||||
depth, bestDepth := strings.Count(p, "/"), strings.Count(best, "/")
|
||||
if n > bestN || (n == bestN && best != "" && (depth < bestDepth || (depth == bestDepth && p < best))) {
|
||||
best, bestN = p, n
|
||||
}
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
func localNameEquals(n xml.Name, local string) bool {
|
||||
return strings.EqualFold(n.Local, local)
|
||||
}
|
||||
|
||||
// isXMLPropertyNameAttr lists attributes that name a property element
|
||||
// (<lastnost naziv="…">, <param name="…">).
|
||||
func isXMLPropertyNameAttr(local string) bool {
|
||||
switch strings.ToLower(local) {
|
||||
case "naziv", "name", "ime", "key", "label":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func xmlPropertyLabel(attrs []xml.Attr) string {
|
||||
for _, a := range attrs {
|
||||
if isXMLPropertyNameAttr(a.Name.Local) {
|
||||
return sanitizePathSegment(a.Value)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error) {
|
||||
row := make(feedRow)
|
||||
for _, a := range start.Attr {
|
||||
@@ -198,6 +266,12 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
row[start.Name.Local+"/"+key] = a.Value
|
||||
}
|
||||
var path []string
|
||||
// propNames[i] is the label of path[i] when it is a named property inside a group,
|
||||
// such as <dodatneLastnosti><lastnost naziv="Vrsta">Nahrbtnik</lastnost>… (else "").
|
||||
// Its text is also stored under group/<label>; propCounts tracks group/leaf paths
|
||||
// so the merged "all siblings" value can be dropped once two labels share it.
|
||||
var propNames []string
|
||||
propCounts := map[string]int{}
|
||||
var paramName, paramValue string
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
@@ -207,6 +281,14 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
switch t := tok.(type) {
|
||||
case xml.StartElement:
|
||||
path = append(path, t.Name.Local)
|
||||
label := ""
|
||||
if len(path) >= 2 {
|
||||
label = xmlPropertyLabel(t.Attr)
|
||||
}
|
||||
if label != "" {
|
||||
propCounts[strings.Join(path, "/")]++
|
||||
}
|
||||
propNames = append(propNames, label)
|
||||
for _, a := range t.Attr {
|
||||
key := strings.Join(path, "/") + "/@" + a.Name.Local
|
||||
row["@"+a.Name.Local] = a.Value
|
||||
@@ -225,6 +307,11 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
paramName, paramValue = "", ""
|
||||
}
|
||||
if len(path) == 0 {
|
||||
for p, n := range propCounts {
|
||||
if n > 1 {
|
||||
delete(row, p) // "MicroUSB 1 m": every sibling's text merged
|
||||
}
|
||||
}
|
||||
count := 0
|
||||
for k := range row {
|
||||
if strings.HasPrefix(k, "PARAMS/") && !strings.Contains(strings.TrimPrefix(k, "PARAMS/"), "/") {
|
||||
@@ -248,11 +335,20 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
return row, nil
|
||||
}
|
||||
path = path[:len(path)-1]
|
||||
propNames = propNames[:len(propNames)-1]
|
||||
case xml.CharData:
|
||||
text := strings.TrimSpace(string(t))
|
||||
if text == "" || len(path) == 0 {
|
||||
continue
|
||||
}
|
||||
if label := propNames[len(propNames)-1]; label != "" {
|
||||
key := strings.Join(append(path[:len(path)-1:len(path)-1], label), "/")
|
||||
if prev := row[key]; prev != "" && prev != text {
|
||||
row[key] = prev + " " + text
|
||||
} else {
|
||||
row[key] = text
|
||||
}
|
||||
}
|
||||
leaf := path[len(path)-1]
|
||||
if len(path) >= 3 && strings.EqualFold(path[len(path)-3], "PARAMS") && strings.EqualFold(path[len(path)-2], "PARAM") {
|
||||
if strings.EqualFold(leaf, "PARAM_NAME") {
|
||||
|
||||
Reference in new issue
Block a user