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