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.HashMap;
19  import java.util.Iterator;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.apache.axiom.om.OMElement;
24  import org.apache.neethi.builders.AssertionBuilder;
25  import org.apache.neethi.builders.xml.XMLPrimitiveAssertionBuilder;
26  
27  import sun.misc.Service;
28  
29  /**
30   * AssertionFactory is used to create an Assertion from an OMElement. It uses an
31   * appropriate AssertionBuilder instace to create an Assertion based on the
32   * QName of the given OMElement. Domain Policy authors could right custom
33   * AssertionBuilders to build Assertions for domain specific assertions.
34   * 
35   */
36  public class AssertionBuilderFactory {
37  
38      public static final String POLICY_NAMESPACE = "http://schemas.xmlsoap.org/ws/2004/09/policy";
39  
40      public static final String POLICY = "Policy";
41  
42      public static final String EXACTLY_ONE = "ExactlyOne";
43  
44      public static final String ALL = "All";
45  
46      private static final QName XML_ASSERTION_BUILDER = new QName(
47              "http://test.org/test", "test");
48  
49      private static HashMap registeredBuilders = new HashMap();
50  
51      static {
52          AssertionBuilder builder;
53  
54          for (Iterator providers = Service.providers(AssertionBuilder.class); providers
55                  .hasNext();) {
56              builder = (AssertionBuilder) providers.next();
57  
58              QName[] knownElements = builder.getKnownElements();
59              for (int i = 0; i < knownElements.length; i++) {
60                  registerBuilder(knownElements[i], builder);
61              }
62          }
63          
64          registerBuilder(XML_ASSERTION_BUILDER, new XMLPrimitiveAssertionBuilder());
65      }
66  
67      /**
68       * Registers an AssertionBuilder with a specified QName. 
69       *  
70       * @param key the QName that the AssertionBuilder understand
71       * @param builder the AssertionBuilder that can build an Assertion from
72       *      OMElement of specified type
73       */
74      public static void registerBuilder(QName key, AssertionBuilder builder) {
75          registeredBuilders.put(key, builder);
76      }
77  
78      public AssertionBuilderFactory() {
79      }
80  
81      /**
82       * Returns an assertion that is built using the specified OMElement.
83       * 
84       * @param element the element that the AssertionBuilder can use to build
85       *      an Assertion.
86       * @return an Assertion that is built using the specified element.
87       */
88      public Assertion build(OMElement element) {
89  
90          AssertionBuilder builder;
91  
92          QName qname = element.getQName();
93          builder = (AssertionBuilder) registeredBuilders.get(qname);
94  
95          if (builder != null) {
96              return builder.build(element, this);
97          }
98  
99          /*
100          *  if we can't locate an appropriate AssertionBuilder, we always
101          *  use the XMLPrimitiveAssertionBuilder 
102          */ 
103         builder = (AssertionBuilder) registeredBuilders
104                 .get(XML_ASSERTION_BUILDER);
105         return builder.build(element, this);
106     }
107     
108     /**
109      * Returns an AssertionBuilder that build an Assertion from an OMElement
110      * of qname type.
111      * 
112      * @param qname the type that the AssertionBuilder understands and builds an Assertion from
113      * @return an AssertionBuilder that understands qname type
114      */
115     public AssertionBuilder getBuilder(QName qname) {
116         return (AssertionBuilder) registeredBuilders.get(qname);
117     }
118 }