public class BuildingBlock extends CompositeNode
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Remarks:
BuildingBlock can contain only Section nodes.
BuildingBlock can only be a child of GlossaryDocument.
You can create new building blocks and insert them into a glossary document. You can modify or delete existing building blocks. You can copy or move building blocks between documents. You can insert content of a building block into a document.
Corresponds to the docPart, docPartPr and docPartBody elements in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
GlossaryDocument| Constructor and Description |
|---|
BuildingBlock(GlossaryDocument glossaryDoc)
Initializes a new instance of this class.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(DocumentVisitor visitor)
Accepts a visitor.
|
int |
acceptEnd(DocumentVisitor visitor)
Accepts a visitor for visiting the end of the BuildingBlock.
|
int |
acceptStart(DocumentVisitor visitor)
Accepts a visitor for visiting the start of the BuildingBlock.
|
int |
getBehavior()
Specifies the behavior that shall be applied when the contents of the building block is inserted into the main document.
|
java.lang.String |
getCategory()
Specifies the second-level categorization for the building block.
|
java.lang.String |
getDescription()
Gets the description associated with this building block.
|
Section |
getFirstSection()
Gets the first section in the building block.
|
int |
getGallery()
Specifies the first-level categorization for the building block for the purposes of classification or user interface sorting.
|
java.util.UUID |
getGuid()
Gets an identifier (a 128-bit GUID) that uniquely identifies this building block.
|
Section |
getLastSection()
Gets the last section in the building block.
|
java.lang.String |
getName()
Gets the name of this building block.
|
int |
getNodeType()
Returns the
NodeType.BUILDING_BLOCK value. |
SectionCollection |
getSections()
Returns a collection that represents all sections in the building block.
|
int |
getType()
Specifies the building block type.
|
void |
setBehavior(int value)
Specifies the behavior that shall be applied when the contents of the building block is inserted into the main document.
|
void |
setCategory(java.lang.String value)
Specifies the second-level categorization for the building block.
|
void |
setDescription(java.lang.String value)
Sets the description associated with this building block.
|
void |
setGallery(int value)
Specifies the first-level categorization for the building block for the purposes of classification or user interface sorting.
|
void |
setGuid(java.util.UUID value)
Sets an identifier (a 128-bit GUID) that uniquely identifies this building block.
|
void |
setName(java.lang.String value)
Sets the name of this building block.
|
void |
setType(int value)
Specifies the building block type.
|
acceptChildren, acceptCore, appendChild, coreRemoveSelfOnly, getChild, getChildNodes, getContainer, getCount, getCurrentNode, getFirstChild, getLastChild, getNextMatchingNode, getText, hasChildNodes, indexOf, insertAfter, insertBefore, isComposite, iterator, prependChild, removeAllChildren, removeChild, removeSmartTags, selectNodes, selectSingleNodedeepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getParentNode, getPreviousSibling, getRange, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic BuildingBlock(GlossaryDocument glossaryDoc)
Remarks:
When BuildingBlock is created, it belongs to the specified glossary document, but is not yet part of the glossary document and Node.getParentNode() is null.
To append BuildingBlock to a GlossaryDocument use CompositeNode.appendChild(com.aspose.words.Node).
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
glossaryDoc - The owner document.public int getNodeType()
NodeType.BUILDING_BLOCK value.
Examples:
Shows how to traverse a composite node's tree of child nodes.
public void recurseChildren() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
// Any node that can contain child nodes, such as the document itself, is composite.
Assert.assertTrue(doc.isComposite());
// Invoke the recursive function that will go through and print all the child nodes of a composite node.
traverseAllNodes(doc, 0);
}
/// <summary>
/// Recursively traverses a node tree while printing the type of each node
/// with an indent depending on depth as well as the contents of all inline nodes.
/// </summary>
public void traverseAllNodes(CompositeNode parentNode, int depth) {
for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType())));
// Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node.
if (childNode.isComposite()) {
System.out.println();
traverseAllNodes((CompositeNode) childNode, depth + 1);
} else if (childNode instanceof Inline) {
System.out.println(MessageFormat.format(" - \"{0}\"", childNode.getText().trim()));
} else {
System.out.println();
}
}
}
getNodeType in class NodeNodeType.BUILDING_BLOCK value. The returned value is one of NodeType constants.public boolean accept(DocumentVisitor visitor) throws java.lang.Exception
Remarks:
Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.
For more info see the Visitor design pattern.
Calls DocumentVisitor.visitBuildingBlockStart(com.aspose.words.BuildingBlock), then calls Node.accept(com.aspose.words.DocumentVisitor) for all child nodes of this building block, then calls DocumentVisitor.visitBuildingBlockEnd(com.aspose.words.BuildingBlock).
Note: A building block node and its children are not visited when you execute a Visitor over a Document. If you want to execute a Visitor over a building block, you need to execute the visitor over GlossaryDocument or call accept(com.aspose.words.DocumentVisitor).
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
accept in class Nodevisitor - The visitor that will visit the nodes.DocumentVisitor stopped the operation before visiting all nodes.java.lang.Exceptionpublic int acceptStart(DocumentVisitor visitor) throws java.lang.Exception
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
acceptStart in class CompositeNodevisitor - The document visitor.VisitorAction constants.java.lang.Exceptionpublic int acceptEnd(DocumentVisitor visitor) throws java.lang.Exception
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
acceptEnd in class CompositeNodevisitor - The document visitor.VisitorAction constants.java.lang.Exceptionpublic SectionCollection getSections()
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
public Section getFirstSection()
Remarks:
Returns null if there are no sections.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
public Section getLastSection()
Remarks:
Returns null if there are no sections.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
public java.lang.String getName()
Remarks:
The name may contain any string content, usually a friendly identifier. Multiple building blocks can have the same name.
Cannot be null and cannot be an empty string.
Corresponds to the docPartPr.name element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
getGallery(),
setGallery(int),
getCategory(),
setCategory(java.lang.String)public void setName(java.lang.String value)
Remarks:
The name may contain any string content, usually a friendly identifier. Multiple building blocks can have the same name.
Cannot be null and cannot be an empty string.
Corresponds to the docPartPr.name element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - The name of this building block.getGallery(),
setGallery(int),
getCategory(),
setCategory(java.lang.String)public java.util.UUID getGuid()
Remarks:
Can be used by an application to uniquely reference a building block regardless of different naming due to localization.
Corresponds to the docPartPr.guid element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
public void setGuid(java.util.UUID value)
Remarks:
Can be used by an application to uniquely reference a building block regardless of different naming due to localization.
Corresponds to the docPartPr.guid element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - An identifier (a 128-bit GUID) that uniquely identifies this building block.public java.lang.String getDescription()
Remarks:
The description may contain any string content, usually additional information.
Cannot be null, but can be an empty string.
Corresponds to the docPartPr.description element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
public void setDescription(java.lang.String value)
Remarks:
The description may contain any string content, usually additional information.
Cannot be null, but can be an empty string.
Corresponds to the docPartPr.description element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - The description associated with this building block.public int getGallery()
Remarks:
Building blocks in Microsoft Word user interface are arranged into Galleries. Each getGallery() / setGallery(int) can have multiple Categories. Each block within a getCategory() / setCategory(java.lang.String) has a getName() / setName(java.lang.String).
Corresponds to the docPartPr.category.gallery element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
int value. The returned value is one of BuildingBlockGallery constants.getCategory(),
setCategory(java.lang.String),
getName(),
setName(java.lang.String)public void setGallery(int value)
Remarks:
Building blocks in Microsoft Word user interface are arranged into Galleries. Each getGallery() / setGallery(int) can have multiple Categories. Each block within a getCategory() / setCategory(java.lang.String) has a getName() / setName(java.lang.String).
Corresponds to the docPartPr.category.gallery element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - The corresponding int value. The value must be one of BuildingBlockGallery constants.getCategory(),
setCategory(java.lang.String),
getName(),
setName(java.lang.String)public java.lang.String getCategory()
Remarks:
Building blocks in Microsoft Word user interface are arranged into Galleries. Each getGallery() / setGallery(int) can have multiple Categories. Each block within a getCategory() / setCategory(java.lang.String) has a getName() / setName(java.lang.String).
Cannot be null and cannot be an empty string.
Corresponds to the docPartPr.category.name element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
String value.getGallery(),
setGallery(int),
getName(),
setName(java.lang.String)public void setCategory(java.lang.String value)
Remarks:
Building blocks in Microsoft Word user interface are arranged into Galleries. Each getGallery() / setGallery(int) can have multiple Categories. Each block within a getCategory() / setCategory(java.lang.String) has a getName() / setName(java.lang.String).
Cannot be null and cannot be an empty string.
Corresponds to the docPartPr.category.name element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - The corresponding String value.getGallery(),
setGallery(int),
getName(),
setName(java.lang.String)public int getBehavior()
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
int value. The returned value is one of BuildingBlockBehavior constants.public void setBehavior(int value)
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - The corresponding int value. The value must be one of BuildingBlockBehavior constants.public int getType()
Remarks:
The building block type can influence the visibility and behavior of the building block in Microsoft Word.
Corresponds to the docPartPr.types element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
int value. The returned value is one of BuildingBlockType constants.public void setType(int value)
Remarks:
The building block type can influence the visibility and behavior of the building block in Microsoft Word.
Corresponds to the docPartPr.types element in OOXML.
Examples:
Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception {
// A document's glossary document stores building blocks.
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
doc.setGlossaryDocument(glossaryDoc);
// Create a building block, name it, and then add it to the glossary document.
BuildingBlock block = new BuildingBlock(glossaryDoc);
block.setName("Custom Block");
glossaryDoc.appendChild(block);
// All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
block.setGuid(UUID.randomUUID());
// The following properties categorize building blocks
// in the menu we can access in Microsoft Word via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
Assert.assertEquals(block.getCategory(), "(Empty Category)");
Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);
// Before we can add this building block to our document, we will need to give it some contents,
// which we will do using a document visitor. This visitor will also set a category, gallery, and behavior.
BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc);
// Visit start/end of the BuildingBlock.
block.accept(visitor);
// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
"My custom building blocks", "Custom Block");
// The block itself is a section that contains the text.
Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// Now, we can insert it into the document as a new section.
doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));
// We can also find it in Microsoft Word's Building Blocks Organizer and place it manually.
doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx");
}
/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
mBuilder = new StringBuilder();
mGlossaryDoc = ownerGlossaryDoc;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
// Configure the building block as a quick part, and add properties used by Building Blocks Organizer.
block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
block.setCategory("My custom building blocks");
block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");
block.setGallery(BuildingBlockGallery.QUICK_PARTS);
// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(mGlossaryDoc);
block.appendChild(section);
block.getFirstSection().ensureMinimum();
Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());
block.getFirstSection().getBody().getFirstParagraph().appendChild(run);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("Visited " + block.getName() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
private final GlossaryDocument mGlossaryDoc;
}
value - The corresponding int value. The value must be one of BuildingBlockType constants.