BlocklyXmlHelper

public final class BlocklyXmlHelper extends Object

Helper class to serialize and deserialize blockly workspaces, including constructing new parsers and serializers as needed.

Nested Class Summary

interface BlocklyXmlHelper.XmlContentWriter  

Public Method Summary

static String
captureElement(XmlPullParser parser)
Serializes the current element and all child nodes as a String.
static String
escape(String text)
static List<Block>
loadFromXml(InputStream inputXml, BlockFactory blockFactory)
Convenience function that creates a new ArrayList.
static void
loadFromXml(InputStream inputXml, BlockFactory blockFactory, List<Block> result)
Loads a list of top-level Blocks from XML.
static Block
loadOneBlockFromXml(InputStream inputXml, BlockFactory blockFactory)
Convenience function to load only one Block.
static Block
loadOneBlockFromXml(String xml, BlockFactory blockFactory)
Convenience function to load only one Block.
static BlocklyCategory
loadToolboxFromXml(InputStream is, BlockFactory blockFactory, String workspaceId)
Loads toolbox from XML.
static void
updateMutator(Block block, Mutator mutator, String mutation)
Performs the XML IO boilerplate used to update mutator with the <mutation> element provided in mutation.
static String
writeBlockToXml(Block rootBlock, IOOptions options)
Convenience function to serialize one stack of Blocks (a BlockGroup, effectively).
static void
writeBlockToXml(Block rootBlock, OutputStream os, IOOptions options)
Convenience function to serialize one stack of Blocks (a BlockGroup, effectively).
static void
writeToXml(List<Block> toSerialize, OutputStream os, IOOptions options)
Serializes all Blocks in the given list and writes them to the given output stream.
static void
writeToXml(List<Block> toSerialize, Writer writer, IOOptions options)
Serializes all Blocks in the given list and writes them to the given writer.
static void
writeToXmlImpl(List<Block> toSerialize, OutputStream os, Writer writer, IOOptions options)
Serializes all Blocks in the given list and writes them to the either the output stream or writer, whichever is not null.
static void
writeXml(OutputStream output, BlocklyXmlHelper.XmlContentWriter writer)
static String
static void

Inherited Method Summary

Public Methods

public static String captureElement (XmlPullParser parser)

Serializes the current element and all child nodes as a String.

Parameters
parser The parser to pull from.
Returns
  • The composed element string.
Throws
XmlPullParserException
IOException
XmlPullParserException

public static String escape (String text)

Parameters
text The text to escape.

public static List<Block> loadFromXml (InputStream inputXml, BlockFactory blockFactory)

Convenience function that creates a new ArrayList.

Parameters
inputXml The input stream of XML from which to read.
blockFactory
Throws
BlockLoadingException If any error occurs with the input. It may wrap an IOException or XmlPullParserException as a root cause.

public static void loadFromXml (InputStream inputXml, BlockFactory blockFactory, List<Block> result)

Loads a list of top-level Blocks from XML. Each top-level Block may have many Blocks contained in it or descending from it.

Parameters
inputXml The input stream from which to read.
blockFactory The BlockFactory for the workspace where the Blocks are being loaded.
result The List to add the parsed blocks to.
Throws
BlockLoadingException If any error occurs with the input. It may wrap an IOException or XmlPullParserException as a root cause.

public static Block loadOneBlockFromXml (InputStream inputXml, BlockFactory blockFactory)

Convenience function to load only one Block.

Parameters
inputXml The input stream of XML from which to read.
blockFactory The BlockFactory for the workspace where the Blocks are being loaded.
Returns
  • The first Block read from is, or null if no Block was read.
Throws
BlockLoadingException If any error occurs with the input. It may wrap an IOException or XmlPullParserException as a root cause.

public static Block loadOneBlockFromXml (String xml, BlockFactory blockFactory)

Convenience function to load only one Block.

Parameters
xml The XML in string form to read the block from.
blockFactory The BlockFactory for the workspace where the Blocks are being loaded.
Returns
  • The first Block read from is, or null if no Block was read.
Throws
BlockLoadingException If any error occurs with the input. It may wrap an IOException or XmlPullParserException as a root cause.

public static BlocklyCategory loadToolboxFromXml (InputStream is, BlockFactory blockFactory, String workspaceId)

Loads toolbox from XML. Each category may have multiple subcategories and/or multiple blocks contained in it or descending from it.

Parameters
is The input stream from which to read.
blockFactory The BlockFactory for the workspace where the Blocks are being loaded.
workspaceId The workspaceId to set on all blocks attached to this Category.
Returns
  • The top-level category in the toolbox.
Throws
BlockLoadingException If any error occurs with the input. It may wrap an IOException or XmlPullParserException as a root cause.

public static void updateMutator (Block block, Mutator mutator, String mutation)

Performs the XML IO boilerplate used to update mutator with the <mutation> element provided in mutation.

Parameters
block The block containing the Mutator.
mutator The Mutator to be updated.
mutation The <mutation> element as a string.

public static String writeBlockToXml (Block rootBlock, IOOptions options)

Convenience function to serialize one stack of Blocks (a BlockGroup, effectively).

Parameters
rootBlock The root block of the stack to serialize.
options The options to configure the block serialization. If omitted, WRITE_ALL_DATA will be used by default.
Returns
  • XML string for block and all descendant blocks.

public static void writeBlockToXml (Block rootBlock, OutputStream os, IOOptions options)

Convenience function to serialize one stack of Blocks (a BlockGroup, effectively).

Parameters
rootBlock The root block of the stack to serialize.
os An OutputStream to which to write them.
options The options to configure the block serialization. If omitted, WRITE_ALL_DATA will be used by default.

public static void writeToXml (List<Block> toSerialize, OutputStream os, IOOptions options)

Serializes all Blocks in the given list and writes them to the given output stream.

Parameters
toSerialize A list of Blocks to serialize.
os An OutputStream to write the blocks to.
options The options to configure the block serialization. If omitted, WRITE_ALL_DATA will be used by default.

public static void writeToXml (List<Block> toSerialize, Writer writer, IOOptions options)

Serializes all Blocks in the given list and writes them to the given writer.

Parameters
toSerialize A list of Blocks to serialize.
writer A writer to write the blocks to.
options The options to configure the block serialization. If omitted, WRITE_ALL_DATA will be used by default.

public static void writeToXmlImpl (List<Block> toSerialize, OutputStream os, Writer writer, IOOptions options)

Serializes all Blocks in the given list and writes them to the either the output stream or writer, whichever is not null.

Parameters
toSerialize A list of Blocks to serialize.
os An OutputStream to write the blocks to.
writer A writer to write the blocks to, if os is null.
options The options to configure the block serialization. If omitted, WRITE_ALL_DATA will be used by default.

public static void writeXml (OutputStream output, BlocklyXmlHelper.XmlContentWriter writer)

Parameters
output
writer
Throws
IOException

public static String writeXml (BlocklyXmlHelper.XmlContentWriter contentWriter)

Parameters
contentWriter
Throws
IOException

public static void writeXml (Writer output, BlocklyXmlHelper.XmlContentWriter writer)

Parameters
output
writer
Throws
IOException