de.bsd.x2svg
Class ParserLoader

java.lang.Object
  extended by de.bsd.x2svg.ParserLoader

public class ParserLoader
extends java.lang.Object

This singleton class is used to load the Parsers as plugins. Parsers are expected to live in the package de.bsd.x2svg.parsers and to implement the interface InputParser.

The loader currently expects that all parsers live in the main jar file (x2svg) within the classpath.

Before the any other methods can be called, it is necessary to at lease once call load().

Since:
1.0
Author:
hwr@pilhuhn.de
See Also:
de.bsd.x2svg.parsers

Nested Class Summary
private static class ParserLoader.ClassNameFileFilter
          Filter that accepts files ending in .class
 
Field Summary
private static boolean debug
           
private  java.util.Map<java.lang.String,java.lang.String> helpByMode
           
private  boolean loaded
           
private  java.util.Map<java.lang.String,java.lang.String> modeAndSuffix
           
private static java.lang.String NO_SPECIFIC_HELP_AVAILABLE
           
private static java.lang.String PACKAGE_STRING
           
private  java.util.Map<java.lang.String,java.lang.Class<InputParser>> parserByMode
           
private  java.util.Map<java.lang.String,java.lang.Class<InputParser>> parserBySuffix
           
private static ParserLoader theLoader
           
 
Constructor Summary
private ParserLoader()
          Private constructor as we are a singleton
 
Method Summary
private  void addToParserList(java.lang.Class<InputParser> clazz)
          Enter the passed class into the lists of parsers
private  void checkClassIsParser(java.lang.String classname)
          Checks if the class passed in is a valid InputParser.
 java.lang.String getHelpForMode(java.lang.String mode)
          Return the parser specific help for the passed mode.
static ParserLoader getLoader()
          Get the instance of ParserLoader
 InputParser getParserByMode(java.lang.String mode)
          Return a parser for a given mode.
 InputParser getParserForFilename(java.lang.String filename)
          Return a parser instance by selecting a matching suffix for the file name.
 java.lang.String listParsers()
          List all parsers that we know about
 void load()
          Load the parsers from the main jar and register them in our list.
static void main(java.lang.String[] args)
          For debugging the ParserLoader separately
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PACKAGE_STRING

private static final java.lang.String PACKAGE_STRING
See Also:
Constant Field Values

theLoader

private static volatile ParserLoader theLoader

parserByMode

private java.util.Map<java.lang.String,java.lang.Class<InputParser>> parserByMode

parserBySuffix

private java.util.Map<java.lang.String,java.lang.Class<InputParser>> parserBySuffix

helpByMode

private java.util.Map<java.lang.String,java.lang.String> helpByMode

modeAndSuffix

private java.util.Map<java.lang.String,java.lang.String> modeAndSuffix

debug

private static boolean debug

NO_SPECIFIC_HELP_AVAILABLE

private static final java.lang.String NO_SPECIFIC_HELP_AVAILABLE
See Also:
Constant Field Values

loaded

private boolean loaded
Constructor Detail

ParserLoader

private ParserLoader()
Private constructor as we are a singleton

Method Detail

getLoader

public static ParserLoader getLoader()
Get the instance of ParserLoader

Returns:
The instance of the ParserLoader

getParserByMode

public InputParser getParserByMode(java.lang.String mode)
                            throws NoParserException,
                                   java.lang.IllegalAccessException,
                                   java.lang.InstantiationException
Return a parser for a given mode.

Parameters:
mode - a Mode that needs to be registered by the parser.
Returns:
a valid InputParser if found
Throws:
NoParserException - if the passed mode is invalid or no parser was found
java.lang.IllegalAccessException - If the parser class is not accessible
java.lang.InstantiationException - If the parser class can not be instantiated
java.lang.IllegalStateException - If load() has not yet been called

getParserForFilename

public InputParser getParserForFilename(java.lang.String filename)
                                 throws NoParserException,
                                        java.lang.InstantiationException,
                                        java.lang.IllegalAccessException
Return a parser instance by selecting a matching suffix for the file name.

Parameters:
filename - Name of the file to determine the suffix
Returns:
a valid InputParser
Throws:
NoParserException - if the input is invalid or no parser was found for the passed filename.
java.lang.IllegalAccessException - If the parser class is not accessible
java.lang.InstantiationException - If the parser class can not be instantiated
java.lang.IllegalStateException - If load() has not yet been called
See Also:
String.endsWith(String)

load

public void load()
Load the parsers from the main jar and register them in our list. This method needs to be called before calling one of the getParser*(), listParsers() or getHelpForMode() methods.

We first open the main jar, get the entries in the package de.bsd.x2svg.parsers and then look at those entries if they implement InputParser. After this, we pass the the matching class to the addToParserList() method, which does the magic for us.


checkClassIsParser

private void checkClassIsParser(java.lang.String classname)
                         throws java.lang.ClassNotFoundException
Checks if the class passed in is a valid InputParser. This is done by looking at the interfaces of that class.

Parameters:
classname - Name of a class in form of a path
Throws:
java.lang.ClassNotFoundException - If the passed class could not be found in the classpath

addToParserList

private void addToParserList(java.lang.Class<InputParser> clazz)
Enter the passed class into the lists of parsers

Parameters:
clazz - The main class of an InputParser

listParsers

public java.lang.String listParsers()
List all parsers that we know about

Returns:
A string with a list of parsers separated by newlines
Throws:
java.lang.IllegalStateException - If load() has not yet been called

getHelpForMode

public java.lang.String getHelpForMode(java.lang.String mode)
Return the parser specific help for the passed mode. If mode is '*', then help is shown for all parsers.

Parameters:
mode - a valid mode or '*'
Returns:
the parser specific help
Throws:
java.lang.IllegalStateException - If load() has not yet been called

main

public static void main(java.lang.String[] args)
For debugging the ParserLoader separately

Parameters:
args - No command line options needed.


x2svg version 1.2.1