View Javadoc

1   /*
2    * Copyright 2008 Members of the EGEE Collaboration.
3    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.opensaml.xacml.policy.impl;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.opensaml.xacml.XACMLObject;
24  import org.opensaml.xacml.policy.AttributeDesignatorType;
25  import org.opensaml.xacml.policy.AttributeSelectorType;
26  import org.opensaml.xacml.policy.AttributeValueType;
27  import org.opensaml.xacml.policy.ResourceMatchType;
28  import org.opensaml.xml.XMLObject;
29  import org.opensaml.xml.util.IndexedXMLObjectChildrenList;
30  import org.opensaml.xml.validation.AbstractValidatingXMLObject;
31  
32  /** Concrete implementation of {@link ResourceMatchType}. */
33  public class ResourceMatchTypeImpl extends AbstractValidatingXMLObject implements ResourceMatchType {
34  
35      /** Match's attribute value. */
36      private AttributeValueType attributeValue;
37  
38      /** Match's choice of attribute elements. */
39      private IndexedXMLObjectChildrenList<XACMLObject> attributeChoice;
40  
41      /** Gets the ID of this match. */
42      private String matchId;
43  
44      /**
45       * Constructor.
46       * 
47       * @param namespaceURI the namespace the element is in
48       * @param elementLocalName the local name of the XML element this Object represents
49       * @param namespacePrefix the prefix for the given namespace
50       */
51      public ResourceMatchTypeImpl(String namespaceURI, String elementLocalName, String namespacePrefix) {
52          super(namespaceURI, elementLocalName, namespacePrefix);
53          attributeChoice = new IndexedXMLObjectChildrenList<XACMLObject>(this);
54      }
55  
56      /** {@inheritDoc} */
57      public AttributeSelectorType getAttributeSelector() {
58          List<XACMLObject> selectors = (List<XACMLObject>) attributeChoice.subList(AttributeSelectorType.DEFAULT_ELEMENT_NAME);
59          if (selectors != null && !selectors.isEmpty()) {
60              return (AttributeSelectorType) selectors.get(0);
61          }
62  
63          return null;
64      }
65  
66      /** {@inheritDoc} */
67      public AttributeValueType getAttributeValue() {
68          return attributeValue;
69      }
70  
71      /** {@inheritDoc} */
72      public AttributeDesignatorType getResourceAttributeDesignator() {
73          List<XACMLObject> selectors = (List<XACMLObject>) attributeChoice
74                  .subList(AttributeDesignatorType.RESOURCE_ATTRIBUTE_DESIGNATOR_ELEMENT_NAME);
75          if (selectors != null && !selectors.isEmpty()) {
76              return (AttributeDesignatorType) selectors.get(0);
77          }
78  
79          return null;
80      }
81  
82      /** {@inheritDoc} */
83      public String getMatchId() {
84          return matchId;
85      }
86  
87      /** {@inheritDoc} */
88      public void setAttributeSelector(AttributeSelectorType selector) {
89          AttributeSelectorType currentSelector = getAttributeSelector();
90          if (currentSelector != null) {
91              attributeChoice.remove(currentSelector);
92          }
93  
94          attributeChoice.add(selector);
95      }
96  
97      /** {@inheritDoc} */
98      public void setAttributeValue(AttributeValueType value) {
99          attributeValue = prepareForAssignment(attributeValue, value);
100     }
101 
102     /** {@inheritDoc} */
103     public void setResourceAttributeDesignator(AttributeDesignatorType attribute) {
104         AttributeDesignatorType currentDesignator = getResourceAttributeDesignator();
105         if (currentDesignator != null) {
106             attributeChoice.remove(currentDesignator);
107         }
108 
109         attributeChoice.add(attribute);
110     }
111 
112     /** {@inheritDoc} */
113     public void setMatchId(String id) {
114         matchId = prepareForAssignment(matchId, id);
115     }
116 
117     /** {@inheritDoc} */
118     public List<XMLObject> getOrderedChildren() {
119         ArrayList<XMLObject> children = new ArrayList<XMLObject>();
120         children.add(attributeValue);
121         if (!attributeChoice.isEmpty()) {
122             children.addAll(attributeChoice);
123         }
124 
125         return children;
126     }
127 }