Search in sources :

Example 6 with PropertyIterator

use of javax.jcr.PropertyIterator in project jackrabbit-oak by apache.

the class MiscTest method traverse.

private void traverse(Node node) throws RepositoryException {
    PropertyIterator iter = node.getProperties();
    while (iter.hasNext()) {
        Property p = iter.nextProperty();
        p.getDefinition();
    }
    NodeIterator niter = node.getNodes();
    while (niter.hasNext()) {
        traverse(niter.nextNode());
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 7 with PropertyIterator

use of javax.jcr.PropertyIterator in project jackrabbit-oak by apache.

the class VersionTest method getReferencingPaths.

private static Set<String> getReferencingPaths(Node n) throws RepositoryException {
    Set<String> refs = Sets.newHashSet();
    PropertyIterator it = n.getReferences();
    while (it.hasNext()) {
        refs.add(it.nextProperty().getPath());
    }
    return refs;
}
Also used : PropertyIterator(javax.jcr.PropertyIterator)

Example 8 with PropertyIterator

use of javax.jcr.PropertyIterator in project sling by apache.

the class JcrModifiableValueMap method readFully.

/**
     * Read all properties.
     * @throws IllegalArgumentException if a repository exception occurs
     */
void readFully() {
    if (!fullyRead) {
        try {
            final PropertyIterator pi = node.getProperties();
            while (pi.hasNext()) {
                final Property prop = pi.nextProperty();
                this.cacheProperty(prop);
            }
            fullyRead = true;
        } catch (final RepositoryException re) {
            throw new IllegalArgumentException(re);
        }
    }
}
Also used : PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 9 with PropertyIterator

use of javax.jcr.PropertyIterator in project sling by apache.

the class JcrValueMap method readFully.

/**
     * Read all properties.
     * @throws IllegalArgumentException if a repository exception occurs
     */
void readFully() {
    if (!fullyRead) {
        try {
            final PropertyIterator pi = node.getProperties();
            while (pi.hasNext()) {
                final Property prop = pi.nextProperty();
                this.cacheProperty(prop);
            }
            fullyRead = true;
        } catch (final RepositoryException re) {
            throw new IllegalArgumentException(re);
        }
    }
}
Also used : PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 10 with PropertyIterator

use of javax.jcr.PropertyIterator in project sling by apache.

the class MockedResource method adaptTo.

@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
    if (type.equals(Node.class)) {
        try {
            return (AdapterType) getSession().getNode(getPath());
        } catch (Exception e) {
            logger.error("Exception occurred: " + e, e);
            throw new RuntimeException("Exception occurred: " + e, e);
        }
    } else if (type.equals(ValueMap.class)) {
        try {
            Session session = getSession();
            Node node = session.getNode(getPath());
            HashMap<String, Object> map = new HashMap<String, Object>();
            PropertyIterator properties = node.getProperties();
            while (properties.hasNext()) {
                Property p = properties.nextProperty();
                if (p.getType() == PropertyType.BOOLEAN) {
                    map.put(p.getName(), p.getBoolean());
                } else if (p.getType() == PropertyType.STRING) {
                    map.put(p.getName(), p.getString());
                } else if (p.getType() == PropertyType.DATE) {
                    map.put(p.getName(), p.getDate().getTime());
                } else if (p.getType() == PropertyType.NAME) {
                    map.put(p.getName(), p.getName());
                } else if (p.getType() == PropertyType.LONG) {
                    map.put(p.getName(), p.getLong());
                } else {
                    throw new RuntimeException("Unsupported property type: " + p.getType());
                }
            }
            ValueMap valueMap = new ValueMapDecorator(map);
            return (AdapterType) valueMap;
        } catch (Exception e) {
            logger.error("adaptTo failed with : " + e);
            return null;
        }
    } else if (type.equals(ModifiableValueMap.class)) {
        return (AdapterType) new ModifiableValueMap() {

            public Collection<Object> values() {
                throw new UnsupportedOperationException();
            }

            public int size() {
                throw new UnsupportedOperationException();
            }

            public Object remove(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final Property p = node.getProperty(String.valueOf(arg0));
                    if (p != null) {
                        p.remove();
                    }
                    // the return value is never used
                    return null;
                } catch (PathNotFoundException pnfe) {
                    // perfectly fine
                    return null;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public void putAll(Map<? extends String, ? extends Object> arg0) {
                throw new UnsupportedOperationException();
            }

            public Object put(String arg0, Object arg1) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    Object result = null;
                    if (node.hasProperty(arg0)) {
                        final Property previous = node.getProperty(arg0);
                        if (previous == null) {
                        // null
                        } else if (previous.getType() == PropertyType.STRING) {
                            result = previous.getString();
                        } else if (previous.getType() == PropertyType.DATE) {
                            result = previous.getDate();
                        } else if (previous.getType() == PropertyType.BOOLEAN) {
                            result = previous.getBoolean();
                        } else if (previous.getType() == PropertyType.LONG) {
                            result = previous.getLong();
                        } else {
                            throw new UnsupportedOperationException();
                        }
                    }
                    if (arg1 instanceof String) {
                        node.setProperty(arg0, (String) arg1);
                    } else if (arg1 instanceof Calendar) {
                        node.setProperty(arg0, (Calendar) arg1);
                    } else if (arg1 instanceof Boolean) {
                        node.setProperty(arg0, (Boolean) arg1);
                    } else if (arg1 instanceof Date) {
                        final Calendar c = Calendar.getInstance();
                        c.setTime((Date) arg1);
                        node.setProperty(arg0, c);
                    } else if (arg1 instanceof Long) {
                        node.setProperty(arg0, (Long) arg1);
                    } else if (arg1 == null) {
                        node.setProperty(arg0, (Value) null);
                    } else {
                        throw new UnsupportedOperationException();
                    }
                    return result;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public Set<String> keySet() {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final PropertyIterator pi = node.getProperties();
                    final Set<String> result = new HashSet<String>();
                    while (pi.hasNext()) {
                        final Property p = pi.nextProperty();
                        result.add(p.getName());
                    }
                    return result;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public boolean isEmpty() {
                throw new UnsupportedOperationException();
            }

            public Object get(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final String key = String.valueOf(arg0);
                    if (node.hasProperty(key)) {
                        return node.getProperty(key);
                    } else {
                        return null;
                    }
                } catch (RepositoryException re) {
                    throw new RuntimeException(re);
                }
            }

            public Set<Entry<String, Object>> entrySet() {
                throw new UnsupportedOperationException();
            }

            public boolean containsValue(Object arg0) {
                throw new UnsupportedOperationException();
            }

            public boolean containsKey(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    return node.hasProperty(String.valueOf(arg0));
                } catch (RepositoryException re) {
                    throw new RuntimeException(re);
                }
            }

            public void clear() {
                throw new UnsupportedOperationException();
            }

            public <T> T get(String name, T defaultValue) {
                throw new UnsupportedOperationException();
            }

            public <T> T get(String name, Class<T> type) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    if (node == null) {
                        return null;
                    }
                    if (!node.hasProperty(name)) {
                        return null;
                    }
                    Property p = node.getProperty(name);
                    if (p == null) {
                        return null;
                    }
                    if (type.equals(Calendar.class)) {
                        return (T) p.getDate();
                    } else if (type.equals(String.class)) {
                        return (T) p.getString();
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    } else {
        return super.adaptTo(type);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Node(javax.jcr.Node) Property(javax.jcr.Property) Calendar(java.util.Calendar) PropertyIterator(javax.jcr.PropertyIterator) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date) Collection(java.util.Collection) PathNotFoundException(javax.jcr.PathNotFoundException) Session(javax.jcr.Session)

Aggregations

PropertyIterator (javax.jcr.PropertyIterator)130 Property (javax.jcr.Property)97 Node (javax.jcr.Node)79 NodeIterator (javax.jcr.NodeIterator)31 RepositoryException (javax.jcr.RepositoryException)28 Value (javax.jcr.Value)28 Session (javax.jcr.Session)21 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)14 PathNotFoundException (javax.jcr.PathNotFoundException)13 HashMap (java.util.HashMap)12 AccessDeniedException (javax.jcr.AccessDeniedException)7 Item (javax.jcr.Item)5 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)4 AccessControlException (java.security.AccessControlException)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 RepositoryFileDaoReferentialIntegrityException (org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException)4