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);
      }
    }
示例#2
0
 protected void write(Property property, Writer stream, ValueFactory<String> strings)
     throws IOException {
   String name = strings.create(property.getName());
   stream.append(encoder.encode(name));
   if (property.isEmpty()) return;
   stream.append(" (");
   PropertyType type = PropertyType.discoverType(property.getFirstValue());
   stream.append(type.getName().toLowerCase());
   stream.append(") ");
   if (property.isMultiple()) {
     stream.append('[');
   }
   boolean first = true;
   boolean quote = type == PropertyType.STRING;
   for (Object value : property) {
     if (first) first = false;
     else stream.append(", ");
     String str = null;
     if (value instanceof Binary) {
       str = StringUtil.getHexString(((Binary) value).getBytes());
     } else {
       str = strings.create(value);
     }
     if (quote) {
       stream.append('"');
       stream.append(quoter.encode(str));
       stream.append('"');
     } else {
       stream.append(str);
     }
   }
   if (property.isMultiple()) {
     stream.append(']');
   }
   stream.append('\n');
   stream.flush();
 }
示例#3
0
  protected Property parse(String line, ValueFactories factories, PropertyFactory propFactory) {
    if (line.length() == 0) return null; // blank line
    char firstChar = line.charAt(0);
    if (firstChar == '#') return null; // comment line
    if (firstChar == ' ') return null; // ignore line
    Matcher matcher = PROPERTY_PATTERN.matcher(line);
    if (!matcher.matches()) {
      // It should be an empty multi-valued property, and the line consists only of the name ...
      Name name = factories.getNameFactory().create(decoder.decode(line));
      return propFactory.create(name);
    }

    String nameString = decoder.decode(matcher.group(1));
    String typeString = matcher.group(2);
    String valuesString = matcher.group(4);

    Name name = factories.getNameFactory().create(nameString);
    PropertyType type = PropertyType.valueFor(typeString);

    Pattern pattern = VALUE_PATTERN;
    ValueFactory<?> valueFactory = factories.getValueFactory(type);
    boolean binary = false;
    boolean decode = false;
    if (type == PropertyType.STRING) {
      // Parse the double-quoted value(s) ...
      pattern = STRING_VALUE_PATTERN;
      decode = true;
    } else if (type == PropertyType.BINARY) {
      binary = true;
    }
    Matcher valuesMatcher = pattern.matcher(valuesString);
    List<Object> values = new ArrayList<Object>();
    while (valuesMatcher.find()) {
      String valueString = valuesMatcher.group(1);
      if (binary) {
        // The value is a hexadecimal-encoded byte array ...
        byte[] binaryValue = StringUtil.fromHexString(valueString);
        Object value = valueFactory.create(binaryValue);
        values.add(value);
      } else {
        if (decode) valueString = quoter.decode(valueString);
        Object value = valueFactory.create(valueString);
        values.add(value);
      }
    }
    if (values.isEmpty()) return null;
    return propFactory.create(name, type, values);
  }