public class GlossaryDocument extends DocumentBase
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Remarks:
Some documents, usually templates, can contain AutoText, AutoCorrect entries and/or Building Blocks (also known as glossary document entries, document parts or building blocks).
To access building blocks, you need to load a document into a Document object. Building blocks will be available via the Document.getGlossaryDocument() / Document.setGlossaryDocument(com.aspose.words.GlossaryDocument) property.
GlossaryDocument can contain any number of BuildingBlock objects. Each BuildingBlock represents one document part.
Corresponds to the glossaryDocument and docParts elements in OOXML.
Examples:
Shows ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
mImageTransparencyFlagCache| 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 Glossary document.
|
int |
acceptStart(DocumentVisitor visitor)
Accepts a visitor for visiting the start of the Glossary document.
|
BuildingBlock |
getBuildingBlock(int gallery,
java.lang.String category,
java.lang.String name) |
BuildingBlockCollection |
getBuildingBlocks()
Returns a typed collection that represents all building blocks in the glossary document.
|
BuildingBlock |
getFirstBuildingBlock()
Gets the first building block in the glossary document.
|
BuildingBlock |
getLastBuildingBlock()
Gets the last building block in the glossary document.
|
int |
getNodeType()
Returns the
NodeType.GLOSSARY_DOCUMENT value. |
getBackgroundShape, getDocument, getFontInfos, getFootnoteSeparators, getLists, getNodeChangingCallback, getPageColor, getResourceLoadingCallback, getStyles, getWarningCallback, importNode, importNode, resetState, setBackgroundShape, setNodeChangingCallback, setPageColor, setResourceLoadingCallback, setWarningCallbackacceptChildren, 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, getNextSibling, getParentNode, getPreviousSibling, getRange, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic int getNodeType()
NodeType.GLOSSARY_DOCUMENT 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.GLOSSARY_DOCUMENT value. The returned value is one of NodeType constants.public BuildingBlockCollection getBuildingBlocks()
Examples:
Shows ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
public BuildingBlock getFirstBuildingBlock()
Remarks:
Returns null if there are no building blocks available.
Examples:
Shows ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
public BuildingBlock getLastBuildingBlock()
Remarks:
Returns null if there are no building blocks available.
Examples:
Shows ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
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.visitGlossaryDocumentStart(com.aspose.words.GlossaryDocument), then calls Node.accept(com.aspose.words.DocumentVisitor) for all child nodes of this node and then calls DocumentVisitor.visitGlossaryDocumentEnd(com.aspose.words.GlossaryDocument) at the end.
Note: A glossary document node and its children are not visited when you execute a Visitor over a Document. If you want to execute a Visitor over a glossary document, you need to call accept(com.aspose.words.DocumentVisitor).
Examples:
Shows ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
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 ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
acceptStart in class CompositeNodevisitor - The document visitor.VisitorAction constants.java.lang.Exceptionpublic int acceptEnd(DocumentVisitor visitor) throws java.lang.Exception
Examples:
Shows ways of accessing building blocks in a glossary document.
public void glossaryDocument() throws Exception {
Document doc = new Document();
GlossaryDocument glossaryDoc = new GlossaryDocument();
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 1"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 2"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 3"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 4"));
glossaryDoc.appendChild(createNewBuildingBlock(glossaryDoc, "Block 5"));
Assert.assertEquals(glossaryDoc.getBuildingBlocks().getCount(), 5);
doc.setGlossaryDocument(glossaryDoc);
// There are various ways of accessing building blocks.
// 1 - Get the first/last building blocks in the collection:
Assert.assertEquals("Block 1", glossaryDoc.getFirstBuildingBlock().getName());
Assert.assertEquals("Block 5", glossaryDoc.getLastBuildingBlock().getName());
// 2 - Get a building block by index:
Assert.assertEquals("Block 2", glossaryDoc.getBuildingBlocks().get(1).getName());
Assert.assertEquals("Block 3", glossaryDoc.getBuildingBlocks().toArray()[2].getName());
// 3 - Get the first building block that matches a gallery, name and category:
Assert.assertEquals("Block 4",
glossaryDoc.getBuildingBlock(BuildingBlockGallery.ALL, "(Empty Category)", "Block 4").getName());
// We will do that using a custom visitor,
// which will give every BuildingBlock in the GlossaryDocument a unique GUID
GlossaryDocVisitor visitor = new GlossaryDocVisitor();
// Visit start/end of the Glossary document.
glossaryDoc.accept(visitor);
// Visit only start of the Glossary document.
glossaryDoc.acceptStart(visitor);
// Visit only end of the Glossary document.
glossaryDoc.acceptEnd(visitor);
System.out.println(visitor.getText());
// In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
doc.save(getArtifactsDir() + "BuildingBlocks.GlossaryDocument.dotx");
}
public static BuildingBlock createNewBuildingBlock(final GlossaryDocument glossaryDoc, final String buildingBlockName) {
BuildingBlock buildingBlock = new BuildingBlock(glossaryDoc);
buildingBlock.setName(buildingBlockName);
return buildingBlock;
}
/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public static class GlossaryDocVisitor extends DocumentVisitor {
public GlossaryDocVisitor() {
mBlocksByGuid = new HashMap<>();
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public HashMap<UUID, BuildingBlock> getDictionary() {
return mBlocksByGuid;
}
public int visitGlossaryDocumentStart(final GlossaryDocument glossary) {
mBuilder.append("Glossary document found!\n");
return VisitorAction.CONTINUE;
}
public int visitGlossaryDocumentEnd(final GlossaryDocument glossary) {
mBuilder.append("Reached end of glossary!\n");
mBuilder.append("BuildingBlocks found: " + mBlocksByGuid.size() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockStart(final BuildingBlock block) {
block.setGuid(UUID.randomUUID());
mBlocksByGuid.put(block.getGuid(), block);
return VisitorAction.CONTINUE;
}
public int visitBuildingBlockEnd(final BuildingBlock block) {
mBuilder.append("\tVisited block \"" + block.getName() + "\"" + "\r\n");
mBuilder.append("\t Type: " + block.getType() + "\r\n");
mBuilder.append("\t Gallery: " + block.getGallery() + "\r\n");
mBuilder.append("\t Behavior: " + block.getBehavior() + "\r\n");
mBuilder.append("\t Description: " + block.getDescription() + "\r\n");
return VisitorAction.CONTINUE;
}
private final HashMap<UUID, BuildingBlock> mBlocksByGuid;
private final StringBuilder mBuilder;
}
acceptEnd in class CompositeNodevisitor - The document visitor.VisitorAction constants.java.lang.Exceptionpublic BuildingBlock getBuildingBlock(int gallery, java.lang.String category, java.lang.String name)