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.SubjectMatchType;
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 SubjectMatchType}. */
33  public class SubjectMatchTypeImpl extends AbstractValidatingXMLObject implements SubjectMatchType {
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 SubjectMatchTypeImpl(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
59                  .subList(AttributeSelectorType.DEFAULT_ELEMENT_NAME);
60          if (selectors != null && !selectors.isEmpty()) {
61              return (AttributeSelectorType) selectors.get(0);
62          }
63  
64          return null;
65      }
66  
67      /** {@inheritDoc} */
68      public AttributeValueType getAttributeValue() {
69          return attributeValue;
70      }
71  
72      /** {@inheritDoc} */
73      public AttributeDesignatorType getSubjectAttributeDesignator() {
74          List<XACMLObject> selectors = (List<XACMLObject>) attributeChoice
75                  .subList(AttributeDesignatorType.SUBJECT_ATTRIBUTE_DESIGNATOR_ELEMENT_NAME);
76          if (selectors != null && !selectors.isEmpty()) {
77              return (AttributeDesignatorType) selectors.get(0);
78          }
79  
80          return null;
81      }
82  
83      /** {@inheritDoc} */
84      public String getMatchId() {
85          return matchId;
86      }
87  
88      /** {@inheritDoc} */
89      public void setAttributeSelector(AttributeSelectorType selector) {
90          AttributeSelectorType currentSelector = getAttributeSelector();
91          if (currentSelector != null) {
92              attributeChoice.remove(currentSelector);
93          }
94  
95          attributeChoice.add(selector);
96      }
97  
98      /** {@inheritDoc} */
99      public void setAttributeValue(AttributeValueType value) {
100         attributeValue = prepareForAssignment(attributeValue, value);
101     }
102 
103     /** {@inheritDoc} */
104     public void setSubjectAttributeDesignator(AttributeDesignatorType attribute) {
105         AttributeDesignatorType currentDesignator = getSubjectAttributeDesignator();
106         if (currentDesignator != null) {
107             attributeChoice.remove(currentDesignator);
108         }
109 
110         attributeChoice.add(attribute);
111     }
112 
113     /** {@inheritDoc} */
114     public void setMatchId(String id) {
115         matchId = prepareForAssignment(matchId, id);
116     }
117 
118     /** {@inheritDoc} */
119     public List<XMLObject> getOrderedChildren() {
120         ArrayList<XMLObject> children = new ArrayList<XMLObject>();
121         children.add(attributeValue);
122         if (!attributeChoice.isEmpty()) {
123             children.addAll(attributeChoice);
124         }
125 
126         return children;
127     }
128 }