protected void registerNewNamespaces(Set<Namespace> namespacesBefore) { Set<Namespace> namespacesAfter = batch.getGraph().getContext().getNamespaceRegistry().getNamespaces(); Set<Namespace> newNamespaces = new HashSet<Namespace>(namespacesAfter); newNamespaces.removeAll(namespacesBefore); for (Namespace namespace : newNamespaces) { registerNamespace(namespace.getPrefix(), namespace.getNamespaceUri()); } }
/** * {@inheritDoc} * * @see org.modeshape.graph.sequencer.StreamSequencer#sequence(java.io.InputStream, * org.modeshape.graph.sequencer.SequencerOutput, * org.modeshape.graph.sequencer.StreamSequencerContext) */ @Override public void sequence(InputStream stream, SequencerOutput output, StreamSequencerContext context) { // Figure out the name of the model ... String originalFilePath = null; Name modelName = null; if (vdbModel != null) { Path pathToOriginalVdb = context.getInputPath(); originalFilePath = context.getValueFactories().getStringFactory().create(pathToOriginalVdb); String pathInVdb = vdbModel.getPathInVdb(); String modelFileName = pathInVdb; int index = modelFileName.lastIndexOf('/') + 1; if (index != -1 && index < modelFileName.length()) { modelFileName = modelFileName.substring(index); } modelName = context.getValueFactories().getNameFactory().create(modelFileName); } else { Path pathToModelFile = context.getInputPath(); if (pathToModelFile != null && !pathToModelFile.isRoot()) { if (pathToModelFile.getLastSegment().getName().equals(JcrLexicon.CONTENT)) pathToModelFile = pathToModelFile.getParent(); if (!pathToModelFile.isRoot()) modelName = pathToModelFile.getLastSegment().getName(); } originalFilePath = context.getValueFactories().getStringFactory().create(pathToModelFile); } if (modelName == null) { modelName = XmiLexicon.MODEL; } // Remove the ".xmi" extension String modelNameWithoutExtension = modelName.getLocalName().replaceAll("\\.xmi$", ""); modelName = context .getValueFactories() .getNameFactory() .create(modelName.getNamespaceUri(), modelNameWithoutExtension); // Use a local namespace registry so that we know which namespaces were used ... NamespaceRegistry registry = context.getNamespaceRegistry(); LocalNamespaceRegistry localRegistry = new LocalNamespaceRegistry(registry); context = context.with(localRegistry); Graph graph = Graph.create(context); try { // Load the input into the transient graph ... HashingInputStream hashingStream = SecureHash.createHashingStream(Algorithm.SHA_1, stream); graph.importXmlFrom(hashingStream).usingAttributeForName("name").into("/"); hashingStream.close(); String sha1 = hashingStream.getHashAsHexString(); // Now read the graph ... Subgraph subgraph = graph.getSubgraphOfDepth(100).at("/xmi:XMI"); // Register any namespaces that were used, but use the desired case (not what's used in XMI) // ... XmiModelReader reader = new XmiModelReader( parentPath, modelName, originalFilePath, subgraph, true, useXmiUuidsAsJcrUuids, vdbModel); if (resolver != null) reader.setResolver(resolver); if (sha1 != null) reader.setSha1Hash(sha1); for (Namespace namespace : localRegistry.getLocalNamespaces()) { String uri = namespace.getNamespaceUri(); if (!registry.isRegisteredNamespaceUri(uri)) { String prefix = reader.namespacePrefix(namespace.getPrefix()); registry.register(prefix, uri); // And re-register so that the sequencing context uses the updated prefixes ... localRegistry.register(prefix, uri); } } // Now process the input graph and output the desired format ... reader.write(output); } catch (RuntimeException e) { throw e; } catch (Throwable e) { context .getProblems() .addError(e, TeiidI18n.errorSequencingModelContent, e.getLocalizedMessage()); } finally { try { if (stream != null) stream.close(); } catch (Throwable e) { context .getProblems() .addError(e, TeiidI18n.errorSequencingModelContent, e.getLocalizedMessage()); } } }