@@ -139,9 +139,10 @@ public String getPrefix(String uri) throws NamespaceException {
139139 "No namespace prefix registered for URI " + uri );
140140 }
141141
142- protected void checkConsistency () throws IllegalStateException {
142+ public boolean checkConsistency () throws IllegalStateException {
143143 NamespaceRegistryModel model = NamespaceRegistryModel .create (namespaces );
144- if (!model .isConsistent ()) {
144+ boolean consistent = model .isConsistent ();
145+ if (!consistent ) {
145146 LOG .warn ("Namespace registry is inconsistent. "
146147 + "Unregistered mapped prefixes: {}. "
147148 + "Unregistered mapped namespaces: {}. "
@@ -152,7 +153,11 @@ protected void checkConsistency() throws IllegalStateException {
152153 model .getRegisteredUnmappedPrefixes (),
153154 model .getRegisteredUnmappedNamespaces ());
154155 }
155- CONSISTENCY_CHECKED = true ;
156+ return consistent ;
157+ }
158+
159+ public NamespaceRegistryModel createNamespaceRegistryModel () {
160+ return NamespaceRegistryModel .create (namespaces );
156161 }
157162
158163 protected static final class NamespaceRegistryModel {
@@ -181,8 +186,8 @@ private NamespaceRegistryModel(
181186 // encoded URIs to prefixes
182187 Map <String , String > namespaceToPrefixMap ) {
183188 // ignore the empty namespace which is not mapped
184- this .registeredPrefixes = registeredPrefixes .stream ().filter (s -> !Objects .isNull (s ) && s .isEmpty ()).collect (Collectors .toSet ());
185- this .registeredNamespacesEncoded = registeredNamespacesEncoded .stream ().filter (s -> !Objects .isNull (s ) && s .isEmpty ()).collect (Collectors .toSet ());
189+ this .registeredPrefixes = registeredPrefixes .stream ().filter (s -> !( Objects .isNull (s ) || s .isEmpty () )).collect (Collectors .toSet ());
190+ this .registeredNamespacesEncoded = registeredNamespacesEncoded .stream ().filter (s -> !( Objects .isNull (s ) || s .isEmpty () )).collect (Collectors .toSet ());
186191 this .prefixToNamespaceMap = prefixToNamespaceMap .entrySet ().stream ().collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
187192 this .namespaceToPrefixMap = namespaceToPrefixMap .entrySet ().stream ().collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
188193 init ();
@@ -215,7 +220,7 @@ static NamespaceRegistryModel create(Tree namespaces) {
215220 Map <String , String > namespaceToPrefixMap = new HashMap <>();
216221 for (PropertyState propertyState : namespaces .getProperties ()) {
217222 String prefix = propertyState .getName ();
218- if (!prefix .equals (NodeTypeConstants .REP_PRIMARY_TYPE )) {
223+ if (!prefix .equals (NodeTypeConstants .JCR_PRIMARYTYPE )) {
219224 prefixToNamespaceMap .put (prefix , propertyState .getValue (STRING ));
220225 }
221226 }
@@ -224,20 +229,21 @@ static NamespaceRegistryModel create(Tree namespaces) {
224229 switch (encodedUri ) {
225230 case REP_PREFIXES :
226231 case REP_URIS :
227- case NodeTypeConstants .REP_PRIMARY_TYPE :
232+ case NodeTypeConstants .JCR_PRIMARYTYPE :
228233 break ;
229234 default :
230235 namespaceToPrefixMap .put (encodedUri , propertyState .getValue (STRING ));
231236 }
232237 }
238+ Iterable <String > uris = nsdata .getProperty (REP_URIS ).getValue (STRINGS );
233239 NamespaceRegistryModel model = new NamespaceRegistryModel (
234240 new HashSet <>(Arrays .asList (IterableUtils .toArray (nsdata .getProperty (REP_PREFIXES ).getValue (STRINGS ), String .class ))),
235- StreamUtils .toStream (nsdata . getProperty ( REP_URIS ). getValue ( STRINGS ) ).map (Namespaces ::encodeUri ).collect (Collectors .toSet ()),
241+ StreamUtils .toStream (uris ).map (Namespaces ::encodeUri ).collect (Collectors .toSet ()),
236242 prefixToNamespaceMap , namespaceToPrefixMap );
237243 return model ;
238244 }
239245
240- NamespaceRegistryModel createFixedModel () {
246+ NamespaceRegistryModel tryRegistryRepair () {
241247 if (consistent ) {
242248 return this ;
243249 }
@@ -247,11 +253,13 @@ NamespaceRegistryModel createFixedModel() {
247253 HashSet <String > fixedRegisteredPrefixes = new HashSet <>();
248254 HashMap <String , String > fixedPrefixToNamespaceMap = new HashMap <>();
249255 for (String prefix : allPrefixes ) {
250- if (!mappedPrefixes .contains (prefix )) {
256+ fixedRegisteredPrefixes .add (prefix );
257+ if (mappedPrefixes .contains (prefix )) {
258+ fixedPrefixToNamespaceMap .put (prefix , prefixToNamespaceMap .get (prefix ));
259+ } else {
251260 for (Map .Entry <String , String > entry : namespaceToPrefixMap .entrySet ()) {
252261 if (entry .getValue ().equals (prefix )) {
253262 fixedPrefixToNamespaceMap .put (prefix , Text .unescapeIllegalJcrChars (entry .getKey ()));
254- fixedRegisteredPrefixes .add (prefix );
255263 break ;
256264 }
257265 }
@@ -260,11 +268,13 @@ NamespaceRegistryModel createFixedModel() {
260268 HashSet <String > fixedRegisteredNamespacesEncoded = new HashSet <>();
261269 HashMap <String , String > fixedNamespaceToPrefixMap = new HashMap <>();
262270 for (String encodedNamespace : allNamespacesEncoded ) {
263- if (!mappedNamespaces .contains (encodedNamespace )) {
271+ fixedRegisteredNamespacesEncoded .add (encodedNamespace );
272+ if (mappedNamespaces .contains (encodedNamespace )) {
273+ fixedNamespaceToPrefixMap .put (encodedNamespace , namespaceToPrefixMap .get (encodedNamespace ));
274+ } else {
264275 for (Map .Entry <String , String > entry : prefixToNamespaceMap .entrySet ()) {
265276 if (Namespaces .encodeUri (entry .getValue ()).equals (encodedNamespace )) {
266277 fixedNamespaceToPrefixMap .put (encodedNamespace , entry .getKey ());
267- fixedRegisteredNamespacesEncoded .add (encodedNamespace );
268278 break ;
269279 }
270280 }
0 commit comments