protected void setProperty(Node node, Property property, boolean isMultiValued)
        throws RepositoryException {
      Name name = property.getName();
      if (name.equals(JcrLexicon.PRIMARY_TYPE)) return;
      if (name.equals(ModeShapeIntLexicon.NODE_DEFINITON)) return;
      if (name.equals(ModeShapeIntLexicon.MULTI_VALUED_PROPERTIES)) return;
      if (name.equals(JcrLexicon.MIXIN_TYPES)) {
        for (Object mixinVvalue : property.getValuesAsArray()) {
          String mixinTypeName = stringFor(mixinVvalue);
          node.addMixin(mixinTypeName);
        }
        return;
      }

      // Otherwise, just set the normal property. First determine the expected type ...
      String propertyName = stringFor(name);
      if (isMultiValued) {
        Value[] values = new Value[property.size()];
        int index = 0;
        PropertyType propertyType = null;
        for (Object value : property) {
          if (value == null) continue;
          if (propertyType == null) propertyType = PropertyType.discoverType(value);
          values[index] = convertToJcrValue(propertyType, value);
          ++index;
        }
        node.setProperty(propertyName, values);
      } else {
        Object firstValue = property.getFirstValue();
        PropertyType propertyType = PropertyType.discoverType(firstValue);
        Value value = convertToJcrValue(propertyType, firstValue);
        node.setProperty(propertyName, value);
      }
    }
Esempio n. 2
0
  protected void assertMatchesStore(AbstractJcrNode jcrNode) throws RepositoryException {
    // Find the corresponding session node ...
    Node<JcrNodePayload, JcrPropertyPayload> nodeInfo =
        cache.findNode(jcrNode.nodeId, jcrNode.path());
    // And the graph node ...
    org.modeshape.graph.Node dnaNode = store.getNodeAt(jcrNode.location);

    assertThat(nodeInfo.getLocation(), is(dnaNode.getLocation()));
    Set<Name> propertyNames = nodeInfo.getPropertyNames();
    for (Name propertyName : propertyNames) {
      PropertyInfo<JcrPropertyPayload> info = nodeInfo.getProperty(propertyName);
      assertThat(info.getName(), is(propertyName));
      assertThat(info.getProperty().getName(), is(propertyName));
      Property actual = dnaNode.getProperty(propertyName);
      if (actual != null) {
        assertThat(info.getProperty().size(), is(actual.size()));
        assertThat(info.getProperty().getValuesAsArray(), is(actual.getValuesAsArray()));
      } else {
        if (propertyName.equals(JcrLexicon.UUID)) {
          // check for a ModeShape UUID property ...
          actual = dnaNode.getProperty(ModeShapeLexicon.UUID);
          if (actual != null) {
            assertThat(info.getProperty().size(), is(actual.size()));
            assertThat(info.getProperty().getValuesAsArray(), is(actual.getValuesAsArray()));
          } else {
            fail("missing property \"" + propertyName + "\" on " + dnaNode);
          }
        } else if (propertyName.equals(JcrLexicon.PRIMARY_TYPE)) {
          // This is okay
        } else if (propertyName.equals(ModeShapeIntLexicon.MULTI_VALUED_PROPERTIES)) {
          // This is okay
        } else {
          fail("missing property \"" + propertyName + "\" on " + dnaNode);
        }
      }
    }
  }
Esempio n. 3
0
  /**
   * {@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());
      }
    }
  }
Esempio n. 4
0
 protected final String readable(Name name) {
   return name.getString(context.getNamespaceRegistry());
 }