Last active
April 12, 2018 18:39
-
-
Save ahmed-musallam/3fa8d1fe98bc34206b12597e09a60e7d to your computer and use it in GitHub Desktop.
An AEM groovy console script to find all unregistered namespace prefixes in JCR subtree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def BASE_PATH = '/content/test' // CHANGE the path to search in | |
// BE CAREFUL! this may have performance implecations on large trees | |
// start with a deep tree and work your way up. | |
/* | |
* Recurses over all nodes in the subtree "basePath" | |
* and checks every property of every node for prefixes that are not registered | |
* @returns an array or unregistered prefixes | |
*/ | |
def findUnRegisteredNamespaces(basePath){ | |
def reg = session.getWorkspace().getNamespaceRegistry(); | |
def prefixes = reg.getPrefixes() | |
def unregistered = []; // The list of unregistered prefixes | |
getNode(basePath).recurse{ | |
def properties = it.getProperties(); | |
while(properties.hasNext()){ | |
def propName = properties.nextProperty().getName(); | |
if(propName.indexOf(':') >= 0 ){ // the prop name has ":" | |
def propPrefix = propName.split(':')[0]; | |
if(!prefixes.contains(propPrefix)){ // registered prefixes do not contain property prefix | |
unregistered.add(propPrefix); // add unregistered prefix to the list | |
} | |
} | |
} | |
} | |
return unregistered; | |
} | |
def unregisteredPrefixes = findUnRegisteredNamespaces(BASE_PATH) | |
// println unregisteredPrefixes // uncomment this to print as string array | |
unregisteredPrefixes = unregisteredPrefixes.collect{prefix -> [prefix]} | |
// this will print the result as a table | |
table { | |
columns('Unregistered Prefixes') | |
rows(unregisteredPrefixes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment