View Javadoc

1   /*
2    * Copyright 2005 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.validation;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.opensaml.xml.AbstractXMLObject;
23  import org.opensaml.xml.XMLObject;
24  import org.opensaml.xml.util.LazyList;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * Extension of {@link org.opensaml.xml.AbstractXMLObject} that implements
30   * {@link org.opensaml.xml.validation.ValidatingXMLObject}.
31   */
32  public abstract class AbstractValidatingXMLObject extends AbstractXMLObject implements ValidatingXMLObject {
33  
34      /** Class logger. */
35      private final Logger log = LoggerFactory.getLogger(AbstractValidatingXMLObject.class);
36  
37      /** Validators used to validate this XMLObject. */
38      private List<Validator> validators;
39  
40      /**
41       * Constructor.
42       * 
43       * @param namespaceURI the namespace the element is in
44       * @param elementLocalName the local name of the XML element this Object represents
45       * @param namespacePrefix the prefix for the given namespace
46       */
47      protected AbstractValidatingXMLObject(String namespaceURI, String elementLocalName, String namespacePrefix) {
48          super(namespaceURI, elementLocalName, namespacePrefix);
49          validators = new LazyList<Validator>();
50      }
51  
52      /** {@inheritDoc} */
53      public List<Validator> getValidators() {
54          if (validators.size() > 0) {
55              return Collections.unmodifiableList(validators);
56          }
57  
58          return null;
59      }
60  
61      /** {@inheritDoc} */
62      public void registerValidator(Validator validator) {
63          if (validator != null) {
64              validators.add(validator);
65          }
66      }
67  
68      /** {@inheritDoc} */
69      public void deregisterValidator(Validator validator) {
70          validators.remove(validator);
71      }
72  
73      /** {@inheritDoc} */
74      public void validate(boolean validateDescendants) throws ValidationException {
75          for (Validator validator : validators) {
76              log.debug("Validating {} using Validator class {}", getElementQName(), validator.getClass().getName());
77              validator.validate(this);
78          }
79  
80          if (validateDescendants) {
81              log.debug("Validating descendants of {}", getElementQName());
82              validateChildren(this);
83          }
84      }
85  
86      /**
87       * Recursive method used to validate all the children of the given XMLObject that implement
88       * {@link ValidatingXMLObject}. Note, this can be a very expensive operation.
89       * 
90       * @param xmlObject xmlObject whose descendants should be validated
91       * 
92       * @throws ValidationException thrown if any child objects are not valid
93       */
94      protected void validateChildren(XMLObject xmlObject) throws ValidationException {
95          for (XMLObject childObject : xmlObject.getOrderedChildren()) {
96              if(childObject == null){
97                  continue;
98              }
99              
100             if (childObject instanceof ValidatingXMLObject) {
101                 ((ValidatingXMLObject) childObject).validate(false);
102             } else {
103                 log.debug("{} does not implement ValidatingXMLObject, ignoring it.", childObject.getElementQName());
104             }
105 
106             if (childObject.hasChildren()) {
107                 validateChildren(childObject);
108             }
109         }
110     }
111 }