📄 rtfparser.java
字号:
// return the proper reader object to the parser setup
return (PushbackInputStream)readerIn;
}
/**
* Imports the mappings defined in the RtfImportMappings into the
* RtfImportHeader of this RtfParser2.
*
* @param importMappings
* The RtfImportMappings to import.
* @since 2.1.3
*/
private void handleImportMappings(RtfImportMappings importMappings) {
Iterator it = importMappings.getFontMappings().keySet().iterator();
while(it.hasNext()) {
String fontNr = (String) it.next();
this.importMgr.importFont(fontNr, (String) importMappings.getFontMappings().get(fontNr));
}
it = importMappings.getColorMappings().keySet().iterator();
while(it.hasNext()) {
String colorNr = (String) it.next();
this.importMgr.importColor(colorNr, (Color) importMappings.getColorMappings().get(colorNr));
}
it = importMappings.getListMappings().keySet().iterator();
while(it.hasNext()) {
String listNr = (String) it.next();
this.importMgr.importList(listNr, (String)importMappings.getListMappings().get(listNr));
}
it = importMappings.getStylesheetListMappings().keySet().iterator();
while(it.hasNext()) {
String stylesheetListNr = (String) it.next();
this.importMgr.importStylesheetList(stylesheetListNr, (List) importMappings.getStylesheetListMappings().get(stylesheetListNr));
}
}
/* *****************************************
* DOCUMENT CONTROL METHODS
*
* Handles -
* handleOpenGroup: Open groups - '{'
* handleCloseGroup: Close groups - '}'
* handleCtrlWord: Ctrl Words - '\...'
* handleCharacter: Characters - Plain Text, etc.
*
*/
/**
* Handles open group tokens. ({)
*
* @return errOK if ok, other if an error occurred.
* @since 2.1.3
*/
public int handleOpenGroup() {
int result = errOK;
this.openGroupCount++; // stats
this.groupLevel++; // current group level in tokeniser
this.docGroupLevel++; // current group level in document
if (this.getTokeniserState() == TOKENISER_SKIP_GROUP) {
this.groupSkippedCount++;
}
RtfDestination dest = this.getCurrentDestination();
boolean handled = false;
if(dest != null) {
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: before dest.handleOpeningSubGroup()");
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: destination=" + dest.toString());
}
handled = dest.handleOpeningSubGroup();
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: after dest.handleOpeningSubGroup()");
}
}
this.stackState.push(this.currentState);
this.currentState = new RtfParserState(this.currentState);
// do not set this true until after the state is pushed
// otherwise it inserts a { where one does not belong.
this.currentState.newGroup = true;
dest = this.getCurrentDestination();
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: handleOpenGroup()");
if(this.lastCtrlWordParam != null)
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: LastCtrlWord=" + this.lastCtrlWordParam.ctrlWord);
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: grouplevel=" + Integer.toString(groupLevel));
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: destination=" + dest.toString());
}
if(dest != null) {
handled = dest.handleOpenGroup();
}
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: after dest.handleOpenGroup(); handled=" + Boolean.toString(handled));
}
return result;
}
public static void outputDebug(Object doc, int groupLevel, String str) {
System.out.println(str);
if(doc == null) return;
if(groupLevel<0) groupLevel = 0;
char[] a; Arrays.fill(a= new char[groupLevel*2], ' ');
String spaces= new String(a);
if(doc instanceof RtfDocument) {
((RtfDocument)doc).add(new RtfDirectContent("\n" + spaces + str));
}
else
if(doc instanceof Document) {
try {
((Document)doc).add(new RtfDirectContent("\n" + spaces + str));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* Handles close group tokens. (})
*
* @return errOK if ok, other if an error occurred.
* @since 2.1.3
*/
public int handleCloseGroup() {
int result = errOK;
this.closeGroupCount++; // stats
if (this.getTokeniserState() != TOKENISER_SKIP_GROUP) {
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: handleCloseGroup()");
if(this.lastCtrlWordParam != null)
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: LastCtrlWord=" + this.lastCtrlWordParam.ctrlWord);
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: grouplevel=" + Integer.toString(groupLevel));
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: destination=" + this.getCurrentDestination().toString());
RtfParser.outputDebug(this.rtfDoc, groupLevel, "");
}
RtfDestination dest = this.getCurrentDestination();
boolean handled = false;
if(dest != null) {
handled = dest.handleCloseGroup();
}
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: After dest.handleCloseGroup(); handled = " + Boolean.toString(handled));
RtfParser.outputDebug(this.rtfDoc, groupLevel, "");
}
}
if(this.stackState.size() >0 ) {
this.currentState = (RtfParserState)this.stackState.pop();
} else {
result = errStackUnderflow;
}
this.docGroupLevel--;
this.groupLevel--;
if (this.getTokeniserState() == TOKENISER_SKIP_GROUP && this.groupLevel < this.skipGroupLevel) {
this.setTokeniserState(TOKENISER_NORMAL);
}
return result;
}
/**
* Handles control word tokens. Depending on the current
* state a control word can lead to a state change. When
* parsing the actual document contents, certain tabled
* values are remapped. i.e. colors, fonts, styles, etc.
*
* @param ctrlWordData The control word to handle.
* @return errOK if ok, other if an error occurred.
* @since 2.1.3
*/
public int handleCtrlWord(RtfCtrlWordData ctrlWordData) {
int result = errOK;
this.ctrlWordCount++; // stats
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: handleCtrlWord=" + ctrlWordData.ctrlWord + " param=[" + ctrlWordData.param + "]");
}
if (this.getTokeniserState() == TOKENISER_SKIP_GROUP) {
this.ctrlWordSkippedCount++;
if(debugParser) {
RtfParser.outputDebug(this.rtfDoc, groupLevel, "DEBUG: SKIPPED");
}
return result;
}
// RtfDestination dest = (RtfDestination)this.getCurrentDestination();
// boolean handled = false;
// if(dest != null) {
// handled = dest.handleControlWord(ctrlWordData);
// }
result = this.rtfKeywordMgr.handleKeyword(ctrlWordData, this.groupLevel);
if( result == errOK){
this.ctrlWordHandledCount++;
} else {
this.ctrlWordNotHandledCount++;
result = errOK; // hack for now.
}
return result;
}
/**
* Handles text tokens. These are either handed on to the
* appropriate destination handler.
*
* @param nextChar
* The text token to handle.
* @return errOK if ok, other if an error occurred.
* @since 2.1.3
*/
// public int handleCharacter(char[] nextChar) {
public int handleCharacter(int nextChar) {
this.characterCount++; // stats
if (this.getTokeniserState() == TOKENISER_SKIP_GROUP) {
return errOK;
}
boolean handled = false;
RtfDestination dest = this.getCurrentDestination();
if(dest != null) {
handled = dest.handleCharacter(nextChar);
}
return errOK;
}
/**
* Get the state of the parser.
*
* @return
* The current RtfParserState state object.
* @since 2.1.3
*/
public RtfParserState getState(){
return this.currentState;
}
/**
* Get the current state of the parser.
*
* @return
* The current state of the parser.
* @since 2.1.3
*/
public int getParserState(){
return this.currentState.parserState;
}
/**
* Set the state value of the parser.
*
* @param newState
* The new state for the parser
* @return
* The state of the parser.
* @since 2.1.3
*/
public int setParserState(int newState){
this.currentState.parserState = newState;
return this.currentState.parserState;
}
/**
* Get the conversion type.
*
* @return
* The type of the conversion. Import or Convert.
* @since 2.1.3
*/
public int getConversionType() {
return this.conversionType;
}
/**
* Get the RTF Document object.
* @return
* Returns the object rtfDoc.
* @since 2.1.3
*/
public RtfDocument getRtfDocument() {
return this.rtfDoc;
}
/**
* Get the Document object.
* @return
* Returns the object rtfDoc.
* @since 2.1.3
*/
public Document getDocument() {
return this.document;
}
/**
* Get the RtfImportHeader object.
* @return
* Returns the object importHeader.
* @since 2.1.3
*/
public RtfImportMgr getImportManager() {
return importMgr;
}
/////////////////////////////////////////////////////////////
// accessors for destinations
/**
* Set the current destination object for the current state.
* @param destination The destination value to set.
* @since 2.1.3
*/
public boolean setCurrentDestination(String destination) {
RtfDestination dest = RtfDestinationMgr.getDestination(destination);
if(dest != null) {
this.currentState.destination = dest;
return false;
} else {
this.setTokeniserStateSkipGroup();
return false;
}
}
/**
* Get the current destination object.
*
* @return The current state destination
* @since 2.1.3
*/
public RtfDestination getCurrentDestination() {
return this.currentState.destination;
}
/**
* Get a destination from the map
*
* @param destination The string destination.
* @return The destination object from the map
* @since 2.1.3
*/
public RtfDestination getDestination(String destination) {
return RtfDestinationMgr.getDestination(destination);
}
/**
* Helper method to determine if this is a new group.
*
* @return true if this is a new group, otherwise it returns false.
* @since 2.1.3
*/
public boolean isNewGroup() {
return this.currentState.newGroup;
}
/**
* Helper method to set the new group flag
* @param value The boolean value to set the flag
* @return The value of newGroup
* @since 2.1.3
*/
public boolean setNewGroup(boolean value) {
this.currentState.newGroup = value;
return this.currentState.newGroup;
}
/* ************
* TOKENISER *
**************/
/**
* Read through the input file and parse the data stream into tokens.
*
* @throws IOException on IO error.
* @since 2.1.3
*/
public void tokenise() throws IOException {
int errorCode = errOK; // error code
int nextChar = 0;
// char[] nextChar = new char[1]; // input variable
// nextChar[0]=0; // set to 0
this.setTokeniserState(TOKENISER_NORMAL); // set initial tokeniser state
// while(this.pbReader.read(nextChar) != -1) {
while((nextChar = this.pbReader.read()) != -1) {
this.byteCount++;
if (this.getTokeniserState() == TOKENISER_BINARY) // if we're parsing binary data, handle it directly
{
if ((errorCode = parseChar(nextChar)) != errOK)
return;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -