protected void assertCanObtainValue(Value value, int expectedType) throws Exception {
   switch (expectedType) {
     case PropertyType.BINARY:
       Binary binary = value.getBinary();
       try {
         InputStream stream = binary.getStream();
         assertThat(stream, is(notNullValue()));
         try {
           stream.read();
         } finally {
           stream.close();
         }
       } finally {
         binary.dispose();
       }
       break;
     case PropertyType.BOOLEAN:
       assertThat(value.getBoolean() || !value.getBoolean(), is(true));
       break;
     case PropertyType.DATE:
       Calendar cal = value.getDate();
       assertThat(cal, is(notNullValue()));
       break;
     case PropertyType.DOUBLE:
       double doubleValue = value.getDouble();
       assertThat(doubleValue < 0.0d || doubleValue >= -1.0d, is(true));
       break;
     case PropertyType.LONG:
       long longValue = value.getLong();
       assertThat(longValue < 0L || longValue >= 0L, is(true));
       break;
     case PropertyType.NAME:
       context.getValueFactories().getNameFactory().create(value.getString());
       break;
     case PropertyType.PATH:
       context.getValueFactories().getPathFactory().create(value.getString());
       break;
     case PropertyType.REFERENCE:
       UUID uuid = context.getValueFactories().getUuidFactory().create(value.getString());
       assertThat(uuid, is(notNullValue()));
       break;
     case PropertyType.STRING:
       value.getString();
       break;
   }
 }
  /**
   * Fires the appropriate SAX events on the content handler to build the XML elements for the
   * value.
   *
   * @param value the value to be exported
   * @param contentHandler the SAX content handler for which SAX events will be invoked as the XML
   *     document is created.
   * @param propertyType the {@link PropertyType} for the given value
   * @param skipBinary if <code>true</code>, indicates that binary properties should not be exported
   * @param isModified true if the property is modified; any modified binary properties will not be
   *     purged
   * @throws SAXException if an exception occurs during generation of the XML document
   * @throws RepositoryException if an exception occurs accessing the content repository
   */
  private void emitValue(
      Value value,
      ContentHandler contentHandler,
      int propertyType,
      boolean skipBinary,
      boolean isModified)
      throws RepositoryException, SAXException {

    if (PropertyType.BINARY == propertyType) {
      startElement(contentHandler, JcrSvLexicon.VALUE, null);

      // Per section 6.5 of the 1.0.1 spec, we need to emit one empty-value tag for each value if
      // the property is
      // multi-valued and skipBinary is true
      if (!skipBinary) {
        byte[] bytes = new byte[BASE_64_BUFFER_SIZE];
        int len;

        Binary binary = value.getBinary();
        try {
          InputStream stream = new Base64.InputStream(binary.getStream(), Base64.ENCODE);
          while (-1 != (len = stream.read(bytes))) {
            contentHandler.characters(new String(bytes, 0, len).toCharArray(), 0, len);
          }
        } catch (IOException ioe) {
          throw new RepositoryException(ioe);
        } finally {
          try {
            binary.dispose();
          } finally {
            if (!isModified) {
              if (binary instanceof org.modeshape.graph.property.Binary) {
                ((org.modeshape.graph.property.Binary) binary).purge();
              }
            }
          }
        }
      }
      endElement(contentHandler, JcrSvLexicon.VALUE);
    } else {
      emitValue(value.getString(), contentHandler);
    }
  }
Exemple #3
0
 private File createFile(Node node) throws RepositoryException {
   if (node.hasProperty("jcr:content/jcr:data")) {
     Property data = node.getProperty("jcr:content/jcr:data");
     Binary binary = data.getBinary();
     OutputStream out = null;
     InputStream in = null;
     try {
       File result = new File(fontsDir, node.getName());
       result.createNewFile();
       result.deleteOnExit();
       in = binary.getStream();
       out = new FileOutputStream(result);
       IOUtils.copy(in, out);
       return result;
     } catch (IOException e) {
       logger.error("Failed exporting FOP font data.", e);
     } finally {
       IOUtils.closeQuietly(in);
       IOUtils.closeQuietly(out);
       binary.dispose();
     }
   }
   return null;
 }