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

Commit 3a646c67 authored by Max Loh's avatar Max Loh Committed by Android (Google) Code Review
Browse files

Merge "ASL command-line tool initial implementation" into main

parents 3e2e8704 a59f6567
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -12,11 +12,6 @@ java_library_host {
    srcs: [
        "src/lib/java/**/*.java",
    ],
    target: {
        windows: {
            enabled: true,
        },
    },
}

java_binary_host {
+10 −8
Original line number Diff line number Diff line
@@ -19,16 +19,20 @@ package com.android.aslgen;
import com.android.asllib.AndroidSafetyLabel;
import com.android.asllib.AndroidSafetyLabel.Format;

import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

public class Main {

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

        String inFile = null;
        String outFile = null;
@@ -78,15 +82,13 @@ public class Main {
            throw new IllegalArgumentException("output format is required");
        }


        System.out.println("in path: " + inFile);
        System.out.println("out path: " + outFile);
        System.out.println("in format: " + inFormat);
        System.out.println("out format: " + outFormat);

        var asl = AndroidSafetyLabel.readFromStream(new FileInputStream(inFile),
                Format.HUMAN_READABLE);
        asl.writeToStream(new FileOutputStream(outFile), Format.ON_DEVICE);
        var asl = AndroidSafetyLabel.readFromStream(new FileInputStream(inFile), inFormat);
        asl.writeToStream(new FileOutputStream(outFile), outFormat);
    }

    private static Format getFormat(String argValue) {
+66 −23
Original line number Diff line number Diff line
@@ -16,13 +16,22 @@

package com.android.asllib;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class AndroidSafetyLabel {

@@ -30,30 +39,64 @@ public class AndroidSafetyLabel {
        NULL, HUMAN_READABLE, ON_DEVICE;
    }

    /**
     * Reads a {@link AndroidSafetyLabel} from an {@link InputStream}.
     */
    private final SafetyLabels mSafetyLabels;

    public SafetyLabels getSafetyLabels() {
        return mSafetyLabels;
    }

    private AndroidSafetyLabel(SafetyLabels safetyLabels) {
        this.mSafetyLabels = safetyLabels;
    }

    /** Reads a {@link AndroidSafetyLabel} from an {@link InputStream}. */
    // TODO(b/329902686): Support conversion in both directions, specified by format.
    public static AndroidSafetyLabel readFromStream(InputStream in, Format format)
            throws IOException {
        System.out.println(format);
        var br = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            throws IOException, ParserConfigurationException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document document = factory.newDocumentBuilder().parse(in);

        Element appMetadataBundles =
                XmlUtils.getSingleElement(document, XmlUtils.HR_TAG_APP_METADATA_BUNDLES);

        return AndroidSafetyLabel.createFromHrElement(appMetadataBundles);
    }
        return new AndroidSafetyLabel();

    /** Write the content of the {@link AndroidSafetyLabel} to a {@link OutputStream}. */
    // TODO(b/329902686): Support conversion in both directions, specified by format.
    public void writeToStream(OutputStream out, Format format)
            throws IOException, ParserConfigurationException, TransformerException {
        var docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        var document = docBuilder.newDocument();
        document.appendChild(this.toOdDomElement(document));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StreamResult streamResult = new StreamResult(out); // out
        DOMSource domSource = new DOMSource(document);
        transformer.transform(domSource, streamResult);
    }

    /**
     * Write the content of the {@link AndroidSafetyLabel} to a {@link OutputStream}.
     */
    public void writeToStream(OutputStream out, Format format) throws IOException {
        var bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        bw.write("Just a Test");
        bw.close();
    /** Creates an {@link AndroidSafetyLabel} from human-readable DOM element */
    public static AndroidSafetyLabel createFromHrElement(Element appMetadataBundlesEle) {
        Element safetyLabelsEle =
                XmlUtils.getSingleElement(appMetadataBundlesEle, XmlUtils.HR_TAG_SAFETY_LABELS);
        SafetyLabels safetyLabels = SafetyLabels.createFromHrElement(safetyLabelsEle);
        return new AndroidSafetyLabel(safetyLabels);
    }

    /** Creates an on-device DOM element from an {@link AndroidSafetyLabel} */
    public Element toOdDomElement(Document doc) {
        Element aslEle = doc.createElement(XmlUtils.OD_TAG_BUNDLE);
        aslEle.appendChild(mSafetyLabels.toOdDomElement(doc));
        return aslEle;
    }

    public static void test() {
        System.out.println("test lib");
        // TODO(b/329902686): Add tests.
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -35,4 +35,9 @@ public class DataCategory {
    public Map<String, DataType> getDataTypes() {
        return mDataTypes;
    }

    /** Creates a {@link DataCategory} given map of {@param dataTypes}. */
    public static DataCategory create(Map<String, DataType> dataTypes) {
        return new DataCategory(dataTypes);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import java.util.HashSet;
import java.util.Set;

/**
 * Constants for determining valid {@link String} data types for usage within {@link SafetyLabel},
 * Constants for determining valid {@link String} data types for usage within {@link SafetyLabels},
 * {@link DataCategory}, and {@link DataType}
 */
public class DataCategoryConstants {
Loading