/** * {@inheritDoc} */ public String getDefaultNamespaceUri() { return this.getNamespaceForPrefix(""); }
/** * {@inheritDoc} * * @see java.lang.Object#toString() */ @Override public String toString() { List<Namespace> namespaces = new ArrayList<Namespace>(getNamespaces()); Collections.sort(namespaces); return namespaces.toString(); }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.NamespaceRegistry#register(java.lang.Iterable) */ public void register( Iterable<Namespace> namespaces ) { Map<String, String> urisByPrefix = new HashMap<String, String>(); for (Namespace namespace : namespaces) { urisByPrefix.put(namespace.getPrefix(), namespace.getNamespaceUri()); } register(urisByPrefix); }
public GraphNamespaceRegistry( Graph store, Path parentOfNamespaceNodes, Name uriPropertyName, Name generatedPropertyName, Property... additionalProperties ) { this.cache = new SimpleNamespaceRegistry(); this.store = store; this.parentOfNamespaceNodes = parentOfNamespaceNodes; this.uriPropertyName = uriPropertyName != null ? uriPropertyName : DEFAULT_URI_PROPERTY_NAME; this.generatedPropertyName = generatedPropertyName != null ? generatedPropertyName : DEFAULT_GENERATED_PROPERTY_NAME; List<Property> properties = Collections.emptyList(); if (additionalProperties != null && additionalProperties.length != 0) { properties = new ArrayList<Property>(additionalProperties.length); Set<Name> propertyNames = new HashSet<Name>(); for (Property property : additionalProperties) { if (!propertyNames.contains(property.getName())) properties.add(property); } } this.namespaceProperties = Collections.unmodifiableList(properties); createNamespaceParentIfNeeded(); initializeCacheFromStore(cache); }
@Test public void shouldInitializeFromPersistedContent() { // Add some namespaces ... namespaceRegistry.register(validPrefix1, validNamespaceUri1); namespaceRegistry.register(validPrefix2, validNamespaceUri2); assertThat(namespaceRegistry.getNamespaceForPrefix(validPrefix1), is(validNamespaceUri1)); assertThat(namespaceRegistry.getNamespaceForPrefix(validPrefix2), is(validNamespaceUri2)); // Now set up the graph-based namespace registry ... GraphNamespaceRegistry registry2 = new GraphNamespaceRegistry(graph, pathToParentOfNamespaceNodes, uriPropertyName, generatedPropertyName, additionalNamespaceProperties); // All namespaces should match ... Set<NamespaceRegistry.Namespace> all = namespaceRegistry.getNamespaces(); Set<NamespaceRegistry.Namespace> all2 = registry2.getNamespaces(); assertThat(all, is(all2)); assertThat(registry2.getNamespaceForPrefix(validPrefix1), is(validNamespaceUri1)); assertThat(registry2.getNamespaceForPrefix(validPrefix2), is(validNamespaceUri2)); registry2.refresh(); all2 = registry2.getNamespaces(); assertThat(all, is(all2)); assertThat(registry2.getNamespaceForPrefix(validPrefix1), is(validNamespaceUri1)); assertThat(registry2.getNamespaceForPrefix(validPrefix2), is(validNamespaceUri2)); }
@Test public void shouldRefreshFromPersistedContent() { // Add some namespaces ... namespaceRegistry.register(validPrefix1, validNamespaceUri1); namespaceRegistry.register(validPrefix2, validNamespaceUri2); assertThat(namespaceRegistry.getNamespaceForPrefix(validPrefix1), is(validNamespaceUri1)); assertThat(namespaceRegistry.getNamespaceForPrefix(validPrefix2), is(validNamespaceUri2)); // Get the namespaces, refresh, then get all the namespaces again Set<NamespaceRegistry.Namespace> allBefore = namespaceRegistry.getNamespaces(); namespaceRegistry.refresh(); Set<NamespaceRegistry.Namespace> allAfter = namespaceRegistry.getNamespaces(); assertThat(allBefore, is(allAfter)); for (NamespaceRegistry.Namespace namespace : allBefore) { assertThat(namespaceRegistry.getNamespaceForPrefix(namespace.getPrefix()), is(namespace.getNamespaceUri())); assertThat(namespaceRegistry.getPrefixForNamespaceUri(namespace.getNamespaceUri(), false), is(namespace.getPrefix())); } }
for (Location nsLocation : nsGraph.getRoot().getChildren()) { Node ns = nsGraph.getNode(nsLocation); String prefix = getPrefixFor(nsLocation.getPath()); String uri = stringFactory.create(ns.getProperty(uriPropertyName).getFirstValue()); if (prefix != null && uri != null) { boolean generated = generatedProperty == null ? isGeneratedPrefix(prefix) : booleanFactory.create(generatedProperty.getFirstValue()); if (generated) { if (generatedPrefixes == null) generatedPrefixes = new HashSet<String>(); String prefix = generatePrefix(generatedPrefixes); Path pathToNamespaceNode = pathFactory.create(parentOfNamespaceNodes, prefix); Property uriProperty = store.getContext().getPropertyFactory().create(uriPropertyName, namespaceUri); return getPrefixFor(pathToNamespaceNode);
protected String generatePrefix( Set<String> existingGeneratedPrefixes ) { int maxCounter = 0; if (existingGeneratedPrefixes != null) { for (String prefix : existingGeneratedPrefixes) { if (prefix == null) continue; Matcher matcher = GENERATED_PREFIX_PATTERN.matcher(prefix); if (matcher.matches()) { // Get the counter ... int value = Integer.parseInt(matcher.group(1)); maxCounter = Math.max(maxCounter, value); } } } return getPrefixFor(GENERATED_PREFIX, ++maxCounter); }
/** * Register a set of namespaces. * * @param namespaceUrisByPrefix the map of new namespace URIs by their prefix */ public void register( Map<String, String> namespaceUrisByPrefix ) { if (namespaceUrisByPrefix == null || namespaceUrisByPrefix.isEmpty()) return; doRegister(namespaceUrisByPrefix); for (Map.Entry<String, String> entry : namespaceUrisByPrefix.entrySet()) { String prefix = entry.getKey().trim(); String uri = entry.getValue().trim(); if (prefix.length() == 0) continue; this.cache.register(prefix, uri); } }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.NamespaceRegistry#unregister(java.lang.String) */ public boolean unregister( String namespaceUri ) { CheckArg.isNotNull(namespaceUri, "namespaceUri"); namespaceUri = namespaceUri.trim(); // Remove it from the cache ... boolean found = this.cache.unregister(namespaceUri); // Then from the source ... return doUnregister(namespaceUri) || found; }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.basic.AbstractNamespaceRegistryTest#setUp() */ @Override public void setUp() { super.setUp(); // Set up the context and register any namespaces that we'll be using to manage the namespaces ... context = new ExecutionContext(); NameFactory nameFactory = context.getValueFactories().getNameFactory(); PropertyFactory propertyFactory = context.getPropertyFactory(); context.getNamespaceRegistry().register("nsx", "http://www.example.com/namespaces"); context.getNamespaceRegistry().register("other", "http://www.example.com/other"); uriPropertyName = context.getValueFactories().getNameFactory().create("nsx:uri"); generatedPropertyName = context.getValueFactories().getNameFactory().create("nsx:gen"); additionalNamespaceProperties = new Property[] { propertyFactory.create(nameFactory.create("nsx:something"), "Some value"), propertyFactory.create(nameFactory.create("nsx:something2"), "Some value2"), propertyFactory.create(nameFactory.create("other:something2"), "Some other value2")}; // Set up the repository that we'll be using ... source = new InMemoryRepositorySource(); source.setName("namespace repository"); graph = Graph.create(source, context); // Create the path to the where the namespaces will be managed ... pathToParentOfNamespaceNodes = graph.create("/a").and().create("/a/b").and().createAt("/a/b/c").getLocation().getPath(); // Now set up the graph-based namespace registry ... namespaceRegistry = new GraphNamespaceRegistry(graph, pathToParentOfNamespaceNodes, uriPropertyName, generatedPropertyName, additionalNamespaceProperties); }
protected String getPrefixFor( Path path ) { Path.Segment lastSegment = path.getLastSegment(); String localName = lastSegment.getName().getLocalName(); int index = lastSegment.getIndex(); return getPrefixFor(localName, index); }
/** * {@inheritDoc} */ public String register( String prefix, String namespaceUri ) { CheckArg.isNotNull(namespaceUri, "namespaceUri"); namespaceUri = namespaceUri.trim(); // Register it in the cache first ... String previousCachedUriForPrefix = this.cache.register(prefix, namespaceUri); if (!namespaceUri.equals(previousCachedUriForPrefix)) { // And register it in the source ... return doRegister(Collections.singletonMap(prefix, namespaceUri)).get(prefix); } return previousCachedUriForPrefix; }
Node ns = nsGraph.getNode(nsLocation); Path path = nsLocation.getPath(); String actualPrefix = getPrefixFor(path); String actualUri = stringFactory.create(ns.getProperty(uriPropertyName).getFirstValue()); if (actualPrefix != null && actualUri != null) {
String prefix = getPrefixFor(nsLocation.getPath()); cache.register(prefix, uri);