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

Commit e09d74c6 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Prepare for binary XML pivot.

As part of preparing to enable the new binary XML wire format across
the device, we ran a large suite of unit tests which uncovered a
handful of bugs where client code was assuming that the serializer
would create TEXT tokens for newlines between tags.

Also relaxes all binary XML classes to accept either "UTF-8" or
"utf-8" as the encoding name.  Slight bug fixes to clear names, text,
and attributes for each event to match XmlPullParser documentation.

Fixes for existing unit tests that were assuming human-readable XML,
or assuming that the serialization would perform UTF-8 validation.

Bug: 171832118
Test: atest FrameworksServicesTests
Change-Id: I841fe41e4967b36ab17551294ae0f42fb87a8e71
parent f9266a29
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.util;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.SystemProperties;
import android.system.ErrnoException;
import android.system.Os;

@@ -64,8 +65,11 @@ public class Xml {
    /**
     * Feature flag: when set, {@link #resolveSerializer(OutputStream)} will
     * emit binary XML by default.
     *
     * @hide
     */
    private static final boolean ENABLE_BINARY_DEFAULT = false;
    public static final boolean ENABLE_BINARY_DEFAULT = SystemProperties
            .getBoolean("persist.sys.binary_xml", false);

    /**
     * Parses the given xml string and fires events on the given SAX handler.
@@ -172,7 +176,7 @@ public class Xml {
            if (!in.markSupported()) {
                in = new BufferedInputStream(in);
            }
            in.mark(4);
            in.mark(8);
            in.read(magic);
            in.reset();
        }
+20 −5
Original line number Diff line number Diff line
@@ -95,8 +95,8 @@ public final class BinaryXmlPullParser implements TypedXmlPullParser {
    private Attribute[] mAttributes;

    @Override
    public void setInput(InputStream is, String inputEncoding) throws XmlPullParserException {
        if (inputEncoding != null && !StandardCharsets.UTF_8.name().equals(inputEncoding)) {
    public void setInput(InputStream is, String encoding) throws XmlPullParserException {
        if (encoding != null && !StandardCharsets.UTF_8.name().equalsIgnoreCase(encoding)) {
            throw new UnsupportedOperationException();
        }

@@ -262,19 +262,27 @@ public final class BinaryXmlPullParser implements TypedXmlPullParser {
                break;
            }
            case XmlPullParser.START_DOCUMENT: {
                mCurrentName = null;
                mCurrentText = null;
                if (mAttributeCount > 0) resetAttributes();
                break;
            }
            case XmlPullParser.END_DOCUMENT: {
                mCurrentName = null;
                mCurrentText = null;
                if (mAttributeCount > 0) resetAttributes();
                break;
            }
            case XmlPullParser.START_TAG: {
                mCurrentName = mIn.readInternedUTF();
                resetAttributes();
                mCurrentText = null;
                if (mAttributeCount > 0) resetAttributes();
                break;
            }
            case XmlPullParser.END_TAG: {
                mCurrentName = mIn.readInternedUTF();
                resetAttributes();
                mCurrentText = null;
                if (mAttributeCount > 0) resetAttributes();
                break;
            }
            case XmlPullParser.TEXT:
@@ -283,12 +291,15 @@ public final class BinaryXmlPullParser implements TypedXmlPullParser {
            case XmlPullParser.COMMENT:
            case XmlPullParser.DOCDECL:
            case XmlPullParser.IGNORABLE_WHITESPACE: {
                mCurrentName = null;
                mCurrentText = mIn.readUTF();
                if (mAttributeCount > 0) resetAttributes();
                break;
            }
            case XmlPullParser.ENTITY_REF: {
                mCurrentName = mIn.readUTF();
                mCurrentText = resolveEntity(mCurrentName);
                if (mAttributeCount > 0) resetAttributes();
                break;
            }
            default: {
@@ -428,7 +439,11 @@ public final class BinaryXmlPullParser implements TypedXmlPullParser {
    @Override
    public String getAttributeValue(String namespace, String name) {
        final int index = getAttributeIndex(namespace, name);
        if (index != -1) {
            return mAttributes[index].getValueString();
        } else {
            return null;
        }
    }

    @Override
+5 −2
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ public final class BinaryXmlSerializer implements TypedXmlSerializer {

    @Override
    public void setOutput(@NonNull OutputStream os, @Nullable String encoding) throws IOException {
        if (encoding != null && !StandardCharsets.UTF_8.name().equals(encoding)) {
        if (encoding != null && !StandardCharsets.UTF_8.name().equalsIgnoreCase(encoding)) {
            throw new UnsupportedOperationException();
        }

@@ -144,7 +144,10 @@ public final class BinaryXmlSerializer implements TypedXmlSerializer {
    @Override
    public void startDocument(@Nullable String encoding, @Nullable Boolean standalone)
            throws IOException {
        if (encoding != null && !StandardCharsets.UTF_8.name().equals(encoding)) {
        if (encoding != null && !StandardCharsets.UTF_8.name().equalsIgnoreCase(encoding)) {
            throw new UnsupportedOperationException();
        }
        if (standalone != null && !standalone) {
            throw new UnsupportedOperationException();
        }
        mOut.writeByte(START_DOCUMENT | TYPE_NULL);
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import java.util.Objects;
/**
 * Wrapper which delegates all calls through to the given {@link XmlSerializer}.
 */
public class XmlSerializerWrapper {
public class XmlSerializerWrapper implements XmlSerializer {
    private final XmlSerializer mWrapped;

    public XmlSerializerWrapper(@NonNull XmlSerializer wrapped) {
+13 −3
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public class BinaryXmlTest {
        final TypedXmlPullParser in = Xml.newBinaryPullParser();
        final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        in.setInput(is, StandardCharsets.UTF_8.name());
        assertNext(in, START_TAG, "tag", 1);
        assertNext(in, START_TAG, "tag");
        assertEquals(count, in.getAttributeCount());
    }

@@ -140,7 +140,12 @@ public class BinaryXmlTest {
                doVerifyWrite(xml);
                data = os.toByteArray();
            }
            try (InputStream is = new ByteArrayInputStream(data)) {
            try (InputStream is = new ByteArrayInputStream(data) {
                @Override
                public boolean markSupported() {
                    return false;
                }
            }) {
                doVerifyRead(Xml.resolvePullParser(is));
            }
        }
@@ -152,7 +157,12 @@ public class BinaryXmlTest {
                doVerifyWrite(xml);
                data = os.toByteArray();
            }
            try (InputStream is = new ByteArrayInputStream(data)) {
            try (InputStream is = new ByteArrayInputStream(data) {
                @Override
                public boolean markSupported() {
                    return false;
                }
            }) {
                doVerifyRead(Xml.resolvePullParser(is));
            }
        }
Loading