com.foxsmart.csu.config
Class CiscoRouterConfigParser

java.lang.Object
  extended by com.foxsmart.csu.config.CiscoRouterConfigParser
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
AccessListEntry, AccessListMatchInfo, AccessListPortMatchInfo, AccessListTcpFlagInfo, BandwidthPercentage, BandwidthValue, CiscoRouterConfigComponent, DynamicAccessListInfo, EncapsulationOptions, ExtendedMacAddressCompareInfo, GlobalEncryptionPassword, IpRouteEntry, LineEncryptionPassword, MatchInfo, ModemAutoConfigure, RandomDetectPrecedenceParams, RateLimitAction, RateLimitEntry, SchedulerAllocate, SmallServers, StateMachineInstance, TelnetSpeed, Timestamps

public class CiscoRouterConfigParser
extends java.lang.Object
implements java.io.Serializable

The CiscoRouterConfigParser class contains many methods to aid in the parsing and generation of IOS strings.

See Also:
Serialized Form

Field Summary
protected  org.apache.commons.logging.Log log
           
static java.lang.String NO
           
protected  int tokenIndex
           
 
Constructor Summary
CiscoRouterConfigParser()
          Constructs the parser.
 
Method Summary
protected  void checkExists(java.lang.Object object, java.lang.String description)
          Checks to see if a variable is not null.
protected  void checkLineComplete(java.lang.String line, java.util.List tokens)
          Checks that there are no more tokens on the current line.
protected  void checkNotEmpty(java.lang.String value, java.lang.String description)
          Checks to see if a variable is not empty.
protected  void checkRange(java.lang.Number value, java.lang.Number minRange, java.lang.Number maxRange, boolean required, java.lang.String description)
          Checks that a value is possibly not null and between a specified range.
protected  void checkTokenCount(java.lang.String line, java.util.List tokens, java.lang.Integer count)
          Ensures that the passed in list of tokens contains the count specified.
protected  void checkTokenCount(java.lang.String line, java.util.List tokens, java.lang.Integer lowRange, java.lang.Integer highRange)
          Ensures that the passed in list of tokens contains the correct count.
protected  void checkTokenMatch(java.lang.String line, java.util.List tokens, int count, java.lang.String match)
          Checks that the token number in the array of tokens matches the passed in string.
protected  void checkTokenMatch(java.lang.String line, java.lang.String token, java.lang.String match)
          Checks that a token matches the passed in match string.
protected  void decrementTokenIndex()
          Decrements the current token index.
protected  Commands generateCommands(java.lang.String ios)
          Generates a Commands object from a multi-line string.
protected  java.math.BigDecimal getBigDecimalNumberFromString(java.lang.String line, java.lang.String token)
          Checks that a token is a BigDecimal number and returns it.
protected  java.lang.Double getDoubleNumberFromString(java.lang.String line, java.lang.String token)
          Checks that a token is a double number and returns it.
protected  Enum getEnum(java.lang.Class enumClass, java.lang.String enumStringValue, java.lang.String line)
          Instantiates an enum of the specified type with the value specified.
protected  Hexadecimal getHexadecimalNumberFromString(java.lang.String line, java.lang.String token)
          Checks that a token is a hexadecimal number and returns it.
protected  java.lang.Integer getIntegerNumberFromString(java.lang.String line, java.lang.String token)
          Checks that a token is an integer and returns it.
protected  IpAddress getIpAddressFromString(java.lang.String line, java.lang.String token)
          Checks that a token is an IP Address and returns it.
protected  java.lang.String getLine(java.util.List lines, int count)
          Gets a specified line, that is to say, an element, from the passed in List.
protected  java.lang.Long getLongNumberFromString(java.lang.String line, java.lang.String token)
          Checks that a token is a long number and returns it.
protected  MacAddress getMacAddressFromString(java.lang.String line, java.lang.String token)
          Checks that a token is a 48-bit mac address and returns it.
protected  java.lang.String getNextOptionalToken(java.lang.String line, java.util.List tokens)
          Returns the next token in the list of tokens if one exists.
protected  java.lang.String getNextToken(java.lang.String line, java.util.List tokens)
          Returns the next token in the list of tokens.
protected  java.lang.String getToken(java.util.List tokens, int count)
          Returns the string token at the specified position.
protected  int getTokenIndex()
          Gets the current token index.
protected  java.util.List getTokens(java.lang.String line)
          Gets a list of tokens from the specified line.
protected  int getTotalIndentedLines(java.util.List lines)
          Deprecated. Use the getTotalLines(List configLines) method instead.
protected  int getTotalLines(java.util.List configLines)
          Gets the total number of lines that are a part of this configuration component.
protected  void incrementTokenIndex()
          Increments the current token index.
protected  void invalidConfiguration(java.lang.String errorMessage, java.lang.String line)
          Throws an InvalidConfigurationException with a specified error message.
protected  void invalidTokenFound(java.lang.String line, java.lang.String token)
          Throws an InvalidConfigurationException because an invalid token was found.
protected  boolean isEmpty(java.lang.String string)
          Determines if the passed in string is empty.
protected  boolean moreIndentedLines(java.util.List lines, int originalIndentPos)
          Determines if the next line in the list of lines is indented beyond the originally indented position for a component.
protected  boolean moreLines(java.util.List lines, int index)
          Determines if there are more lines to read.
protected  boolean moreTokens(java.util.List tokens)
          Determines if there are more tokens to read.
protected  java.util.List removeLines(java.util.List lines, int count)
          Removes a specified number of lines (List elements) from the beginning of the passed in lines list.
protected  void resetTokenIndex()
          Resets the current token index.
protected  void setTokenIndex(int index)
          Sets the current token index.
protected  int tokensLeft(java.util.List tokens)
          Returns the number of tokens left to read.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log

tokenIndex

protected int tokenIndex

NO

public static final java.lang.String NO
See Also:
Constant Field Values
Constructor Detail

CiscoRouterConfigParser

public CiscoRouterConfigParser()
Constructs the parser.

Method Detail

checkTokenCount

protected void checkTokenCount(java.lang.String line,
                               java.util.List tokens,
                               java.lang.Integer lowRange,
                               java.lang.Integer highRange)
                        throws InvalidConfigurationException
Ensures that the passed in list of tokens contains the correct count.

Parameters:
line - The IOS configuration line
tokens - The list of tokens to check
lowRange - The low range for the token count. If null, no low range check will be performed.
highRange - The high range for the token count. If null, no high range check will be performed.
Throws:
InvalidConfigurationException - if the number of token doesn't match what is expected.

checkLineComplete

protected void checkLineComplete(java.lang.String line,
                                 java.util.List tokens)
                          throws InvalidConfigurationException
Checks that there are no more tokens on the current line.

Parameters:
line - The IOS configuration line
tokens - The list of tokens to check
Throws:
InvalidConfigurationException - if there are still additional token on the line.

checkTokenCount

protected void checkTokenCount(java.lang.String line,
                               java.util.List tokens,
                               java.lang.Integer count)
                        throws InvalidConfigurationException
Ensures that the passed in list of tokens contains the count specified. This has the same functionality as calling checkTokenCount(line, tokens, count, count).

Parameters:
line - The IOS configuration line
tokens - The list of tokens to check
count - The number of tokens expected.
Throws:
InvalidConfigurationException - if the number of token doesn't match what is expected.

checkTokenMatch

protected void checkTokenMatch(java.lang.String line,
                               java.util.List tokens,
                               int count,
                               java.lang.String match)
                        throws InvalidConfigurationException
Checks that the token number in the array of tokens matches the passed in string.

Parameters:
line - The IOS configuration line
tokens - The list of tokens to check
count - The token number to check for a match. The token number should start at position 0.
match - The match string
Throws:
InvalidConfigurationException - if the token at the specified position doesn't match the passed in match string.

checkTokenMatch

protected void checkTokenMatch(java.lang.String line,
                               java.lang.String token,
                               java.lang.String match)
                        throws InvalidConfigurationException
Checks that a token matches the passed in match string. Note that this check is case-insensitive.

Parameters:
line - The IOS configuration line
token - The token to match
match - The match string
Throws:
InvalidConfigurationException - if the token doesn't match the passed in match string.

checkExists

protected void checkExists(java.lang.Object object,
                           java.lang.String description)
                    throws InvalidConfigurationException
Checks to see if a variable is not null.

Parameters:
object - The object to check
description - A description of the value (e.g. "width").
Throws:
InvalidConfigurationException - If the object is null.

checkNotEmpty

protected void checkNotEmpty(java.lang.String value,
                             java.lang.String description)
                      throws InvalidConfigurationException
Checks to see if a variable is not empty.

Parameters:
value - The value to check
description - A description of the value (e.g. "width").
Throws:
InvalidConfigurationException - If the value is empty.

checkRange

protected void checkRange(java.lang.Number value,
                          java.lang.Number minRange,
                          java.lang.Number maxRange,
                          boolean required,
                          java.lang.String description)
                   throws InvalidConfigurationException
Checks that a value is possibly not null and between a specified range.

Parameters:
value - The value to check
minRange - The lower bound of the range check
maxRange - The upper bound of the range check
required - Whether the value is required or not
description - A description of the value (e.g. "width").
Throws:
InvalidConfigurationException - If the value was not specified when required or falls outside the range.

invalidTokenFound

protected void invalidTokenFound(java.lang.String line,
                                 java.lang.String token)
                          throws InvalidConfigurationException
Throws an InvalidConfigurationException because an invalid token was found.

Parameters:
line - The line the token was found on
token - The invalid token
Throws:
InvalidConfigurationException - always.

invalidConfiguration

protected void invalidConfiguration(java.lang.String errorMessage,
                                    java.lang.String line)
                             throws InvalidConfigurationException
Throws an InvalidConfigurationException with a specified error message. The IOS configuration line will be appended automatically.

Parameters:
errorMessage - The error message.
line - The IOS line that contained the error.
Throws:
InvalidConfigurationException - always.

getIntegerNumberFromString

protected java.lang.Integer getIntegerNumberFromString(java.lang.String line,
                                                       java.lang.String token)
                                                throws InvalidConfigurationException
Checks that a token is an integer and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the integer number from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid number.

getLongNumberFromString

protected java.lang.Long getLongNumberFromString(java.lang.String line,
                                                 java.lang.String token)
                                          throws InvalidConfigurationException
Checks that a token is a long number and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the long number from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid number.

getDoubleNumberFromString

protected java.lang.Double getDoubleNumberFromString(java.lang.String line,
                                                     java.lang.String token)
                                              throws InvalidConfigurationException
Checks that a token is a double number and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the double number from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid number.

getBigDecimalNumberFromString

protected java.math.BigDecimal getBigDecimalNumberFromString(java.lang.String line,
                                                             java.lang.String token)
                                                      throws InvalidConfigurationException
Checks that a token is a BigDecimal number and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the big decimal number from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid number.

getHexadecimalNumberFromString

protected Hexadecimal getHexadecimalNumberFromString(java.lang.String line,
                                                     java.lang.String token)
                                              throws InvalidConfigurationException
Checks that a token is a hexadecimal number and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the hexadecimal number from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid hexadecimal number.

getMacAddressFromString

protected MacAddress getMacAddressFromString(java.lang.String line,
                                             java.lang.String token)
                                      throws InvalidConfigurationException
Checks that a token is a 48-bit mac address and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the mac address from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid 48-bit mac address.

getIpAddressFromString

protected IpAddress getIpAddressFromString(java.lang.String line,
                                           java.lang.String token)
                                    throws InvalidConfigurationException
Checks that a token is an IP Address and returns it.

Parameters:
line - The IOS configuration line
token - The token to check
Returns:
the integer number from the string value
Throws:
InvalidConfigurationException - if the token isn't a valid number.

getToken

protected java.lang.String getToken(java.util.List tokens,
                                    int count)
Returns the string token at the specified position. It is assumed that the List of tokens contains a string at the specified position.

Parameters:
tokens - The list of tokens
count - The token number to get.
Returns:
the string token.

getNextToken

protected java.lang.String getNextToken(java.lang.String line,
                                        java.util.List tokens)
                                 throws InvalidConfigurationException
Returns the next token in the list of tokens.

Parameters:
line - The IOS configuration line
tokens - The list of tokens
Returns:
the next token.
Throws:
InvalidConfigurationException - if there are no more tokens available.

getNextOptionalToken

protected java.lang.String getNextOptionalToken(java.lang.String line,
                                                java.util.List tokens)
Returns the next token in the list of tokens if one exists. Otherwise, null is returned.

Parameters:
line - The IOS configuration line
tokens - The list of tokens
Returns:
the next token or null.

getTokenIndex

protected int getTokenIndex()
Gets the current token index.

Returns:
The current token index.

setTokenIndex

protected void setTokenIndex(int index)
Sets the current token index.

Parameters:
index - The new token index. This value will be the next index used by getNextToken().

incrementTokenIndex

protected void incrementTokenIndex()
Increments the current token index.


decrementTokenIndex

protected void decrementTokenIndex()
Decrements the current token index.


resetTokenIndex

protected void resetTokenIndex()
Resets the current token index. This is the same as calling setTokenIndex(0).


moreTokens

protected boolean moreTokens(java.util.List tokens)
Determines if there are more tokens to read.

Parameters:
tokens - The list of tokens
Returns:
True if there are more tokens to read or false if not.

tokensLeft

protected int tokensLeft(java.util.List tokens)
Returns the number of tokens left to read.

Parameters:
tokens - The list of tokens
Returns:
the number of tokens.

moreLines

protected boolean moreLines(java.util.List lines,
                            int index)
Determines if there are more lines to read.

Parameters:
lines - The list of lines
index - The current line index
Returns:
True if there are more lines to read or false if not.

getLine

protected java.lang.String getLine(java.util.List lines,
                                   int count)
Gets a specified line, that is to say, an element, from the passed in List. No bounds checking is performed to ensure that the List contains enough elements to retrieve count's element. If a carriage return is the last character on a line, it is removed.

Parameters:
lines - The list of lines
count - The line in the list to retrieve.
Returns:
The line.

getTotalIndentedLines

protected int getTotalIndentedLines(java.util.List lines)
Deprecated. Use the getTotalLines(List configLines) method instead.

Gets the total number of lines that are part of the current configuration component. The total number of lines is calculated by taking the first line and any number of lines that are indented more than the first line.

***** WARNING ***** Note that this method is currently slightly flawed since a component may be a multiline component that has a carriage return built in. That means that the 2nd line of the component may not be indented eventhough it is part of an indented component. For example,

 line aux 0
   refuse-message
 ^CLine 1 Line2^C  <-- Should be part of indented line, but isn't.
   modem answer-timeout 65535
 

Parameters:
lines - The list of lines
Returns:
The number of indented lines.

moreIndentedLines

protected boolean moreIndentedLines(java.util.List lines,
                                    int originalIndentPos)
Determines if the next line in the list of lines is indented beyond the originally indented position for a component.

Parameters:
lines - The list of lines
originalIndentPos - The original indent offset of the first line for the component.
Returns:
True if more indented lines are present or false if not.

getTokens

protected java.util.List getTokens(java.lang.String line)
Gets a list of tokens from the specified line. Each set of characters separated by a space character is a unique token. If line is null or is empty, an empty List is returned.

Parameters:
line - The line of text used to retrieve the tokens
Returns:
The list of tokens on the specified line.

getTotalLines

protected int getTotalLines(java.util.List configLines)
Gets the total number of lines that are a part of this configuration component. The first line is assumed to be a part of the component and all additional lines are considered part of the component if they are indented more than the first line.

Parameters:
configLines - The total possible lines
Returns:
The total number of configuration lines.

removeLines

protected java.util.List removeLines(java.util.List lines,
                                     int count)
Removes a specified number of lines (List elements) from the beginning of the passed in lines list. If count is greater than the number of elements remaining, all the elements will be removed and an empty list will be returned.

Parameters:
lines - The list of lines
count - The number of lines to remove.
Returns:
A List with the specified number of lines removed.

isEmpty

protected boolean isEmpty(java.lang.String string)
Determines if the passed in string is empty.

Parameters:
string - the string to check.
Returns:
True if the string is empty or false if not.

generateCommands

protected Commands generateCommands(java.lang.String ios)
Generates a Commands object from a multi-line string. Each line in the string that is separated by a carriage return is a separate command.

Parameters:
ios - The list of commands in a string form.
Returns:
The commands object.

getEnum

protected Enum getEnum(java.lang.Class enumClass,
                       java.lang.String enumStringValue,
                       java.lang.String line)
                throws InvalidConfigurationException
Instantiates an enum of the specified type with the value specified. First, the string version of the value is attempted. If that fails, then the Integer version of the value is attempted. If that fails, null is returned.

Parameters:
enumClass - The class of the enum.
enumStringValue - The enum value.
line - The configuration line that is being processed.
Returns:
The enum.
Throws:
InvalidConfigurationException - if the enum couldn't be created properly.


Copyright © 2005-2008 Fox Smart, Inc. All Rights Reserved.