Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd237be4 authored by Max Loh's avatar Max Loh
Browse files

ASL validation logic

Adds validation logic for isSharingOptional, isCollectionOptional, and various expected elements in the input XML file.

Bug: 287487923
Test: TODO in future CLs
Change-Id: I0a2261ec3c71a1d2df977810d065dfc5a4dda5e3
parent 85cae1da
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.aslgen;

import com.android.asllib.AndroidSafetyLabel;
import com.android.asllib.AndroidSafetyLabel.Format;
import com.android.asllib.util.MalformedXmlException;

import org.xml.sax.SAXException;

@@ -32,7 +33,11 @@ public class Main {

    /** Takes the options to make file conversion. */
    public static void main(String[] args)
            throws IOException, ParserConfigurationException, SAXException, TransformerException {
            throws IOException,
                    ParserConfigurationException,
                    SAXException,
                    TransformerException,
                    MalformedXmlException {

        String inFile = null;
        String outFile = null;
+6 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.asllib;

import com.android.asllib.util.MalformedXmlException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
@@ -53,7 +55,7 @@ public class AndroidSafetyLabel implements AslMarshallable {
    /** Reads a {@link AndroidSafetyLabel} from an {@link InputStream}. */
    // TODO(b/329902686): Support parsing from on-device.
    public static AndroidSafetyLabel readFromStream(InputStream in, Format format)
            throws IOException, ParserConfigurationException, SAXException {
            throws IOException, ParserConfigurationException, SAXException, MalformedXmlException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document document = factory.newDocumentBuilder().parse(in);
@@ -65,9 +67,9 @@ public class AndroidSafetyLabel implements AslMarshallable {

                return new AndroidSafetyLabelFactory()
                        .createFromHrElements(
                                XmlUtils.asElementList(
                                        document.getElementsByTagName(
                                                XmlUtils.HR_TAG_APP_METADATA_BUNDLES)));
                                List.of(
                                        XmlUtils.getSingleElement(
                                                document, XmlUtils.HR_TAG_APP_METADATA_BUNDLES)));
            case ON_DEVICE:
                throw new IllegalArgumentException(
                        "Parsing from on-device format is not supported at this time.");
+4 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.asllib;

import com.android.asllib.util.MalformedXmlException;

import org.w3c.dom.Element;

import java.util.List;
@@ -24,7 +26,8 @@ public class AndroidSafetyLabelFactory implements AslMarshallableFactory<Android

    /** Creates an {@link AndroidSafetyLabel} from human-readable DOM element */
    @Override
    public AndroidSafetyLabel createFromHrElements(List<Element> appMetadataBundles) {
    public AndroidSafetyLabel createFromHrElements(List<Element> appMetadataBundles)
            throws MalformedXmlException {
        Element appMetadataBundlesEle = XmlUtils.getSingleElement(appMetadataBundles);
        Element safetyLabelsEle =
                XmlUtils.getSingleChildElement(
+3 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.asllib;

import com.android.asllib.util.MalformedXmlException;

import org.w3c.dom.Element;

import java.util.List;
@@ -23,5 +25,5 @@ import java.util.List;
public interface AslMarshallableFactory<T extends AslMarshallable> {

    /** Creates an {@link AslMarshallableFactory} from human-readable DOM element */
    T createFromHrElements(List<Element> elements);
    T createFromHrElements(List<Element> elements) throws MalformedXmlException;
}
+7 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.asllib;

import com.android.asllib.util.MalformedXmlException;

import org.w3c.dom.Element;

import java.util.HashMap;
@@ -24,12 +26,16 @@ import java.util.Map;

public class DataCategoryFactory implements AslMarshallableFactory<DataCategory> {
    @Override
    public DataCategory createFromHrElements(List<Element> elements) {
    public DataCategory createFromHrElements(List<Element> elements) throws MalformedXmlException {
        String categoryName = null;
        Map<String, DataType> dataTypeMap = new HashMap<String, DataType>();
        for (Element ele : elements) {
            categoryName = ele.getAttribute(XmlUtils.HR_ATTR_DATA_CATEGORY);
            String dataTypeName = ele.getAttribute(XmlUtils.HR_ATTR_DATA_TYPE);
            if (!DataTypeConstants.getValidDataTypes().contains(dataTypeName)) {
                throw new MalformedXmlException(
                        String.format("Unrecognized data type name: %s", dataTypeName));
            }
            dataTypeMap.put(dataTypeName, new DataTypeFactory().createFromHrElements(List.of(ele)));
        }

Loading