org.jfugue
Class MusicStringParser

java.lang.Object
  extended by org.jfugue.Parser
      extended by org.jfugue.MusicStringParser

public final class MusicStringParser
extends Parser

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. As of Version 3.0, the Parser supports turning MIDI Sequences into JFugue Patterns with the parse(Sequence) method. In this case, the ParserListeners established by a ParserBuilder use the parsed events to construct the Pattern string.

Version:
3.0, 4.0 - Note parsing split up into many separate methods; verification added for testing purposes
Author:
David Koelle

Field Summary
 
Fields inherited from class org.jfugue.Parser
TRACING_OFF, TRACING_ON
 
Constructor Summary
MusicStringParser()
          Creates a new Parser object, and populates the dictionary with initial entries.
 
Method Summary
static Note getNote(Pattern pattern)
          Parses a pattern which presumably contains one token, which is a note.
static Note getNote(java.lang.String string)
          Parses a string which presumably contains one token, which is a note.
 boolean isValidToken(java.lang.String token)
          Checks whether a token is valid.
static void main(java.lang.String[] args)
          Used for diagnostic purposes.
 void parse(Pattern pattern)
          Parses a Pattern and fires events to subscribed ParserListener interfaces.
 void verifyToken(java.lang.String token, java.lang.String verifyString)
           
 
Methods inherited from class org.jfugue.Parser
addParserListener, addParserProgressListener, getTracing, removeParserListener, removeParserProgressListener, setTracing
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MusicStringParser

public MusicStringParser()
Creates a new Parser object, and populates the dictionary with initial entries.

See Also:
JFugueDefinitions
Method Detail

parse

public void parse(Pattern pattern)
           throws JFugueException
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 (K, V, T, I, L, X, #, $, @, &, +, *, |), the notes (A, B, C, D, E, F, G, R), 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
JFugueException

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.

verifyToken

public void verifyToken(java.lang.String token,
                        java.lang.String verifyString)

getNote

public static Note getNote(java.lang.String string)
Parses a string which presumably contains one token, which is a note.

Parameters:
string - The String that contains one token with a note, like "C5"
Returns:
a Note object representing the note parsed from the string

getNote

public static Note getNote(Pattern pattern)
Parses a pattern which presumably contains one token, which is a note.

Parameters:
pattern - The Pattern that contains one token with a note, like "C5"
Returns:
a Note object representing the note parsed from the pattern

main

public static void main(java.lang.String[] args)
Used for diagnostic purposes. main() makes calls to test the Pattern-to-MIDI parser. If you make any changes to this parser, run this method ("java org.jfugue.MusicStringParser"), and make sure everything works correctly.

Parameters:
args - not used