org.jfugue
Class Parser

java.lang.Object
  |
  +--org.jfugue.Parser

public class Parser
extends java.lang.Object

Parses music strings, and fires events for ParserListener interfaces when tokens are interpreted. The ParserListener does intelligent things with the resulting events, such as create music, draw sheet music, or transform the data.

Version:
2.0
Author:
David Koelle

Field Summary
static int TRACING_OFF
          Pass this value to setTracing( ) to turn tracing off.
static int TRACING_ON
          Pass this value to setTracing( ) to turn tracing on.
 
Constructor Summary
Parser()
          Creates a new Parser object, and populates the dictionary with initial entries.
 
Method Summary
 void addParserListener(ParserListener listener)
          Adds a ParserListener.
protected  int getTempo()
          Returns the tempo for the current song.
static int getTracing()
          Returns the current state of tracing.
 boolean isValidToken(java.lang.String token)
          Checks whether a token is valid.
static void main(java.lang.String[] args)
          main( ) is used for diagnostic purposes.
 void parse(Pattern pattern)
          Parses a Pattern and fires events to subscribed ParserListener interfaces.
 void removeParserListener(ParserListener listener)
          Removes a ParserListener.
protected  void setTempo(int tempo)
          Sets the tempo for the current song.
static void setTracing(int tracing)
          Turns tracing on or off.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRACING_OFF

public static final int TRACING_OFF
Pass this value to setTracing( ) to turn tracing off. Tracing is off by default.

TRACING_ON

public static final int TRACING_ON
Pass this value to setTracing( ) to turn tracing on. Tracing is off by default.
Constructor Detail

Parser

public Parser()
Creates a new Parser object, and populates the dictionary with initial entries.
See Also:
JFugueDefinitions
Method Detail

setTracing

public static void setTracing(int tracing)
Turns tracing on or off. If you're having trouble with your music string, or if you've added new tokens to the parser, turn tracing on to make sure that your new tokens are parsed correctly.
Parameters:
tracing - the state of tracing - on or off

getTracing

public static int getTracing()
Returns the current state of tracing.
Returns:
the state of tracing

addParserListener

public void addParserListener(ParserListener listener)
Adds a ParserListener. The listener will receive events when the parser interprets music string tokens.
Parameters:
listener - the listener that is to be notified of parser events

removeParserListener

public void removeParserListener(ParserListener listener)
Removes a ParserListener.
Parameters:
listener - the listener to remove

setTempo

protected void setTempo(int tempo)
Sets the tempo for the current song. Tempo is measured in "pulses per quarter". The parser uses this value to convert note durations, which are relative values and not directly related to time measurements, into actual times. For example, a whole note has the same duration as four quarter notes, but neither a whole note nor a quarter note equates to any real-life time delay until it's multplied by the tempo. The default value for Tempo is 120 pulses per quarter.
Parameters:
tempo - the tempo for the current song, in pulses per quarter.

getTempo

protected int getTempo()
Returns the tempo for the current song.

parse

public void parse(Pattern pattern)
           throws java.lang.Exception
Parses a Pattern and fires events to subscribed ParserListener interfaces. As the Pattern is parsed, events are sent to ParserLisener interfaces, which are responsible for doing something interesting with the music data, such as playing the music, displaying it as sheet music, or transforming the pattern.

The parser breaks a music string into tokens, which are separated by spaces. It then determines the type of command based on the first character of the token. If the parser does not recognize the first character of the token, which is limited to the command letters (V, T, I, X, #), the notes (A, B, C, D, E, F, G), and the open-bracket character ( [ ), then the token will be ignored.

Parameters:
pattern - the Pattern to parse
Throws:
java.lang.Exception - if there is an error parsing the pattern

isValidToken

public boolean isValidToken(java.lang.String token)
Checks whether a token is valid. This method is provided for testing purposes, and is not used during normal operation.
Parameters:
token - the token to test for validity
Returns:
true is the token is valid; false otherwise.

main

public static void main(java.lang.String[] args)
main( ) is used for diagnostic purposes. It contains an assortment of tokens that are known to parse correctly. If you make any changes to the parser, run this method ("java org.jfugue.Parser"), and make sure all of these tokens parse correctly.
Parameters:
args - not used