Package net.sf.okapi.common.filters
Class EventBuilder
- java.lang.Object
-
- net.sf.okapi.common.filters.EventBuilder
-
- Direct Known Subclasses:
AbstractMarkupEventBuilder,JsonEventBuilder,MarkdownEventBuilder,WhitespaceAdjustingEventBuilder,YamlEventBuilder
public class EventBuilder extends Object implements Iterator<Event>
EventBuilder provides a simplified API for filter writers that hides the low level resource API.EventBuilder allows filter writers to think in terms of start and end calls. For example, to produce a non-translatable
Eventyou would use startDocumentPart() and endDocumentPart(). For a text-basedEventyou would use startTextUnit() and endTextUnit().More complex cases such as tags with embedded translatable text can also be handled. See the AbstractMarkupFilter, HtmlFilter and OpenXmlFilter for examples of using EventBuilder.
-
-
Constructor Summary
Constructors Constructor Description EventBuilder()Instantiates a new EventBuilder.EventBuilder(String rootId, IFilter filter)Instantiates a new EventBuilder with a root ID.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DocumentPartaddDocumentPart(String part)voidaddFilterEvent(Event event)voidaddFilterEvents(List<Event> events)voidaddTextUnit(String text)Start and end aTextUnit.voidaddToDocumentPart(String part)Add to the currentDocumentPart.voidaddToTextUnit(String text)Adds text to the currentTextUnitvoidaddToTextUnit(Code code)voidaddToTextUnit(Code code, boolean endCodeNow)voidaddToTextUnit(Code code, boolean endCodeNow, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)voidaddToTextUnit(Code code, boolean endCodeNow, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)voidappendCodeInlineExcludedData(String data)append to the currentCode's datavoidappendCodeOuterData(String outerData)append to the currentCode's outerDatavoidappendToFirstSkeletonPart(String text)Appends text to the first data part of the skeleton.voidappendToSkeleton(GenericSkeleton part)Appends text to the first data part of the skeleton.voidcancel()Cancel current processing and add the CANCELEDEventto the event queue.booleancanStartNewTextUnit()voidconvertTempTextUnitToDocumentPart()Convert the skeleton attached to an in-progressTextUnitinto aDocumentPart.voidendCode()voidendCode(String tag)voidendDocumentPart()End theDocumentPartand finalize theEvent.voidendDocumentPart(String part)End the currentDocumentPartand finalize theEvent.EndingendGroup(GenericSkeleton endMarker)EndingendGroup(GenericSkeleton endMarker, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)voidendSubDocument()Add the END_SUBDOCUMENTEventto the event queue.ITextUnitendTextUnit()ITextUnitendTextUnit(GenericSkeleton endMarker)ITextUnitendTextUnit(GenericSkeleton endMarker, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)StringfindMostRecentParentId()StringfindMostRecentParentName()StringfindMostRecentTextUnitName()Find in our buffered queue the most recent TextUnit that has an assigned namevoidflushRemainingTempEvents()Flush all remaining events from theEventtemp queueCodegetCurrentCode()Get the currentCode.longgetDocumentPartId()Gets the current customDocumentPartid.IdGeneratorgetGroupId()Get theIdGeneratorfor GroupslonggetGroupIdSequence()StringgetRootId()Get the current root idIdGeneratorgetSubDocumentId()Get theIdGeneratorfor SubDocumentslonggetTextUnitId()Gets the custom textUnitId set by the caller.booleanhasNext()Are we finished parsing the input document?booleanhasParentTextUnit()Does the currentTextUnithave a parent?booleanhasQueuedEvents()Are there any queued events? We queue events in the correct order as expected by the Okapi filter writers (IWriter).booleanhasUnfinishedSkeleton()Is there an unfinishedDocumentPart(aka skeleton)?booleanisCurrentComplexTextUnit()booleanisCurrentGroup()Is the current bufferedEventaStartGroup?booleanisCurrentSubfilter()Is the current bufferedEventaStartSubfilter?booleanisCurrentTextUnit()booleanisInsideTextRun()Is the filter is inside text run?booleanisPreserveWhitespace()What is the current preserve whitespace state?booleanisTextUnitWithSameType(String type)Is the current TextUnit of the specified type?Eventnext()Return the next filter event waiting in the event queue.CodepeekMostRecentCode()Peek at the most recently createdStartGroup.StartGrouppeekMostRecentGroup()Peek at the most recently createdStartGrouporStartSubfilter.StartSubDocumentpeekMostRecentSubDocument()Peek at the most recently createdStartSubDocumentITextUnitpeekMostRecentTextUnit()Peek At the most recently createdTextUnit.EventpeekTempEvent()EventpopTempEvent()protected ITextUnitpostProcessTextUnit(ITextUnit textUnit)Do any required post-processing on the TextUnit after endTextUnit is called.voidreset(String rootId, IFilter filter)voidsetDocumentPartId(long id)Set the current customDocumentPartid.voidsetGroupIdSequence(long id)voidsetMimeType(String mimeType)Sets the input document mime type.voidsetPreserveWhitespace(boolean preserveWhitespace)Tell theIFilterwhat to do with whitespace.voidsetTextUnitId(long id)Set the current textUnitId.voidsetTextUnitMimeType(String mimeType)Set the mime type on the currentTextUnitvoidsetTextUnitName(String name)Set the currentTextUnitname.voidsetTextUnitPreserveWhitespace(boolean preserveWhitespace)Set preserve whitespace flag on currentTextUnitvoidsetTextUnitTranslatable(boolean translatable)Set the currentTextUnittranslatable flag.voidsetTextUnitType(String type)Set the currentTextUnittype.voidstartCode(Code code)DocumentPartstartDocumentPart(String part)Create aDocumentPartand store it for later processing.voidstartDocumentPart(String part, String name, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)Create aDocumentPartthat references actionable (i.e., translatable, localizable) properties and store it for later processing.voidstartDocumentPart(String part, String name, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)Create aDocumentPartthat references actionable (i.e., translatable, localizable) properties and store it for later processing.StartGroupstartGroup(GenericSkeleton startMarker, String commonTagType)Create aStartGroupEventand add it to the event queue.StartGroupstartGroup(GenericSkeleton startMarker, String commonTagType, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)Create aStartGroupEventand add it to the event queue.StartSubDocumentstartSubDocument()Add the START_SUBDOCUMENTEventto the event queue.voidstartTextUnit()Start aTextUnit.voidstartTextUnit(String text)Start aTextUnit.voidstartTextUnit(String text, GenericSkeleton startMarker, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)Start a complexTextUnitwith actionable (translatable, writable or read-only) attributes.voidstartTextUnit(String text, GenericSkeleton startMarker, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)Start a complexTextUnitwith actionable (translatable, writable or read-only) attributes.voidstartTextUnit(GenericSkeleton startMarker)Start a complexTextUnit.voidstartTextUnit(GenericSkeleton startMarker, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)Start a complexTextUnitwith actionable (translatable, writable or read-only) attributes.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Method Detail
-
hasNext
public boolean hasNext()
Are we finished parsing the input document?
-
next
public Event next()
Return the next filter event waiting in the event queue.
-
addFilterEvent
public void addFilterEvent(Event event)
Add a singleEventat the end of the currentEventqueue. Assume all child events have already been added in case of anIReferenceableEventas all children events must come before their parent reference.- Parameters:
event- TheEventto be added
-
addFilterEvents
public void addFilterEvents(List<Event> events)
Add an list ofEventat the end of the currentEventqueue. Assume all child events have already been added in case of anIReferenceableEventas all children events must come before their parent reference.- Parameters:
events- The list ofEventto be added
-
cancel
public void cancel()
Cancel current processing and add the CANCELEDEventto the event queue.
-
peekTempEvent
public Event peekTempEvent()
-
popTempEvent
public Event popTempEvent()
-
flushRemainingTempEvents
public void flushRemainingTempEvents()
Flush all remaining events from theEventtemp queue
-
isCurrentTextUnit
public boolean isCurrentTextUnit()
- Returns:
- true if TextUnit, false otherwise.
-
isCurrentComplexTextUnit
public boolean isCurrentComplexTextUnit()
Is the current bufferedEventa complexTextUnit? A complex TextUnit is one which carries along with it it's surrounding formatting or skeleton such <p> text </p> or <title> text </title>- Returns:
- true, if current
Eventis a complex text unit, false otherwise.
-
isCurrentGroup
public boolean isCurrentGroup()
Is the current bufferedEventaStartGroup?- Returns:
- true, if current
Eventis aStartGroup
-
isCurrentSubfilter
public boolean isCurrentSubfilter()
Is the current bufferedEventaStartSubfilter?- Returns:
- true, if current
Eventis aStartSubfilter
-
isInsideTextRun
public boolean isInsideTextRun()
Is the filter is inside text run?- Returns:
- true, if is inside text run
-
canStartNewTextUnit
public boolean canStartNewTextUnit()
Can we start newTextUnit? A newTextUnitcan only be started if the current one has been ended with endTextUnit. Or noTextUnithas been created yet.- Returns:
- true, if can start a new
TextUnit
-
hasQueuedEvents
public boolean hasQueuedEvents()
Are there any queued events? We queue events in the correct order as expected by the Okapi filter writers (IWriter).- Returns:
- true, if successful
-
findMostRecentParentId
public String findMostRecentParentId()
-
findMostRecentParentName
public String findMostRecentParentName()
-
findMostRecentTextUnitName
public String findMostRecentTextUnitName()
Find in our buffered queue the most recent TextUnit that has an assigned name- Returns:
- - the TextUnit name, null if not found
-
peekMostRecentGroup
public StartGroup peekMostRecentGroup()
Peek at the most recently createdStartGrouporStartSubfilter.- Returns:
- the filter event
-
peekMostRecentSubDocument
public StartSubDocument peekMostRecentSubDocument()
Peek at the most recently createdStartSubDocument- Returns:
- the filter event
-
peekMostRecentTextUnit
public ITextUnit peekMostRecentTextUnit()
Peek At the most recently createdTextUnit.- Returns:
- the filter event
-
peekMostRecentCode
public Code peekMostRecentCode()
Peek at the most recently createdStartGroup.- Returns:
- the filter event
-
hasUnfinishedSkeleton
public boolean hasUnfinishedSkeleton()
Is there an unfinishedDocumentPart(aka skeleton)?- Returns:
- true, if successful
-
hasParentTextUnit
public boolean hasParentTextUnit()
Does the currentTextUnithave a parent?- Returns:
- true, if successful
-
reset
public void reset(String rootId, IFilter filter)
- Parameters:
rootId- new root id (can be null or empty)filter- subfilter to use (can be null) ResetIFilterfor a new input. Callers should reset the EventBuilder for each input.
-
startSubDocument
public StartSubDocument startSubDocument()
Add the START_SUBDOCUMENTEventto the event queue.- Returns:
- the
StartSubDocumentcreated and added, or null if there is nosubFilter
-
endSubDocument
public void endSubDocument()
Add the END_SUBDOCUMENTEventto the event queue.
-
addTextUnit
public void addTextUnit(String text)
- Parameters:
text- the text used to prime theTextUnit
-
startTextUnit
public void startTextUnit(String text)
- Parameters:
text- the text used to prime theTextUnit
-
startTextUnit
public void startTextUnit()
-
startTextUnit
public void startTextUnit(GenericSkeleton startMarker)
- Parameters:
startMarker- the tag that begins the complexTextUnit
-
startTextUnit
public void startTextUnit(GenericSkeleton startMarker, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
-
startTextUnit
public void startTextUnit(String text, GenericSkeleton startMarker, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
-
startTextUnit
public void startTextUnit(String text, GenericSkeleton startMarker, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
-
convertTempTextUnitToDocumentPart
public void convertTempTextUnitToDocumentPart()
Convert the skeleton attached to an in-progressTextUnitinto aDocumentPart. This is used when a TextUnit has been started but then is immediately ended without any content. The final skeleton part in the TU is always a self-reference, which is stripped during the conversion.
-
endTextUnit
public ITextUnit endTextUnit(GenericSkeleton endMarker)
-
endTextUnit
public ITextUnit endTextUnit(GenericSkeleton endMarker, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
- Parameters:
endMarker- the tag that ends the complexTextUnitlocale- the locale of the textpropertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.- Returns:
- the ended
TextUnit - Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
addToTextUnit
public void addToTextUnit(String text)
Adds text to the currentTextUnit- Parameters:
text- the text- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
addToTextUnit
public void addToTextUnit(Code code)
Add aCodeto the currentTextUnit. Nothing is actionable within the tag (i.e., no properties or translatable, localizable text)- Parameters:
code- the code type- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
addToTextUnit
public void addToTextUnit(Code code, boolean endCodeNow)
Add aCodeto the currentTextUnit. Nothing is actionable within the tag (i.e., no properties or translatable, localizable text)- Parameters:
code- the code typeendCodeNow- do we end the code now or delay?- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
addToTextUnit
public void addToTextUnit(Code code, boolean endCodeNow, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
Add aCodeto the currentTextUnit. The Code contains actionable (i.e., translatable, localizable) attributes.- Parameters:
code- the codeendCodeNow- do we end the code now or delay?propertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.
-
addToTextUnit
public void addToTextUnit(Code code, boolean endCodeNow, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
Add aCodeto the currentTextUnit. The Code contains actionable (i.e., translatable, localizable) attributes.- Parameters:
code- the codeendCodeNow- do we end the code now or delay?locale- the language of the textpropertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
appendToFirstSkeletonPart
public void appendToFirstSkeletonPart(String text)
Appends text to the first data part of the skeleton.- Parameters:
text- the text to happend.- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
appendToSkeleton
public void appendToSkeleton(GenericSkeleton part)
Appends text to the first data part of the skeleton.- Parameters:
text- the text to happend.- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
startGroup
public StartGroup startGroup(GenericSkeleton startMarker, String commonTagType)
Create aStartGroupEventand add it to the event queue.- Parameters:
startMarker- the tag which starts the groupcommonTagType- the tag name or abstract type for this group.- Returns:
- the newly created StartGroup
-
startGroup
public StartGroup startGroup(GenericSkeleton startMarker, String commonTagType, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
Create aStartGroupEventand add it to the event queue.- Parameters:
startMarker- the tag which starts the group.commonTagType- the tag name or abstract type for this group.locale- the language of any actionable itemspropertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.- Returns:
- the newly created StartGroup
-
endGroup
public Ending endGroup(GenericSkeleton endMarker)
- Parameters:
endMarker- the tags that ends the group.- Returns:
- the newly created Ending
-
endGroup
public Ending endGroup(GenericSkeleton endMarker, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
- Parameters:
endMarker- the tags that ends the group.locale- the language of any actionable itemspropertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.- Returns:
- the newly created Ending
- Throws:
OkapiIllegalFilterOperationException- if we try to perform an invalid operation.
-
startCode
public void startCode(Code code)
-
endCode
public void endCode()
-
endCode
public void endCode(String tag)
-
appendCodeOuterData
public void appendCodeOuterData(String outerData)
append to the currentCode's outerData- Parameters:
outerData- the outerData to append
-
appendCodeInlineExcludedData
public void appendCodeInlineExcludedData(String data)
append to the currentCode's data- Parameters:
data- the data to append
-
addDocumentPart
public DocumentPart addDocumentPart(String part)
- Parameters:
part- theDocumentPart(aka skeleton)- Returns:
- the newly created DocumentPart
-
startDocumentPart
public DocumentPart startDocumentPart(String part)
Create aDocumentPartand store it for later processing.- Parameters:
part- theDocumentPart(aka skeleton)- Returns:
- the newly created DocumentPart
-
startDocumentPart
public void startDocumentPart(String part, String name, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
Create aDocumentPartthat references actionable (i.e., translatable, localizable) properties and store it for later processing.- Parameters:
part- theDocumentPart(aka skeleton)name- the namepropertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.
-
startDocumentPart
public void startDocumentPart(String part, String name, LocaleId locale, List<PropertyTextUnitPlaceholder> propertyTextUnitPlaceholders)
Create aDocumentPartthat references actionable (i.e., translatable, localizable) properties and store it for later processing.- Parameters:
part- theDocumentPart(aka skeleton)name- the namelocale- the language of any actionable itemspropertyTextUnitPlaceholders- the list of actionableTextUnitorPropertieswith offset information into the tag.
-
endDocumentPart
public void endDocumentPart(String part)
- Parameters:
part- theDocumentPart(aka skeleton)
-
endDocumentPart
public void endDocumentPart()
-
addToDocumentPart
public void addToDocumentPart(String part)
Add to the currentDocumentPart.- Parameters:
part- theDocumentPartas a String.
-
setMimeType
public void setMimeType(String mimeType)
Sets the input document mime type.- Parameters:
mimeType- the new mime type
-
setTextUnitMimeType
public void setTextUnitMimeType(String mimeType)
Set the mime type on the currentTextUnit- Parameters:
mimeType- - mime type
-
setPreserveWhitespace
public void setPreserveWhitespace(boolean preserveWhitespace)
Tell theIFilterwhat to do with whitespace.- Parameters:
preserveWhitespace- the preserveWhitespace as boolean.
-
setTextUnitPreserveWhitespace
public void setTextUnitPreserveWhitespace(boolean preserveWhitespace)
Set preserve whitespace flag on currentTextUnit- Parameters:
preserveWhitespace- the preserveWhitespace as boolean.
-
isPreserveWhitespace
public boolean isPreserveWhitespace()
What is the current preserve whitespace state?- Returns:
- true if whitespace is to be preserved, false otherwise
-
getTextUnitId
public long getTextUnitId()
Gets the custom textUnitId set by the caller.- Returns:
- the textUnitId
-
setTextUnitId
public void setTextUnitId(long id)
Set the current textUnitId. Note that using this method overrides the built-in id creation algorithm. Useful for some callers that wish to create custom ids.- Parameters:
id- the initial value for the textUnitId
-
setTextUnitName
public void setTextUnitName(String name)
Set the currentTextUnitname.- Parameters:
name- the name (resname in XLIFF) of theTextUnit- Throws:
NullPointerException- if there is no currentTextUnit
-
setTextUnitType
public void setTextUnitType(String type)
Set the currentTextUnittype. If there is no defined type the type is the element name.- Parameters:
type- - the TextUnit type.
-
setTextUnitTranslatable
public void setTextUnitTranslatable(boolean translatable)
Set the currentTextUnittranslatable flag.- Parameters:
translatable- - the TextUnit translatable flag.
-
getDocumentPartId
public long getDocumentPartId()
Gets the current customDocumentPartid.- Returns:
- the id
-
setDocumentPartId
public void setDocumentPartId(long id)
Set the current customDocumentPartid. Note that using this method overrides the built-in id creation algorithm. Useful for some callers that wish to create custom ids.- Parameters:
id- the initial value for the textUnitId
-
getRootId
public String getRootId()
Get the current root id- Returns:
- the rootId
-
postProcessTextUnit
protected ITextUnit postProcessTextUnit(ITextUnit textUnit)
Do any required post-processing on the TextUnit after endTextUnit is called. Default implementation leaves TextUnit unchanged. Override this method if you need to do format specific handing such as collapsing whitespace.
-
isTextUnitWithSameType
public boolean isTextUnitWithSameType(String type)
Is the current TextUnit of the specified type?
-
getGroupId
public IdGenerator getGroupId()
Get theIdGeneratorfor Groups- Returns:
- the
IdGenerator
-
getGroupIdSequence
public long getGroupIdSequence()
-
setGroupIdSequence
public void setGroupIdSequence(long id)
-
getSubDocumentId
public IdGenerator getSubDocumentId()
Get theIdGeneratorfor SubDocuments- Returns:
- the
IdGenerator
-
-