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.LanguageRegistry;
9   import net.sourceforge.pmd.lang.LanguageVersion;
10  import net.sourceforge.pmd.lang.LanguageVersionHandler;
11  
12  /**
13   * This class serves as the means to perform XPath related static initialization.
14   * For example, initializing custom Jaxen Functions.
15   * Initialization should be performed before any XPath related operations are
16   * performed.
17   */
18  public class Initializer {
19  
20      /**
21       * Perform all initialization.
22       */
23      public static void initialize() {
24  	// noop as initialization is done in static block below
25      }
26  
27      /**
28       * Perform all initialization.
29       */
30      public static void initialize(IndependentContext context) {
31  		context.declareNamespace("pmd", "java:" + PMDFunctions.class.getName());
32  		for (Language language : LanguageRegistry.getLanguages()) {
33  		    for (LanguageVersion languageVersion : language.getVersions()) {
34  			LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
35  			if (languageVersionHandler != null) {
36  			    languageVersionHandler.getXPathHandler().initialize(context);
37  			}
38  		    }
39  		}
40      }
41  
42      static {
43  		initializeGlobal();
44  		initializeLanguages();
45      }
46  
47      private static void initializeGlobal() {
48      	MatchesFunction.registerSelfInSimpleContext();
49      }
50  
51      private static void initializeLanguages() {
52  		for (Language language : LanguageRegistry.getLanguages()) {
53  		    for (LanguageVersion languageVersion : language.getVersions()) {
54  			LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
55  			if (languageVersionHandler != null) {
56  			    languageVersionHandler.getXPathHandler().initialize();
57  			}
58  		    }
59  		}
60      }
61  }