View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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  package org.apache.neethi;
17  
18  import java.util.Iterator;
19  
20  import javax.xml.stream.XMLStreamException;
21  import javax.xml.stream.XMLStreamWriter;
22  
23  /**
24   * ExactlyOne PolicyOperator requires exactly one of its PolicyComponents to be
25   * met.
26   * 
27   */
28  public class ExactlyOne extends AbstractPolicyOperator {
29  
30      public void serialize(XMLStreamWriter writer) throws XMLStreamException {
31          String prefix = writer.getPrefix(Constants.URI_POLICY_NS);
32  
33          if (prefix == null) {
34              writer.writeStartElement(Constants.ATTR_WSP,
35                      Constants.ELEM_EXACTLYONE, Constants.URI_POLICY_NS);
36              writer.writeNamespace(Constants.ATTR_WSP,
37                      Constants.URI_POLICY_NS);
38              writer.setPrefix(Constants.ATTR_WSP, Constants.URI_POLICY_NS);
39          } else {
40              writer.writeStartElement(Constants.URI_POLICY_NS,
41                      Constants.ELEM_EXACTLYONE);
42          }
43  
44          PolicyComponent policyComponent;
45  
46          for (Iterator iterator = getPolicyComponents().iterator(); iterator
47                  .hasNext();) {
48              policyComponent = (PolicyComponent) iterator.next();
49              policyComponent.serialize(writer);
50          }
51  
52          writer.writeEndElement();
53      }
54  
55      /**
56       * Returns Constants.TYPE_EXACTLYONE;
57       */
58      public final short getType() {
59          return Constants.TYPE_EXACTLYONE;
60      }
61  }