use of javax.jcr.Property in project jackrabbit by apache.
the class NtFilePredicate method evaluate.
/**
* @return <code>true</code> if the item is a nt:file or nt:resource property
* @see org.apache.jackrabbit.commons.predicate.Predicate#evaluate(java.lang.Object)
*/
public boolean evaluate(Object item) {
if (item instanceof Item) {
if (!((Item) item).isNode()) {
try {
Property prop = (Property) item;
String dnt = prop.getDefinition().getDeclaringNodeType().getName();
// exclude all nt:file props
if (dnt.equals(NT_FILE) || dnt.equals(NT_HIERARCHYNODE)) {
return true;
}
if (ignoreEncoding && prop.getName().equals(JCR_ENCODING)) {
return false;
}
if (ignoreMimeType && prop.getName().equals(JCR_MIMETYPE)) {
return false;
}
// exclude nt:resource props, if parent is 'jcr:content'
if (prop.getParent().getName().equals(JCR_CONTENT)) {
if (dnt.equals(NT_RESOURCE)) {
return true;
}
// exclude primary type if nt:resource
/*
if (prop.getName().equals(JCR_PRIMARY_TYPE)
&& prop.getValue().getString().equals(NT_RESOURCE)) {
return true;
}
*/
}
} catch (RepositoryException re) {
return false;
}
}
}
return false;
}
use of javax.jcr.Property in project jackrabbit by apache.
the class OperandEvaluator method getLengthValues.
/**
* Returns the values of the given value length operand for the given node.
*
* @see #getProperty(PropertyValue, Node)
* @param operand value length operand
* @param node node
* @return values of the operand for the given node
* @throws RepositoryException if the operand can't be evaluated
*/
private Value[] getLengthValues(Length operand, Node n) throws RepositoryException {
Property property = getProperty(operand.getPropertyValue(), n);
if (property == null) {
return new Value[0];
}
if (property.isMultiple()) {
long[] lengths = property.getLengths();
Value[] values = new Value[lengths.length];
for (int i = 0; i < lengths.length; i++) {
values[i] = factory.createValue(lengths[i]);
}
return values;
}
long length = property.getLength();
return new Value[] { factory.createValue(length) };
}
use of javax.jcr.Property in project jackrabbit by apache.
the class JcrUtils method readFile.
/**
* Returns a stream for reading the contents of the file stored at the
* given node. This method works with both on nt:file and nt:resource and
* on any other similar node types, as it only looks for the jcr:data
* property or a jcr:content child node.
* <p>
* The returned stream contains a reference to the underlying
* {@link Binary} value instance that will be disposed when the stream
* is closed. It is the responsibility of the caller to close the stream
* once it is no longer needed.
*
* @since Apache Jackrabbit 2.3
* @param node node to be read
* @return stream for reading the file contents
* @throws RepositoryException if the file can not be accessed
*/
public static InputStream readFile(Node node) throws RepositoryException {
if (node.hasProperty(Property.JCR_DATA)) {
Property data = node.getProperty(Property.JCR_DATA);
final Binary binary = data.getBinary();
return new FilterInputStream(binary.getStream()) {
@Override
public void close() throws IOException {
super.close();
binary.dispose();
}
};
} else if (node.hasNode(Node.JCR_CONTENT)) {
return readFile(node.getNode(Node.JCR_CONTENT));
} else {
throw new RepositoryException("Unable to read file node: " + node.getPath());
}
}
use of javax.jcr.Property in project jackrabbit by apache.
the class DocumentViewExporter method exportNode.
/**
* Exports the given node either as XML characters (if it's an
* <code>xml:text</code> node) or as an XML element with properties
* mapped to XML attributes.
*/
protected void exportNode(String uri, String local, Node node) throws RepositoryException, SAXException {
if (NamespaceHelper.JCR.equals(uri) && "xmltext".equals(local)) {
try {
// assume jcr:xmlcharacters is single-valued
Property property = node.getProperty(helper.getJcrName("jcr:xmlcharacters"));
char[] ch = property.getString().toCharArray();
characters(ch, 0, ch.length);
} catch (PathNotFoundException e) {
// jcr:xmlcharacters not found, ignore this node
}
} else {
// attributes (properties)
exportProperties(node);
// encode node name to make sure it's a valid xml name
String encoded = ISO9075.encode(local);
startElement(uri, encoded);
exportNodes(node);
endElement(uri, encoded);
}
}
use of javax.jcr.Property in project jackrabbit by apache.
the class SetValueConstraintViolationExceptionTest method testBinaryProperty.
/**
* Tests if setValue(InputStream value) and setValue(Value value) where
* value is a BinaryValue throw a ConstraintViolationException if the change
* would violate a value constraint
*/
public void testBinaryProperty() throws NotExecutableException, RepositoryException {
// locate a PropertyDefinition with ValueConstraints
PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.BINARY, false, false, true, false);
if (propDef == null) {
throw new NotExecutableException("No binary property def with " + "testable value constraints has been found");
}
// find a Value that does not satisfy the ValueConstraints of propDef
Value valueNotSatisfied1 = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
Value valueNotSatisfied2 = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
if (valueNotSatisfied1 == null || valueNotSatisfied2 == null) {
throw new NotExecutableException("No binary property def with " + "testable value constraints has been found");
}
// find a Value that does satisfy the ValueConstraints of propDef
Value valueSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, true);
if (valueSatisfied == null) {
throw new NotExecutableException("The value constraints do not allow any value.");
}
// create a sub node of testRootNode of type propDef.getDeclaringNodeType()
// and add a property with constraints to this node
Node node;
Property prop;
try {
String nodeType = propDef.getDeclaringNodeType().getName();
node = testRootNode.addNode(nodeName2, nodeType);
prop = node.setProperty(propDef.getName(), valueSatisfied);
testRootNode.getSession().save();
} catch (ConstraintViolationException e) {
// implementation specific constraints do not allow to set up test environment
throw new NotExecutableException("Not able to create required test items.");
}
// test of signature setValue(InputStream value)
InputStream in = valueNotSatisfied1.getStream();
try {
prop.setValue(in);
node.save();
fail("setValue(InputStream value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
} catch (ConstraintViolationException e) {
// success
} finally {
try {
in.close();
} catch (IOException ignore) {
}
}
// test of signature setValue(Value value)
try {
prop.setValue(valueNotSatisfied2);
node.save();
fail("setValue(Value value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
}
Aggregations