/**
   * Fires the appropriate SAX events on the content handler to build the XML elements for the
   * property.
   *
   * @param property the property to be exported
   * @param contentHandler the SAX content handler for which SAX events will be invoked as the XML
   *     document is created.
   * @param skipBinary if <code>true</code>, indicates that binary properties should not be exported
   * @throws SAXException if an exception occurs during generation of the XML document
   * @throws RepositoryException if an exception occurs accessing the content repository
   */
  private void emitProperty(Property property, ContentHandler contentHandler, boolean skipBinary)
      throws RepositoryException, SAXException {
    assert property instanceof AbstractJcrProperty
        : "Illegal attempt to use " + getClass().getName() + " on non-ModeShape property";

    AbstractJcrProperty prop = (AbstractJcrProperty) property;

    // first set the property sv:name attribute
    AttributesImpl propAtts = new AttributesImpl();
    propAtts.addAttribute(
        JcrSvLexicon.NAME.getNamespaceUri(),
        JcrSvLexicon.NAME.getLocalName(),
        getPrefixedName(JcrSvLexicon.NAME),
        PropertyType.nameFromValue(PropertyType.STRING),
        prop.getName());

    // and it's sv:type attribute
    propAtts.addAttribute(
        JcrSvLexicon.TYPE.getNamespaceUri(),
        JcrSvLexicon.TYPE.getLocalName(),
        getPrefixedName(JcrSvLexicon.TYPE),
        PropertyType.nameFromValue(PropertyType.STRING),
        PropertyType.nameFromValue(prop.getType()));

    // and it's sv:multiple attribute
    if (prop.isMultiple()) {
      propAtts.addAttribute(
          JcrSvLexicon.TYPE.getNamespaceUri(),
          JcrSvLexicon.TYPE.getLocalName(),
          getPrefixedName(JcrSvLexicon.MULTIPLE),
          PropertyType.nameFromValue(PropertyType.BOOLEAN),
          Boolean.TRUE.toString());
    }

    // output the sv:property element
    startElement(contentHandler, JcrSvLexicon.PROPERTY, propAtts);

    boolean modified = property.isModified();

    // then output a sv:value element for each of its values
    if (prop instanceof JcrMultiValueProperty) {
      Value[] values = prop.getValues();
      for (int i = 0; i < values.length; i++) {

        emitValue(values[i], contentHandler, property.getType(), skipBinary, modified);
      }
    } else {
      emitValue(property.getValue(), contentHandler, property.getType(), skipBinary, modified);
    }

    // end the sv:property element
    endElement(contentHandler, JcrSvLexicon.PROPERTY);
  }
  /**
   * @param property
   * @param contentHandler the SAX content handler for which SAX events will be invoked as the XML
   *     document is created.
   * @param skipBinary if <code>true</code>, indicates that binary properties should not be exported
   * @throws SAXException if an exception occurs during generation of the XML document
   * @throws RepositoryException if an exception occurs accessing the content repository
   */
  private void exportProperty(Property property, ContentHandler contentHandler, boolean skipBinary)
      throws RepositoryException, SAXException {
    assert property instanceof AbstractJcrProperty
        : "Illegal attempt to use " + getClass().getName() + " on non-ModeShape property";

    AbstractJcrProperty prop = (AbstractJcrProperty) property;

    Name propertyName = prop.name();
    if (SPECIAL_PROPERTY_NAMES.contains(propertyName)) {
      return;
    }

    emitProperty(property, contentHandler, skipBinary);
  }
Beispiel #3
0
 protected void assertProperty(
     AbstractJcrProperty property,
     javax.jcr.Node node,
     String name,
     int propertyType,
     Object... values)
     throws Exception {
   assertThat(property.getName(), is(name));
   assertThat(property.getType(), is(propertyType));
   assertThat(property.getParent(), is(node));
   if (values.length > 1) {
     int i = 0;
     for (Value actual : property.getValues()) {
       String actualString = actual.getString();
       String expectedString = context.getValueFactories().getStringFactory().create(values[i]);
       assertThat(actualString, is(expectedString));
       assertCanObtainValue(actual, propertyType);
       ++i;
     }
     // Getting the single value should result in an error ...
     try {
       property.getValue();
       fail("Should not be able to call Property.getValue() on multi-valued properties");
     } catch (ValueFormatException e) {
       // expected ...
     }
   } else {
     String actualString = property.getValue().getString();
     String expectedString = context.getValueFactories().getStringFactory().create(values[0]);
     assertThat(actualString, is(expectedString));
     assertThat(actualString, is(property.getString()));
     assertCanObtainValue(property.getValue(), propertyType);
     // Getting the multiple values should result in an error ...
     try {
       property.getValues();
       fail("Should not be able to call Property.getValues() on single-valued properties");
     } catch (ValueFormatException e) {
       // expected ...
     }
     // Check resolving the reference ...
     if (propertyType == PropertyType.REFERENCE) {
       javax.jcr.Node referenced = property.getNode();
       assertThat(referenced, is(notNullValue()));
     }
   }
 }
 @Test(expected = ItemNotFoundException.class)
 public void shouldFailToReturnAncestorWhenDepthIsGreaterThanNodeDepth() throws Exception {
   altimaModel.getAncestor(40);
 }
 @Test
 public void shouldIndicateIsNotNode() {
   assertThat(altimaModel.isNode(), is(false));
 }
 @Test
 public void shouldReturnAncestorAtLevelOneForAncestorOfDepthOne() throws Exception {
   assertThat(altimaModel.getAncestor(1), is((Item) cars));
 }
 @Test
 public void shouldReturnSelfForAncestorOfDepthEqualToDepthOfNode() throws Exception {
   assertThat(altimaModel.getAncestor(altimaModel.getDepth()), is((Item) altimaModel));
   assertThat(altimaModel.getAncestor(altimaModel.getDepth() - 1), is((Item) altima));
 }
 @Test(expected = ItemNotFoundException.class)
 public void shouldNotAllowNegativeAncestorDepth() throws Exception {
   altimaModel.getAncestor(-1);
 }
 @Test
 public void shouldReturnRootForAncestorOfDepthZero() throws Exception {
   assertThat(altimaModel.getAncestor(0), is((Item) rootNode));
 }
 @Test
 public void shouldAllowVisitation() throws Exception {
   ItemVisitor visitor = Mockito.mock(ItemVisitor.class);
   altimaModel.accept(visitor);
   Mockito.verify(visitor).visit(altimaModel);
 }
 @Test(expected = IllegalArgumentException.class)
 public void shouldNotAllowVisitationIfNoVisitor() throws Exception {
   altimaModel.accept(null);
 }
 @Test
 public void shouldProvideSession() throws Exception {
   assertThat(altimaModel.getSession(), is((Session) jcrSession));
 }
 @Test
 public void shouldProvidePath() throws Exception {
   assertThat(altimaModel.getPath(), is(altima.getPath() + "/vehix:model"));
 }
 @Test
 public void shouldProvideParent() throws Exception {
   assertThat(altimaModel.getParent(), is((Node) altima));
 }
 @Test
 public void shouldProvideName() throws Exception {
   assertThat(altimaModel.getName(), is("vehix:model"));
 }
 @Test
 public void shouldProvideExecutionContext() throws Exception {
   assertThat(altimaModel.context(), is(context));
 }