1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.helger.schematron.pure.binding;
18
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import javax.annotation.concurrent.ThreadSafe;
22
23 import com.helger.commons.ValueEnforcer;
24 import com.helger.commons.annotation.Nonempty;
25 import com.helger.commons.annotation.PresentForCodeCoverage;
26 import com.helger.commons.annotation.ReturnsMutableCopy;
27 import com.helger.commons.collection.ext.CommonsHashMap;
28 import com.helger.commons.collection.ext.ICommonsMap;
29 import com.helger.commons.concurrent.SimpleReadWriteLock;
30 import com.helger.commons.exception.InitializationException;
31 import com.helger.schematron.pure.binding.xpath.PSXPathQueryBinding;
32
33
34
35
36
37
38
39 @ThreadSafe
40 public final class PSQueryBindingRegistry
41 {
42
43
44
45 public static final String QUERY_BINDING_XSLT = "xslt";
46
47
48
49
50 public static final String QUERY_BINDING_XSLT2 = "xslt2";
51
52
53
54
55 public static final IPSQueryBinding DEFAULT_QUERY_BINDING = PSXPathQueryBinding.getInstance ();
56
57 private static final SimpleReadWriteLock s_aRWLock = new SimpleReadWriteLock ();
58 private static final ICommonsMap <String, IPSQueryBinding> s_aMap = new CommonsHashMap <> ();
59
60 static
61 {
62 try
63 {
64 registerQueryBinding (QUERY_BINDING_XSLT, DEFAULT_QUERY_BINDING);
65 registerQueryBinding (QUERY_BINDING_XSLT2, DEFAULT_QUERY_BINDING);
66 }
67 catch (final SchematronBindException ex)
68 {
69 throw new InitializationException (ex);
70 }
71 }
72
73 @PresentForCodeCoverage
74 private static final PSQueryBindingRegistry s_aInstance = new PSQueryBindingRegistry ();
75
76 private PSQueryBindingRegistry ()
77 {}
78
79 public static void registerQueryBinding (@Nonnull @Nonempty final String sName,
80 @Nonnull final IPSQueryBinding aQueryBinding) throws SchematronBindException
81 {
82 ValueEnforcer.notEmpty (sName, "Name");
83 ValueEnforcer.notNull (aQueryBinding, "QueryBinding");
84
85 s_aRWLock.writeLockedThrowing ( () -> {
86 if (s_aMap.containsKey (sName))
87 throw new SchematronBindException ("A queryBinding with the name '" + sName + "' is already registered!");
88 s_aMap.put (sName, aQueryBinding);
89 });
90 }
91
92
93
94
95
96
97
98
99
100
101 @Nullable
102 public static IPSQueryBinding getQueryBindingOfName (@Nullable final String sName)
103 {
104 if (sName == null)
105 return DEFAULT_QUERY_BINDING;
106
107 return s_aRWLock.readLocked ( () -> s_aMap.get (sName));
108 }
109
110
111
112
113
114
115
116
117
118
119 @Nonnull
120 public static IPSQueryBinding getQueryBindingOfNameOrThrow (@Nullable final String sName) throws SchematronBindException
121 {
122 final IPSQueryBinding aQB = getQueryBindingOfName (sName);
123 if (aQB == null)
124 throw new SchematronBindException ("No query binding implementation present for query binding '" + sName + "'");
125 return aQB;
126 }
127
128
129
130
131
132 @Nonnull
133 @ReturnsMutableCopy
134 public static ICommonsMap <String, IPSQueryBinding> getAllRegisteredQueryBindings ()
135 {
136 return s_aRWLock.readLocked ( () -> s_aMap.getClone ());
137 }
138 }