src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/ModelNode.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/ModelNode.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,596 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.policy.sourcemodel;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
    1.32 +import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
    1.33 +import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
    1.34 +import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken;
    1.35 +
    1.36 +import java.util.Collection;
    1.37 +import java.util.Collections;
    1.38 +import java.util.Iterator;
    1.39 +import java.util.LinkedList;
    1.40 +
    1.41 +/**
    1.42 + * The general representation of a single node within a {@link com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModel} instance.
    1.43 + * The model node is created via factory methods of the {@link com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModel} instance.
    1.44 + * It may also hold {@link com.sun.xml.internal.ws.policy.sourcemodel.AssertionData} instance in case its type is {@code ModelNode.Type.ASSERTION}.
    1.45 + *
    1.46 + * @author Marek Potociar
    1.47 + */
    1.48 +public final class ModelNode implements Iterable<ModelNode>, Cloneable {
    1.49 +    private static final PolicyLogger LOGGER = PolicyLogger.getLogger(ModelNode.class);
    1.50 +
    1.51 +    /**
    1.52 +     * Policy source model node type enumeration
    1.53 +     */
    1.54 +    public static enum Type {
    1.55 +        POLICY(XmlToken.Policy),
    1.56 +        ALL(XmlToken.All),
    1.57 +        EXACTLY_ONE(XmlToken.ExactlyOne),
    1.58 +        POLICY_REFERENCE(XmlToken.PolicyReference),
    1.59 +        ASSERTION(XmlToken.UNKNOWN),
    1.60 +        ASSERTION_PARAMETER_NODE(XmlToken.UNKNOWN);
    1.61 +
    1.62 +        private XmlToken token;
    1.63 +
    1.64 +        Type(XmlToken token) {
    1.65 +            this.token = token;
    1.66 +        }
    1.67 +
    1.68 +        public XmlToken getXmlToken() {
    1.69 +            return token;
    1.70 +        }
    1.71 +
    1.72 +        /**
    1.73 +         * Method checks the PSM state machine if the creation of new child of given type is plausible for a node element
    1.74 +         * with type set to this type instance.
    1.75 +         *
    1.76 +         * @param childType The type.
    1.77 +         * @return True if the type is supported, false otherwise
    1.78 +         */
    1.79 +        private boolean isChildTypeSupported(final Type childType) {
    1.80 +            switch (this) {
    1.81 +                case POLICY:
    1.82 +                case ALL:
    1.83 +                case EXACTLY_ONE:
    1.84 +                    switch (childType) {
    1.85 +                        case ASSERTION_PARAMETER_NODE:
    1.86 +                            return false;
    1.87 +                        default:
    1.88 +                            return true;
    1.89 +                    }
    1.90 +                case POLICY_REFERENCE:
    1.91 +                    return false;
    1.92 +                case ASSERTION:
    1.93 +                    switch (childType) {
    1.94 +                        case POLICY:
    1.95 +                        case POLICY_REFERENCE:
    1.96 +                        case ASSERTION_PARAMETER_NODE:
    1.97 +                            return true;
    1.98 +                        default:
    1.99 +                            return false;
   1.100 +                    }
   1.101 +                case ASSERTION_PARAMETER_NODE:
   1.102 +                    switch (childType) {
   1.103 +                        case ASSERTION_PARAMETER_NODE:
   1.104 +                            return true;
   1.105 +                        default:
   1.106 +                            return false;
   1.107 +                    }
   1.108 +                default:
   1.109 +                    throw LOGGER.logSevereException(new IllegalStateException(
   1.110 +                            LocalizationMessages.WSP_0060_POLICY_ELEMENT_TYPE_UNKNOWN(this)));
   1.111 +            }
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    // comon model node attributes
   1.116 +    private LinkedList<ModelNode> children;
   1.117 +    private Collection<ModelNode> unmodifiableViewOnContent;
   1.118 +    private final ModelNode.Type type;
   1.119 +    private ModelNode parentNode;
   1.120 +    private PolicySourceModel parentModel;
   1.121 +
   1.122 +    // attributes used only in 'POLICY_REFERENCE' model node
   1.123 +    private PolicyReferenceData referenceData;
   1.124 +    private PolicySourceModel referencedModel;
   1.125 +
   1.126 +    // attibutes used only in 'ASSERTION' or 'ASSERTION_PARAMETER_NODE' model node
   1.127 +    private AssertionData nodeData;
   1.128 +
   1.129 +    /**
   1.130 +     * The factory method creates and initializes the POLICY model node and sets it's parent model reference to point to
   1.131 +     * the model supplied as an input parameter. This method is intended to be used ONLY from {@link PolicySourceModel} during
   1.132 +     * the initialization of its own internal structures.
   1.133 +     *
   1.134 +     * @param model policy source model to be used as a parent model of the newly created {@link ModelNode}. Must not be {@code null}
   1.135 +     * @return POLICY model node with the parent model reference initialized to the model supplied as an input parameter
   1.136 +     * @throws IllegalArgumentException if the {@code model} input parameter is {@code null}
   1.137 +     */
   1.138 +    static ModelNode createRootPolicyNode(final PolicySourceModel model) throws IllegalArgumentException {
   1.139 +        if (model == null) {
   1.140 +            throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0039_POLICY_SRC_MODEL_INPUT_PARAMETER_MUST_NOT_BE_NULL()));
   1.141 +        }
   1.142 +        return new ModelNode(ModelNode.Type.POLICY, model);
   1.143 +    }
   1.144 +
   1.145 +    private ModelNode(Type type, PolicySourceModel parentModel) {
   1.146 +        this.type = type;
   1.147 +        this.parentModel = parentModel;
   1.148 +        this.children = new LinkedList<ModelNode>();
   1.149 +        this.unmodifiableViewOnContent = Collections.unmodifiableCollection(this.children);
   1.150 +    }
   1.151 +
   1.152 +    private ModelNode(Type type, PolicySourceModel parentModel, AssertionData data) {
   1.153 +        this(type, parentModel);
   1.154 +
   1.155 +        this.nodeData = data;
   1.156 +    }
   1.157 +
   1.158 +    private ModelNode(PolicySourceModel parentModel, PolicyReferenceData data) {
   1.159 +        this(Type.POLICY_REFERENCE, parentModel);
   1.160 +
   1.161 +        this.referenceData = data;
   1.162 +    }
   1.163 +
   1.164 +    private void checkCreateChildOperationSupportForType(final Type type) throws UnsupportedOperationException {
   1.165 +        if (!this.type.isChildTypeSupported(type)) {
   1.166 +            throw LOGGER.logSevereException(new UnsupportedOperationException(LocalizationMessages.WSP_0073_CREATE_CHILD_NODE_OPERATION_NOT_SUPPORTED(type, this.type)));
   1.167 +        }
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.172 +     * Each node is created with respect to its enclosing policy source model.
   1.173 +     *
   1.174 +     * @return A new Policy node.
   1.175 +     */
   1.176 +    public ModelNode createChildPolicyNode() {
   1.177 +        checkCreateChildOperationSupportForType(Type.POLICY);
   1.178 +
   1.179 +        final ModelNode node = new ModelNode(ModelNode.Type.POLICY, parentModel);
   1.180 +        this.addChild(node);
   1.181 +
   1.182 +        return node;
   1.183 +    }
   1.184 +
   1.185 +    /**
   1.186 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.187 +     * Each node is created with respect to its enclosing policy source model.
   1.188 +     *
   1.189 +     * @return A new All node.
   1.190 +     */
   1.191 +    public ModelNode createChildAllNode() {
   1.192 +        checkCreateChildOperationSupportForType(Type.ALL);
   1.193 +
   1.194 +        final ModelNode node = new ModelNode(ModelNode.Type.ALL, parentModel);
   1.195 +        this.addChild(node);
   1.196 +
   1.197 +        return node;
   1.198 +    }
   1.199 +
   1.200 +    /**
   1.201 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.202 +     * Each node is created with respect to its enclosing policy source model.
   1.203 +     *
   1.204 +     * @return A new ExactlyOne node.
   1.205 +     */
   1.206 +    public ModelNode createChildExactlyOneNode() {
   1.207 +        checkCreateChildOperationSupportForType(Type.EXACTLY_ONE);
   1.208 +
   1.209 +        final ModelNode node = new ModelNode(ModelNode.Type.EXACTLY_ONE, parentModel);
   1.210 +        this.addChild(node);
   1.211 +
   1.212 +        return node;
   1.213 +    }
   1.214 +
   1.215 +    /**
   1.216 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.217 +     * Each node is created with respect to its enclosing policy source model.
   1.218 +     *
   1.219 +     * @return A new policy assertion node.
   1.220 +     */
   1.221 +    public ModelNode createChildAssertionNode() {
   1.222 +        checkCreateChildOperationSupportForType(Type.ASSERTION);
   1.223 +
   1.224 +        final ModelNode node = new ModelNode(ModelNode.Type.ASSERTION, parentModel);
   1.225 +        this.addChild(node);
   1.226 +
   1.227 +        return node;
   1.228 +    }
   1.229 +
   1.230 +    /**
   1.231 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.232 +     * Each node is created with respect to its enclosing policy source model.
   1.233 +     *
   1.234 +     * @param nodeData The policy assertion data.
   1.235 +     * @return A new policy assertion node.
   1.236 +     */
   1.237 +    public ModelNode createChildAssertionNode(final AssertionData nodeData) {
   1.238 +        checkCreateChildOperationSupportForType(Type.ASSERTION);
   1.239 +
   1.240 +        final ModelNode node = new ModelNode(Type.ASSERTION, parentModel, nodeData);
   1.241 +        this.addChild(node);
   1.242 +
   1.243 +        return node;
   1.244 +    }
   1.245 +
   1.246 +    /**
   1.247 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.248 +     * Each node is created with respect to its enclosing policy source model.
   1.249 +     *
   1.250 +     * @return A new assertion parameter node.
   1.251 +     */
   1.252 +    public ModelNode createChildAssertionParameterNode() {
   1.253 +        checkCreateChildOperationSupportForType(Type.ASSERTION_PARAMETER_NODE);
   1.254 +
   1.255 +        final ModelNode node = new ModelNode(ModelNode.Type.ASSERTION_PARAMETER_NODE, parentModel);
   1.256 +        this.addChild(node);
   1.257 +
   1.258 +        return node;
   1.259 +    }
   1.260 +
   1.261 +    /**
   1.262 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.263 +     * Each node is created with respect to its enclosing policy source model.
   1.264 +     *
   1.265 +     * @param nodeData The assertion parameter data.
   1.266 +     * @return A new assertion parameter node.
   1.267 +     */
   1.268 +    ModelNode createChildAssertionParameterNode(final AssertionData nodeData) {
   1.269 +        checkCreateChildOperationSupportForType(Type.ASSERTION_PARAMETER_NODE);
   1.270 +
   1.271 +        final ModelNode node = new ModelNode(Type.ASSERTION_PARAMETER_NODE, parentModel, nodeData);
   1.272 +        this.addChild(node);
   1.273 +
   1.274 +        return node;
   1.275 +    }
   1.276 +
   1.277 +    /**
   1.278 +     * Factory method that creates new policy source model node as specified by a factory method name and input parameters.
   1.279 +     * Each node is created with respect to its enclosing policy source model.
   1.280 +     *
   1.281 +     * @param referenceData The PolicyReference data.
   1.282 +     * @return A new PolicyReference node.
   1.283 +     */
   1.284 +    ModelNode createChildPolicyReferenceNode(final PolicyReferenceData referenceData) {
   1.285 +        checkCreateChildOperationSupportForType(Type.POLICY_REFERENCE);
   1.286 +
   1.287 +        final ModelNode node = new ModelNode(parentModel, referenceData);
   1.288 +        this.parentModel.addNewPolicyReference(node);
   1.289 +        this.addChild(node);
   1.290 +
   1.291 +        return node;
   1.292 +    }
   1.293 +
   1.294 +    Collection<ModelNode> getChildren() {
   1.295 +        return unmodifiableViewOnContent;
   1.296 +    }
   1.297 +
   1.298 +    /**
   1.299 +     * Sets the parent model reference on the node and its children. The method may be invoked only on the root node
   1.300 +     * of the policy source model (or - in general - on a model node that dose not reference a parent node). Otherwise an
   1.301 +     * exception is thrown.
   1.302 +     *
   1.303 +     * @param model new parent policy source model to be set.
   1.304 +     * @throws IllegalAccessException in case this node references a parent node (i.e. is not a root node of the model).
   1.305 +     */
   1.306 +    void setParentModel(final PolicySourceModel model) throws IllegalAccessException {
   1.307 +        if (parentNode != null) {
   1.308 +            throw LOGGER.logSevereException(new IllegalAccessException(LocalizationMessages.WSP_0049_PARENT_MODEL_CAN_NOT_BE_CHANGED()));
   1.309 +        }
   1.310 +
   1.311 +        this.updateParentModelReference(model);
   1.312 +    }
   1.313 +
   1.314 +    /**
   1.315 +     * The method updates the parentModel reference on current model node instance and all of it's children
   1.316 +     *
   1.317 +     * @param model new policy source model reference.
   1.318 +     */
   1.319 +    private void updateParentModelReference(final PolicySourceModel model) {
   1.320 +        this.parentModel = model;
   1.321 +
   1.322 +        for (ModelNode child : children) {
   1.323 +            child.updateParentModelReference(model);
   1.324 +        }
   1.325 +    }
   1.326 +
   1.327 +    /**
   1.328 +     * Returns the parent policy source model that contains this model node.
   1.329 +     *
   1.330 +     * @return the parent policy source model
   1.331 +     */
   1.332 +    public PolicySourceModel getParentModel() {
   1.333 +        return parentModel;
   1.334 +    }
   1.335 +
   1.336 +    /**
   1.337 +     * Returns the type of this policy source model node.
   1.338 +     *
   1.339 +     * @return actual type of this policy source model node
   1.340 +     */
   1.341 +    public ModelNode.Type getType() {
   1.342 +        return type;
   1.343 +    }
   1.344 +
   1.345 +    /**
   1.346 +     * Returns the parent referenced by this policy source model node.
   1.347 +     *
   1.348 +     * @return current parent of this policy source model node or {@code null} if the node does not have a parent currently.
   1.349 +     */
   1.350 +    public ModelNode getParentNode() {
   1.351 +        return parentNode;
   1.352 +    }
   1.353 +
   1.354 +    /**
   1.355 +     * Returns the data for this policy source model node (if any). The model node data are expected to be not {@code null} only in
   1.356 +     * case the type of this node is ASSERTION or ASSERTION_PARAMETER_NODE.
   1.357 +     *
   1.358 +     * @return the data of this policy source model node or {@code null} if the node does not have any data associated to it
   1.359 +     * attached.
   1.360 +     */
   1.361 +    public AssertionData getNodeData() {
   1.362 +        return nodeData;
   1.363 +    }
   1.364 +
   1.365 +    /**
   1.366 +     * Returns the policy reference data for this policy source model node. The policy reference data are expected to be not {@code null} only in
   1.367 +     * case the type of this node is POLICY_REFERENCE.
   1.368 +     *
   1.369 +     * @return the policy reference data for this policy source model node or {@code null} if the node does not have any policy reference data
   1.370 +     * attached.
   1.371 +     */
   1.372 +    PolicyReferenceData getPolicyReferenceData() {
   1.373 +        return referenceData;
   1.374 +    }
   1.375 +
   1.376 +    /**
   1.377 +     * The method may be used to set or replace assertion data set for this node. If there are assertion data set already,
   1.378 +     * those are replaced by a new reference and eventualy returned from the method.
   1.379 +     * <p/>
   1.380 +     * This method is supported only in case this model node instance's type is {@code ASSERTION} or {@code ASSERTION_PARAMETER_NODE}.
   1.381 +     * If used from other node types, an exception is thrown.
   1.382 +     *
   1.383 +     * @param newData new assertion data to be set.
   1.384 +     * @return old and replaced assertion data if any or {@code null} otherwise.
   1.385 +     *
   1.386 +     * @throws UnsupportedOperationException in case this method is called on nodes of type other than {@code ASSERTION}
   1.387 +     * or {@code ASSERTION_PARAMETER_NODE}
   1.388 +     */
   1.389 +    public AssertionData setOrReplaceNodeData(final AssertionData newData) {
   1.390 +        if (!isDomainSpecific()) {
   1.391 +            throw LOGGER.logSevereException(new UnsupportedOperationException(LocalizationMessages.WSP_0051_OPERATION_NOT_SUPPORTED_FOR_THIS_BUT_ASSERTION_RELATED_NODE_TYPE(type)));
   1.392 +        }
   1.393 +
   1.394 +        final AssertionData oldData = this.nodeData;
   1.395 +        this.nodeData = newData;
   1.396 +
   1.397 +        return oldData;
   1.398 +    }
   1.399 +
   1.400 +    /**
   1.401 +     * The method specifies whether the model node instance represents assertion related node, it means whether its type
   1.402 +     * is 'ASSERTION' or 'ASSERTION_PARAMETER_NODE'. This is, for example, the way to determine whether the node supports
   1.403 +     * setting a {@link AssertionData} object via {@link #setOrReplaceNodeData(AssertionData)} method or not.
   1.404 +     *
   1.405 +     * @return {@code true} or {@code false} according to whether the node instance represents assertion related node or not.
   1.406 +     */
   1.407 +    boolean isDomainSpecific() {
   1.408 +        return type == Type.ASSERTION || type == Type.ASSERTION_PARAMETER_NODE;
   1.409 +    }
   1.410 +
   1.411 +    /**
   1.412 +     * Appends the specified child node to the end of the children list of this node and sets it's parent to reference
   1.413 +     * this node.
   1.414 +     *
   1.415 +     * @param child node to be appended to the children list of this node.
   1.416 +     * @return {@code true} (as per the general contract of the {@code Collection.add} method).
   1.417 +     *
   1.418 +     * @throws NullPointerException if the specified node is {@code null}.
   1.419 +     * @throws IllegalArgumentException if child has a parent node set already to point to some node
   1.420 +     */
   1.421 +    private boolean addChild(final ModelNode child) {
   1.422 +        children.add(child);
   1.423 +        child.parentNode = this;
   1.424 +
   1.425 +        return true;
   1.426 +    }
   1.427 +
   1.428 +    void setReferencedModel(final PolicySourceModel model) {
   1.429 +        if (this.type != Type.POLICY_REFERENCE) {
   1.430 +            throw LOGGER.logSevereException(new UnsupportedOperationException(LocalizationMessages.WSP_0050_OPERATION_NOT_SUPPORTED_FOR_THIS_BUT_POLICY_REFERENCE_NODE_TYPE(type)));
   1.431 +        }
   1.432 +
   1.433 +        referencedModel = model;
   1.434 +    }
   1.435 +
   1.436 +    PolicySourceModel getReferencedModel() {
   1.437 +        return referencedModel;
   1.438 +    }
   1.439 +
   1.440 +    /**
   1.441 +     * Returns the number of child policy source model nodes. If this model node contains
   1.442 +     * more than {@code Integer.MAX_VALUE} children, returns {@code Integer.MAX_VALUE}.
   1.443 +     *
   1.444 +     * @return the number of children of this node.
   1.445 +     */
   1.446 +    public int childrenSize() {
   1.447 +        return children.size();
   1.448 +    }
   1.449 +
   1.450 +    /**
   1.451 +     * Returns true if the node has at least one child node.
   1.452 +     *
   1.453 +     * @return true if the node has at least one child node, false otherwise.
   1.454 +     */
   1.455 +    public boolean hasChildren() {
   1.456 +        return !children.isEmpty();
   1.457 +    }
   1.458 +
   1.459 +    /**
   1.460 +     * Iterates through all child nodes.
   1.461 +     *
   1.462 +     * @return An iterator for the child nodes
   1.463 +     */
   1.464 +    public Iterator<ModelNode> iterator() {
   1.465 +        return children.iterator();
   1.466 +    }
   1.467 +
   1.468 +    /**
   1.469 +     * An {@code Object.equals(Object obj)} method override. Method ignores the parent source model. It means that two
   1.470 +     * model nodes may be the same even if they belong to different models.
   1.471 +     * <p/>
   1.472 +     * If parent model comparison is desired, it must be accomplished separately. To perform that, the reference equality
   1.473 +     * test is sufficient ({@code nodeA.getParentModel() == nodeB.getParentModel()}), since all model nodes are created
   1.474 +     * for specific model instances.
   1.475 +     */
   1.476 +    @Override
   1.477 +    public boolean equals(final Object obj) {
   1.478 +        if (this == obj) {
   1.479 +            return true;
   1.480 +        }
   1.481 +
   1.482 +        if (!(obj instanceof ModelNode)) {
   1.483 +            return false;
   1.484 +        }
   1.485 +
   1.486 +        boolean result = true;
   1.487 +        final ModelNode that = (ModelNode) obj;
   1.488 +
   1.489 +        result = result && this.type.equals(that.type);
   1.490 +        // result = result && ((this.parentNode == null) ? that.parentNode == null : this.parentNode.equals(that.parentNode));
   1.491 +        result = result && ((this.nodeData == null) ? that.nodeData == null : this.nodeData.equals(that.nodeData));
   1.492 +        result = result && ((this.children == null) ? that.children == null : this.children.equals(that.children));
   1.493 +
   1.494 +        return result;
   1.495 +    }
   1.496 +
   1.497 +    /**
   1.498 +     * An {@code Object.hashCode()} method override.
   1.499 +     */
   1.500 +    @Override
   1.501 +    public int hashCode() {
   1.502 +        int result = 17;
   1.503 +
   1.504 +        result = 37 * result + this.type.hashCode();
   1.505 +        result = 37 * result + ((this.parentNode == null) ? 0 : this.parentNode.hashCode());
   1.506 +        result = 37 * result + ((this.nodeData == null) ? 0 : this.nodeData.hashCode());
   1.507 +        result = 37 * result + this.children.hashCode();
   1.508 +
   1.509 +        return result;
   1.510 +    }
   1.511 +
   1.512 +    /**
   1.513 +     * Returns a string representation of the object. In general, the <code>toString</code> method
   1.514 +     * returns a string that "textually represents" this object.
   1.515 +     *
   1.516 +     * @return  a string representation of the object.
   1.517 +     */
   1.518 +    @Override
   1.519 +    public String toString() {
   1.520 +        return toString(0, new StringBuffer()).toString();
   1.521 +    }
   1.522 +
   1.523 +    /**
   1.524 +     * A helper method that appends indented string representation of this instance to the input string buffer.
   1.525 +     *
   1.526 +     * @param indentLevel indentation level to be used.
   1.527 +     * @param buffer buffer to be used for appending string representation of this instance
   1.528 +     * @return modified buffer containing new string representation of the instance
   1.529 +     */
   1.530 +    public StringBuffer toString(final int indentLevel, final StringBuffer buffer) {
   1.531 +        final String indent = PolicyUtils.Text.createIndent(indentLevel);
   1.532 +        final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
   1.533 +
   1.534 +        buffer.append(indent).append(type).append(" {").append(PolicyUtils.Text.NEW_LINE);
   1.535 +        if (type == Type.ASSERTION) {
   1.536 +            if (nodeData == null) {
   1.537 +                buffer.append(innerIndent).append("no assertion data set");
   1.538 +            } else {
   1.539 +                nodeData.toString(indentLevel + 1, buffer);
   1.540 +            }
   1.541 +            buffer.append(PolicyUtils.Text.NEW_LINE);
   1.542 +        } else if (type == Type.POLICY_REFERENCE) {
   1.543 +            if (referenceData == null) {
   1.544 +                buffer.append(innerIndent).append("no policy reference data set");
   1.545 +            } else {
   1.546 +                referenceData.toString(indentLevel + 1, buffer);
   1.547 +            }
   1.548 +            buffer.append(PolicyUtils.Text.NEW_LINE);
   1.549 +        } else if (type == Type.ASSERTION_PARAMETER_NODE) {
   1.550 +            if (nodeData == null) {
   1.551 +                buffer.append(innerIndent).append("no parameter data set");
   1.552 +            }
   1.553 +            else {
   1.554 +                nodeData.toString(indentLevel + 1, buffer);
   1.555 +            }
   1.556 +            buffer.append(PolicyUtils.Text.NEW_LINE);
   1.557 +        }
   1.558 +
   1.559 +        if (children.size() > 0) {
   1.560 +            for (ModelNode child : children) {
   1.561 +                child.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
   1.562 +            }
   1.563 +        } else {
   1.564 +            buffer.append(innerIndent).append("no child nodes").append(PolicyUtils.Text.NEW_LINE);
   1.565 +        }
   1.566 +
   1.567 +        buffer.append(indent).append('}');
   1.568 +        return buffer;
   1.569 +    }
   1.570 +
   1.571 +    @Override
   1.572 +    protected ModelNode clone() throws CloneNotSupportedException {
   1.573 +        final ModelNode clone = (ModelNode) super.clone();
   1.574 +
   1.575 +        if (this.nodeData != null) {
   1.576 +            clone.nodeData = this.nodeData.clone();
   1.577 +        }
   1.578 +
   1.579 +        // no need to clone PolicyReferenceData, since those are immutable
   1.580 +
   1.581 +        if (this.referencedModel != null) {
   1.582 +            clone.referencedModel = this.referencedModel.clone();
   1.583 +        }
   1.584 +
   1.585 +
   1.586 +        clone.children = new LinkedList<ModelNode>();
   1.587 +        clone.unmodifiableViewOnContent = Collections.unmodifiableCollection(clone.children);
   1.588 +
   1.589 +        for (ModelNode thisChild : this.children) {
   1.590 +            clone.addChild(thisChild.clone());
   1.591 +        }
   1.592 +
   1.593 +        return clone;
   1.594 +    }
   1.595 +
   1.596 +    PolicyReferenceData getReferenceData() {
   1.597 +        return referenceData;
   1.598 +    }
   1.599 +}

mercurial