001/*
002 * Copyright (c) 2004-2010, Kohsuke Kawaguchi
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 *     * Redistributions of source code must retain the above copyright notice, this list of
009 *       conditions and the following disclaimer.
010 *     * Redistributions in binary form must reproduce the above copyright notice, this list of
011 *       conditions and the following disclaimer in the documentation and/or other materials
012 *       provided with the distribution.
013 *
014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
016 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
017 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
018 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
019 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
020 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
021 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.kohsuke.stapler.export;
025
026/**
027 * Controls the portion of the object graph to be written to {@link DataWriter}.
028 *
029 * @author Kohsuke Kawaguchi
030 * @see Model#writeTo(Object, TreePruner, DataWriter)
031 */
032public abstract class TreePruner {
033    /**
034     * Called before Hudson writes a new property.
035     *
036     * @return
037     *      null if this property shouldn't be written. Otherwise the returned {@link TreePruner} object
038     *      will be consulted to determine properties of the child object in turn.
039     */
040    public abstract TreePruner accept(Object node, Property prop);
041
042    public Range getRange() {
043        return Range.ALL;
044    }
045
046    public static class ByDepth extends TreePruner {
047        final int n;
048        private ByDepth next;
049
050        public ByDepth(int n) {
051            this.n = n;
052        }
053
054        private ByDepth next() {
055            if (next==null)
056                next = new ByDepth(n+1);
057            return next;
058        }
059
060        @Override
061        public TreePruner accept(Object node, Property prop) {
062            if (prop.visibility < n)    return null;    // not visible
063
064            if (prop.inline || prop.merge)    return this;
065            return next();
066        }
067    }
068
069    /**
070     * Probably the most common {@link TreePruner} that just visits the top object and its properties,
071     * but none of the referenced objects.
072     */
073    public static final TreePruner DEFAULT = new ByDepth(1);
074}