From 03a7c67082fec708230ebe918ea59028cbd272a1 Mon Sep 17 00:00:00 2001 From: Igor Eisberg <8811086+IgorEisberg@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:58:44 +0300 Subject: [PATCH] clean up tab intendation (#3707) --- .../main/java/android/util/TypedValue.java | 6 +- .../CantFind9PatchChunkException.java | 6 +- .../CantFindFrameworkResException.java | 22 +- .../exceptions/InFileNotFoundException.java | 4 +- .../exceptions/OutDirExistsException.java | 4 +- .../UndefinedResObjectException.java | 6 +- .../androlib/res/decoder/ARSCDecoder.java | 4 +- .../androlib/res/decoder/StringBlock.java | 4 +- .../org/xmlpull/renamed/MXSerializer.java | 1720 ++++++++--------- ...tringBlockWithSurrogatePairInUtf8Test.java | 12 +- brut.j.util/src/main/java/brut/util/OS.java | 2 +- 11 files changed, 895 insertions(+), 895 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/android/util/TypedValue.java b/brut.apktool/apktool-lib/src/main/java/android/util/TypedValue.java index 77d75bf6..914f145c 100644 --- a/brut.apktool/apktool-lib/src/main/java/android/util/TypedValue.java +++ b/brut.apktool/apktool-lib/src/main/java/android/util/TypedValue.java @@ -119,7 +119,7 @@ public class TypedValue { /** Identifies the end of plain integer values. */ public static final int TYPE_LAST_INT = 0x1f; - /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ /** Complex data: bit location of unit information. */ public static final int COMPLEX_UNIT_SHIFT = 0; @@ -182,7 +182,7 @@ public class TypedValue { */ public static final int COMPLEX_MANTISSA_MASK = 0xffffff; - /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ /** * {@link #TYPE_NULL} data indicating the value was not specified. @@ -207,7 +207,7 @@ public class TypedValue { */ public static final int DENSITY_NONE = 0xffff; - /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ /** * The type held by this value, as defined by the constants here. This tells diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFind9PatchChunkException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFind9PatchChunkException.java index 841f9823..acee4d20 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFind9PatchChunkException.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFind9PatchChunkException.java @@ -17,7 +17,7 @@ package brut.androlib.exceptions; public class CantFind9PatchChunkException extends AndrolibException { - public CantFind9PatchChunkException(String message, Throwable cause) { - super(message, cause); - } + public CantFind9PatchChunkException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFindFrameworkResException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFindFrameworkResException.java index 1dc55231..56434610 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFindFrameworkResException.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/CantFindFrameworkResException.java @@ -17,18 +17,18 @@ package brut.androlib.exceptions; public class CantFindFrameworkResException extends AndrolibException { - public CantFindFrameworkResException(int id) { - mPkgId = id; - } + public CantFindFrameworkResException(int id) { + mPkgId = id; + } - public int getPkgId() { - return mPkgId; - } + public int getPkgId() { + return mPkgId; + } - @Override - public String getMessage() { - return String.format("Can't find framework resources for package of id: %d", this.getPkgId()); - } + @Override + public String getMessage() { + return String.format("Can't find framework resources for package of id: %d", this.getPkgId()); + } - private final int mPkgId; + private final int mPkgId; } diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/InFileNotFoundException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/InFileNotFoundException.java index 508682b2..8f06a966 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/InFileNotFoundException.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/InFileNotFoundException.java @@ -17,6 +17,6 @@ package brut.androlib.exceptions; public class InFileNotFoundException extends AndrolibException { - public InFileNotFoundException() { - } + public InFileNotFoundException() { + } } diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/OutDirExistsException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/OutDirExistsException.java index 2cd8346d..994cd48e 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/OutDirExistsException.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/OutDirExistsException.java @@ -17,6 +17,6 @@ package brut.androlib.exceptions; public class OutDirExistsException extends AndrolibException { - public OutDirExistsException() { - } + public OutDirExistsException() { + } } diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/UndefinedResObjectException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/UndefinedResObjectException.java index 14b61d84..ff411bde 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/UndefinedResObjectException.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/exceptions/UndefinedResObjectException.java @@ -17,7 +17,7 @@ package brut.androlib.exceptions; public class UndefinedResObjectException extends AndrolibException { - public UndefinedResObjectException(String message) { - super(message); - } + public UndefinedResObjectException(String message) { + super(message); + } } diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ARSCDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ARSCDecoder.java index 25ea53f3..4b455914 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ARSCDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ARSCDecoder.java @@ -469,12 +469,12 @@ public class ARSCDecoder { } private ResIntBasedValue readValue() throws IOException, AndrolibException { - short size = mIn.readShort(); + short size = mIn.readShort(); if (size < 8) { return null; } - mIn.skipCheckByte((byte) 0); // zero + mIn.skipCheckByte((byte) 0); // zero byte type = mIn.readByte(); int data = mIn.readInt(); diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java index 91237325..2927e06c 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java @@ -262,8 +262,8 @@ public class StringBlock { val = array[offset]; offset += 1; if ((val & 0x80) != 0) { - int low = (array[offset] & 0xFF); - length = ((val & 0x7F) << 8) + low; + int low = (array[offset] & 0xFF); + length = ((val & 0x7F) << 8) + low; offset += 1; } else { length = val; diff --git a/brut.apktool/apktool-lib/src/main/java/org/xmlpull/renamed/MXSerializer.java b/brut.apktool/apktool-lib/src/main/java/org/xmlpull/renamed/MXSerializer.java index f950bd25..96c5f41f 100644 --- a/brut.apktool/apktool-lib/src/main/java/org/xmlpull/renamed/MXSerializer.java +++ b/brut.apktool/apktool-lib/src/main/java/org/xmlpull/renamed/MXSerializer.java @@ -44,250 +44,250 @@ import java.util.Set; * */ public class MXSerializer implements XmlSerializer { - protected final static String XML_URI = "http://www.w3.org/XML/1998/namespace"; - protected final static String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; - private static final boolean TRACE_SIZING = false; - private static final boolean TRACE_ESCAPING = false; + protected final static String XML_URI = "http://www.w3.org/XML/1998/namespace"; + protected final static String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; + private static final boolean TRACE_SIZING = false; + private static final boolean TRACE_ESCAPING = false; - protected final String FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE = "http://xmlpull.org/v1/doc/features.html#serializer-attvalue-use-apostrophe"; - protected final String FEATURE_NAMES_INTERNED = "http://xmlpull.org/v1/doc/features.html#names-interned"; - protected final String PROPERTY_SERIALIZER_INDENTATION = "http://xmlpull.org/v1/doc/properties.html#serializer-indentation"; - protected final String PROPERTY_SERIALIZER_LINE_SEPARATOR = "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator"; - protected final static String PROPERTY_LOCATION = "http://xmlpull.org/v1/doc/properties.html#location"; + protected final String FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE = "http://xmlpull.org/v1/doc/features.html#serializer-attvalue-use-apostrophe"; + protected final String FEATURE_NAMES_INTERNED = "http://xmlpull.org/v1/doc/features.html#names-interned"; + protected final String PROPERTY_SERIALIZER_INDENTATION = "http://xmlpull.org/v1/doc/properties.html#serializer-indentation"; + protected final String PROPERTY_SERIALIZER_LINE_SEPARATOR = "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator"; + protected final static String PROPERTY_LOCATION = "http://xmlpull.org/v1/doc/properties.html#location"; - // properties/features - protected boolean namesInterned; - protected boolean attributeUseApostrophe; - protected String indentationString = null; // " "; - protected String lineSeparator = "\n"; + // properties/features + protected boolean namesInterned; + protected boolean attributeUseApostrophe; + protected String indentationString = null; // " "; + protected String lineSeparator = "\n"; - protected String location; - protected Writer out; + protected String location; + protected Writer out; - protected int autoDeclaredPrefixes; + protected int autoDeclaredPrefixes; - protected int depth = 0; + protected int depth = 0; - // element stack - protected String[] elNamespace = new String[2]; - protected String[] elName = new String[elNamespace.length]; - protected String[] elPrefix = new String[elNamespace.length]; - protected int[] elNamespaceCount = new int[elNamespace.length]; + // element stack + protected String[] elNamespace = new String[2]; + protected String[] elName = new String[elNamespace.length]; + protected String[] elPrefix = new String[elNamespace.length]; + protected int[] elNamespaceCount = new int[elNamespace.length]; - // namespace stack - protected int namespaceEnd = 0; - protected String[] namespacePrefix = new String[8]; - protected String[] namespaceUri = new String[namespacePrefix.length]; + // namespace stack + protected int namespaceEnd = 0; + protected String[] namespacePrefix = new String[8]; + protected String[] namespaceUri = new String[namespacePrefix.length]; - protected boolean finished; - protected boolean pastRoot; - protected boolean setPrefixCalled; - protected boolean startTagIncomplete; + protected boolean finished; + protected boolean pastRoot; + protected boolean setPrefixCalled; + protected boolean startTagIncomplete; - protected boolean doIndent; - protected boolean seenTag; + protected boolean doIndent; + protected boolean seenTag; - protected boolean seenBracket; - protected boolean seenBracketBracket; + protected boolean seenBracket; + protected boolean seenBracketBracket; - // buffer output if needed to write escaped String see text(String) - private static final int BUF_LEN = Runtime.getRuntime().freeMemory() > 1000000L ? 8 * 1024 : 256; - protected char[] buf = new char[BUF_LEN]; + // buffer output if needed to write escaped String see text(String) + private static final int BUF_LEN = Runtime.getRuntime().freeMemory() > 1000000L ? 8 * 1024 : 256; + protected char[] buf = new char[BUF_LEN]; - protected static final String[] precomputedPrefixes; + protected static final String[] precomputedPrefixes; - static { - precomputedPrefixes = new String[32]; // arbitrary number ... - for (int i = 0; i < precomputedPrefixes.length; i++) { - precomputedPrefixes[i] = ("n" + i).intern(); - } - } + static { + precomputedPrefixes = new String[32]; // arbitrary number ... + for (int i = 0; i < precomputedPrefixes.length; i++) { + precomputedPrefixes[i] = ("n" + i).intern(); + } + } - private final boolean checkNamesInterned = false; + private final boolean checkNamesInterned = false; - private void checkInterning(String name) { - if (namesInterned && !Objects.equals(name, name.intern())) { - throw new IllegalArgumentException("all names passed as arguments must be interned" - + "when NAMES INTERNED feature is enabled"); - } - } + private void checkInterning(String name) { + if (namesInterned && !Objects.equals(name, name.intern())) { + throw new IllegalArgumentException("all names passed as arguments must be interned" + + "when NAMES INTERNED feature is enabled"); + } + } - protected void reset() { - location = null; - out = null; - autoDeclaredPrefixes = 0; - depth = 0; + protected void reset() { + location = null; + out = null; + autoDeclaredPrefixes = 0; + depth = 0; - // nullify references on all levels to allow it to be GCed - for (int i = 0; i < elNamespaceCount.length; i++) { - elName[i] = null; - elPrefix[i] = null; - elNamespace[i] = null; - elNamespaceCount[i] = 2; - } + // nullify references on all levels to allow it to be GCed + for (int i = 0; i < elNamespaceCount.length; i++) { + elName[i] = null; + elPrefix[i] = null; + elNamespace[i] = null; + elNamespaceCount[i] = 2; + } - namespaceEnd = 0; + namespaceEnd = 0; - // TODO: how to prevent from reporting this namespace? - // this is special namespace declared for consistency with XML infoset - namespacePrefix[namespaceEnd] = "xmlns"; - namespaceUri[namespaceEnd] = XMLNS_URI; - ++namespaceEnd; + // TODO: how to prevent from reporting this namespace? + // this is special namespace declared for consistency with XML infoset + namespacePrefix[namespaceEnd] = "xmlns"; + namespaceUri[namespaceEnd] = XMLNS_URI; + ++namespaceEnd; - namespacePrefix[namespaceEnd] = "xml"; - namespaceUri[namespaceEnd] = XML_URI; - ++namespaceEnd; + namespacePrefix[namespaceEnd] = "xml"; + namespaceUri[namespaceEnd] = XML_URI; + ++namespaceEnd; - finished = false; - pastRoot = false; - setPrefixCalled = false; - startTagIncomplete = false; - seenTag = false; + finished = false; + pastRoot = false; + setPrefixCalled = false; + startTagIncomplete = false; + seenTag = false; - seenBracket = false; - seenBracketBracket = false; - } + seenBracket = false; + seenBracketBracket = false; + } - protected void ensureElementsCapacity() { - final int elStackSize = elName.length; - final int newSize = (depth >= 7 ? 2 * depth : 8) + 2; + protected void ensureElementsCapacity() { + final int elStackSize = elName.length; + final int newSize = (depth >= 7 ? 2 * depth : 8) + 2; - if (TRACE_SIZING) { - System.err.println(getClass().getName() + " elStackSize " - + elStackSize + " ==> " + newSize); - } - final boolean needsCopying = elStackSize > 0; - String[] arr; - // reuse arr local variable slot - arr = new String[newSize]; - if (needsCopying) - System.arraycopy(elName, 0, arr, 0, elStackSize); - elName = arr; + if (TRACE_SIZING) { + System.err.println(getClass().getName() + " elStackSize " + + elStackSize + " ==> " + newSize); + } + final boolean needsCopying = elStackSize > 0; + String[] arr; + // reuse arr local variable slot + arr = new String[newSize]; + if (needsCopying) + System.arraycopy(elName, 0, arr, 0, elStackSize); + elName = arr; - arr = new String[newSize]; - if (needsCopying) - System.arraycopy(elPrefix, 0, arr, 0, elStackSize); - elPrefix = arr; + arr = new String[newSize]; + if (needsCopying) + System.arraycopy(elPrefix, 0, arr, 0, elStackSize); + elPrefix = arr; - arr = new String[newSize]; - if (needsCopying) - System.arraycopy(elNamespace, 0, arr, 0, elStackSize); - elNamespace = arr; + arr = new String[newSize]; + if (needsCopying) + System.arraycopy(elNamespace, 0, arr, 0, elStackSize); + elNamespace = arr; - final int[] iarr = new int[newSize]; - if (needsCopying) { - System.arraycopy(elNamespaceCount, 0, iarr, 0, elStackSize); - } else { - // special initialization - iarr[0] = 0; - } - elNamespaceCount = iarr; - } + final int[] iarr = new int[newSize]; + if (needsCopying) { + System.arraycopy(elNamespaceCount, 0, iarr, 0, elStackSize); + } else { + // special initialization + iarr[0] = 0; + } + elNamespaceCount = iarr; + } - protected void ensureNamespacesCapacity() { // int size) { - final int newSize = namespaceEnd > 7 ? 2 * namespaceEnd : 8; - if (TRACE_SIZING) { - System.err.println(getClass().getName() + " namespaceSize " + namespacePrefix.length + " ==> " + newSize); - } - final String[] newNamespacePrefix = new String[newSize]; - final String[] newNamespaceUri = new String[newSize]; - if (namespacePrefix != null) { - System.arraycopy(namespacePrefix, 0, newNamespacePrefix, 0, namespaceEnd); - System.arraycopy(namespaceUri, 0, newNamespaceUri, 0, namespaceEnd); - } - namespacePrefix = newNamespacePrefix; - namespaceUri = newNamespaceUri; - } + protected void ensureNamespacesCapacity() { // int size) { + final int newSize = namespaceEnd > 7 ? 2 * namespaceEnd : 8; + if (TRACE_SIZING) { + System.err.println(getClass().getName() + " namespaceSize " + namespacePrefix.length + " ==> " + newSize); + } + final String[] newNamespacePrefix = new String[newSize]; + final String[] newNamespaceUri = new String[newSize]; + if (namespacePrefix != null) { + System.arraycopy(namespacePrefix, 0, newNamespacePrefix, 0, namespaceEnd); + System.arraycopy(namespaceUri, 0, newNamespaceUri, 0, namespaceEnd); + } + namespacePrefix = newNamespacePrefix; + namespaceUri = newNamespaceUri; + } - @Override - public void setFeature(String name, boolean state) - throws IllegalArgumentException, IllegalStateException { - if (name == null) { - throw new IllegalArgumentException("feature name can not be null"); - } - if (FEATURE_NAMES_INTERNED.equals(name)) { - namesInterned = state; - } else if (FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE.equals(name)) { - attributeUseApostrophe = state; - } else { - throw new IllegalStateException("unsupported feature " + name); - } - } + @Override + public void setFeature(String name, boolean state) + throws IllegalArgumentException, IllegalStateException { + if (name == null) { + throw new IllegalArgumentException("feature name can not be null"); + } + if (FEATURE_NAMES_INTERNED.equals(name)) { + namesInterned = state; + } else if (FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE.equals(name)) { + attributeUseApostrophe = state; + } else { + throw new IllegalStateException("unsupported feature " + name); + } + } - @Override - public boolean getFeature(String name) throws IllegalArgumentException { - if (name == null) { - throw new IllegalArgumentException("feature name can not be null"); - } - if (FEATURE_NAMES_INTERNED.equals(name)) { - return namesInterned; - } else if (FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE.equals(name)) { - return attributeUseApostrophe; - } else { - return false; - } - } + @Override + public boolean getFeature(String name) throws IllegalArgumentException { + if (name == null) { + throw new IllegalArgumentException("feature name can not be null"); + } + if (FEATURE_NAMES_INTERNED.equals(name)) { + return namesInterned; + } else if (FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE.equals(name)) { + return attributeUseApostrophe; + } else { + return false; + } + } - // precomputed variables to simplify writing indentation - protected int offsetNewLine; - protected int indentationJump; - protected char[] indentationBuf; - protected int maxIndentLevel; - protected boolean writeLineSepartor; // should end-of-line be written - protected boolean writeIndentation; // is indentation used? + // precomputed variables to simplify writing indentation + protected int offsetNewLine; + protected int indentationJump; + protected char[] indentationBuf; + protected int maxIndentLevel; + protected boolean writeLineSepartor; // should end-of-line be written + protected boolean writeIndentation; // is indentation used? - /** - * For maximum efficiency when writing indents the required output is - * pre-computed This is internal function that recomputes buffer after user - * requested chnages. - */ - protected void rebuildIndentationBuf() { - if (!doIndent) - return; - final int maxIndent = 65; // hardcoded maximum indentation size in characters - int bufSize = 0; - offsetNewLine = 0; - if (writeLineSepartor) { - offsetNewLine = lineSeparator.length(); - bufSize += offsetNewLine; - } - maxIndentLevel = 0; - if (writeIndentation) { - indentationJump = indentationString.length(); - maxIndentLevel = maxIndent / indentationJump; - bufSize += maxIndentLevel * indentationJump; - } - if (indentationBuf == null || indentationBuf.length < bufSize) { - indentationBuf = new char[bufSize + 8]; - } - int bufPos = 0; - if (writeLineSepartor) { - for (int i = 0; i < lineSeparator.length(); i++) { - indentationBuf[bufPos++] = lineSeparator.charAt(i); - } - } - if (writeIndentation) { - for (int i = 0; i < maxIndentLevel; i++) { - for (int j = 0; j < indentationString.length(); j++) { - indentationBuf[bufPos++] = indentationString.charAt(j); - } - } - } - } + /** + * For maximum efficiency when writing indents the required output is + * pre-computed This is internal function that recomputes buffer after user + * requested chnages. + */ + protected void rebuildIndentationBuf() { + if (!doIndent) + return; + final int maxIndent = 65; // hardcoded maximum indentation size in characters + int bufSize = 0; + offsetNewLine = 0; + if (writeLineSepartor) { + offsetNewLine = lineSeparator.length(); + bufSize += offsetNewLine; + } + maxIndentLevel = 0; + if (writeIndentation) { + indentationJump = indentationString.length(); + maxIndentLevel = maxIndent / indentationJump; + bufSize += maxIndentLevel * indentationJump; + } + if (indentationBuf == null || indentationBuf.length < bufSize) { + indentationBuf = new char[bufSize + 8]; + } + int bufPos = 0; + if (writeLineSepartor) { + for (int i = 0; i < lineSeparator.length(); i++) { + indentationBuf[bufPos++] = lineSeparator.charAt(i); + } + } + if (writeIndentation) { + for (int i = 0; i < maxIndentLevel; i++) { + for (int j = 0; j < indentationString.length(); j++) { + indentationBuf[bufPos++] = indentationString.charAt(j); + } + } + } + } - protected void writeIndent() throws IOException { - final int start = writeLineSepartor ? 0 : offsetNewLine; - final int level = Math.min(depth, maxIndentLevel); + protected void writeIndent() throws IOException { + final int start = writeLineSepartor ? 0 : offsetNewLine; + final int level = Math.min(depth, maxIndentLevel); - out.write(indentationBuf, start, ((level - 1) * indentationJump) + offsetNewLine); - } + out.write(indentationBuf, start, ((level - 1) * indentationJump) + offsetNewLine); + } - @Override - public void setProperty(String name, Object value) - throws IllegalArgumentException, IllegalStateException { - if (name == null) { - throw new IllegalArgumentException("property name can not be null"); - } + @Override + public void setProperty(String name, Object value) + throws IllegalArgumentException, IllegalStateException { + if (name == null) { + throw new IllegalArgumentException("property name can not be null"); + } switch (name) { case PROPERTY_SERIALIZER_INDENTATION: indentationString = (String) value; @@ -301,23 +301,23 @@ public class MXSerializer implements XmlSerializer { default: throw new IllegalStateException("unsupported property " + name); } - writeLineSepartor = lineSeparator != null && lineSeparator.length() > 0; - writeIndentation = indentationString != null - && indentationString.length() > 0; - // optimize - do not write when nothing to write ... - doIndent = indentationString != null - && (writeLineSepartor || writeIndentation); - // NOTE: when indentationString == null there is no indentation - // (even though writeLineSeparator may be true ...) - rebuildIndentationBuf(); - seenTag = false; // for consistency - } + writeLineSepartor = lineSeparator != null && lineSeparator.length() > 0; + writeIndentation = indentationString != null + && indentationString.length() > 0; + // optimize - do not write when nothing to write ... + doIndent = indentationString != null + && (writeLineSepartor || writeIndentation); + // NOTE: when indentationString == null there is no indentation + // (even though writeLineSeparator may be true ...) + rebuildIndentationBuf(); + seenTag = false; // for consistency + } - @Override - public Object getProperty(String name) throws IllegalArgumentException { - if (name == null) { - throw new IllegalArgumentException("property name can not be null"); - } + @Override + public Object getProperty(String name) throws IllegalArgumentException { + if (name == null) { + throw new IllegalArgumentException("property name can not be null"); + } switch (name) { case PROPERTY_SERIALIZER_INDENTATION: return indentationString; @@ -328,149 +328,149 @@ public class MXSerializer implements XmlSerializer { default: return null; } - } + } - private String getLocation() { - return location != null ? " @" + location : ""; - } + private String getLocation() { + return location != null ? " @" + location : ""; + } - // this is special method that can be accessed directly to retrieve Writer - // serializer is using - public Writer getWriter() { - return out; - } + // this is special method that can be accessed directly to retrieve Writer + // serializer is using + public Writer getWriter() { + return out; + } - @Override - public void setOutput(Writer writer) { - reset(); - out = writer; - } + @Override + public void setOutput(Writer writer) { + reset(); + out = writer; + } - @Override - public void setOutput(OutputStream os, String encoding) throws IOException { - if (os == null) - throw new IllegalArgumentException("output stream can not be null"); - reset(); - if (encoding != null) { - out = new OutputStreamWriter(os, encoding); - } else { - out = new OutputStreamWriter(os); - } - } + @Override + public void setOutput(OutputStream os, String encoding) throws IOException { + if (os == null) + throw new IllegalArgumentException("output stream can not be null"); + reset(); + if (encoding != null) { + out = new OutputStreamWriter(os, encoding); + } else { + out = new OutputStreamWriter(os); + } + } - @Override - public void startDocument(String encoding, Boolean standalone) - throws IOException { - if (attributeUseApostrophe) { - out.write(""); - } + @Override + public void startDocument(String encoding, Boolean standalone) + throws IOException { + if (attributeUseApostrophe) { + out.write(""); + } - @Override - public void endDocument() throws IOException { - // close all unclosed tag; - while (depth > 0) { - endTag(elNamespace[depth], elName[depth]); - } - finished = pastRoot = startTagIncomplete = true; - out.flush(); - } + @Override + public void endDocument() throws IOException { + // close all unclosed tag; + while (depth > 0) { + endTag(elNamespace[depth], elName[depth]); + } + finished = pastRoot = startTagIncomplete = true; + out.flush(); + } - @Override - public void setPrefix(String prefix, String namespace) throws IOException { - if (startTagIncomplete) - closeStartTag(); + @Override + public void setPrefix(String prefix, String namespace) throws IOException { + if (startTagIncomplete) + closeStartTag(); - if (prefix == null) { - prefix = ""; - } - if (!namesInterned) { - prefix = prefix.intern(); // will throw NPE if prefix==null - } else if (checkNamesInterned) { - checkInterning(prefix); - } else if (prefix == null) { - throw new IllegalArgumentException("prefix must be not null" + getLocation()); - } + if (prefix == null) { + prefix = ""; + } + if (!namesInterned) { + prefix = prefix.intern(); // will throw NPE if prefix==null + } else if (checkNamesInterned) { + checkInterning(prefix); + } else if (prefix == null) { + throw new IllegalArgumentException("prefix must be not null" + getLocation()); + } - if (!namesInterned) { - namespace = namespace.intern(); - } else if (checkNamesInterned) { - checkInterning(namespace); - } else if (namespace == null) { - throw new IllegalArgumentException("namespace must be not null" + getLocation()); - } + if (!namesInterned) { + namespace = namespace.intern(); + } else if (checkNamesInterned) { + checkInterning(namespace); + } else if (namespace == null) { + throw new IllegalArgumentException("namespace must be not null" + getLocation()); + } - if (namespaceEnd >= namespacePrefix.length) { - ensureNamespacesCapacity(); - } - namespacePrefix[namespaceEnd] = prefix; - namespaceUri[namespaceEnd] = namespace; - ++namespaceEnd; - setPrefixCalled = true; - } + if (namespaceEnd >= namespacePrefix.length) { + ensureNamespacesCapacity(); + } + namespacePrefix[namespaceEnd] = prefix; + namespaceUri[namespaceEnd] = namespace; + ++namespaceEnd; + setPrefixCalled = true; + } - protected String lookupOrDeclarePrefix(String namespace) { - return getPrefix(namespace, true); - } + protected String lookupOrDeclarePrefix(String namespace) { + return getPrefix(namespace, true); + } - @Override - public String getPrefix(String namespace, boolean generatePrefix) { - return getPrefix(namespace, generatePrefix, false); - } + @Override + public String getPrefix(String namespace, boolean generatePrefix) { + return getPrefix(namespace, generatePrefix, false); + } - protected String getPrefix(String namespace, boolean generatePrefix, - boolean nonEmpty) { - if (!namesInterned) { - // when String is interned we can do much faster namespace stack lookups ... - namespace = namespace.intern(); - } else if (checkNamesInterned) { - checkInterning(namespace); - } - if (namespace == null) { - throw new IllegalArgumentException("namespace must be not null" + getLocation()); - } else if (namespace.length() == 0) { - throw new IllegalArgumentException("default namespace cannot have prefix" + getLocation()); - } + protected String getPrefix(String namespace, boolean generatePrefix, + boolean nonEmpty) { + if (!namesInterned) { + // when String is interned we can do much faster namespace stack lookups ... + namespace = namespace.intern(); + } else if (checkNamesInterned) { + checkInterning(namespace); + } + if (namespace == null) { + throw new IllegalArgumentException("namespace must be not null" + getLocation()); + } else if (namespace.length() == 0) { + throw new IllegalArgumentException("default namespace cannot have prefix" + getLocation()); + } - // first check if namespace is already in scope - for (int i = namespaceEnd - 1; i >= 0; --i) { - if (namespace.equals(namespaceUri[i])) { - final String prefix = namespacePrefix[i]; - if (nonEmpty && prefix.length() == 0) { - continue; + // first check if namespace is already in scope + for (int i = namespaceEnd - 1; i >= 0; --i) { + if (namespace.equals(namespaceUri[i])) { + final String prefix = namespacePrefix[i]; + if (nonEmpty && prefix.length() == 0) { + continue; } - return prefix; - } - } + return prefix; + } + } - // so not found it ... - if (!generatePrefix) { - return null; - } - return generatePrefix(namespace); - } + // so not found it ... + if (!generatePrefix) { + return null; + } + return generatePrefix(namespace); + } - private String generatePrefix(String namespace) { + private String generatePrefix(String namespace) { ++autoDeclaredPrefixes; // fast lookup uses table that was pre-initialized in static{} .... final String prefix = autoDeclaredPrefixes < precomputedPrefixes.length @@ -486,170 +486,170 @@ public class MXSerializer implements XmlSerializer { ++namespaceEnd; return prefix; - } + } - @Override - public int getDepth() { - return depth; - } + @Override + public int getDepth() { + return depth; + } - @Override - public String getNamespace() { - return elNamespace[depth]; - } + @Override + public String getNamespace() { + return elNamespace[depth]; + } - @Override - public String getName() { - return elName[depth]; - } + @Override + public String getName() { + return elName[depth]; + } - @Override - public XmlSerializer startTag(String namespace, String name) - throws IOException { - if (startTagIncomplete) { - closeStartTag(); - } - seenBracket = seenBracketBracket = false; - ++depth; - if (doIndent && depth > 0 && seenTag) { - writeIndent(); - } - seenTag = true; - setPrefixCalled = false; - startTagIncomplete = true; - if ((depth + 1) >= elName.length) { - ensureElementsCapacity(); - } + @Override + public XmlSerializer startTag(String namespace, String name) + throws IOException { + if (startTagIncomplete) { + closeStartTag(); + } + seenBracket = seenBracketBracket = false; + ++depth; + if (doIndent && depth > 0 && seenTag) { + writeIndent(); + } + seenTag = true; + setPrefixCalled = false; + startTagIncomplete = true; + if ((depth + 1) >= elName.length) { + ensureElementsCapacity(); + } - if (checkNamesInterned && namesInterned) - checkInterning(namespace); + if (checkNamesInterned && namesInterned) + checkInterning(namespace); - elNamespace[depth] = (namesInterned || namespace == null) ? namespace : namespace.intern(); - if (checkNamesInterned && namesInterned) - checkInterning(name); + elNamespace[depth] = (namesInterned || namespace == null) ? namespace : namespace.intern(); + if (checkNamesInterned && namesInterned) + checkInterning(name); - elName[depth] = (namesInterned || name == null) ? name : name.intern(); - if (out == null) { - throw new IllegalStateException("setOutput() must called set before serialization can start"); - } - out.write('<'); - if (namespace != null) { - if (namespace.length() > 0) { - // in future make this algo a feature on serializer - String prefix = null; - if (depth > 0 && (namespaceEnd - elNamespaceCount[depth - 1]) == 1) { - // if only one prefix was declared un-declare it if the - // prefix is already declared on parent el with the same URI - String uri = namespaceUri[namespaceEnd - 1]; - if (uri == namespace || uri.equals(namespace)) { - String elPfx = namespacePrefix[namespaceEnd - 1]; - for (int pos = elNamespaceCount[depth - 1] - 1; pos >= 2; --pos) { - String pf = namespacePrefix[pos]; - if (pf == elPfx || pf.equals(elPfx)) { - String n = namespaceUri[pos]; - if (n == uri || n.equals(uri)) { - --namespaceEnd; // un-declare namespace: this is kludge! - prefix = elPfx; - } - break; - } - } - } - } - if (prefix == null) { - prefix = lookupOrDeclarePrefix(namespace); - } - // make sure that default ("") namespace to not print ":" - if (prefix.length() > 0) { - elPrefix[depth] = prefix; - out.write(prefix); - out.write(':'); - } else { - elPrefix[depth] = ""; - } - } else { - // make sure that default namespace can be declared - for (int i = namespaceEnd - 1; i >= 0; --i) { - if (namespacePrefix[i] == "") { - final String uri = namespaceUri[i]; - if (uri == null) { - setPrefix("", ""); - } else if (uri.length() > 0) { - throw new IllegalStateException("start tag can not be written in empty default namespace " - + "as default namespace is currently bound to '" - + uri + "'" + getLocation()); - } - break; - } - } - elPrefix[depth] = ""; - } - } else { - elPrefix[depth] = ""; - } - out.write(name); - return this; - } + elName[depth] = (namesInterned || name == null) ? name : name.intern(); + if (out == null) { + throw new IllegalStateException("setOutput() must called set before serialization can start"); + } + out.write('<'); + if (namespace != null) { + if (namespace.length() > 0) { + // in future make this algo a feature on serializer + String prefix = null; + if (depth > 0 && (namespaceEnd - elNamespaceCount[depth - 1]) == 1) { + // if only one prefix was declared un-declare it if the + // prefix is already declared on parent el with the same URI + String uri = namespaceUri[namespaceEnd - 1]; + if (uri == namespace || uri.equals(namespace)) { + String elPfx = namespacePrefix[namespaceEnd - 1]; + for (int pos = elNamespaceCount[depth - 1] - 1; pos >= 2; --pos) { + String pf = namespacePrefix[pos]; + if (pf == elPfx || pf.equals(elPfx)) { + String n = namespaceUri[pos]; + if (n == uri || n.equals(uri)) { + --namespaceEnd; // un-declare namespace: this is kludge! + prefix = elPfx; + } + break; + } + } + } + } + if (prefix == null) { + prefix = lookupOrDeclarePrefix(namespace); + } + // make sure that default ("") namespace to not print ":" + if (prefix.length() > 0) { + elPrefix[depth] = prefix; + out.write(prefix); + out.write(':'); + } else { + elPrefix[depth] = ""; + } + } else { + // make sure that default namespace can be declared + for (int i = namespaceEnd - 1; i >= 0; --i) { + if (namespacePrefix[i] == "") { + final String uri = namespaceUri[i]; + if (uri == null) { + setPrefix("", ""); + } else if (uri.length() > 0) { + throw new IllegalStateException("start tag can not be written in empty default namespace " + + "as default namespace is currently bound to '" + + uri + "'" + getLocation()); + } + break; + } + } + elPrefix[depth] = ""; + } + } else { + elPrefix[depth] = ""; + } + out.write(name); + return this; + } - @Override - public XmlSerializer attribute(String namespace, String name, String value) - throws IOException { - if (!startTagIncomplete) { - throw new IllegalArgumentException("startTag() must be called before attribute()" + getLocation()); - } - out.write(' '); - if (namespace != null && namespace.length() > 0) { - if (!namesInterned) { - namespace = namespace.intern(); - } else if (checkNamesInterned) { - checkInterning(namespace); - } - String prefix = getPrefix(namespace, false, true); - if (prefix == null) { - // needs to declare prefix to hold default namespace - // NOTE: attributes such as a='b' are in NO namespace - prefix = generatePrefix(namespace); - } - out.write(prefix); - out.write(':'); - } - out.write(name); - out.write('='); - out.write(attributeUseApostrophe ? '\'' : '"'); - writeAttributeValue(value, out); - out.write(attributeUseApostrophe ? '\'' : '"'); - return this; - } + @Override + public XmlSerializer attribute(String namespace, String name, String value) + throws IOException { + if (!startTagIncomplete) { + throw new IllegalArgumentException("startTag() must be called before attribute()" + getLocation()); + } + out.write(' '); + if (namespace != null && namespace.length() > 0) { + if (!namesInterned) { + namespace = namespace.intern(); + } else if (checkNamesInterned) { + checkInterning(namespace); + } + String prefix = getPrefix(namespace, false, true); + if (prefix == null) { + // needs to declare prefix to hold default namespace + // NOTE: attributes such as a='b' are in NO namespace + prefix = generatePrefix(namespace); + } + out.write(prefix); + out.write(':'); + } + out.write(name); + out.write('='); + out.write(attributeUseApostrophe ? '\'' : '"'); + writeAttributeValue(value, out); + out.write(attributeUseApostrophe ? '\'' : '"'); + return this; + } - protected void closeStartTag() throws IOException { - if (finished) { - throw new IllegalArgumentException("trying to write past already finished output" - + getLocation()); - } - if (seenBracket) { - seenBracket = seenBracketBracket = false; - } - if (startTagIncomplete || setPrefixCalled) { - if (setPrefixCalled) { - throw new IllegalArgumentException("startTag() must be called immediately after setPrefix()" - + getLocation()); - } - if (!startTagIncomplete) { - throw new IllegalArgumentException("trying to close start tag that is not opened" - + getLocation()); - } + protected void closeStartTag() throws IOException { + if (finished) { + throw new IllegalArgumentException("trying to write past already finished output" + + getLocation()); + } + if (seenBracket) { + seenBracket = seenBracketBracket = false; + } + if (startTagIncomplete || setPrefixCalled) { + if (setPrefixCalled) { + throw new IllegalArgumentException("startTag() must be called immediately after setPrefix()" + + getLocation()); + } + if (!startTagIncomplete) { + throw new IllegalArgumentException("trying to close start tag that is not opened" + + getLocation()); + } - // write all namespace declarations! - writeNamespaceDeclarations(); - out.write('>'); - elNamespaceCount[depth] = namespaceEnd; - startTagIncomplete = false; - } - } + // write all namespace declarations! + writeNamespaceDeclarations(); + out.write('>'); + elNamespaceCount[depth] = namespaceEnd; + startTagIncomplete = false; + } + } - protected void writeNamespaceDeclarations() throws IOException { + protected void writeNamespaceDeclarations() throws IOException { Set uniqueNamespaces = new HashSet<>(); - for (int i = elNamespaceCount[depth - 1]; i < namespaceEnd; i++) { + for (int i = elNamespaceCount[depth - 1]; i < namespaceEnd; i++) { String prefix = namespacePrefix[i]; String uri = namespaceUri[i]; @@ -659,395 +659,395 @@ public class MXSerializer implements XmlSerializer { continue; } - if (doIndent && uri.length() > 40) { - writeIndent(); - out.write(" "); - } - if (prefix != "") { - out.write(" xmlns:"); - out.write(prefix); - out.write('='); - } else { - out.write(" xmlns="); - } - out.write(attributeUseApostrophe ? '\'' : '"'); + if (doIndent && uri.length() > 40) { + writeIndent(); + out.write(" "); + } + if (prefix != "") { + out.write(" xmlns:"); + out.write(prefix); + out.write('='); + } else { + out.write(" xmlns="); + } + out.write(attributeUseApostrophe ? '\'' : '"'); - // NOTE: escaping of namespace value the same way as attributes!!!! - writeAttributeValue(uri, out); - out.write(attributeUseApostrophe ? '\'' : '"'); + // NOTE: escaping of namespace value the same way as attributes!!!! + writeAttributeValue(uri, out); + out.write(attributeUseApostrophe ? '\'' : '"'); uniqueNamespaces.add(prefix + uri); - } - } + } + } - @Override - public XmlSerializer endTag(String namespace, String name) - throws IOException { - seenBracket = seenBracketBracket = false; - if (namespace != null) { - if (!namesInterned) { - namespace = namespace.intern(); - } else if (checkNamesInterned) { - checkInterning(namespace); - } - } + @Override + public XmlSerializer endTag(String namespace, String name) + throws IOException { + seenBracket = seenBracketBracket = false; + if (namespace != null) { + if (!namesInterned) { + namespace = namespace.intern(); + } else if (checkNamesInterned) { + checkInterning(namespace); + } + } - if (name == null) { - throw new IllegalArgumentException("end tag name can not be null" + getLocation()); - } - if (checkNamesInterned && namesInterned) { - checkInterning(name); - } - if (startTagIncomplete) { - writeNamespaceDeclarations(); - out.write(" />"); // space is added to make it easier to work in XHTML!!! + if (name == null) { + throw new IllegalArgumentException("end tag name can not be null" + getLocation()); + } + if (checkNamesInterned && namesInterned) { + checkInterning(name); + } + if (startTagIncomplete) { + writeNamespaceDeclarations(); + out.write(" />"); // space is added to make it easier to work in XHTML!!! } else { - if (doIndent && seenTag) { - writeIndent(); - } - out.write(" 0) { - out.write(startTagPrefix); - out.write(':'); - } - out.write(name); - out.write('>'); + if (doIndent && seenTag) { + writeIndent(); + } + out.write(" 0) { + out.write(startTagPrefix); + out.write(':'); + } + out.write(name); + out.write('>'); } --depth; namespaceEnd = elNamespaceCount[depth]; - startTagIncomplete = false; - seenTag = true; - return this; - } + startTagIncomplete = false; + seenTag = true; + return this; + } - @Override - public XmlSerializer text(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - writeElementContent(text, out); - return this; - } + @Override + public XmlSerializer text(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + writeElementContent(text, out); + return this; + } - @Override - public XmlSerializer text(char[] buf, int start, int len) - throws IOException { - if (startTagIncomplete || setPrefixCalled) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - writeElementContent(buf, start, len, out); - return this; - } + @Override + public XmlSerializer text(char[] buf, int start, int len) + throws IOException { + if (startTagIncomplete || setPrefixCalled) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + writeElementContent(buf, start, len, out); + return this; + } - @Override - public void cdsect(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled || seenBracket) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - out.write(""); - } + @Override + public void cdsect(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled || seenBracket) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + out.write(""); + } - @Override - public void entityRef(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled || seenBracket) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - out.write('&'); - out.write(text); // escape? - out.write(';'); - } + @Override + public void entityRef(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled || seenBracket) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + out.write('&'); + out.write(text); // escape? + out.write(';'); + } - @Override - public void processingInstruction(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled || seenBracket) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - out.write(""); - } + @Override + public void processingInstruction(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled || seenBracket) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + out.write(""); + } - @Override - public void comment(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled || seenBracket) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - out.write(""); - } + @Override + public void comment(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled || seenBracket) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + out.write(""); + } - @Override - public void docdecl(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled || seenBracket) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - out.write(""); - } + @Override + public void docdecl(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled || seenBracket) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + out.write(""); + } - @Override - public void ignorableWhitespace(String text) throws IOException { - if (startTagIncomplete || setPrefixCalled || seenBracket) - closeStartTag(); - if (doIndent && seenTag) - seenTag = false; - if (text.length() == 0) { - throw new IllegalArgumentException("empty string is not allowed for ignorable whitespace" + getLocation()); - } - out.write(text); // no escape? - } + @Override + public void ignorableWhitespace(String text) throws IOException { + if (startTagIncomplete || setPrefixCalled || seenBracket) + closeStartTag(); + if (doIndent && seenTag) + seenTag = false; + if (text.length() == 0) { + throw new IllegalArgumentException("empty string is not allowed for ignorable whitespace" + getLocation()); + } + out.write(text); // no escape? + } - @Override - public void flush() throws IOException { - if (!finished && startTagIncomplete) - closeStartTag(); - out.flush(); - } + @Override + public void flush() throws IOException { + if (!finished && startTagIncomplete) + closeStartTag(); + out.flush(); + } - // --- utility methods + // --- utility methods - protected void writeAttributeValue(String value, Writer out) - throws IOException { - // .[apostrophe and <, & escaped], - final char quot = attributeUseApostrophe ? '\'' : '"'; - final String quotEntity = attributeUseApostrophe ? "'" : """; + protected void writeAttributeValue(String value, Writer out) + throws IOException { + // .[apostrophe and <, & escaped], + final char quot = attributeUseApostrophe ? '\'' : '"'; + final String quotEntity = attributeUseApostrophe ? "'" : """; - int pos = 0; - for (int i = 0; i < value.length(); i++) { - char ch = value.charAt(i); - if (ch == '&') { - if (i > pos) - out.write(value.substring(pos, i)); - out.write("&"); - pos = i + 1; - } - if (ch == '<') { - if (i > pos) - out.write(value.substring(pos, i)); - out.write("<"); - pos = i + 1; - } else if (ch == quot) { - if (i > pos) - out.write(value.substring(pos, i)); - out.write(quotEntity); - pos = i + 1; - } else if (ch < 32) { - // in XML 1.0 only legal character are #x9 | #xA | #xD - // and they must be escaped otherwise in attribute value they - // are normalized to spaces - if (ch == 13 || ch == 10 || ch == 9) { - if (i > pos) - out.write(value.substring(pos, i)); - out.write("&#"); - out.write(Integer.toString(ch)); - out.write(';'); - pos = i + 1; - } else { - if (TRACE_ESCAPING) - System.err.println(getClass().getName() + " DEBUG ATTR value.len=" + value.length() - + " " + printable(value)); + int pos = 0; + for (int i = 0; i < value.length(); i++) { + char ch = value.charAt(i); + if (ch == '&') { + if (i > pos) + out.write(value.substring(pos, i)); + out.write("&"); + pos = i + 1; + } + if (ch == '<') { + if (i > pos) + out.write(value.substring(pos, i)); + out.write("<"); + pos = i + 1; + } else if (ch == quot) { + if (i > pos) + out.write(value.substring(pos, i)); + out.write(quotEntity); + pos = i + 1; + } else if (ch < 32) { + // in XML 1.0 only legal character are #x9 | #xA | #xD + // and they must be escaped otherwise in attribute value they + // are normalized to spaces + if (ch == 13 || ch == 10 || ch == 9) { + if (i > pos) + out.write(value.substring(pos, i)); + out.write("&#"); + out.write(Integer.toString(ch)); + out.write(';'); + pos = i + 1; + } else { + if (TRACE_ESCAPING) + System.err.println(getClass().getName() + " DEBUG ATTR value.len=" + value.length() + + " " + printable(value)); - throw new IllegalStateException( - "character " + printable(ch) + " (" + Integer.toString(ch) + ") is not allowed in output" - + getLocation() + " (attr value=" - + printable(value) + ")"); - } - } - } - if (pos > 0) { - out.write(value.substring(pos)); - } else { - out.write(value); // this is shortcut to the most common case - } - } + throw new IllegalStateException( + "character " + printable(ch) + " (" + Integer.toString(ch) + ") is not allowed in output" + + getLocation() + " (attr value=" + + printable(value) + ")"); + } + } + } + if (pos > 0) { + out.write(value.substring(pos)); + } else { + out.write(value); // this is shortcut to the most common case + } + } - protected void writeElementContent(String text, Writer out) - throws IOException { + protected void writeElementContent(String text, Writer out) + throws IOException { - // For some reason, some non-empty, empty characters are surviving this far and getting filtered out - // So we are left with null, which causes an NPE - if (text == null) { - return; - } + // For some reason, some non-empty, empty characters are surviving this far and getting filtered out + // So we are left with null, which causes an NPE + if (text == null) { + return; + } - // escape '<', '&', ']]>', <32 if necessary - int pos = 0; - for (int i = 0; i < text.length(); i++) { - // TODO: check if doing char[] text.getChars() would be faster than - // getCharAt(i) ... - char ch = text.charAt(i); - if (ch == ']') { - if (seenBracket) { - seenBracketBracket = true; - } else { - seenBracket = true; - } - } else { - if (ch == '&') { - if (!(i < text.length() - 3 && text.charAt(i+1) == 'l' - && text.charAt(i+2) == 't' && text.charAt(i+3) == ';')) { - if (i > pos) - out.write(text.substring(pos, i)); - out.write("&"); - pos = i + 1; + // escape '<', '&', ']]>', <32 if necessary + int pos = 0; + for (int i = 0; i < text.length(); i++) { + // TODO: check if doing char[] text.getChars() would be faster than + // getCharAt(i) ... + char ch = text.charAt(i); + if (ch == ']') { + if (seenBracket) { + seenBracketBracket = true; + } else { + seenBracket = true; + } + } else { + if (ch == '&') { + if (!(i < text.length() - 3 && text.charAt(i+1) == 'l' + && text.charAt(i+2) == 't' && text.charAt(i+3) == ';')) { + if (i > pos) + out.write(text.substring(pos, i)); + out.write("&"); + pos = i + 1; } - } else if (ch == '<') { - if (i > pos) - out.write(text.substring(pos, i)); - out.write("<"); - pos = i + 1; - } else if (seenBracketBracket && ch == '>') { - if (i > pos) - out.write(text.substring(pos, i)); - out.write(">"); - pos = i + 1; - } else if (ch < 32) { - // in XML 1.0 only legal character are #x9 | #xA | #xD - if (ch == 9 || ch == 10 || ch == 13) { - // pass through - } else { - if (TRACE_ESCAPING) - System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + text.length() - + " " + printable(text)); - throw new IllegalStateException("character " + Integer.toString(ch) - + " is not allowed in output" + getLocation() - + " (text value=" + printable(text) + ")"); - } - } - if (seenBracket) { - seenBracketBracket = seenBracket = false; - } + } else if (ch == '<') { + if (i > pos) + out.write(text.substring(pos, i)); + out.write("<"); + pos = i + 1; + } else if (seenBracketBracket && ch == '>') { + if (i > pos) + out.write(text.substring(pos, i)); + out.write(">"); + pos = i + 1; + } else if (ch < 32) { + // in XML 1.0 only legal character are #x9 | #xA | #xD + if (ch == 9 || ch == 10 || ch == 13) { + // pass through + } else { + if (TRACE_ESCAPING) + System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + text.length() + + " " + printable(text)); + throw new IllegalStateException("character " + Integer.toString(ch) + + " is not allowed in output" + getLocation() + + " (text value=" + printable(text) + ")"); + } + } + if (seenBracket) { + seenBracketBracket = seenBracket = false; + } - } - } - if (pos > 0) { - out.write(text.substring(pos)); - } else { - out.write(text); // this is shortcut to the most common case - } + } + } + if (pos > 0) { + out.write(text.substring(pos)); + } else { + out.write(text); // this is shortcut to the most common case + } - } + } - protected void writeElementContent(char[] buf, int off, int len, Writer out) - throws IOException { - // escape '<', '&', ']]>' - final int end = off + len; - int pos = off; - for (int i = off; i < end; i++) { - final char ch = buf[i]; - if (ch == ']') { - if (seenBracket) { - seenBracketBracket = true; - } else { - seenBracket = true; - } - } else { - if (ch == '&') { - if (i > pos) { - out.write(buf, pos, i - pos); - } - out.write("&"); - pos = i + 1; - } else if (ch == '<') { - if (i > pos) { - out.write(buf, pos, i - pos); - } - out.write("<"); - pos = i + 1; + protected void writeElementContent(char[] buf, int off, int len, Writer out) + throws IOException { + // escape '<', '&', ']]>' + final int end = off + len; + int pos = off; + for (int i = off; i < end; i++) { + final char ch = buf[i]; + if (ch == ']') { + if (seenBracket) { + seenBracketBracket = true; + } else { + seenBracket = true; + } + } else { + if (ch == '&') { + if (i > pos) { + out.write(buf, pos, i - pos); + } + out.write("&"); + pos = i + 1; + } else if (ch == '<') { + if (i > pos) { + out.write(buf, pos, i - pos); + } + out.write("<"); + pos = i + 1; - } else if (seenBracketBracket && ch == '>') { - if (i > pos) { - out.write(buf, pos, i - pos); - } - out.write(">"); - pos = i + 1; - } else if (ch < 32) { - // in XML 1.0 only legal character are #x9 | #xA | #xD - if (ch == 9 || ch == 10 || ch == 13) { - // pass through - } else { - if (TRACE_ESCAPING) - System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + len + " " - + printable(new String(buf, off, len))); - throw new IllegalStateException("character " - + printable(ch) + " (" + Integer.toString(ch) - + ") is not allowed in output" + getLocation()); - } - } - if (seenBracket) { - seenBracketBracket = seenBracket = false; - } - } - } - if (end > pos) { - out.write(buf, pos, end - pos); - } - } + } else if (seenBracketBracket && ch == '>') { + if (i > pos) { + out.write(buf, pos, i - pos); + } + out.write(">"); + pos = i + 1; + } else if (ch < 32) { + // in XML 1.0 only legal character are #x9 | #xA | #xD + if (ch == 9 || ch == 10 || ch == 13) { + // pass through + } else { + if (TRACE_ESCAPING) + System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + len + " " + + printable(new String(buf, off, len))); + throw new IllegalStateException("character " + + printable(ch) + " (" + Integer.toString(ch) + + ") is not allowed in output" + getLocation()); + } + } + if (seenBracket) { + seenBracketBracket = seenBracket = false; + } + } + } + if (end > pos) { + out.write(buf, pos, end - pos); + } + } - protected static String printable(String s) { - if (s == null) { - return "null"; - } - StringBuffer retval = new StringBuffer(s.length() + 16); - retval.append("'"); - for (int i = 0; i < s.length(); i++) { - addPrintable(retval, s.charAt(i)); - } - retval.append("'"); - return retval.toString(); - } + protected static String printable(String s) { + if (s == null) { + return "null"; + } + StringBuffer retval = new StringBuffer(s.length() + 16); + retval.append("'"); + for (int i = 0; i < s.length(); i++) { + addPrintable(retval, s.charAt(i)); + } + retval.append("'"); + return retval.toString(); + } - protected static String printable(char ch) { - StringBuffer retval = new StringBuffer(); - addPrintable(retval, ch); - return retval.toString(); - } + protected static String printable(char ch) { + StringBuffer retval = new StringBuffer(); + addPrintable(retval, ch); + return retval.toString(); + } - private static void addPrintable(StringBuffer retval, char ch) { - switch (ch) { - case '\b': - retval.append("\\b"); - break; - case '\t': - retval.append("\\t"); - break; - case '\n': - retval.append("\\n"); - break; - case '\f': - retval.append("\\f"); - break; - case '\r': - retval.append("\\r"); - break; - case '\"': - retval.append("\\\""); - break; - case '\'': - retval.append("\\'"); - break; - case '\\': - retval.append("\\\\"); - break; - default: - if (ch < 0x20 || ch > 0x7e) { - final String ss = "0000" + Integer.toString(ch, 16); - retval.append("\\u").append(ss.substring(ss.length() - 4)); - } else { - retval.append(ch); - } - } - } + private static void addPrintable(StringBuffer retval, char ch) { + switch (ch) { + case '\b': + retval.append("\\b"); + break; + case '\t': + retval.append("\\t"); + break; + case '\n': + retval.append("\\n"); + break; + case '\f': + retval.append("\\f"); + break; + case '\r': + retval.append("\\r"); + break; + case '\"': + retval.append("\\\""); + break; + case '\'': + retval.append("\\'"); + break; + case '\\': + retval.append("\\\\"); + break; + default: + if (ch < 0x20 || ch > 0x7e) { + final String ss = "0000" + Integer.toString(ch, 16); + retval.append("\\u").append(ss.substring(ss.length() - 4)); + } else { + retval.append(ch); + } + } + } } diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/res/decoder/StringBlockWithSurrogatePairInUtf8Test.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/res/decoder/StringBlockWithSurrogatePairInUtf8Test.java index 7d9a6075..004399e2 100644 --- a/brut.apktool/apktool-lib/src/test/java/brut/androlib/res/decoder/StringBlockWithSurrogatePairInUtf8Test.java +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/res/decoder/StringBlockWithSurrogatePairInUtf8Test.java @@ -31,26 +31,26 @@ public class StringBlockWithSurrogatePairInUtf8Test { @Test public void decodeTwoOctets() { - final String actual0 = new StringBlock(new byte[] { (byte) 0xC2, (byte) 0x80}, true).decodeString(0, 2); + final String actual0 = new StringBlock(new byte[] {(byte) 0xC2, (byte) 0x80}, true).decodeString(0, 2); assertEquals("Incorrect decoding", "\u0080", actual0); - final String actual1 = new StringBlock(new byte[] { (byte) 0xDF, (byte) 0xBF}, true).decodeString(0, 2); + final String actual1 = new StringBlock(new byte[] {(byte) 0xDF, (byte) 0xBF}, true).decodeString(0, 2); assertEquals("Incorrect decoding", "\u07FF", actual1); } @Test public void decodeThreeOctets() { - final String actual0 = new StringBlock(new byte[] { (byte) 0xE0, (byte) 0xA0, (byte) 0x80}, true).decodeString(0, 3); + final String actual0 = new StringBlock(new byte[] {(byte) 0xE0, (byte) 0xA0, (byte) 0x80}, true).decodeString(0, 3); assertEquals("Incorrect decoding", "\u0800", actual0); - final String actual1 = new StringBlock(new byte[] { (byte) 0xEF, (byte) 0xBF, (byte) 0xBF}, true).decodeString(0, 3); + final String actual1 = new StringBlock(new byte[] {(byte) 0xEF, (byte) 0xBF, (byte) 0xBF}, true).decodeString(0, 3); assertEquals("Incorrect decoding", "\uFFFF", actual1); } @Test public void decodeSurrogatePair_when_givesAsThreeOctetsFromInvalidRangeOfUtf8() { // See: https://github.com/iBotPeaches/Apktool/issues/2299 - final String actual = new StringBlock(new byte[] { (byte) 0xED, (byte) 0xA0, (byte) 0xBD, (byte) 0xED, (byte) 0xB4, (byte) 0x86}, true).decodeString(0, 6); + final String actual = new StringBlock(new byte[] {(byte) 0xED, (byte) 0xA0, (byte) 0xBD, (byte) 0xED, (byte) 0xB4, (byte) 0x86}, true).decodeString(0, 6); assertEquals("Incorrect decoding", "\uD83D\uDD06", actual); // See: https://github.com/iBotPeaches/Apktool/issues/2546 @@ -74,7 +74,7 @@ public class StringBlockWithSurrogatePairInUtf8Test { // \u10FFFF is encoded in UTF-8 as "0xDBFF 0xDFFF" (4-byte encoding), // but when used in Android resources which are encoded in UTF-8, 3-byte encoding is used, // so each of these is encoded as 3-bytes - final String actual = new StringBlock(new byte[] { (byte) 0xED, (byte) 0xAF, (byte) 0xBF, (byte) 0xED, (byte) 0xBF, (byte) 0xBF}, true).decodeString(0, 6); + final String actual = new StringBlock(new byte[] {(byte) 0xED, (byte) 0xAF, (byte) 0xBF, (byte) 0xED, (byte) 0xBF, (byte) 0xBF}, true).decodeString(0, 6); assertEquals("Incorrect decoding", "\uDBFF\uDFFF", actual); } } diff --git a/brut.j.util/src/main/java/brut/util/OS.java b/brut.j.util/src/main/java/brut/util/OS.java index 354db520..590a8a33 100644 --- a/brut.j.util/src/main/java/brut/util/OS.java +++ b/brut.j.util/src/main/java/brut/util/OS.java @@ -53,7 +53,7 @@ public class OS { } public static void rmfile(String file) { - File del = new File(file); + File del = new File(file); //noinspection ResultOfMethodCallIgnored del.delete(); }