|
| 1 | +/* |
| 2 | + * The XML:DB Initiative Software License, Version 1.0 |
| 3 | + * |
| 4 | + * Copyright (c) 2000-2022 The XML:DB Initiative. All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without modification, are permitted |
| 7 | + * provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 10 | + * and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 13 | + * conditions and the following disclaimer in the documentation and/or other materials provided with |
| 14 | + * the distribution. |
| 15 | + * |
| 16 | + * 3. The end-user documentation included with the redistribution, if any, must include the |
| 17 | + * following acknowledgment: "This product includes software developed by the XML:DB Initiative |
| 18 | + * (http://www.xmldb.org/)." Alternately, this acknowledgment may appear in the software itself, if |
| 19 | + * and wherever such third-party acknowledgments normally appear. |
| 20 | + * |
| 21 | + * 4. The name "XML:DB Initiative" must not be used to endorse or promote products derived from this |
| 22 | + * software without prior written permission. For written permission, please contact info@xmldb.org. |
| 23 | + * |
| 24 | + * 5. Products derived from this software may not be called "XML:DB", nor may "XML:DB" appear in |
| 25 | + * their name, without prior written permission of the XML:DB Initiative. |
| 26 | + * |
| 27 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 28 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR |
| 30 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 32 | + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + * ================================================================================================= |
| 36 | + * This software consists of voluntary contributions made by many individuals on behalf of the |
| 37 | + * XML:DB Initiative. For more information on the XML:DB Initiative, please see |
| 38 | + * <https://github.com/xmldb-org/> |
| 39 | + */ |
| 40 | +package org.xmldb.api.base; |
| 41 | + |
| 42 | +/** |
| 43 | + * A visitor of collections. An implementation of this interface is provided to the |
| 44 | + * {@link Collection#walkCollectionTree} methods to visit each resource in a collection tree. |
| 45 | + * |
| 46 | + * <p> |
| 47 | + * <b>Usage Examples:</b> |
| 48 | + * <p> |
| 49 | + * Suppose we want to delete a collection tree. In that case, each collection should be deleted |
| 50 | + * after the resources in the collection are deleted. |
| 51 | + * |
| 52 | + * <pre> |
| 53 | + * collection.walkCollectionTree(new SimpleCollectionVisitor() { |
| 54 | + * @Override |
| 55 | + * public CollectionVisitResult visitResource(Resource resource) throws XMLDBException { |
| 56 | + * resource.getParentCollection().removeResource(resource); |
| 57 | + * return CollectionVisitResult.CONTINUE; |
| 58 | + * } |
| 59 | + * |
| 60 | + * @Override |
| 61 | + * public CollectionVisitResult postVisitCollection(Collection collection, XMLDBException e) |
| 62 | + * throws XMLDBException { |
| 63 | + * if (e == null) { |
| 64 | + * String collectionName = collection.getName() |
| 65 | + * resource.getParentCollection().getService(CollectionManagementService.class).removeCollection(collectionName); |
| 66 | + * return CollectionVisitResult.CONTINUE; |
| 67 | + * } else { |
| 68 | + * // collection iteration failed |
| 69 | + * throw e; |
| 70 | + * } |
| 71 | + * } |
| 72 | + * }); |
| 73 | + * </pre> |
| 74 | + * <p> |
| 75 | + * Furthermore, suppose we want to copy a collection tree to a target location. In that case, |
| 76 | + * symbolic links should be followed and the target collection should be created before the |
| 77 | + * resources in the collection are copied. |
| 78 | + * |
| 79 | + * <pre> |
| 80 | + * sourceColelction.walkCollectionTree(Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { |
| 81 | + * @Override |
| 82 | + * public CollectionVisitResult preVisitCollection(Collection collection) throws XMLDBException { |
| 83 | + * Path targetcollection = target.resolve(source.relativize(collection)); |
| 84 | + * try { |
| 85 | + * Files.copy(collection, targetcollection); |
| 86 | + * } catch (FileAlreadyExistsException e) { |
| 87 | + * if (!Files.isDirectory(targetcollection)) |
| 88 | + * throw e; |
| 89 | + * } |
| 90 | + * return CONTINUE; |
| 91 | + * } |
| 92 | + * |
| 93 | + * @Override |
| 94 | + * public CollectionVisitResult visitFile(Resource resource) throws XMLDBException { |
| 95 | + * Files.copy(resource, target.resolve(source.relativize(resource))); |
| 96 | + * return CONTINUE; |
| 97 | + * } |
| 98 | + * }); |
| 99 | + * </pre> |
| 100 | + * |
| 101 | + * @since 2.0 |
| 102 | + */ |
| 103 | +public interface CollectionVisitor { |
| 104 | + /** |
| 105 | + * Invoked for a collection before resources in the collection are visited. |
| 106 | + * |
| 107 | + * <p> |
| 108 | + * If this method returns {@link CollectionVisitResult#CONTINUE CONTINUE}, then resources in the |
| 109 | + * collection are visited. If this method returns {@link CollectionVisitResult#SKIP_SUBTREE |
| 110 | + * SKIP_SUBTREE} or {@link CollectionVisitResult#SKIP_SIBLINGS SKIP_SIBLINGS} then resources in |
| 111 | + * the collection (and any descendants) will not be visited. |
| 112 | + * |
| 113 | + * @param collection a reference to the collection |
| 114 | + * |
| 115 | + * @return the visit result |
| 116 | + * |
| 117 | + * @throws XMLDBException if an XML DB error occurs |
| 118 | + */ |
| 119 | + Collection preVisitCollection(Collection collection) throws XMLDBException; |
| 120 | + |
| 121 | + /** |
| 122 | + * Invoked for a resource in a collection. |
| 123 | + * |
| 124 | + * @param resource a reference to the resource |
| 125 | + * |
| 126 | + * @return the visit result |
| 127 | + * |
| 128 | + * @throws XMLDBException if an XML DB error occurs |
| 129 | + */ |
| 130 | + Collection visitResource(Resource resource) throws XMLDBException; |
| 131 | + |
| 132 | + /** |
| 133 | + * Invoked for a resource that could not be visited. This method is invoked if the resource's |
| 134 | + * attributes could not be read, the resource is a collection that could not be opened, and other |
| 135 | + * reasons. |
| 136 | + * |
| 137 | + * @param resource a reference to the resource |
| 138 | + * @param exc the XML DB exception that prevented the resource from being visited |
| 139 | + * |
| 140 | + * @return the visit result |
| 141 | + * |
| 142 | + * @throws XMLDBException if an XML DB error occurs |
| 143 | + */ |
| 144 | + Collection visitResourceFailed(Resource resource, XMLDBException exc) throws XMLDBException; |
| 145 | + |
| 146 | + /** |
| 147 | + * Invoked for a collection after resources in the collection, and all of their descendants, have |
| 148 | + * been visited. This method is also invoked when iteration of the collection completes |
| 149 | + * prematurely (by a {@link #visitResource visitFile} method returning |
| 150 | + * {@link CollectionVisitResult#SKIP_SIBLINGS SKIP_SIBLINGS}, or an XML DB error when iterating |
| 151 | + * over the collection). |
| 152 | + * |
| 153 | + * @param collection a reference to the collection |
| 154 | + * @param exc {@code null} if the iteration of the collection completes without an error; |
| 155 | + * otherwise the XML DB exception that caused the iteration of the collection to complete |
| 156 | + * prematurely |
| 157 | + * |
| 158 | + * @return the visit result |
| 159 | + * |
| 160 | + * @throws XMLDBException if an XML DB error occurs |
| 161 | + */ |
| 162 | + Collection postVisitCollection(Collection collection, XMLDBException exc) throws XMLDBException; |
| 163 | + |
| 164 | +} |
0 commit comments