use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project jackrabbit by apache.
the class NodeImpl method getWeakReferences.
/**
* {@inheritDoc}
*/
public PropertyIterator getWeakReferences(String name) throws RepositoryException {
if (name == null) {
return getWeakReferences();
}
// check state of this instance
sanityCheck();
// shortcut if node isn't referenceable
if (!isNodeType(NameConstants.MIX_REFERENCEABLE)) {
return PropertyIteratorAdapter.EMPTY;
}
try {
StringBuilder stmt = new StringBuilder();
stmt.append("//*[@").append(ISO9075.encode(name));
stmt.append(" = '").append(data.getId()).append("']");
Query q = getSession().getWorkspace().getQueryManager().createQuery(stmt.toString(), Query.XPATH);
QueryResult result = q.execute();
ArrayList<Property> l = new ArrayList<Property>();
for (NodeIterator nit = result.getNodes(); nit.hasNext(); ) {
Node n = nit.nextNode();
l.add(n.getProperty(name));
}
if (l.isEmpty()) {
return PropertyIteratorAdapter.EMPTY;
} else {
return new PropertyIteratorAdapter(l);
}
} catch (RepositoryException e) {
String msg = "Unable to retrieve WEAKREFERENCE properties that refer to " + id;
log.debug(msg);
throw new RepositoryException(msg, e);
}
}
use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project jackrabbit by apache.
the class JsonWriterTest method testDoubleOutput.
@Test
public void testDoubleOutput() throws Exception {
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(writer);
Node parent = createMock(Node.class);
Property doubleProperty = createMock(Property.class);
Value doublePropertyValue = createMock(Value.class);
expect(doubleProperty.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
expect(doubleProperty.getName()).andReturn("singleValued").anyTimes();
expect(doubleProperty.isMultiple()).andReturn(false).anyTimes();
expect(doubleProperty.getValue()).andReturn(doublePropertyValue).anyTimes();
expect(doublePropertyValue.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
expect(doublePropertyValue.getDouble()).andReturn(5d).anyTimes();
expect(doublePropertyValue.getString()).andReturn("5").anyTimes();
Property mvDoubleProperty = createMock(Property.class);
Value mvDoublePropertyValue1 = createMock(Value.class);
Value mvDoublePropertyValue2 = createMock(Value.class);
expect(mvDoubleProperty.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
expect(mvDoubleProperty.getName()).andReturn("multiValued").anyTimes();
expect(mvDoubleProperty.isMultiple()).andReturn(true).anyTimes();
expect(mvDoubleProperty.getValues()).andReturn(new Value[] { mvDoublePropertyValue1, mvDoublePropertyValue2 }).anyTimes();
expect(mvDoublePropertyValue1.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
expect(mvDoublePropertyValue1.getDouble()).andReturn(42d).anyTimes();
expect(mvDoublePropertyValue1.getString()).andReturn("42").anyTimes();
expect(mvDoublePropertyValue2.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
expect(mvDoublePropertyValue2.getDouble()).andReturn(98.6).anyTimes();
expect(mvDoublePropertyValue2.getString()).andReturn("98.6").anyTimes();
final List<Property> properties = new ArrayList<Property>();
properties.add(doubleProperty);
properties.add(mvDoubleProperty);
expect(parent.getProperties()).andAnswer(new IAnswer<PropertyIterator>() {
@Override
public PropertyIterator answer() throws Throwable {
return new PropertyIteratorAdapter(properties.iterator());
}
});
expect(parent.getNodes()).andAnswer(new IAnswer<NodeIterator>() {
@Override
public NodeIterator answer() throws Throwable {
return new NodeIteratorAdapter(Collections.<Node>emptyIterator());
}
});
replayAll();
jsonWriter.write(parent, 1);
assertEquals("{\":singleValued\":\"Double\",\"singleValued\":5,\":multiValued\":\"Double\",\"multiValued\":[42,98.6],\"::NodeIteratorSize\":0}", writer.toString());
verifyAll();
}
use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project sling by apache.
the class MockNode method getProperties.
@Override
public PropertyIterator getProperties(final String namePattern) throws RepositoryException {
final Pattern pattern = Pattern.compile(namePattern);
RangeIterator items = getMockedSession().listChildren(getPath(), new ItemFilter() {
@Override
public boolean accept(final ItemData item) throws RepositoryException {
return item.isProperty() && pattern.matcher(item.getName()).matches();
}
});
return new PropertyIteratorAdapter(items, items.getSize());
}
use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project jackrabbit by apache.
the class NodeImpl method getWeakReferences.
/**
* {@inheritDoc}
*/
public PropertyIterator getWeakReferences() throws RepositoryException {
// check state of this instance
sanityCheck();
// shortcut if node isn't referenceable
if (!isNodeType(NameConstants.MIX_REFERENCEABLE)) {
return PropertyIteratorAdapter.EMPTY;
}
Value ref = getSession().getValueFactory().createValue(this, true);
List<Property> props = new ArrayList<Property>();
QueryManagerImpl qm = (QueryManagerImpl) getSession().getWorkspace().getQueryManager();
for (Node n : qm.getWeaklyReferringNodes(this)) {
for (PropertyIterator it = n.getProperties(); it.hasNext(); ) {
Property p = it.nextProperty();
if (p.getType() == PropertyType.WEAKREFERENCE) {
Collection<Value> refs;
if (p.isMultiple()) {
refs = Arrays.asList(p.getValues());
} else {
refs = Collections.singleton(p.getValue());
}
if (refs.contains(ref)) {
props.add(p);
}
}
}
}
return new PropertyIteratorAdapter(props);
}
Aggregations