xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/ak/liteparse/non_values_parse.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package liteparse
16
17import (
18	"context"
19	"strings"
20
21	rdpb "src/tools/ak/res/proto/res_data_go_proto"
22	"src/tools/ak/res/res"
23	"src/tools/ak/res/respipe/respipe"
24	"src/tools/ak/res/resxml/resxml"
25)
26
27// nonValuesParse searches a non-values xml document for ID declarations. It creates ID
28// resources for any declarations it finds.
29func nonValuesParse(ctx context.Context, xmlC <-chan resxml.XMLEvent) (<-chan *rdpb.Resource, <-chan error) {
30	resC := make(chan *rdpb.Resource)
31	errC := make(chan error)
32	go func() {
33		defer close(resC)
34		defer close(errC)
35		for xe := range xmlC {
36			for _, a := range resxml.Attrs(xe) {
37				if strings.HasPrefix(a.Value, res.GeneratedIDPrefix) {
38					unparsed := strings.Replace(a.Value, res.GeneratedIDPrefix, "@id", 1)
39					fqn, err := res.ParseName(unparsed, res.ID)
40					if err != nil {
41						if !respipe.SendErr(ctx, errC, respipe.Errorf(ctx, "%s: unparsable id attribute: %+v: %v", a.Value, xe, err)) {
42							return
43						}
44						continue
45					}
46					r := new(rdpb.Resource)
47					if err := fqn.SetResource(r); err != nil {
48						if !respipe.SendErr(ctx, errC, respipe.Errorf(ctx, "%s: name->proto failed: %+v", fqn, err)) {
49							return
50						}
51						continue
52					}
53					if !respipe.SendRes(ctx, resC, r) {
54						return
55					}
56				}
57			}
58		}
59	}()
60	return resC, errC
61}
62