View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.lang.xpath;
    5
    6   import net.sf.saxon.sxpath.IndependentContext;
    7   import net.sourceforge.pmd.lang.Language;
    8   import net.sourceforge.pmd.lang.LanguageVersion;
    9   import net.sourceforge.pmd.lang.LanguageVersionHandler;
    10  import net.sourceforge.pmd.lang.java.xpath.GetCommentOnFunction;
    11  import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction;
    12
    13  /**
    14   * This class serves as the means to perform XPath related static initialization.
    15   * For example, initializing custom Jaxen Functions.
    16   * Initialization should be performed before any XPath related operations are
    17   * performed.
    18   */
    19  public class Initializer {
    20
    21      /**
    22       * Perform all initialization.
    23       */
    24      public static void initialize() {
    25  	// noop as initialization is done in static block below
    26      }
    27
    28      /**
    29       * Perform all initialization.
    30       */
    31      public static void initialize(IndependentContext context) {
    32  		context.declareNamespace("pmd", "java:" + PMDFunctions.class.getName());
    33  		for (Language language : Language.values()) {
    34  		    for (LanguageVersion languageVersion : language.getVersions()) {
    35  			LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
    36  			if (languageVersionHandler != null) {
    37  			    languageVersionHandler.getXPathHandler().initialize(context);
    38  			}
    39  		    }
    40  		}
    41      }
    42
    43      static {
    44  		initializeGlobal();
    45  		initializeLanguages();
    46      }
    47
    48      private static void initializeGlobal() {
    49      	GetCommentOnFunction.registerSelfInSimpleContext();
    50      	MatchesFunction.registerSelfInSimpleContext();
    51      	TypeOfFunction.registerSelfInSimpleContext();
    52      }
    53
    54      private static void initializeLanguages() {
    55  		for (Language language : Language.values()) {
    56  		    for (LanguageVersion languageVersion : language.getVersions()) {
    57  			LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
    58  			if (languageVersionHandler != null) {
    59  			    languageVersionHandler.getXPathHandler().initialize();
    60  			}
    61  		    }
    62  		}
    63      }
    64  }