1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 com.android.server.integrity.serializer;
18 
19 import static com.android.server.integrity.parser.RuleMetadataParser.RULE_PROVIDER_TAG;
20 import static com.android.server.integrity.parser.RuleMetadataParser.VERSION_TAG;
21 
22 import android.util.Xml;
23 
24 import com.android.modules.utils.TypedXmlSerializer;
25 import com.android.server.integrity.model.RuleMetadata;
26 
27 import org.xmlpull.v1.XmlSerializer;
28 
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.nio.charset.StandardCharsets;
32 
33 /** Helper class for writing rule metadata. */
34 public class RuleMetadataSerializer {
35     /** Serialize the rule metadata to an output stream. */
serialize(RuleMetadata ruleMetadata, OutputStream outputStream)36     public static void serialize(RuleMetadata ruleMetadata, OutputStream outputStream)
37             throws IOException {
38         TypedXmlSerializer xmlSerializer = Xml.resolveSerializer(outputStream);
39 
40         serializeTaggedValue(xmlSerializer, RULE_PROVIDER_TAG, ruleMetadata.getRuleProvider());
41         serializeTaggedValue(xmlSerializer, VERSION_TAG, ruleMetadata.getVersion());
42 
43         xmlSerializer.endDocument();
44     }
45 
serializeTaggedValue(TypedXmlSerializer xmlSerializer, String tag, String value)46     private static void serializeTaggedValue(TypedXmlSerializer xmlSerializer, String tag,
47             String value) throws IOException {
48         xmlSerializer.startTag(/* namespace= */ null, tag);
49         xmlSerializer.text(value);
50         xmlSerializer.endTag(/* namespace= */ null, tag);
51     }
52 }
53