Package com.google.protobuf
Class TextFormat
- java.lang.Object
-
- com.google.protobuf.TextFormat
-
public final class TextFormat extends java.lang.Object
Provide text parsing and formatting support for proto2 instances. The implementation largely follows text_format.cc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TextFormat.InvalidEscapeSequenceException
Thrown byunescapeBytes(java.lang.CharSequence)
andunescapeText(java.lang.String)
when an invalid escape sequence is seen.static class
TextFormat.ParseException
Thrown when parsing an invalid text format message.static class
TextFormat.Parser
Parser for text-format proto2 instances.static class
TextFormat.Printer
Helper class for converting protobufs to text.private static class
TextFormat.TextGenerator
An inner class for writing text to the output stream.private static class
TextFormat.Tokenizer
Represents a stream of tokens parsed from aString
.static class
TextFormat.UnknownFieldParseException
Thrown when encountering an unknown field while parsing a text format message.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
DEBUG_STRING_SILENT_MARKER
private static java.util.logging.Logger
logger
private static TextFormat.Parser
PARSER
-
Constructor Summary
Constructors Modifier Constructor Description private
TextFormat()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static int
digitValue(byte c)
Interpret a character as a digit (in any base up to 36) and return the numeric value.static java.lang.String
escapeBytes(byte[] input)
LikeescapeBytes(ByteString)
, but used for byte array.static java.lang.String
escapeBytes(ByteString input)
Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C string literals.static java.lang.String
escapeDoubleQuotesAndBackslashes(java.lang.String input)
Escape double quotes and backslashes in a String for emittingUnicode output of a message.(package private) static java.lang.String
escapeText(java.lang.String input)
LikeescapeBytes(ByteString)
, but escapes a text string.static TextFormat.Parser
getParser()
Return aTextFormat.Parser
instance which can parse text-format messages.private static boolean
isHex(byte c)
Is this a hex digit?private static boolean
isOctal(byte c)
Is this an octal digit?static void
merge(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder)
Parse a text-format message frominput
and merge the contents intobuilder
.static void
merge(java.lang.CharSequence input, Message.Builder builder)
Parse a text-format message frominput
and merge the contents intobuilder
.static void
merge(java.lang.Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder)
Parse a text-format message frominput
and merge the contents intobuilder
.static void
merge(java.lang.Readable input, Message.Builder builder)
Parse a text-format message frominput
and merge the contents intobuilder
.private static TextFormat.TextGenerator
multiLineOutput(java.lang.Appendable output)
static <T extends Message>
Tparse(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, java.lang.Class<T> protoClass)
Parse a text-format message frominput
.static <T extends Message>
Tparse(java.lang.CharSequence input, java.lang.Class<T> protoClass)
Parse a text-format message frominput
.(package private) static int
parseInt32(java.lang.String text)
Parse a 32-bit signed integer from the text.(package private) static long
parseInt64(java.lang.String text)
Parse a 64-bit signed integer from the text.private static long
parseInteger(java.lang.String text, boolean isSigned, boolean isLong)
(package private) static int
parseUInt32(java.lang.String text)
Parse a 32-bit unsigned integer from the text.(package private) static long
parseUInt64(java.lang.String text)
Parse a 64-bit unsigned integer from the text.static void
print(MessageOrBuilder message, java.lang.Appendable output)
Deprecated.Useprinter().print(MessageOrBuilder, Appendable)
static void
print(UnknownFieldSet fields, java.lang.Appendable output)
Deprecated.Useprinter().print(UnknownFieldSet, Appendable)
static TextFormat.Printer
printer()
Printer instance which escapes non-ASCII characters.static void
printField(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output)
Deprecated.Useprinter().printField(FieldDescriptor, Object, Appendable)
static java.lang.String
printFieldToString(Descriptors.FieldDescriptor field, java.lang.Object value)
Deprecated.Useprinter().printFieldToString(FieldDescriptor, Object)
static void
printFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output)
Deprecated.Useprinter().printFieldValue(FieldDescriptor, Object, Appendable)
static java.lang.String
printToString(MessageOrBuilder message)
Deprecated.Usemessage.toString()
static java.lang.String
printToString(UnknownFieldSet fields)
Deprecated.static java.lang.String
printToUnicodeString(MessageOrBuilder message)
Deprecated.Useprinter().escapingNonAscii(false).printToString(MessageOrBuilder)
static java.lang.String
printToUnicodeString(UnknownFieldSet fields)
Deprecated.Useprinter().escapingNonAscii(false).printToString(UnknownFieldSet)
static void
printUnicode(MessageOrBuilder message, java.lang.Appendable output)
Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)
static void
printUnicode(UnknownFieldSet fields, java.lang.Appendable output)
Deprecated.Useprinter().escapingNonAscii(false).print(UnknownFieldSet, Appendable)
static void
printUnicodeFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output)
Deprecated.Useprinter().escapingNonAscii(false).printFieldValue(FieldDescriptor, Object, Appendable)
private static void
printUnknownFieldValue(int tag, java.lang.Object value, TextFormat.TextGenerator generator)
static void
printUnknownFieldValue(int tag, java.lang.Object value, java.lang.Appendable output)
Outputs a textual representation of the value of an unknown field.static java.lang.String
shortDebugString(Descriptors.FieldDescriptor field, java.lang.Object value)
Deprecated.Useprinter().shortDebugString(FieldDescriptor, Object)
static java.lang.String
shortDebugString(MessageOrBuilder message)
Generates a human readable form of this message, useful for debugging and other purposes, with no newline characters.static java.lang.String
shortDebugString(UnknownFieldSet fields)
Deprecated.Useprinter().shortDebugString(UnknownFieldSet)
private static TextFormat.TextGenerator
singleLineOutput(java.lang.Appendable output)
static ByteString
unescapeBytes(java.lang.CharSequence charString)
Un-escape a byte sequence as escaped usingescapeBytes(ByteString)
.(package private) static java.lang.String
unescapeText(java.lang.String input)
Un-escape a text string as escaped usingescapeText(String)
.static java.lang.String
unsignedToString(int value)
Convert an unsigned 32-bit integer to a string.static java.lang.String
unsignedToString(long value)
Convert an unsigned 64-bit integer to a string.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
DEBUG_STRING_SILENT_MARKER
private static final java.lang.String DEBUG_STRING_SILENT_MARKER
- See Also:
- Constant Field Values
-
PARSER
private static final TextFormat.Parser PARSER
-
-
Method Detail
-
print
@Deprecated @InlineMe(replacement="TextFormat.printer().print(message, output)", imports="com.google.protobuf.TextFormat") public static void print(MessageOrBuilder message, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().print(MessageOrBuilder, Appendable)
Outputs a textual representation of the Protocol Message supplied into the parameter output. (This representation is the new version of the classic "ProtocolPrinter" output from the original Protocol Buffer system)- Throws:
java.io.IOException
-
print
@Deprecated public static void print(UnknownFieldSet fields, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().print(UnknownFieldSet, Appendable)
Outputs a textual representation offields
tooutput
.- Throws:
java.io.IOException
-
printUnicode
@Deprecated @InlineMe(replacement="TextFormat.printer().escapingNonAscii(false).print(message, output)", imports="com.google.protobuf.TextFormat") public static void printUnicode(MessageOrBuilder message, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)
Same asprint()
, except that non-ASCII characters are not escaped.- Throws:
java.io.IOException
-
printUnicode
@Deprecated public static void printUnicode(UnknownFieldSet fields, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().escapingNonAscii(false).print(UnknownFieldSet, Appendable)
Same asprint()
, except that non-ASCII characters are not escaped.- Throws:
java.io.IOException
-
shortDebugString
public static java.lang.String shortDebugString(MessageOrBuilder message)
Generates a human readable form of this message, useful for debugging and other purposes, with no newline characters. This is just a trivial wrapper aroundTextFormat.Printer.shortDebugString(MessageOrBuilder)
.
-
shortDebugString
@Deprecated public static java.lang.String shortDebugString(Descriptors.FieldDescriptor field, java.lang.Object value)
Deprecated.Useprinter().shortDebugString(FieldDescriptor, Object)
Generates a human readable form of the field, useful for debugging and other purposes, with no newline characters.
-
shortDebugString
@Deprecated public static java.lang.String shortDebugString(UnknownFieldSet fields)
Deprecated.Useprinter().shortDebugString(UnknownFieldSet)
Generates a human readable form of the unknown fields, useful for debugging and other purposes, with no newline characters.
-
printToString
@Deprecated @InlineMe(replacement="TextFormat.printer().printToString(message)", imports="com.google.protobuf.TextFormat") public static java.lang.String printToString(MessageOrBuilder message)
Deprecated.Usemessage.toString()
Likeprint()
, but writes directly to aString
and returns it.
-
printToString
@Deprecated public static java.lang.String printToString(UnknownFieldSet fields)
Deprecated.Likeprint()
, but writes directly to aString
and returns it.
-
printToUnicodeString
@Deprecated @InlineMe(replacement="TextFormat.printer().escapingNonAscii(false).printToString(message)", imports="com.google.protobuf.TextFormat") public static java.lang.String printToUnicodeString(MessageOrBuilder message)
Deprecated.Useprinter().escapingNonAscii(false).printToString(MessageOrBuilder)
Same asprintToString()
, except that non-ASCII characters in string type fields are not escaped in backslash+octals.
-
printToUnicodeString
@Deprecated public static java.lang.String printToUnicodeString(UnknownFieldSet fields)
Deprecated.Useprinter().escapingNonAscii(false).printToString(UnknownFieldSet)
Same asprintToString()
, except that non-ASCII characters in string type fields are not escaped in backslash+octals.
-
printField
@Deprecated public static void printField(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().printField(FieldDescriptor, Object, Appendable)
- Throws:
java.io.IOException
-
printFieldToString
@Deprecated public static java.lang.String printFieldToString(Descriptors.FieldDescriptor field, java.lang.Object value)
Deprecated.Useprinter().printFieldToString(FieldDescriptor, Object)
-
printUnicodeFieldValue
@Deprecated public static void printUnicodeFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().escapingNonAscii(false).printFieldValue(FieldDescriptor, Object, Appendable)
Outputs a unicode textual representation of the value of given field value.Same as
printFieldValue()
, except that non-ASCII characters in string type fields are not escaped in backslash+octals.- Parameters:
field
- the descriptor of the fieldvalue
- the value of the fieldoutput
- the output to which to append the formatted value- Throws:
java.lang.ClassCastException
- if the value is not appropriate for the given field descriptorjava.io.IOException
- if there is an exception writing to the output
-
printFieldValue
@Deprecated @InlineMe(replacement="TextFormat.printer().printFieldValue(field, value, output)", imports="com.google.protobuf.TextFormat") public static void printFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().printFieldValue(FieldDescriptor, Object, Appendable)
Outputs a textual representation of the value of given field value.- Parameters:
field
- the descriptor of the fieldvalue
- the value of the fieldoutput
- the output to which to append the formatted value- Throws:
java.lang.ClassCastException
- if the value is not appropriate for the given field descriptorjava.io.IOException
- if there is an exception writing to the output
-
printUnknownFieldValue
public static void printUnknownFieldValue(int tag, java.lang.Object value, java.lang.Appendable output) throws java.io.IOException
Outputs a textual representation of the value of an unknown field.- Parameters:
tag
- the field's tag numbervalue
- the value of the fieldoutput
- the output to which to append the formatted value- Throws:
java.lang.ClassCastException
- if the value is not appropriate for the given field descriptorjava.io.IOException
- if there is an exception writing to the output
-
printUnknownFieldValue
private static void printUnknownFieldValue(int tag, java.lang.Object value, TextFormat.TextGenerator generator) throws java.io.IOException
- Throws:
java.io.IOException
-
printer
public static TextFormat.Printer printer()
Printer instance which escapes non-ASCII characters.
-
unsignedToString
public static java.lang.String unsignedToString(int value)
Convert an unsigned 32-bit integer to a string.
-
unsignedToString
public static java.lang.String unsignedToString(long value)
Convert an unsigned 64-bit integer to a string.
-
multiLineOutput
private static TextFormat.TextGenerator multiLineOutput(java.lang.Appendable output)
-
singleLineOutput
private static TextFormat.TextGenerator singleLineOutput(java.lang.Appendable output)
-
getParser
public static TextFormat.Parser getParser()
Return aTextFormat.Parser
instance which can parse text-format messages. The returned instance is thread-safe.
-
merge
public static void merge(java.lang.Readable input, Message.Builder builder) throws java.io.IOException
Parse a text-format message frominput
and merge the contents intobuilder
.- Throws:
java.io.IOException
-
merge
public static void merge(java.lang.CharSequence input, Message.Builder builder) throws TextFormat.ParseException
Parse a text-format message frominput
and merge the contents intobuilder
.- Throws:
TextFormat.ParseException
-
parse
public static <T extends Message> T parse(java.lang.CharSequence input, java.lang.Class<T> protoClass) throws TextFormat.ParseException
Parse a text-format message frominput
.- Returns:
- the parsed message, guaranteed initialized
- Throws:
TextFormat.ParseException
-
merge
public static void merge(java.lang.Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws java.io.IOException
Parse a text-format message frominput
and merge the contents intobuilder
. Extensions will be recognized if they are registered inextensionRegistry
.- Throws:
java.io.IOException
-
merge
public static void merge(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws TextFormat.ParseException
Parse a text-format message frominput
and merge the contents intobuilder
. Extensions will be recognized if they are registered inextensionRegistry
.- Throws:
TextFormat.ParseException
-
parse
public static <T extends Message> T parse(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, java.lang.Class<T> protoClass) throws TextFormat.ParseException
Parse a text-format message frominput
. Extensions will be recognized if they are registered inextensionRegistry
.- Returns:
- the parsed message, guaranteed initialized
- Throws:
TextFormat.ParseException
-
escapeBytes
public static java.lang.String escapeBytes(ByteString input)
Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C string literals. All bytes that are not printable 7-bit ASCII characters are escaped, as well as backslash, single-quote, and double-quote characters. Characters for which no defined short-hand escape sequence is defined will be escaped using 3-digit octal sequences.
-
escapeBytes
public static java.lang.String escapeBytes(byte[] input)
LikeescapeBytes(ByteString)
, but used for byte array.
-
unescapeBytes
public static ByteString unescapeBytes(java.lang.CharSequence charString) throws TextFormat.InvalidEscapeSequenceException
Un-escape a byte sequence as escaped usingescapeBytes(ByteString)
. Two-digit hex escapes (starting with "\x") are also recognized.
-
escapeText
static java.lang.String escapeText(java.lang.String input)
LikeescapeBytes(ByteString)
, but escapes a text string. Non-ASCII characters are first encoded as UTF-8, then each byte is escaped individually as a 3-digit octal escape. Yes, it's weird.
-
escapeDoubleQuotesAndBackslashes
public static java.lang.String escapeDoubleQuotesAndBackslashes(java.lang.String input)
Escape double quotes and backslashes in a String for emittingUnicode output of a message.
-
unescapeText
static java.lang.String unescapeText(java.lang.String input) throws TextFormat.InvalidEscapeSequenceException
Un-escape a text string as escaped usingescapeText(String)
. Two-digit hex escapes (starting with "\x") are also recognized.
-
isOctal
private static boolean isOctal(byte c)
Is this an octal digit?
-
isHex
private static boolean isHex(byte c)
Is this a hex digit?
-
digitValue
private static int digitValue(byte c)
Interpret a character as a digit (in any base up to 36) and return the numeric value. This is likeCharacter.digit()
but we don't accept non-ASCII digits.
-
parseInt32
static int parseInt32(java.lang.String text) throws java.lang.NumberFormatException
Parse a 32-bit signed integer from the text. Unlike the Java standardInteger.parseInt()
, this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively.- Throws:
java.lang.NumberFormatException
-
parseUInt32
static int parseUInt32(java.lang.String text) throws java.lang.NumberFormatException
Parse a 32-bit unsigned integer from the text. Unlike the Java standardInteger.parseInt()
, this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively. The result is coerced to a (signed)int
when returned since Java has no unsigned integer type.- Throws:
java.lang.NumberFormatException
-
parseInt64
static long parseInt64(java.lang.String text) throws java.lang.NumberFormatException
Parse a 64-bit signed integer from the text. Unlike the Java standardInteger.parseInt()
, this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively.- Throws:
java.lang.NumberFormatException
-
parseUInt64
static long parseUInt64(java.lang.String text) throws java.lang.NumberFormatException
Parse a 64-bit unsigned integer from the text. Unlike the Java standardInteger.parseInt()
, this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively. The result is coerced to a (signed)long
when returned since Java has no unsigned long type.- Throws:
java.lang.NumberFormatException
-
parseInteger
private static long parseInteger(java.lang.String text, boolean isSigned, boolean isLong) throws java.lang.NumberFormatException
- Throws:
java.lang.NumberFormatException
-
-